diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c01d57d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +* text=auto +*.c text eol=lf +*.h text eol=lf +*.hlsl text eol=lf +*.hlsli text eol=lf +*.py text eol=lf +*.md text eol=lf +*.bat text eol=crlf +*.txt text eol=crlf +*.nfo text eol=crlf +*.jpg binary diff --git a/.gitignore b/.gitignore index 01017db..476ecac 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,13 @@ .omc/ .claude/ .codex/ +*.pdb +*.ilk +*.bmp +__pycache__/ +fx/out/ +shaders.h +gputest.txt +export-provenance.json +delivery.json +qa/ diff --git a/README.md b/README.md index 052d9e1..4cc7714 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![CRTkafa inside the spiral](assets/cover.jpg) -**2:54 · Windows x64 · Direct3D 11** +**Windows x64 · Direct3D 11** A standalone real-time demo. The music is synthesized as it runs; shaders and model data are embedded in the executable. @@ -37,7 +37,7 @@ Release downloads are separate files, not a ZIP: - **RUNME.txt** — controls, requirements and playback notes. - **CREDITS.md** — model creators, licenses and modifications. -This repository contains release documentation. Source code is not included. +The C engine, HLSL shaders, synthesizer, score and baked model data are included. Model data comes from the creators listed in [CREDITS.md](CREDITS.md); please retain their attribution when redistributing. @@ -45,6 +45,34 @@ Release checks cover silent rendering, audio measurements and complete video decoding. Interactive window/audio playback and a listening-based mix review have not been verified. +## Build + +Install Visual Studio with the **Desktop development with C++** workload and +a Windows SDK containing `fxc.exe`. Python 3 is needed only for score tools +and tests; video export also needs FFmpeg on `PATH`. + +The build script checks the default Community-edition paths for Visual Studio +2022 and Visual Studio 18. For another edition or install location, update the +`VS` path near the top of `buildgfx.bat`. + +```bat +buildgfx.bat +``` + +This compiles the shaders and produces `CRTkafa.exe`. Generated shader headers +are build outputs, not checked-in source. No downloaded models are needed to +build: the converted mesh tables are already included. + +```bat +python -m unittest discover -s tools -p "test_*.py" +python tools/export_song.py --check +buildgfx.bat offline +``` + +`gfxoffline.exe` runs silent graphics tests and writes diagnostic frames. +`makevideo_gpu.bat` builds the video renderer and exports without playback. +The optional `tools/audio_audit.py` analyzer needs NumPy. + --- [crt.fyi](https://crt.fyi) · [hi@crt.fyi](mailto:hi@crt.fyi) diff --git a/RELEASE.md b/RELEASE.md index 36f1994..2382090 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,6 +1,6 @@ # crtkafa, cracktro #1 -2:54. Windows x64. One executable. +Windows x64. One executable. Download **CRTkafa.exe** directly; no ZIP or installation needed. diff --git a/buildgfx.bat b/buildgfx.bat new file mode 100644 index 0000000..6c78404 --- /dev/null +++ b/buildgfx.bat @@ -0,0 +1,95 @@ +@echo off +rem --------------------------------------------------------------------------- +rem D3D11 engine. Shaders are compiled here, at build time, and fxc writes +rem the C byte array itself (/Fh + /Vn) - so d3dcompiler_47.dll is never a +rem runtime dependency and there is no generator to maintain. +rem +rem buildgfx.bat release, windowed subsystem +rem buildgfx.bat offline no window, no sound: renders to bitmaps +rem --------------------------------------------------------------------------- +setlocal +cd /d "%~dp0" + +set "VS=C:\Program Files\Microsoft Visual Studio\2022\Community" +if not exist "%VS%\VC\Auxiliary\Build\vcvars64.bat" set "VS=C:\Program Files\Microsoft Visual Studio\18\Community" +call "%VS%\VC\Auxiliary\Build\vcvars64.bat" >nul + +set "FXC=" +for /f "delims=" %%d in ('dir /b /ad /o-n "C:\Program Files (x86)\Windows Kits\10\bin\10.*" 2^>nul') do ( + if not defined FXC if exist "C:\Program Files (x86)\Windows Kits\10\bin\%%d\x64\fxc.exe" set "FXC=C:\Program Files (x86)\Windows Kits\10\bin\%%d\x64\fxc.exe" +) +if not defined FXC ( echo [!] fxc.exe not found in the Windows SDK & exit /b 1 ) + +if not exist fx\out mkdir fx\out + +rem /O3 optimises, /Ges enforces strict mode, /Qstrip_reflect and +rem /Qstrip_debug keep the blobs small - reflection data is dead weight in a +rem build that never queries it. +set "FXFLAGS=/nologo /O3 /Ges /Qstrip_reflect /Qstrip_debug" + +echo [1/3] shaders +"%FXC%" %FXFLAGS% /T vs_5_0 /E main /Fh fx\out\fullscreen.h /Vn g_vsFullscreen fx\fullscreen.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T vs_5_0 /E main /Fh fx\out\mesh.h /Vn g_vsMeshBlob fx\mesh.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\meshps.h /Vn g_psMeshBlob fx\meshps.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\scene.h /Vn g_psSceneBlob fx\scene.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\logo.h /Vn g_psLogoBlob fx\logo.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\reactor.h /Vn g_psReactorBlob fx\reactor.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\orbital.h /Vn g_psOrbitalBlob fx\orbital.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\eye.h /Vn g_psEyeBlob fx\eye.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\void.h /Vn g_psVoidBlob fx\voidshot.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\horizon.h /Vn g_psHorizonBlob fx\horizon.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\kaleido.h /Vn g_psKaleidoBlob fx\kaleido.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\designframe.h /Vn g_psFramesBlob fx\designframe.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\plinths.h /Vn g_psPlinthBlob fx\plinths.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\drive.h /Vn g_psDriveBlob fx\drive.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\corridor.h /Vn g_psCorridorBlob fx\corridor.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\poweroff.h /Vn g_psPowerOffBlob fx\poweroff.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\burst.h /Vn g_psBurstBlob fx\burst.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\bloom.h /Vn g_psBloomBlob fx\bloom.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\c64.h /Vn g_psC64Blob fx\c64.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\signoff.h /Vn g_psSignBlob fx\signoff.hlsl || exit /b 1 +"%FXC%" %FXFLAGS% /T ps_5_0 /E main /Fh fx\out\post.h /Vn g_psPostBlob fx\post.hlsl || exit /b 1 + +> shaders.h echo /* generated by buildgfx.bat - do not edit */ +>> shaders.h echo #include "fx/out/fullscreen.h" +>> shaders.h echo #include "fx/out/mesh.h" +>> shaders.h echo #include "fx/out/meshps.h" +>> shaders.h echo #include "fx/out/scene.h" +>> shaders.h echo #include "fx/out/logo.h" +>> shaders.h echo #include "fx/out/reactor.h" +>> shaders.h echo #include "fx/out/orbital.h" +>> shaders.h echo #include "fx/out/eye.h" +>> shaders.h echo #include "fx/out/void.h" +>> shaders.h echo #include "fx/out/horizon.h" +>> shaders.h echo #include "fx/out/kaleido.h" +>> shaders.h echo #include "fx/out/designframe.h" +>> shaders.h echo #include "fx/out/plinths.h" +>> shaders.h echo #include "fx/out/drive.h" +>> shaders.h echo #include "fx/out/corridor.h" +>> shaders.h echo #include "fx/out/poweroff.h" +>> shaders.h echo #include "fx/out/burst.h" +>> shaders.h echo #include "fx/out/bloom.h" +>> shaders.h echo #include "fx/out/c64.h" +>> shaders.h echo #include "fx/out/signoff.h" +>> shaders.h echo #include "fx/out/post.h" + +set "DEF=" +set "SUB=WINDOWS" +set "OUT=CRTkafa.exe" +if /i "%~1"=="offline" ( set "DEF=/DOFFLINE" & set "SUB=WINDOWS" & set "OUT=gfxoffline.exe" ) +if /i "%~1"=="video" ( set "DEF=/DVIDEO" & set "SUB=CONSOLE" & set "OUT=gfxvideo.exe" ) + +echo [2/3] compile +cl /nologo /c /O2 /Oi /GS- /fp:fast /std:c11 /W3 /wd4018 %DEF% /Fo:gfx.obj gfx.c || exit /b 1 + +echo [3/3] link +rem dxguid.lib is pure const GUID data with no CRT references, so it is safe +rem under /NODEFAULTLIB. d3d11.lib is deliberately NOT linked: the DLL is +rem resolved with LoadLibrary so a machine without it sees our message rather +rem than the loader's. +link /nologo /SUBSYSTEM:%SUB% /ENTRY:entry /NODEFAULTLIB /INCREMENTAL:NO ^ + /OPT:REF /OPT:ICF ^ + gfx.obj kernel32.lib user32.lib winmm.lib dxguid.lib /OUT:%OUT% || exit /b 1 + +for %%f in (%OUT%) do echo [ok] %%f = %%~zf bytes +for %%f in (fx\out\scene.h) do echo scene shader blob source = %%~zf bytes diff --git a/c64_font.h b/c64_font.h new file mode 100644 index 0000000..518c543 --- /dev/null +++ b/c64_font.h @@ -0,0 +1,54 @@ +/* -------------------------------------------------------------------------- + The boot screen, baked. Generated by tools/bake_font.py - do not edit. + + THE TEXT IS THE AUTHOR'S. It is read out of g_boot[] in crtkafa.c at bake + time rather than retyped here, so it cannot drift from what they wrote. + + The font is drawn in the baker rather than lifted from a ROM: a Commodore + character set is somebody's copyright, and an eight by eight uppercase + grid is a shape anyone may draw. It is that era without being a copy. + -------------------------------------------------------------------------- */ +#define C64_COLS 40 +#define C64_ROWS 16 +#define C64_GLYPHS 47 + +/* two uints per glyph: rows 0-3 in the first, 4-7 in the second, eight bits + to a row, most significant bit leftmost */ +static const unsigned int g_c64Font[] = { + 0x00000000u, 0x00000000u, 0x3C66C3C3u, 0xFFC3C300u, 0xFCC6C6FCu, 0xC6C6FC00u, 0x3E63C0C0u, 0xC0633E00u, + 0xF8CCC6C6u, 0xC6CCF800u, 0xFFC0C0FCu, 0xC0C0FF00u, 0xFFC0C0FCu, 0xC0C0C000u, 0x3E63C0CFu, 0xC3673E00u, + 0xC3C3C3FFu, 0xC3C3C300u, 0x3C181818u, 0x18183C00u, 0x0F060606u, 0xC6C67C00u, 0xC3C6CCF8u, 0xCCC6C300u, + 0xC0C0C0C0u, 0xC0C0FF00u, 0xC3E7FFDBu, 0xC3C3C300u, 0xC3E3F3DBu, 0xCFC7C300u, 0x3C66C3C3u, 0xC3663C00u, + 0xFCC6C6FCu, 0xC0C0C000u, 0x3C66C3C3u, 0xDB663B00u, 0xFCC6C6FCu, 0xCCC6C300u, 0x3E63603Cu, 0x06C67C00u, + 0xFF181818u, 0x18181800u, 0xC3C3C3C3u, 0xC3C37E00u, 0xC3C3C3C3u, 0x663C1800u, 0xC3C3C3DBu, 0xFFE7C300u, + 0xC3663C18u, 0x3C66C300u, 0xC3663C18u, 0x18181800u, 0xFF03063Cu, 0x60C0FF00u, 0x3C66C7DBu, 0xE3663C00u, + 0x18387818u, 0x18187E00u, 0x3C66061Cu, 0x3060FF00u, 0x3C66061Cu, 0x06663C00u, 0x0E1E3666u, 0xFF060600u, + 0xFFC0FC06u, 0x06C67C00u, 0x3C66C0FCu, 0xC6C67C00u, 0xFFC60C18u, 0x30303000u, 0x3C66663Cu, 0x66663C00u, + 0x3C66663Eu, 0x06663C00u, 0x00663CFFu, 0x3C660000u, 0x00000000u, 0x00181800u, 0x00000000u, 0x18183000u, + 0x66666600u, 0x00000000u, 0x6666FF66u, 0xFF666600u, 0x00181800u, 0x18180000u, 0x000000FFu, 0x00000000u, + 0x3C66DEDBu, 0xDE603E00u, 0x03060C18u, 0x3060C000u, 0x18181818u, 0x18001800u, +}; + +/* the screen: four cells to a uint, each an index into the font */ +static const unsigned int g_c64Text[] = { + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x25252525u, 0x0D0F0300u, 0x0F040F0Du, 0x21000512u, 0x0102001Fu, + 0x00030913u, 0x25001D16u, 0x00252525u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x0B1F2100u, 0x0D011200u, + 0x13191300u, 0x000D0514u, 0x24231E00u, 0x02001C1Cu, 0x03091301u, 0x14190200u, 0x06001305u, 0x00050512u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x04010512u, 0x00002619u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x04010F0Cu, 0x12032800u, 0x06010B14u, 0x23272801u, + 0x00001C27u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x12010513u, 0x0E090803u, 0x0F060007u, 0x12030012u, 0x06010B14u, 0x00000001u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x04010F0Cu, 0x00070E09u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x04010512u, 0x00002619u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x000E1512u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, + 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, +}; diff --git a/cats_mesh.h b/cats_mesh.h new file mode 100644 index 0000000..d4796ee --- /dev/null +++ b/cats_mesh.h @@ -0,0 +1,3280 @@ +/* -------------------------------------------------------------------------- + baked meshes, converted offline - nothing is parsed at runtime. + "cat Rigged" by Vr-cvantorium, CC-BY-4.0 + https://sketchfab.com/3d-models/cat-rigged-... + glTF skinning was evaluated offline; 24 frames of the run cycle + are stored as plain vertex positions. + "Oiiaioooooiai Cat" by Zhuier, CC-BY-4.0 + decimated, with a flat colour per triangle sampled from its texture. + -------------------------------------------------------------------------- */ +#define RCAT_NV 396 +#define RCAT_NT 790 +#define RCAT_NF 24 +#define RCAT_QS (1.0f/16000.0f) +static const short g_rcatV[] = { + -8802,237,592,-7899,-895,252,-8770,-825,416,6704,2554,948,8985,2298,1280,9005,2690,682, + -5962,2966,660,-6544,2553,748,-5775,2214,1817,7801,-1037,291,6867,-1680,256,6977,-949,214, + 6257,-2221,663,3552,-1817,1320,3486,-2226,647,-6836,-983,-1,-7441,722,452,-6347,1041,-1, + -4720,-2057,-146,-4764,-2069,261,-11313,-534,677,-10130,-573,523,-11100,-1116,566,-11313,-534,1277, + -10122,-589,1448,-9961,94,1335,-11598,-1869,710,-10870,-1504,670,-11100,-1149,1445,-11598,-1869,1249, + -10878,-1522,1286,-12063,-1819,620,-12743,-2235,645,6381,-13,-1,7543,-192,578,6611,-350,-1, + 7757,-2270,546,7603,-2569,1016,7537,-1193,1762,6500,-1890,1759,7670,-2306,1526,9101,-1311,578, + 8988,-1899,437,10241,-2537,627,11101,-2516,639,11237,-2791,546,6850,389,2110,7626,-425,1525, + 6420,736,1455,6259,1770,1817,7700,1137,1336,8060,1607,1369,-6291,1647,1803,-6710,1461,567, + -7134,1397,1547,-7790,864,1446,-7052,-752,2266,-6693,1361,-1,11726,-2578,590,11111,-2517,1296, + 11239,-2787,1443,11709,-2555,1443,10289,-2564,1347,10286,-2193,1204,8989,-1899,1522,9067,-1451,1290, + 9519,2919,1119,10185,3047,1436,10195,2208,2003,10626,3091,1896,10327,2439,1028,10554,2950,1485, + 9264,-532,999,9546,-812,854,9859,-577,1018,-12671,-2261,1370,-12869,-2310,993,6773,-1125,12, + 4345,-3011,12,6692,-1795,12,-6998,2057,-1,-7437,2254,426,-7473,2238,-1,-7208,-1968,448, + -3016,-2091,481,-4758,-1955,887,-8893,415,763,9767,-1069,415,9446,-957,-1,-6263,2967,411, + -8694,-1591,570,-9957,263,645,-12034,-1811,1396,-12262,-1499,1292,-12831,-1842,1529,-5488,-1744,1779, + -7176,-1871,1433,10245,-2178,754,8864,-2502,1486,10247,-2916,1281,5903,1126,1944,10422,-894,-1, + 10831,-515,-1,10403,-885,447,-7880,-779,1866,-6738,2009,572,6963,-592,2009,9797,1446,1879, + 10528,-279,785,11237,-3278,1276,11118,-3362,985,11279,-3250,701,11928,-2964,545,12141,-2901,1055, + 11898,-2559,1058,-13122,-1924,969,-12186,-1361,969,-12264,-1502,667,9750,-1134,-1,9757,-40,1562, + 10379,385,898,10260,212,1460,10481,407,427,10644,1109,731,10986,-185,-84,10982,-193,165, + -8737,201,-593,-8639,-859,-417,-7783,-889,-254,7204,2589,-949,9005,2690,-684,8985,2298,-1282, + -5964,2972,-661,-5805,2272,-1818,-6545,2553,-749,6802,-1766,-292,6481,-1296,-216,6107,-2182,-257, + 5431,-2908,-664,4083,-2873,-649,3736,-2462,-1321,-7437,737,-453,-11165,-533,-678,-10929,-1106,-567, + -9980,-534,-524,-11165,-533,-1278,-9817,149,-1336,-9977,-543,-1449,-11407,-1883,-711,-10684,-1485,-671, + -10929,-1118,-1446,-10684,-1485,-1288,-11394,-1870,-1250,-11856,-1839,-621,-12515,-2260,-647,6661,-851,-579, + 6289,-3378,-1017,6473,-3097,-547,6344,-2019,-1763,6383,-3124,-1527,5389,-2584,-1760,7729,-2897,-438, + 7923,-2326,-580,8870,-3717,-628,9814,-4163,-547,9741,-3868,-640,5755,-395,-2111,5872,382,-1456, + 6487,-1259,-1526,6166,1647,-1818,8040,1602,-1370,7329,1232,-1337,-6334,1729,-1804,-7172,1442,-1548, + -6727,1477,-568,-7785,883,-1447,-6939,-674,-2267,10345,-4110,-591,9746,-3878,-1298,10339,-4090,-1444, + 9821,-4177,-1445,8918,-3750,-1349,8970,-3383,-1205,7735,-2863,-1523,7871,-2463,-1292,9519,2919,-1120, + 10185,3047,-1437,10626,3091,-1897,10195,2208,-2004,10327,2439,-1029,10554,2950,-1486,9264,-532,-1000, + 9859,-577,-1020,9546,-812,-856,-12695,-2313,-994,-12526,-2256,-1371,-7437,2255,-428,-7092,-1957,-449, + -4680,-1925,-888,-2991,-2081,-482,-8837,375,-764,9767,-1069,-416,-6263,2967,-412,-8553,-1608,-571, + -9817,308,-647,-11861,-1840,-1397,-12654,-1829,-1530,-12067,-1533,-1293,-7015,-1843,-1434,-5341,-1665,-1780, + 8927,-3362,-756,7552,-3441,-1487,8819,-4088,-1282,5626,632,-1945,10403,-885,-448,-7753,-753,-1868, + -6742,2013,-573,5812,-1381,-2011,9797,1446,-1880,10528,-279,-786,9691,-4642,-1277,9737,-4627,-702, + 9555,-4693,-987,10652,-4549,-1057,10429,-4538,-546,10522,-4154,-1059,-12937,-1920,-971,-12065,-1529,-668, + -11992,-1395,-970,9757,-40,-1564,10379,385,-899,10260,212,-1461,10644,1109,-732,10481,407,-428, + 4095,-690,1987,5775,-234,2298,8445,-202,1007,8383,-308,-1,9070,-674,-1,-8801,236,1329, + -10169,-1219,595,562,2671,686,677,2909,-1,-637,3018,-1,-695,2718,782,-2861,-1765,1320, + -3449,-998,1905,-4727,-1097,1939,9343,1161,1831,-12850,-1818,368,5139,1361,2167,5801,787,2281, + -13841,5296,-1,-15054,4949,-1,-15030,4800,458,-13802,5028,545,863,-2121,481,756,-2250,-1, + -10435,3029,367,-12722,3503,387,-12735,3279,-1,-10458,2817,-1,-2238,805,2074,-3589,271,2149, + -12458,4370,777,-13839,4499,742,-13913,3806,442,-15080,4122,401,-15065,4027,-1,-13891,3640,-1, + 880,-1811,1320,4616,934,2203,-8777,-833,1553,-6167,-158,2354,7449,415,1000,7590,184,-1, + 3635,732,2096,-15352,4507,-1,-15045,4508,567,-10164,-1220,1347,10207,2176,775,-5911,3264,-1, + -6176,3143,-1,-1026,1074,2016,-1715,2748,841,8900,452,1749,10136,-3163,971,-1463,-1745,1320, + -1427,-776,1905,-7082,3418,-1,-10005,4570,-1,-10032,4276,376,-7107,3209,400,-4026,3139,1079, + -4599,987,2385,-2652,3011,948,-7228,2827,549,2848,-781,1641,-8694,-1591,1356,8864,-2502,599, + 8739,-2795,1024,-4091,3499,-1,-2625,3209,-1,10651,1127,-1,10510,432,-1,2824,444,1983, + -12276,5032,520,10459,761,1579,-11319,-321,977,2463,2379,746,2468,2762,-1,-10198,3725,591, + 10092,2206,-1,-1740,-2174,481,1004,703,1983,-9984,428,882,10217,-2891,699,1031,-1021,1641, + 3695,2264,791,4765,2448,721,5714,2600,738,11915,-2967,1464,3785,2886,-1,7254,2723,-1, + 6021,2911,-1,4905,2843,-1,9043,2760,-1,-3021,-2208,-1,-1747,-2270,-1,-1667,2996,-1, + -12273,5341,-1,4142,-1703,-1988,4793,-771,-2300,8445,-202,-1008,-8739,202,-1330,-9996,-1186,-596, + 562,2671,-687,-691,2726,-783,-4608,-978,-1940,-3368,-926,-1907,-2804,-1736,-1321,9343,1161,-1832, + -12645,-1839,-369,5092,1287,-2168,5087,241,-2282,-13802,5028,-546,-15030,4800,-459,681,-2100,-482, + -10435,3029,-368,-12725,3504,-388,-3524,458,-2150,-2198,881,-2075,-12458,4370,-778,-13913,3807,-443, + -13839,4500,-743,-15080,4122,-402,743,-1831,-1321,4158,750,-2204,-8642,-859,-1554,-6087,-14,-2355, + 7375,359,-1001,3307,751,-2097,-15045,4508,-568,-9995,-1185,-1348,10207,2176,-776,-1706,2780,-842, + -1006,1103,-2017,8900,452,-1751,8677,-4316,-972,-1393,-749,-1907,-1426,-1727,-1321,-7107,3210,-401, + -10030,4276,-378,-4032,3187,-1080,-2646,3057,-949,-4593,1193,-2386,-7228,2829,-551,3212,-1697,-1642, + -8552,-1607,-1358,7351,-3750,-1026,7517,-3479,-600,2451,708,-1984,-12276,5032,-522,10459,761,-1581, + -11180,-320,-978,2432,2360,-747,-10198,3725,-592,-1729,-2170,-482,624,1340,-1984,-9841,477,-883, + 8818,-4079,-701,611,-781,-1642,3653,2452,-792,4717,2474,-722,5730,2608,-739,10416,-4545,-1465, + -8644,381,592,-7755,-748,252,-8619,-681,416,6613,2383,948,8818,2267,1280,8813,2653,682, + -5908,3025,660,-6538,2673,748,-5728,2249,1817,7922,-791,291,6991,-1524,256,7019,-802,214, + 6494,-2114,663,3650,-1959,1320,3539,-2376,647,-6715,-832,-1,-7336,838,452,-6364,1093,-1, + -4747,-1999,-146,-4783,-2020,261,-11162,-363,678,-9980,-420,523,-10957,-948,567,-11162,-363,1278, + -9971,-435,1448,-9803,246,1335,-11466,-1694,712,-10734,-1340,672,-10958,-981,1446,-11466,-1694,1251, + -10741,-1357,1288,-11931,-1637,622,-12617,-2044,648,6293,-53,-1,7552,11,578,6581,-256,-1, + 8007,-1995,546,7871,-2301,1016,7699,-960,1762,6725,-1769,1759,7924,-2036,1526,9324,-1027,578, + 9211,-1623,437,10456,-2284,627,11320,-2301,639,11444,-2584,546,6807,516,2110,7686,-195,1525, + 6293,733,1455,6150,1629,1817,7617,1013,1336,7953,1497,1369,-6190,1733,1803,-6717,1497,567, + -7020,1504,1547,-7636,992,1446,-6905,-621,2266,-6699,1392,0,11944,-2396,590,11331,-2302,1297, + 11446,-2579,1444,11927,-2373,1443,10504,-2311,1347,10506,-1941,1204,9212,-1622,1522,9292,-1170,1290, + 9292,2938,1119,9946,3122,1436,10025,2286,2003,10382,3202,1896,10138,2527,1028,10321,3056,1485, + 9309,-541,999,9616,-796,854,9914,-532,1018,-12545,-2070,1373,-12744,-2116,996,6769,-1015,12, + 4175,-3087,11,6765,-1664,12,-6998,2163,0,-7431,2354,427,-7465,2334,1,-7117,-1837,448, + -3109,-2133,481,-4752,-1914,887,-8734,560,763,9858,-1033,415,9525,-949,-1,-6229,3081,411, + -8549,-1448,570,-9797,414,646,-11902,-1629,1398,-12125,-1315,1295,-12698,-1648,1532,-5425,-1671,1779, + -7069,-1753,1433,10465,-1925,754,9079,-2229,1486,10453,-2661,1281,5768,1048,1944,10506,-795,-1, + 10888,-374,-1,10486,-788,447,-7729,-640,1866,-6767,2127,572,7044,-444,2009,9693,1494,1879, + 10566,-164,785,11420,-3071,1276,11296,-3149,986,11463,-3045,701,12125,-2792,545,12341,-2740,1055, + 12116,-2386,1058,-12991,-1727,972,-12047,-1178,971,-12127,-1318,669,9846,-1098,-1,9772,-1,1562, + 10362,485,898,10258,303,1460,10462,516,427,10565,1228,731,11014,-32,-84,11011,-41,165, + -8549,502,-594,-8500,-558,-419,-7646,-643,-255,7138,2447,-949,8813,2653,-684,8818,2267,-1282, + -5907,3032,-661,-5723,2323,-1818,-6540,2673,-748,6647,-1808,-293,6416,-1234,-216,5951,-2133,-258, + 5172,-2861,-665,3891,-2955,-649,3542,-2545,-1322,-7293,942,-454,-11019,-13,-680,-10860,-612,-569, + -9843,-168,-526,-11019,-13,-1281,-9610,493,-1338,-9838,-179,-1451,-11435,-1320,-712,-10667,-1020,-673, + -10861,-625,-1448,-10667,-1021,-1289,-11420,-1309,-1252,-11874,-1217,-622,-12583,-1549,-648,6605,-867,-580, + 5903,-3370,-1018,6125,-3119,-548,6142,-2036,-1764,6033,-3132,-1528,5131,-2531,-1761,7393,-3107,-440, + 7670,-2570,-580,8396,-4090,-630,9305,-4580,-549,9246,-4287,-641,5761,-358,-2111,5935,366,-1456, + 6382,-1302,-1527,6149,1595,-1818,7945,1499,-1370,7329,1156,-1337,-6170,1873,-1804,-6990,1643,-1549, + -6717,1529,-568,-7577,1138,-1448,-6792,-442,-2268,9839,-4556,-593,9250,-4296,-1299,9834,-4535,-1446, + 9312,-4591,-1446,8437,-4129,-1350,8543,-3773,-1207,7405,-3069,-1524,7598,-2697,-1292,9292,2938,-1120, + 9946,3122,-1437,10382,3202,-1897,10025,2286,-2004,10138,2527,-1029,10321,3056,-1486,9309,-541,-1000, + 9914,-532,-1020,9616,-796,-856,-12768,-1578,-995,-12593,-1545,-1373,-7432,2355,-426,-7031,-1745,-450, + -4694,-1852,-889,-3092,-2116,-482,-8640,679,-766,9858,-1033,-416,-6229,3081,-411,-8450,-1309,-573, + -9596,651,-649,-11880,-1219,-1398,-12664,-1104,-1532,-12043,-887,-1295,-6945,-1617,-1435,-5310,-1532,-1780, + 8506,-3747,-757,7141,-3613,-1488,8297,-4457,-1284,5649,603,-1945,10485,-788,-448,-7606,-485,-1869, + -6769,2134,-572,5697,-1335,-2011,9693,1494,-1880,10566,-164,-786,9157,-5044,-1279,9204,-5031,-704, + 9020,-5087,-989,10118,-5016,-1059,9896,-4988,-548,10015,-4613,-1061,-12957,-1157,-972,-12041,-883,-670, + -11951,-760,-972,9772,-2,-1564,10362,485,-899,10258,303,-1461,10565,1228,-732,10462,516,-428, + 4156,-836,1987,5815,-229,2298,8455,-284,1007,8393,-398,-1,9122,-702,-1,-8643,380,1329, + -10028,-1065,596,599,2395,686,716,2626,-1,-561,2823,-1,-623,2532,782,-2936,-1821,1320, + -3446,-1012,1905,-4653,-1036,1939,9264,1165,1831,-12718,-1625,371,5038,1229,2167,5709,769,2281, + -13969,4925,5,-15172,4601,6,-15149,4453,464,-13929,4657,550,759,-2360,481,604,-2492,-1, + -10383,3008,370,-12716,3119,391,-12697,2895,4,-10381,2796,3,-2183,743,2074,-3479,321,2149, + -12571,4017,781,-13963,4129,747,-14034,3434,447,-15204,3775,408,-15189,3680,6,-14010,3264,5, + 801,-2049,1320,4568,811,2203,-8627,-689,1553,-6012,-32,2354,7375,281,1000,7534,38,-1, + 3618,540,2096,-15473,4161,6,-15166,4160,574,-10022,-1065,1348,10040,2255,775,-5834,3314,-1, + -6129,3249,0,-1007,910,2016,-1626,2649,841,8875,414,1749,10335,-2903,971,-1582,-1896,1320, + -1493,-920,1905,-7113,3526,0,-10137,4587,2,-10138,4294,379,-7130,3318,401,-3974,3096,1079, + -4460,1080,2385,-2592,2959,948,-7239,2932,550,2891,-1000,1641,-8549,-1447,1357,9079,-2229,599, + 8951,-2519,1024,-4048,3433,-1,-2570,3155,-1,10571,1247,-1,10488,543,-1,2845,203,1983, + -12486,4699,525,10410,867,1579,-11165,-150,978,2349,2127,746,2335,2512,-1,-10239,3728,594, + 9923,2276,-1,-1876,-2306,481,1059,430,1983,-9822,579,882,10424,-2634,699,1024,-1253,1641, + 3581,2086,791,4648,2336,721,5638,2485,738,12112,-2795,1464,3629,2745,-1,7204,2581,-1, + 5960,2825,-1,4780,2773,-1,8844,2726,-1,-3127,-2248,-1,-1889,-2401,-1,-1578,2887,-1, + -12522,5006,4,3971,-1629,-1988,4743,-658,-2300,8455,-284,-1008,-8550,504,-1332,-9934,-812,-598, + 599,2395,-687,-620,2538,-783,-4553,-881,-1940,-3384,-920,-1907,-2897,-1776,-1321,9264,1165,-1832, + -12657,-1114,-371,5076,1317,-2168,5112,306,-2282,-13929,4657,-540,-15149,4453,-453,504,-2346,-482, + -10385,3008,-365,-12717,3119,-383,-3405,507,-2149,-2149,816,-2075,-12572,4017,-773,-14035,3434,-437, + -13964,4129,-737,-15204,3775,-396,578,-2065,-1321,4116,862,-2204,-8503,-559,-1555,-5910,183,-2356, + 7357,235,-1001,3204,699,-2097,-15167,4160,-562,-9933,-812,-1350,10040,2255,-776,-1616,2676,-842, + -993,936,-2017,8875,414,-1751,8131,-4672,-974,-1471,-890,-1907,-1558,-1871,-1321,-7130,3318,-400, + -10131,4292,-375,-3967,3141,-1080,-2581,2999,-949,-4403,1303,-2386,-7240,2933,-550,3042,-1745,-1643, + -8449,-1308,-1359,6891,-3892,-1027,7095,-3651,-602,2346,560,-1984,-12486,4699,-517,10410,867,-1581, + -11005,199,-981,2307,2131,-747,-10240,3729,-589,-1869,-2299,-482,605,1071,-1984,-9606,820,-885, + 8292,-4441,-702,495,-1011,-1642,3513,2352,-792,4611,2453,-722,5683,2560,-739,9882,-4994,-1467, + -8142,489,592,-7280,-614,252,-8098,-569,416,6631,1770,948,8713,1679,1280,8695,2058,682, + -5678,3104,660,-6347,2831,748,-5537,2337,1817,7845,-1351,291,6802,-2013,256,6827,-1291,214, + 6407,-2641,663,3497,-2404,1320,3258,-2770,647,-6401,-689,-1,-6986,962,452,-6303,1191,-1, + -4681,-1910,-146,-4677,-1935,260,-10657,-211,679,-9480,-333,524,-10485,-806,569,-10656,-210,1280, + -9468,-348,1449,-9282,326,1336,-11020,-1519,714,-10283,-1210,673,-10485,-835,1448,-11020,-1520,1253, + -10289,-1225,1289,-11475,-1446,624,-12129,-1897,651,6236,-617,-1,7521,-537,578,6412,-741,-1, + 7963,-2568,546,7827,-2876,1016,7670,-1516,1762,6660,-2295,1759,7879,-2609,1526,9262,-1558,578, + 9170,-2153,437,10443,-2756,627,11308,-2742,639,11442,-3021,546,6842,-2,2110,7691,-747,1525, + 6308,190,1455,6145,1049,1817,7579,385,1336,7891,867,1369,-5862,1861,1803,-6612,1589,567, + -6654,1632,1547,-7175,1118,1446,-6395,-471,2266,-6627,1473,-1,11935,-2817,590,11319,-2742,1296, + 11444,-3016,1443,11918,-2794,1443,10492,-2781,1347,10478,-2411,1204,9171,-2153,1522,9233,-1701,1290, + 9150,2378,1119,9794,2594,1436,9914,1763,2003,10225,2695,1896,10015,2009,1028,10172,2546,1485, + 9318,-1112,999,9638,-1350,854,9929,-1070,1018,-12064,-1920,1376,-12251,-1978,999,6440,-1466,12, + 3393,-3319,12,6469,-2118,12,-6808,2299,-1,-7224,2457,426,-7254,2429,-1,-6720,-1701,448, + -3159,-2133,481,-4581,-1832,887,-8235,665,763,9892,-1574,415,9552,-1509,-1,-6005,3226,411, + -8016,-1335,570,-9273,493,646,-11448,-1438,1400,-11689,-1118,1297,-12246,-1505,1534,-5088,-1554,1779, + -6613,-1622,1433,10436,-2397,754,9064,-2762,1486,10455,-3132,1281,5788,519,1944,10536,-1300,-1, + 10904,-852,-1,10515,-1294,447,-7206,-506,1866,-6626,2296,572,7044,-970,2009,9620,956,1879, + 10573,-658,785,11434,-3509,1276,11313,-3591,985,11476,-3482,701,12129,-3207,545,12344,-3148,1055, + 12107,-2802,1058,-12525,-1607,975,-11609,-974,973,-11691,-1121,671,9882,-1641,-1,9766,-544,1562, + 10338,-20,898,10242,-207,1460,10435,15,427,10504,732,731,11013,-505,-84,11011,-514,165, + -8330,698,-596,-8298,-359,-421,-7407,-457,-256,7141,1800,-949,8695,2058,-684,8713,1678,-1282, + -5682,3111,-661,-5597,2412,-1818,-6347,2832,-749,5842,-2300,-292,5918,-1635,-216,5182,-2424,-257, + 4184,-3009,-664,3108,-3152,-649,2829,-2719,-1321,-7117,1108,-454,-10802,393,-684,-10737,-224,-573, + -9661,58,-528,-10802,393,-1284,-9361,688,-1340,-9654,48,-1454,-11415,-834,-718,-10609,-656,-677, + -10740,-235,-1452,-10609,-656,-1293,-11398,-825,-1257,-11833,-665,-628,-12584,-884,-655,6128,-1396,-579, + 4644,-3648,-1017,4921,-3457,-547,5262,-2400,-1763,4827,-3446,-1527,4184,-2648,-1760,6202,-3679,-438, + 6561,-3205,-580,7048,-4795,-628,7926,-5326,-547,7881,-5036,-640,5476,-711,-2111,5963,-120,-1456, + 5736,-1783,-1526,6214,1117,-1818,7894,875,-1370,7402,590,-1337,-6000,2018,-1804,-6816,1813,-1549, + -6644,1627,-568,-7361,1328,-1449,-6586,-249,-2269,8462,-5357,-591,7882,-5043,-1298,8459,-5336,-1444, + 7933,-5336,-1445,7082,-4839,-1349,7239,-4502,-1205,6200,-3684,-1523,6468,-3314,-1292,9150,2378,-1120, + 9794,2594,-1437,10225,2695,-1897,9914,1763,-2004,10015,2009,-1029,10172,2546,-1486,9318,-1113,-1000, + 9929,-1070,-1020,9638,-1350,-856,-12771,-884,-1003,-12593,-877,-1380,-7224,2457,-428,-6854,-1569,-451, + -4643,-1757,-889,-3177,-2113,-482,-8418,873,-768,9892,-1574,-416,-6005,3226,-412,-8262,-1109,-575, + -9333,841,-651,-11838,-665,-1404,-12595,-431,-1538,-11948,-312,-1301,-6793,-1434,-1436,-5218,-1395,-1781, + 7208,-4471,-756,5821,-4169,-1487,6904,-5153,-1282,5691,177,-1945,10515,-1294,-448,-7394,-285,-1870, + -6633,2303,-573,5084,-1590,-2011,9620,956,-1880,10573,-658,-786,7743,-5764,-1277,7791,-5754,-702, + 7607,-5796,-987,8687,-5851,-1057,8470,-5793,-546,8634,-5439,-1059,-12893,-439,-979,-11946,-310,-675, + -11838,-201,-977,9766,-544,-1564,10338,-20,-899,10242,-207,-1461,10504,732,-732,10435,15,-428, + 4062,-1296,1987,5808,-714,2298,8442,-898,1007,8378,-1017,-1,9132,-1284,-1,-8141,489,1329, + -9555,-972,597,693,2084,686,826,2304,-1,-426,2629,-1,-486,2353,782,-2944,-1831,1320, + -3293,-971,1905,-4315,-915,1939,9207,601,1831,-12265,-1485,373,5056,737,2167,5738,278,2281, + -13852,4527,-1,-15057,4271,-1,-15040,4122,458,-13821,4257,545,487,-2646,481,272,-2751,-1, + -10185,2875,367,-12523,2682,387,-12477,2461,-1,-10158,2665,-1,-2006,731,2074,-3115,456,2149, + -12478,3590,777,-13871,3731,742,-13963,3039,442,-15124,3447,401,-15114,3351,-1,-13942,2864,-1, + 574,-2338,1320,4566,355,2203,-8107,-580,1553,-5502,139,2354,7335,-352,1000,7475,-611,-1, + 3583,103,2096,-15377,3845,-1,-15070,3830,567,-9548,-972,1349,9930,1733,775,-5578,3377,-1, + -5894,3385,-1,-933,766,2016,-1458,2586,841,8848,-175,1749,10346,-3378,971,-1720,-2011,1320, + -1540,-1034,1905,-6994,3644,-1,-10118,4469,-1,-10091,4178,376,-6992,3439,400,-3836,3107,1079, + -4036,1255,2385,-2454,2969,948,-7074,3046,549,2767,-1416,1641,-8016,-1334,1357,9064,-2762,599, + 8948,-3058,1024,-3959,3409,-1,-2465,3163,0,10509,752,-1,10461,44,-1,2796,-206,1983, + -12472,4278,520,10367,364,1579,-10648,2,980,2360,1721,746,2361,2104,-1,-10128,3606,591, + 9812,1747,-1,-2041,-2401,481,1032,112,1983,-9296,658,883,10425,-3106,699,878,-1553,1641, + 3597,1645,791,4676,1854,721,5675,1938,738,12117,-3210,1464,3657,2313,-1,7226,1932,-1, + 6010,2265,-1,4825,2311,-1,8723,2133,-1,-3207,-2247,-1,-2070,-2495,-1,-1430,2808,-1, + -12539,4578,-1,3372,-1606,-1988,4416,-747,-2300,8442,-898,-1008,-8331,701,-1333,-9837,-561,-602, + 692,2084,-687,-494,2356,-783,-4444,-759,-1940,-3373,-880,-1907,-2988,-1782,-1322,9207,600,-1832, + -12591,-444,-377,5121,1009,-2168,5069,44,-2282,-13821,4257,-546,-15040,4122,-459,149,-2587,-482, + -10186,2875,-368,-12521,2682,-388,-3242,612,-2149,-2061,790,-2075,-12478,3590,-778,-13963,3038,-443, + -13871,3731,-743,-15124,3447,-402,238,-2292,-1321,4075,779,-2204,-8301,-360,-1557,-5692,369,-2356, + 7356,-390,-1001,3071,515,-2097,-15070,3830,-568,-9834,-561,-1353,9930,1733,-776,-1475,2605,-842, + -957,786,-2017,8848,-176,-1751,6715,-5350,-972,-1569,-1005,-1907,-1744,-1985,-1321,-6992,3439,-401, + -10080,4177,-378,-3865,3143,-1080,-2479,2998,-949,-4204,1455,-2385,-7074,3046,-551,2479,-1810,-1642, + -8259,-1107,-1361,5587,-4375,-1026,5829,-4165,-600,2194,348,-1984,-12473,4278,-522,10367,364,-1581, + -10756,601,-984,2306,1764,-747,-10127,3607,-592,-2048,-2393,-482,584,781,-1984,-9330,1010,-887, + 6894,-5128,-701,265,-1233,-1642,3523,2006,-792,4655,2079,-722,5748,2081,-739,8455,-5798,-1465, + -7614,225,591,-6677,-759,252,-7434,-817,416,6647,1334,948,8704,1223,1280,8681,1604,682, + -5478,3038,660,-6128,2742,747,-5385,2332,1817,7635,-2124,291,6452,-2577,256,6521,-1812,214, + 6112,-3302,663,3276,-2842,1320,2926,-3097,647,-5986,-762,-2,-6643,832,451,-6157,1122,-1, + -4429,-1953,-147,-4376,-1976,260,-10025,-698,681,-8841,-720,525,-9802,-1277,570,-10024,-698,1281, + -8826,-735,1450,-8703,-47,1336,-10283,-2036,716,-9567,-1662,675,-9800,-1308,1449,-10282,-2036,1255, + -9572,-1679,1291,-10746,-1997,626,-11398,-2455,653,6284,-1139,-1,7437,-1266,578,6183,-1207,-2, + 7676,-3395,546,7516,-3692,1016,7493,-2302,1762,6398,-2964,1759,7587,-3429,1526,9024,-2431,578, + 8917,-3016,437,10180,-3632,627,11045,-3622,639,11178,-3901,546,6897,-683,2110,7628,-1537,1525, + 6444,-387,1455,6199,582,1817,7609,-77,1336,7895,407,1369,-5623,1858,1803,-6468,1516,567, + -6371,1544,1547,-6772,970,1446,-5770,-501,2266,-6502,1392,-1,11673,-3701,590,11056,-3622,1296, + 11180,-3896,1443,11656,-3678,1443,10229,-3658,1347,10220,-3288,1204,8918,-3016,1522,8991,-2570,1290, + 9135,1925,1119,9777,2146,1436,9905,1317,2003,10208,2252,1896,10003,1564,1028,10156,2102,1485, + 9340,-1560,999,9662,-1795,854,9949,-1513,1018,-11328,-2476,1378,-11519,-2536,1002,5987,-1824,11, + 2599,-3230,11,6024,-2564,11,-6567,2186,-1,-6975,2310,425,-7001,2278,-2,-6094,-1802,448, + -3023,-2194,481,-4219,-1863,887,-7727,387,763,9917,-2017,415,9578,-1954,-1,-5796,3150,410, + -7266,-1566,570,-8710,119,647,-10718,-1987,1402,-10964,-1683,1299,-11513,-2065,1537,-4543,-1541,1779, + -5929,-1709,1433,10178,-3274,754,8802,-3619,1486,10188,-4010,1281,5924,8,1944,10557,-1738,-1, + 10919,-1289,-1,10536,-1733,447,-6557,-637,1866,-6406,2205,571,6960,-1667,2009,9618,507,1879, + 10586,-1098,785,11167,-4388,1276,11046,-4469,985,11210,-4361,701,11864,-4092,545,12079,-4036,1055, + 11846,-3688,1058,-11796,-2168,978,-10893,-1539,975,-10967,-1686,673,9908,-2083,-1,9780,-989,1562, + 10345,-462,898,10251,-650,1460,10442,-426,427,10504,292,731,11025,-940,-84,11023,-949,165, + -8129,503,-597,-8008,-545,-422,-7086,-584,-257,7111,1318,-949,8681,1604,-684,8704,1223,-1282, + -5488,3043,-661,-5513,2386,-1818,-6126,2744,-749,4866,-2335,-294,5290,-1780,-217,4309,-2329,-260, + 3152,-2678,-667,2336,-3014,-650,2145,-2566,-1323,-6971,996,-455,-10540,110,-685,-10436,-501,-574, + -9381,-152,-530,-10539,111,-1286,-9110,494,-1341,-9373,-164,-1455,-11079,-1154,-720,-10280,-924,-679, + -10438,-513,-1453,-10280,-924,-1295,-11061,-1143,-1259,-11507,-1008,-630,-12254,-1239,-657,5451,-1585,-582, + 3281,-3288,-1020,3596,-3175,-550,4240,-2249,-1766,3508,-3139,-1530,3202,-2273,-1762,4818,-3673,-441, + 5260,-3290,-582,5438,-4927,-631,6270,-5477,-550,6232,-5195,-643,5023,-759,-2113,5901,-452,-1457, + 4890,-1817,-1529,6285,746,-1818,7903,419,-1370,7491,169,-1337,-5928,1994,-1805,-6741,1732,-1550, + -6533,1551,-569,-7236,1207,-1450,-6317,-293,-2269,6808,-5504,-594,6233,-5199,-1300,6805,-5483,-1447, + 6278,-5483,-1448,5461,-4975,-1351,5678,-4674,-1208,4791,-3700,-1526,5149,-3373,-1294,9135,1925,-1120, + 9777,2146,-1437,10208,2252,-1897,9905,1317,-2004,10003,1564,-1029,10156,2102,-1486,9340,-1560,-1000, + 9948,-1513,-1020,9662,-1795,-856,-12444,-1242,-1005,-12263,-1233,-1381,-6974,2310,-429,-6503,-1665,-452, + -4427,-1797,-889,-3082,-2178,-482,-8229,671,-769,9917,-2017,-416,-5796,3150,-412,-7932,-1284,-576, + -9090,647,-652,-11512,-1008,-1406,-12272,-788,-1540,-11630,-665,-1302,-6482,-1520,-1437,-4967,-1410,-1781, + 5656,-4639,-758,4306,-4086,-1490,5239,-5265,-1285,5725,-122,-1946,10536,-1733,-448,-7114,-397,-1871, + -6418,2210,-574,4339,-1426,-2013,9618,507,-1880,10586,-1098,-786,6084,-5904,-1280,6131,-5895,-705, + 5947,-5935,-990,7028,-6000,-1059,6811,-5940,-549,6978,-5588,-1062,-12572,-799,-981,-11629,-662,-677, + -11527,-552,-979,9780,-989,-1564,10345,-462,-899,10251,-650,-1461,10504,292,-732,10442,-426,-428, + 3924,-1742,1987,5762,-1263,2298,8465,-1353,1007,8403,-1472,-1,9157,-1732,-1,-7612,225,1329, + -8858,-1365,598,781,1832,686,927,2045,-1,-318,2444,-1,-366,2194,782,-2773,-1882,1320, + -2991,-954,1905,-3823,-830,1939,9209,150,1831,-11534,-2045,376,5139,294,2167,5839,-261,2281, + -13641,4151,-7,-14850,3951,-8,-14840,3801,451,-13619,3880,538,292,-2885,481,46,-2937,-1, + -9982,2519,363,-12326,2264,381,-12275,2044,-6,-9947,2310,-4,-1778,748,2074,-2708,666,2149, + -12300,3172,771,-13688,3357,735,-13805,2668,435,-14952,3130,394,-14945,3034,-8,-13789,2492,-7, + 418,-2578,1320,4586,-80,2203,-7443,-829,1553,-4952,225,2354,7392,-826,1000,7503,-1078,-1, + 3553,-284,2096,-15188,3538,-8,-14882,3511,560,-8850,-1365,1350,9921,1287,775,-5383,3305,0, + -5693,3312,-1,-814,658,2016,-1309,2518,841,8859,-628,1749,10078,-4256,971,-1703,-2121,1320, + -1465,-1132,1905,-6827,3509,-2,-9979,4116,-4,-9937,3826,373,-6811,3305,399,-3722,3116,1079, + -3678,1476,2385,-2323,2967,948,-6865,2908,548,2623,-1784,1641,-7266,-1565,1357,8802,-3619,599, + 8680,-3915,1024,-3882,3356,0,-2366,3132,0,10509,311,-1,10467,-397,-1,2728,-543,1983, + -12310,3859,515,10371,-78,1579,-10033,-485,981,2442,1370,746,2469,1745,-1,-9951,3252,587, + 9804,1300,-1,-2031,-2505,481,976,-131,1983,-8748,282,883,10159,-3984,699,750,-1798,1641, + 3665,1233,791,4740,1374,721,5689,1448,738,11851,-4095,1464,3761,1878,-1,7189,1453,-1, + 6004,1752,-1,4902,1833,-1,8708,1679,-1,-3099,-2312,-1,-2074,-2601,-1,-1307,2708,-1, + -12381,4157,-6,2814,-1266,-1990,4044,-586,-2302,8465,-1353,-1008,-8130,506,-1335,-9516,-787,-603, + 781,1832,-687,-386,2191,-783,-4219,-727,-1940,-3239,-899,-1907,-2919,-1847,-1322,9209,149,-1832, + -12267,-801,-379,5178,759,-2169,4984,-76,-2284,-13619,3880,-552,-14840,3801,-467,-86,-2735,-482, + -9982,2519,-372,-12323,2264,-393,-3060,726,-2149,-1936,764,-2075,-12299,3172,-783,-13804,2668,-450, + -13687,3357,-749,-14951,3130,-410,16,-2416,-1322,4075,755,-2205,-8011,-546,-1559,-5475,403,-2356, + 7408,-846,-1001,3021,420,-2097,-14881,3511,-576,-9513,-786,-1355,9921,1287,-776,-1357,2518,-842, + -883,660,-2017,8859,-628,-1751,5027,-5438,-975,-1557,-1118,-1907,-1785,-2104,-1322,-6810,3305,-402, + -9928,3826,-381,-3791,3127,-1079,-2387,2971,-949,-4087,1581,-2385,-6864,2907,-552,1985,-1609,-1644, + -7930,-1282,-1363,4082,-4235,-1028,4363,-4076,-603,2123,242,-1984,-12309,3859,-527,10371,-78,-1581, + -10507,321,-985,2388,1453,-747,-9949,3253,-596,-2056,-2500,-482,593,557,-1984,-9093,817,-888, + 5225,-5228,-703,130,-1364,-1643,3615,1669,-792,4741,1690,-722,5777,1642,-739,6797,-5945,-1468, + -7129,-41,591,-6123,-934,252,-6828,-1059,416,6622,657,948,8766,369,1280,8773,758,682, + -5661,2818,660,-6233,2365,748,-5336,2173,1817,7096,-3152,291,5820,-3334,256,5980,-2541,214, + 5463,-4132,663,2869,-3394,1320,2466,-3534,647,-5558,-901,-2,-6294,629,451,-5967,882,-1, + -4149,-2103,-147,-4075,-2119,260,-9378,-1138,682,-8212,-939,525,-9052,-1665,571,-9377,-1137,1282, + -8200,-963,1450,-8157,-255,1336,-9450,-2512,717,-8749,-1999,676,-9055,-1711,1450,-9450,-2512,1257, + -8761,-2023,1292,-9940,-2518,628,-10731,-2678,656,6210,-1913,-1,7048,-2266,578,5773,-1895,-1, + 6936,-4441,546,6724,-4702,1016,6956,-3334,1762,5782,-3822,1759,6843,-4457,1526,8442,-3750,578, + 8224,-4305,437,9344,-5153,627,10201,-5287,639,10287,-5583,546,6664,-1638,2110,7225,-2606,1525, + 6395,-1229,1455,6188,-128,1817,7570,-828,1336,7884,-360,1369,-5429,1742,1803,-6366,1216,567, + -6107,1366,1547,-6415,772,1446,-5226,-566,2266,-6421,1079,-1,10812,-5488,590,10211,-5290,1296, + 10289,-5579,1443,10800,-5461,1443,9387,-5188,1347,9450,-4823,1204,8225,-4305,1522,8382,-3880,1290, + 9268,1017,1119,9928,1175,1436,9975,338,2003,10367,1239,1896,10097,574,1028,10301,1095,1485, + 9159,-2452,999,9455,-2719,854,9760,-2468,1018,-10631,-2721,1381,-10875,-2703,1004,5383,-2364,12, + 1853,-3197,12,5356,-3185,12,-6558,1743,-1,-6984,1782,426,-7006,1749,-1,-5531,-1951,448, + -2882,-2377,480,-3892,-1993,887,-7258,110,763,9687,-2965,415,9360,-2868,-1,-6009,2835,411, + -6616,-1781,571,-8181,-90,647,-9897,-2508,1404,-10026,-2232,1301,-10635,-2294,1538,-4074,-1597,1779, + -5338,-1837,1433,9412,-4801,754,7995,-4875,1486,9282,-5527,1281,5901,-745,1944,10339,-2756,-1, + 10734,-2354,-1,10319,-2748,447,-5981,-788,1866,-6370,1794,572,6544,-2614,2009,9612,-441,1879, + 10421,-2132,785,10184,-6055,1276,10051,-6113,985,10231,-6037,701,10919,-5910,545,11142,-5903,1055, + 10986,-5513,1058,-10977,-2253,980,-9965,-2137,977,-10030,-2237,675,9673,-3030,-1,9637,-1934,1562, + 10242,-1476,898,10131,-1653,1460,10343,-1449,427,10474,-741,731,10873,-2017,-84,10870,-2026,165, + -7798,215,-598,-7561,-813,-423,-6657,-791,-258,7056,585,-949,8773,758,-684,8766,369,-1282, + -5675,2822,-661,-5505,2218,-1818,-6233,2365,-749,3938,-2445,-292,4611,-2071,-216,3490,-2318,-257, + 2273,-2365,-664,1638,-2936,-649,1552,-2464,-1321,-6724,772,-456,-10048,-284,-686,-9820,-861,-575, + -8864,-308,-531,-10048,-283,-1286,-8663,370,-1342,-8861,-325,-1456,-10336,-1633,-720,-9581,-1243,-679, + -9821,-874,-1454,-9580,-1243,-1296,-10318,-1619,-1259,-10788,-1566,-630,-11525,-1820,-657,4713,-1883,-579, + 2082,-2769,-1017,2418,-2788,-547,3316,-2150,-1763,2350,-2722,-1527,2389,-1958,-1760,3267,-3758,-438, + 3848,-3618,-580,3203,-5157,-628,3831,-5824,-547,3845,-5558,-640,4534,-989,-2111,5712,-1022,-1456, + 4060,-1928,-1526,6307,90,-1818,7895,-347,-1370,7493,-565,-1337,-5829,1857,-1805,-6593,1531,-1550, + -6452,1247,-568,-7023,981,-1451,-5925,-401,-2270,4378,-5843,-591,3850,-5559,-1298,4376,-5822,-1444, + 3841,-5825,-1445,3197,-5208,-1349,3531,-5044,-1205,3277,-3756,-1523,3707,-3645,-1292,9268,1017,-1120, + 9928,1175,-1437,10367,1239,-1897,9975,338,-2004,10097,574,-1029,10301,1095,-1486,9159,-2452,-1000, + 9760,-2468,-1020,9455,-2719,-856,-11723,-1829,-1004,-11536,-1814,-1381,-6984,1782,-428,-6060,-1845,-453, + -4155,-1945,-890,-2955,-2366,-483,-7911,374,-770,9687,-2965,-416,-6009,2835,-412,-7470,-1524,-577, + -8657,518,-653,-10794,-1567,-1406,-11555,-1374,-1540,-10927,-1256,-1303,-6048,-1692,-1438,-4611,-1504,-1782, + 3530,-5007,-756,2682,-3900,-1487,2879,-5386,-1282,5678,-680,-1945,10319,-2748,-448,-6698,-588,-1872, + -6387,1798,-573,3644,-1388,-2011,9612,-441,-1880,10421,-2132,-786,3618,-6237,-1277,3668,-6232,-702, + 3473,-6254,-987,4594,-6326,-1057,4378,-6279,-546,4541,-5914,-1059,-11863,-1390,-981,-10925,-1253,-677, + -10845,-1143,-979,9637,-1934,-1564,10242,-1476,-899,10131,-1653,-1461,10474,-741,-732,10343,-1449,-428, + 3630,-2344,1987,5454,-2034,2298,8320,-2161,1007,8258,-2271,-1,8968,-2604,-1,-7127,-41,1329, + -8117,-1596,599,823,1480,686,983,1687,-1,-278,2133,-1,-315,1910,782,-2622,-2055,1320, + -2747,-1051,1905,-3414,-829,1939,9172,-750,1831,-10663,-2260,378,5096,-374,2167,5727,-1047,2281, + -13695,3237,-1,-14913,3138,-1,-14915,2988,458,-13693,2965,545,75,-3229,481,-171,-3224,-1, + -10099,1452,367,-12484,1260,387,-12439,1039,-1,-10058,1243,-1,-1626,633,2074,-2399,764,2149, + -12429,2166,777,-13801,2450,742,-13969,1772,442,-15078,2327,401,-15080,2231,-1,-13965,1594,-1, + 229,-2918,1320,4478,-735,2203,-6835,-1063,1553,-4489,258,2354,7338,-1568,1000,7430,-1809,-1, + 3435,-838,2096,-15282,2753,-1,-14979,2702,567,-8112,-1595,1350,9989,306,775,-5643,3100,-1, + -5946,3019,-1,-751,430,2016,-1243,2298,841,8757,-1482,1749,9130,-5751,971,-1692,-2349,1320, + -1416,-1349,1905,-7053,2991,-1,-10157,3050,-1,-10088,2762,376,-7002,2791,400,-3727,3014,1079, + -3406,1595,2385,-2275,2817,948,-6984,2391,549,2369,-2272,1641,-6616,-1780,1357,7995,-4875,599, + 7819,-5141,1024,-3932,3205,0,-2328,2937,0,10480,-722,-1,10370,-1422,-1,2572,-1004,1983, + -12420,2853,520,10305,-1096,1579,-9426,-930,982,2542,870,746,2612,1234,-1,-10079,2187,591, + 9873,331,-1,-2006,-2726,481,844,-464,1983,-8231,72,883,9258,-5497,699,559,-2142,1641, + 3723,616,791,4780,640,721,5657,701,738,10906,-5911,1464,3900,1215,-1,7116,719,-1, + 5968,968,-1,4984,1077,-1,8807,829,-1,-2969,-2502,-1,-2055,-2824,-1,-1260,2457,0, + -12481,3152,-1,2374,-1096,-1988,3706,-656,-2300,8320,-2161,-1008,-7799,215,-1336,-8882,-980,-604, + 823,1480,-687,-338,1905,-783,-3914,-764,-1941,-3056,-1019,-1907,-2803,-2033,-1322,9172,-750,-1832, + -11547,-1388,-379,5208,212,-2168,4854,-466,-2282,-13694,2965,-546,-14915,2988,-459,-290,-2980,-482, + -10099,1452,-368,-12480,1260,-388,-2842,786,-2149,-1822,633,-2075,-12429,2166,-778,-13968,1772,-443, + -13801,2450,-743,-15078,2327,-402,-170,-2632,-1321,4099,416,-2204,-7564,-811,-1560,-5159,389,-2356, + 7345,-1574,-1001,3014,103,-2097,-14979,2702,-568,-8882,-978,-1355,9989,306,-776,-1302,2294,-842, + -834,425,-2017,8757,-1483,-1751,2620,-5467,-972,-1529,-1345,-1907,-1794,-2340,-1322,-7002,2791,-401, + -10090,2762,-378,-3815,3018,-1079,-2355,2814,-949,-3929,1659,-2385,-6984,2391,-551,1585,-1534,-1642, + -7471,-1523,-1364,2348,-3904,-1026,2667,-3896,-600,2094,-27,-1984,-12420,2853,-522,10305,-1096,-1581, + -10059,-71,-986,2505,985,-747,-10079,2187,-592,-2038,-2723,-482,577,230,-1984,-8665,692,-889, + 2874,-5325,-701,15,-1595,-1642,3747,1091,-792,4841,1005,-722,5779,916,-739,4363,-6282,-1465, + -6308,-38,591,-5384,-934,252,-6006,-1058,416,6641,517,948,8816,-32,1280,8848,358,682, + -5863,2763,661,-6367,2205,749,-5112,2170,1817,5897,-3121,291,4689,-3153,257,5013,-2452,215, + 4251,-3874,663,2339,-3240,1320,1965,-3366,648,-5079,-878,-3,-5608,640,451,-5684,892,-1, + -3920,-2032,-147,-3780,-2049,260,-8494,-1111,683,-7350,-814,526,-8125,-1609,572,-8493,-1110,1283, + -7342,-844,1451,-7316,-133,1336,-8447,-2487,719,-7794,-1916,676,-8123,-1654,1451,-8447,-2487,1258, + -7803,-1940,1293,-8935,-2535,630,-9706,-2772,658,5951,-2138,-1,6093,-2274,579,5084,-1932,0, + 5475,-4317,546,5229,-4544,1017,5705,-3215,1762,4554,-3536,1759,5379,-4318,1527,7046,-3757,579, + 6798,-4284,438,7879,-5177,628,8734,-5303,640,8824,-5594,548,5860,-1504,2110,6154,-2566,1525, + 6033,-1278,1455,6134,-223,1816,7531,-1113,1336,7882,-687,1369,-4849,1739,1803,-6185,1159,567, + -5413,1361,1547,-5636,765,1446,-4421,-572,2266,-6337,1024,0,9352,-5511,591,8744,-5306,1298, + 8826,-5591,1445,9340,-5484,1444,7920,-5213,1348,7999,-4850,1205,6799,-4284,1522,6978,-3877,1291, + 9364,577,1119,10034,688,1436,10021,-151,2003,10476,720,1896,10160,76,1028,10400,581,1485, + 9014,-2870,999,9289,-3158,854,9610,-2930,1018,-9602,-2805,1383,-9847,-2811,1006,4523,-2309,12, + 1329,-2962,12,4324,-3027,12,-6609,1560,1,-7050,1545,429,-7072,1512,3,-4909,-1939,447, + -2725,-2282,480,-3521,-1926,887,-6436,117,763,9503,-3421,415,9185,-3300,-1,-6234,2700,412, + -5819,-1774,571,-7343,31,647,-8893,-2522,1406,-9049,-2257,1302,-9649,-2381,1541,-3497,-1562,1779, + -4627,-1835,1433,7963,-4827,755,6546,-4836,1486,7806,-5552,1282,5606,-654,1944,10166,-3260,-1, + 10586,-2890,-1,10146,-3251,447,-5161,-798,1866,-6360,1633,573,5498,-2412,2010,9604,-902,1879, + 10289,-2646,785,8717,-6061,1277,8586,-6117,987,8764,-6043,703,9449,-5936,546,9672,-5937,1057, + 9527,-5542,1060,-9993,-2373,982,-8996,-2156,978,-9053,-2263,677,9484,-3485,-1,9523,-2390,1562, + 10158,-1979,898,10034,-2148,1460,10260,-1959,427,10441,-1262,731,10749,-2564,-84,10745,-2573,165, + -7482,324,-600,-7287,-718,-425,-6326,-739,-259,7093,298,-949,8848,358,-684,8816,-32,-1282, + -5888,2765,-660,-5394,2205,-1817,-6371,2205,-747,3251,-2486,-292,3905,-2065,-215,2822,-2215,-257, + 1703,-2152,-664,1191,-2707,-649,1226,-2269,-1321,-6342,834,-456,-9774,-354,-691,-9501,-911,-580, + -8594,-282,-534,-9773,-352,-1291,-8439,407,-1344,-8590,-295,-1459,-9959,-1721,-728,-9232,-1273,-685, + -9500,-923,-1460,-9230,-1271,-1301,-9940,-1705,-1267,-10415,-1688,-638,-11141,-1969,-667,4102,-2035,-579, + 1398,-2527,-1017,1725,-2608,-547,2719,-2141,-1763,1670,-2531,-1527,1898,-1804,-1760,2370,-3720,-439, + 2968,-3694,-580,2031,-5079,-628,2607,-5718,-548,2619,-5462,-640,4147,-1205,-2111,5465,-1332,-1456, + 3489,-2052,-1526,6319,-192,-1818,7895,-687,-1370,7465,-921,-1337,-5518,1864,-1805,-6231,1573,-1551, + -6327,1195,-568,-6673,1047,-1452,-5638,-385,-2271,3162,-5666,-591,2627,-5462,-1298,3157,-5645,-1444, + 2621,-5716,-1445,2014,-5126,-1349,2373,-5028,-1205,2385,-3718,-1523,2824,-3693,-1292,9364,577,-1120, + 10034,688,-1437,10476,720,-1897,10021,-151,-2004,10160,76,-1029,10400,581,-1486,9014,-2870,-1000, + 9610,-2931,-1020,9289,-3158,-856,-11341,-1985,-1015,-11152,-1962,-1391,-7052,1546,-424,-5785,-1791,-454, + -3987,-1878,-890,-2857,-2273,-483,-7592,490,-772,9503,-3421,-416,-6236,2700,-410,-7192,-1444,-579, + -8443,559,-655,-10420,-1688,-1414,-11186,-1524,-1549,-10565,-1389,-1311,-5833,-1641,-1439,-4448,-1477,-1782, + 2381,-4994,-756,1775,-3747,-1487,1678,-5257,-1282,5585,-954,-1945,10146,-3251,-448,-6416,-537,-1873, + -6389,1637,-571,3172,-1446,-2011,9604,-902,-1880,10289,-2646,-786,2435,-6147,-1277,2485,-6138,-702, + 2287,-6174,-987,3435,-6107,-1057,3213,-6097,-546,3326,-5706,-1059,-11497,-1551,-991,-10564,-1387,-685, + -10492,-1275,-987,9523,-2390,-1564,10158,-1979,-899,10034,-2148,-1461,10441,-1262,-732,10260,-1959,-428, + 3176,-2167,1987,4675,-1708,2298,8201,-2520,1007,8134,-2625,-1,8814,-3008,-1,-6306,-37,1329, + -7210,-1475,599,866,1581,686,1034,1784,-1,-249,2256,-1,-261,2029,782,-2425,-1961,1320, + -2418,-976,1905,-2856,-791,1939,9142,-1177,1831,-9680,-2350,380,4848,-161,2167,5204,-812,2281, + -13720,2842,17,-14940,2825,19,-14950,2675,478,-13734,2571,562,-99,-3072,481,-292,-3072,-1, + -10195,915,377,-12606,796,401,-12569,574,14,-10153,706,9,-1392,724,2074,-1853,804,2149, + -12520,1700,791,-13872,2064,759,-14081,1398,460,-15155,2026,421,-15163,1930,20,-14089,1220,17, + 66,-2759,1320,4159,-479,2202,-6011,-1056,1553,-3669,248,2354,7248,-1833,1000,7347,-2109,-1, + 3180,-628,2095,-15332,2463,20,-15032,2393,587,-7208,-1475,1351,10032,-184,775,-5936,3050,0, + -6205,2894,1,-649,537,2016,-1161,2419,841,8679,-1876,1749,7650,-5774,972,-1615,-2233,1320, + -1311,-1241,1905,-7241,2744,3,-10268,2506,9,-10185,2222,386,-7170,2548,403,-3692,3118,1079, + -2708,1602,2385,-2206,2945,948,-7112,2152,553,2070,-2123,1641,-5820,-1774,1357,6546,-4836,600, + 6354,-5095,1025,-4032,3321,-1,-2302,3051,0,10450,-1244,-1,10290,-1935,-1,2294,-739,1983, + -12488,2386,535,10248,-1605,1579,-8560,-908,983,2617,893,746,2730,1222,-1,-10171,1649,601, + 9919,-151,-1,-1947,-2615,481,660,-206,1983,-7390,194,883,7782,-5521,700,337,-1932,1641, + 3710,670,791,4783,591,721,5668,575,738,9436,-5936,1465,4020,1095,-1,7155,429,-1, + 6029,716,-1,5068,900,-1,8887,427,-1,-2845,-2406,-1,-2010,-2712,-1,-1224,2579,0, + -12540,2686,14,2119,-971,-1988,3460,-733,-2300,8201,-2520,-1008,-7483,325,-1337,-8569,-949,-608, + 866,1581,-687,-301,2017,-783,-3735,-762,-1941,-2962,-971,-1907,-2746,-1948,-1322,9142,-1177,-1832, + -11179,-1540,-388,5212,-12,-2168,4701,-696,-2282,-13736,2571,-529,-14952,2675,-439,-366,-2831,-482, + -10195,915,-358,-12604,796,-373,-2620,745,-2148,-1733,683,-2074,-12523,1700,-763,-14083,1398,-425, + -13875,2064,-725,-15157,2026,-382,-245,-2482,-1321,4101,289,-2204,-7289,-716,-1562,-4830,362,-2357, + 7260,-1897,-1001,3025,95,-2097,-15035,2393,-549,-8568,-946,-1359,10032,-184,-776,-1263,2396,-842, + -793,513,-2017,8679,-1876,-1751,1416,-5305,-972,-1510,-1250,-1907,-1797,-2231,-1322,-7172,2548,-398, + -10196,2221,-368,-3841,3103,-1080,-2344,2919,-948,-3596,1597,-2384,-7114,2152,-547,1376,-1353,-1642, + -7192,-1443,-1366,1440,-3683,-1026,1754,-3738,-600,2104,41,-1984,-12491,2386,-507,10248,-1605,-1581, + -9802,-142,-991,2606,987,-747,-10174,1648,-582,-2003,-2613,-482,593,343,-1984,-8462,731,-891, + 1678,-5182,-701,-22,-1456,-1642,3857,990,-792,4933,812,-722,5843,671,-739,3199,-6100,-1465, + -5803,127,590,-4878,-754,247,-5463,-883,411,6917,860,947,8871,158,1281,8930,548,682, + -5817,3026,661,-6356,2485,749,-4995,2419,1819,4644,-2986,287,3616,-2648,253,4212,-2238,211, + 2991,-3062,660,1865,-2710,1319,1545,-2870,647,-4676,-723,-5,-5245,818,452,-5604,1091,0, + -3704,-1825,-150,-3542,-1823,257,-7882,-1005,679,-6777,-587,522,-7462,-1461,566,-7881,-1006,1279, + -6772,-626,1447,-6786,85,1334,-7684,-2368,711,-7100,-1731,670,-7454,-1506,1445,-7683,-2369,1250, + -7106,-1757,1286,-8161,-2471,622,-8889,-2819,650,5889,-2102,-1,5301,-2407,575,4643,-1928,-3, + 3724,-3775,542,3420,-3888,1012,4433,-2885,1758,3374,-2740,1755,3637,-3737,1522,5384,-3685,574, + 5036,-4116,433,5913,-5208,623,6724,-5441,635,6777,-5734,542,5535,-1534,2106,5187,-2576,1521, + 6122,-1362,1452,6395,104,1816,7578,-862,1336,7906,-425,1369,-4562,1964,1806,-6131,1401,569, + -5069,1555,1550,-5188,943,1447,-3907,-354,2262,-6297,1259,0,7309,-5681,586,6734,-5446,1292, + 6779,-5733,1439,7299,-5654,1439,5945,-5252,1343,6098,-4913,1200,5037,-4116,1518,5294,-3775,1286, + 9468,717,1119,10145,774,1436,10065,-62,2003,10588,770,1896,10221,153,1028,10501,638,1485, + 8853,-2682,999,9103,-2992,854,9439,-2792,1018,-8784,-2840,1374,-9022,-2879,998,3847,-2123,10, + 996,-2532,11,3375,-2573,9,-6552,1817,1,-6968,1749,429,-6981,1710,2,-4451,-1743,441, + -2606,-2029,478,-3256,-1681,882,-5935,278,762,9296,-3271,415,8990,-3124,-1,-6226,2985,412, + -5271,-1586,563,-6820,248,645,-8122,-2454,1398,-8317,-2209,1295,-8893,-2426,1533,-3109,-1305,1773, + -4138,-1621,1425,6068,-4883,750,4683,-4595,1481,5770,-5567,1277,5941,-507,1941,9963,-3168,-1, + 10408,-2837,-1,9945,-3157,447,-4629,-607,1862,-6347,1907,573,4730,-2099,2005,9588,-777,1879, + 10132,-2570,785,6632,-6192,1272,6494,-6237,981,6680,-6178,697,7379,-6111,541,7602,-6121,1051, + 7480,-5720,1054,-9231,-2468,975,-8277,-2097,971,-8321,-2213,670,9272,-3333,-1,9392,-2248,1562, + 10055,-1894,898,9918,-2053,1460,10158,-1883,427,10394,-1203,731,10597,-2525,-84,10592,-2533,165, + -7055,414,-601,-6866,-634,-427,-5910,-653,-261,7108,621,-949,8930,548,-684,8871,159,-1282, + -5837,3020,-661,-5251,2389,-1817,-6359,2485,-748,2805,-2381,-292,3415,-1927,-217,2381,-1941,-258, + 1398,-1793,-665,949,-2312,-649,1110,-1935,-1321,-5994,934,-457,-9297,-515,-694,-8933,-1017,-583, + -8149,-242,-536,-9295,-513,-1294,-8082,455,-1347,-8148,-255,-1461,-9248,-1894,-730,-8606,-1328,-687, + -8929,-1030,-1462,-8604,-1327,-1303,-9231,-1876,-1269,-9703,-1938,-641,-10375,-2330,-669,3712,-2045,-580, + 1038,-2158,-1017,1345,-2295,-547,2385,-1990,-1763,1304,-2211,-1527,1668,-1530,-1760,1741,-3501,-438, + 2336,-3602,-580,1111,-4755,-628,1066,-5721,-547,1289,-5542,-640,3942,-1229,-2111,5355,-1338,-1456, + 3158,-2004,-1526,6338,49,-1818,7899,-428,-1370,7447,-726,-1337,-5202,1971,-1805,-5872,1665,-1551, + -6262,1413,-568,-6248,1122,-1453,-5222,-334,-2272,1467,-6070,-591,1286,-5551,-1298,1478,-6051,-1444, + 1063,-5729,-1445,1087,-4804,-1349,1458,-4795,-1205,1778,-3502,-1523,2193,-3572,-1292,9468,717,-1120, + 10145,774,-1437,10588,770,-1897,10065,-62,-2004,10221,153,-1029,10501,638,-1486,8853,-2682,-1000, + 9439,-2792,-1020,9103,-2992,-856,-10571,-2378,-1018,-10387,-2326,-1394,-6969,1749,-425,-5419,-1681,-456, + -3783,-1699,-891,-2758,-2041,-484,-7167,583,-773,9296,-3271,-416,-6227,2985,-411,-6757,-1364,-581, + -8103,608,-657,-9708,-1939,-1417,-10489,-1899,-1552,-9897,-1669,-1314,-5475,-1557,-1440,-4172,-1368,-1783, + 1472,-4754,-756,1179,-3414,-1487,725,-4790,-1282,5547,-794,-1945,9945,-3157,-448,-5995,-477,-1875, + -6373,1906,-572,2925,-1359,-2011,9588,-777,-1880,10132,-2570,-786,605,-5900,-1277,645,-5930,-702, + 482,-5812,-987,1361,-6572,-1057,1205,-6414,-546,1560,-6207,-1059,-10793,-1974,-994,-9897,-1667,-688, + -9845,-1545,-989,9393,-2248,-1564,10055,-1894,-899,9918,-2053,-1461,10394,-1203,-732,10158,-1883,-428, + 2911,-1570,1985,4459,-1147,2295,8077,-2267,1007,8006,-2365,-1,8647,-2802,-1,-5800,125,1327, + -6577,-1242,593,845,1833,686,1012,2036,-1,-262,2518,-1,-258,2295,781,-2293,-1692,1317, + -2208,-697,1902,-2496,-516,1934,9107,-1010,1831,-8928,-2397,373,5295,333,2165,5411,-586,2278, + -13667,2445,12,-14884,2381,14,-14888,2231,473,-13670,2174,557,-286,-2699,481,-399,-2757,-1, + -10039,737,374,-12446,451,398,-12394,231,10,-9980,532,6,-1262,1012,2073,-1535,1108,2147, + -12420,1357,788,-13786,1662,755,-13967,988,455,-15067,1575,416,-15071,1479,15,-13967,809,13, + -110,-2367,1320,4406,200,2201,-5467,-879,1547,-3175,490,2352,7266,-1596,999,7278,-1796,-1, + 3180,63,2094,-15261,2005,15,-14959,1947,582,-6577,-1245,1345,10073,-95,775,-5887,3313,0, + -6196,3179,0,-606,808,2015,-1129,2700,841,8593,-1666,1749,5578,-5762,967,-1571,-1965,1318, + -1255,-970,1904,-7299,2912,2,-10247,2312,7,-10137,2037,384,-7204,2729,403,-3635,3374,1079, + -2329,1883,2387,-2167,3227,948,-7099,2342,552,1870,-1633,1641,-5272,-1589,1350,4683,-4595,595, + 4434,-4807,1020,-4019,3552,-1,-2284,3318,0,10404,-1185,-1,10189,-1861,-1,2194,0,1982, + -12435,2043,531,10174,-1529,1579,-7969,-811,979,2572,1224,746,2673,1504,-1,-10073,1469,598, + 9962,-53,-1,-1899,-2361,479,538,405,1982,-6871,409,881,5752,-5532,695,115,-1403,1640, + 3779,1207,790,4907,1066,720,5821,988,737,7366,-6110,1460,3970,1409,-1,7166,750,-1, + 6025,1053,-1,5072,1259,-1,8975,613,-1,-2739,-2163,-3,-1967,-2463,-2,-1213,2846,0, + -12505,2339,10,2036,-702,-1988,3377,-608,-2300,8077,-2267,-1008,-7054,414,-1339,-8030,-900,-610, + 845,1833,-687,-300,2269,-782,-3461,-658,-1941,-2814,-800,-1907,-2660,-1733,-1323,9107,-1010,-1832, + -10481,-1913,-391,5236,209,-2168,4647,-610,-2282,-13672,2173,-533,-14890,2231,-444,-412,-2557,-482, + -10038,737,-361,-12443,450,-377,-2337,837,-2148,-1627,877,-2074,-12423,1357,-767,-13969,987,-430, + -13789,1661,-730,-15069,1575,-387,-284,-2207,-1321,4132,495,-2204,-6866,-630,-1563,-4395,396,-2357, + 7191,-1637,-1001,3024,374,-2097,-14961,1947,-554,-8029,-897,-1361,10073,-95,-776,-1234,2642,-842, + -765,743,-2017,8593,-1666,-1751,462,-4713,-972,-1482,-1020,-1907,-1782,-1995,-1322,-7206,2729,-399, + -10150,2035,-370,-3775,3307,-1079,-2303,3151,-948,-3210,1646,-2384,-7101,2342,-548,1314,-1032,-1642, + -6758,-1364,-1367,840,-3263,-1026,1133,-3384,-600,2091,327,-1984,-12437,2043,-511,10174,-1529,-1581, + -9360,-311,-994,2561,1268,-747,-10076,1468,-585,-1963,-2368,-483,570,598,-1984,-8137,775,-893, + 744,-4766,-701,-47,-1187,-1642,3830,1293,-792,4926,1117,-722,5857,979,-739,1193,-6406,-1465, + -5506,169,564,-4473,-518,223,-5015,-773,377,7008,1702,948,8888,1096,1281,8968,1487,682, + -5794,3585,661,-6320,3044,749,-5050,2933,1824,3224,-2175,292,2526,-1622,257,3202,-1594,215, + 1932,-1613,664,1549,-1749,1320,1265,-2034,648,-4315,-367,-14,-5122,1041,444,-5544,1561,1, + -3485,-1301,-156,-3303,-1286,246,-7370,-1012,653,-6326,-460,494,-6897,-1411,538,-7367,-1013,1253, + -6316,-511,1418,-6387,195,1307,-6995,-2336,681,-6504,-1635,640,-6879,-1455,1417,-6992,-2338,1221, + -6503,-1662,1256,-7452,-2503,594,-8088,-2996,622,5647,-1543,0,4094,-2007,579,3875,-1556,0, + 2028,-2336,548,1711,-2301,1018,3070,-1884,1763,2341,-1369,1760,1969,-2262,1528,3564,-2949,580, + 3080,-3198,439,3447,-4550,630,4116,-4995,642,4083,-5280,550,4724,-1259,2110,3887,-1990,1526, + 5778,-1103,1455,6478,864,1816,7555,65,1336,7891,563,1369,-4614,2333,1810,-6118,1901,570, + -5073,1784,1547,-5061,1084,1435,-3580,-20,2242,-6212,1797,0,4617,-5286,593,4124,-5003,1300, + 4086,-5282,1447,4612,-5259,1446,3460,-4601,1350,3734,-4351,1207,3080,-3199,1524,3448,-2993,1292, + 9527,1603,1119,10206,1614,1436,10069,786,2003,10648,1579,1896,10239,990,1028,10552,1453,1485, + 8699,-1726,999,8926,-2054,854,9268,-1881,1018,-7983,-3001,1346,-8204,-3084,970,2954,-1600,13, + 654,-2044,12,2385,-1673,13,-6444,2358,0,-6824,2224,428,-6829,2181,1,-4010,-1400,415, + -2503,-1429,472,-2990,-1130,869,-5655,298,736,9098,-2347,415,8807,-2178,-1,-6215,3549,411, + -4775,-1432,528,-6434,353,619,-7415,-2483,1369,-7664,-2269,1268,-8180,-2617,1507,-2770,-771,1754, + -3686,-1277,1396,3719,-4313,757,2568,-3495,1488,3179,-4832,1284,5883,-175,1944,9761,-2297,-1, + 10220,-2007,-1,9743,-2284,447,-4243,-389,1835,-6304,2458,573,3730,-1352,2010,9544,105,1879, + 9963,-1721,785,3858,-5711,1279,3709,-5728,989,3908,-5707,705,4637,-5719,548,4858,-5743,1059, + 4777,-5333,1061,-8496,-2727,949,-7643,-2144,944,-7669,-2273,642,9071,-2407,-1,9254,-1339,1562, + 9932,-1042,898,9785,-1191,1460,10036,-1038,427,10319,-375,731,10429,-1708,-84,10424,-1716,165, + -6663,716,-593,-6435,-327,-419,-5494,-358,-259,7026,1663,-949,8967,1487,-684,8888,1096,-1282, + -5795,3575,-661,-5121,2880,-1817,-6322,3044,-748,2201,-2182,-292,2684,-1572,-215,1770,-1587,-257, + 946,-1492,-664,660,-1890,-649,912,-1564,-1321,-5681,1263,-453,-8605,-756,-687,-8069,-1068,-574, + -7673,-43,-530,-8602,-756,-1288,-7815,616,-1342,-7675,-57,-1455,-8004,-1999,-718,-7644,-1220,-677, + -8059,-1081,-1454,-7642,-1220,-1294,-7995,-1977,-1258,-8404,-2222,-630,-8872,-2844,-657,3086,-1801,-579, + 588,-1909,-1017,868,-2078,-547,1857,-1821,-1763,837,-1995,-1527,1209,-1324,-1760,960,-3278,-438, + 1524,-3525,-579,-11,-4308,-628,-847,-4924,-547,-541,-5009,-640,3408,-964,-2111,4977,-832,-1456, + 2623,-1804,-1526,6183,943,-1818,7867,574,-1370,7315,202,-1337,-4924,2361,-1803,-5551,2009,-1547, + -6162,1928,-567,-5863,1425,-1447,-4809,-26,-2268,-941,-5436,-591,-562,-5013,-1297,-920,-5435,-1444, + -870,-4920,-1444,-53,-4355,-1348,300,-4455,-1205,1071,-3293,-1523,1385,-3463,-1291,9527,1603,-1120, + 10206,1614,-1437,10648,1579,-1897,10069,786,-2004,10239,990,-1029,10552,1453,-1486,8699,-1726,-1000, + 9268,-1880,-1020,8926,-2054,-856,-9034,-2965,-1006,-8882,-2846,-1382,-6825,2224,-426,-5025,-1323,-456, + -3564,-1197,-893,-2674,-1462,-487,-6788,885,-765,9098,-2347,-416,-6216,3549,-411,-6273,-1052,-573, + -7879,759,-653,-8406,-2227,-1406,-9141,-2492,-1542,-8683,-2054,-1303,-5086,-1209,-1436,-3880,-951,-1782, + 332,-4412,-755,526,-3104,-1487,-333,-4189,-1282,5302,-55,-1945,9743,-2284,-448,-5573,-185,-1869, + -6310,2457,-572,2367,-1156,-2011,9544,105,-1880,9963,-1721,-786,-1286,-4623,-1277,-1289,-4674,-702, + -1280,-4471,-986,-1411,-5635,-1056,-1373,-5415,-546,-998,-5590,-1059,-9395,-2677,-983,-8685,-2050,-678, + -8685,-1920,-980,9254,-1338,-1564,9932,-1042,-899,9785,-1191,-1461,10319,-375,-732,10036,-1038,-428, + 2737,-648,1987,4072,-404,2299,7965,-1258,1007,7897,-1348,-1,8492,-1830,-1,-5505,161,1301, + -6047,-1103,562,832,2349,686,994,2549,-1,-231,3079,-1,-225,2874,782,-2182,-1071,1309, + -2048,-56,1894,-2246,93,1923,9048,-88,1831,-8221,-2593,346,5486,904,2167,5205,-273,2281, + -13436,2313,10,-14650,2254,11,-14655,2104,470,-13438,2042,555,-457,-2003,481,-535,-2163,-1, + -9759,751,372,-12167,331,395,-12104,115,8,-9684,553,5,-1179,1692,2073,-1439,1832,2150, + -12185,1236,785,-13552,1530,752,-13730,856,452,-14833,1448,413,-14837,1352,12,-13728,676,10, + -254,-1657,1319,4538,984,2202,-5023,-775,1514,-2986,922,2344,7221,-696,1000,7237,-745,-1, + 3202,971,2095,-15027,1878,12,-14725,1820,579,-6047,-1108,1314,10075,752,775,-5858,3874,0, + -6193,3743,0,-570,1417,2015,-1080,3347,842,8499,-699,1749,2931,-4950,974,-1559,-1364,1314, + -1223,-357,1901,-7325,3321,2,-10098,2292,5,-9958,2033,382,-7203,3157,402,-3676,3996,1083, + -2362,2483,2396,-2173,3901,951,-7042,2791,551,1778,-826,1641,-4777,-1440,1314,2568,-3496,602, + 2254,-3589,1027,-4014,4141,-1,-2270,3959,1,10330,-358,-1,10069,-1018,-1,2186,942,1983, + -12234,1920,529,10077,-685,1579,-7479,-830,954,2430,1887,746,2487,2141,-1,-9847,1475,597, + 9967,802,-1,-1881,-1776,475,545,1231,1983,-6491,512,856,3174,-4794,702,-10,-572,1640, + 3751,2050,791,4890,1928,721,5818,1880,738,4624,-5717,1467,3757,2187,-1,7063,1794,-1, + 5875,2026,-1,4901,2139,-1,9016,1549,-1,-2650,-1574,-8,-1957,-1884,-5,-1157,3462,0, + -12318,2211,8,1644,-385,-1988,2929,-261,-2300,7965,-1258,-1008,-6662,714,-1331,-7334,-610,-601, + 832,2349,-687,-259,2823,-783,-3178,-231,-1941,-2662,-290,-1908,-2591,-1175,-1323,9048,-87,-1832, + -9130,-2498,-381,5000,966,-2168,4275,-91,-2282,-13440,2041,-535,-14656,2104,-447,-516,-2037,-482, + -9758,751,-363,-12163,331,-379,-2054,1278,-2150,-1496,1414,-2076,-12187,1236,-769,-13731,856,-432, + -13554,1530,-732,-14834,1448,-390,-380,-1702,-1321,3805,1082,-2204,-6438,-321,-1555,-3984,709,-2356, + 7100,-645,-1001,2804,959,-2097,-14727,1820,-556,-7335,-610,-1353,10075,752,-776,-1155,3230,-842, + -720,1284,-2017,8499,-699,-1751,-497,-4000,-972,-1469,-470,-1907,-1802,-1439,-1322,-7204,3157,-399, + -9975,2028,-372,-3722,3867,-1080,-2248,3751,-949,-2869,2018,-2385,-7043,2790,-549,1075,-650,-1642, + -6275,-1055,-1360,153,-2801,-1026,397,-2994,-600,1921,861,-1984,-12236,1920,-513,10077,-685,-1581, + -8744,-596,-988,2382,1889,-747,-9850,1474,-587,-1957,-1795,-486,533,1115,-1984,-7948,907,-890, + -355,-4207,-700,-125,-682,-1643,3603,2029,-792,4700,1949,-722,5682,1902,-739,-1373,-5401,-1465, + -5433,265,529,-4231,-97,203,-4714,-513,345,6653,2399,948,8785,2078,1281,8851,2474,682, + -6151,3898,662,-6517,3241,750,-5370,3378,1823,1307,-2171,292,864,-1466,257,1528,-1521,215, + 411,-1305,664,984,-1177,1321,732,-1475,648,-4009,149,-18,-5197,1310,427,-5429,1979,2, + -3251,-521,-161,-3080,-462,241,-6951,-1116,622,-6055,-349,461,-6401,-1402,509,-6948,-1115,1222, + -6035,-404,1384,-6236,274,1272,-6280,-2317,655,-5969,-1533,611,-6374,-1433,1388,-6277,-2316,1194, + -5961,-1555,1227,-6680,-2586,568,-7106,-3267,598,5073,-1021,0,2203,-2112,580,2452,-1410,0, + 33,-2109,547,-274,-2004,1018,1146,-1871,1763,707,-1170,1760,-10,-2025,1527,1372,-3095,580, + 830,-3206,439,807,-4604,629,1357,-5130,640,1284,-5395,548,2895,-1509,2111,1925,-2108,1527, + 4696,-1007,1455,6073,1438,1817,7397,930,1336,7781,1507,1369,-5022,2723,1802,-6127,2212,567, + -5383,2022,1530,-5250,1276,1410,-3547,588,2226,-6099,2137,2,1818,-5380,591,1364,-5139,1298, + 1286,-5400,1445,1813,-5357,1444,805,-4657,1349,1138,-4490,1205,830,-3207,1523,1245,-3107,1292, + 9416,2594,1119,10095,2618,1436,9974,1788,2003,10537,2593,1896,10141,1995,1028,10444,2465,1485, + 8667,-740,999,8899,-1064,854,9234,-885,1018,-7011,-3241,1322,-7184,-3389,946,1433,-1442,13, + -35,-1592,12,850,-1466,13,-6524,2554,1,-6905,2359,429,-6907,2319,2,-3656,-783,399, + -2423,-540,468,-2756,-256,863,-5607,352,700,9077,-1354,415,8785,-1189,-1,-6530,3754,413, + -4365,-1087,498,-6308,415,582,-6652,-2556,1343,-6974,-2424,1242,-7332,-2945,1482,-2600,135,1746, + -3365,-618,1377,1134,-4450,756,252,-3351,1487,477,-4816,1282,4872,-104,1945,9733,-1294,-1, + 10183,-1001,-1,9716,-1283,447,-4086,46,1811,-6374,2677,574,1873,-1449,2011,9464,1096,1879, + 9920,-721,785,1046,-5822,1277,893,-5830,987,1097,-5819,703,1851,-5810,546,2075,-5814,1057, + 1975,-5410,1060,-7582,-3155,924,-6990,-2289,918,-6977,-2430,616,9051,-1414,-1,9206,-346,1562, + 9875,-42,898,9731,-194,1460,9979,-36,427,10248,632,731,10386,-699,-84,10381,-707,165, + -6365,929,-560,-6014,-75,-395,-5084,-20,-248,6843,2580,-949,8851,2474,-684,8784,2078,-1282, + -6121,3886,-659,-5146,3320,-1811,-6522,3240,-747,1090,-1821,-292,1364,-1300,-215,636,-1249,-257, + 28,-1113,-664,27,-1430,-649,398,-1069,-1321,-5422,1602,-432,-7640,-1272,-692,-7021,-1264,-571, + -7217,-180,-521,-7630,-1267,-1292,-7620,324,-1335,-7208,-184,-1445,-6484,-2029,-714,-6577,-1175,-668, + -6995,-1264,-1450,-6568,-1170,-1284,-6481,-2001,-1253,-6714,-2426,-631,-6804,-3199,-665,1923,-1369,-579, + -303,-1468,-1017,-66,-1658,-547,818,-1413,-1763,-88,-1581,-1527,265,-937,-1760,-316,-2727,-438, + 162,-3124,-580,-1590,-3376,-628,-2593,-3638,-547,-2340,-3831,-640,2350,-412,-2111,4204,-181,-1456, + 1573,-1328,-1526,5856,1781,-1818,7756,1536,-1370,7066,1127,-1337,-4798,2784,-1786,-5350,2362,-1521, + -6041,2263,-562,-5606,1727,-1417,-4435,384,-2260,-2861,-4083,-591,-2362,-3827,-1298,-2840,-4089,-1444, + -2613,-3625,-1445,-1646,-3406,-1349,-1347,-3618,-1205,-116,-2780,-1523,39,-3030,-1291,9416,2594,-1120, + 10095,2618,-1437,10537,2593,-1897,9974,1788,-2004,10141,1995,-1029,10444,2465,-1486,8667,-740,-1000, + 9234,-885,-1020,8899,-1064,-855,-6878,-3383,-1015,-6803,-3201,-1390,-6907,2359,-425,-4564,-835,-453, + -3279,-472,-897,-2587,-627,-491,-6523,1080,-729,9077,-1354,-416,-6532,3754,-410,-5693,-757,-555, + -7746,427,-646,-6704,-2427,-1407,-7202,-3025,-1553,-7030,-2419,-1308,-4615,-752,-1430,-3542,-351,-1787, + -1302,-3588,-756,-559,-2504,-1487,-1849,-3157,-1282,4814,712,-1945,9716,-1282,-448,-5170,143,-1853, + -6351,2679,-570,1272,-687,-2011,9464,1096,-1880,9920,-721,-786,-2897,-3197,-1277,-2918,-3243,-702, + -2836,-3057,-987,-3367,-4107,-1057,-3258,-3913,-546,-2964,-4207,-1059,-7335,-3318,-998,-7042,-2420,-683, + -7103,-2307,-985,9206,-346,-1564,9875,-42,-899,9731,-194,-1461,10248,632,-732,9979,-36,-428, + 2143,-181,1987,2610,-441,2300,7929,-285,1007,7869,-375,-1,8465,-846,-1,-5435,254,1266, + -5638,-927,530,879,3001,686,1046,3192,-1,-150,3823,-1,-165,3654,783,-2132,-142,1307, + -2023,927,1895,-2239,1085,1922,8972,897,1831,-7375,-2938,321,4639,1050,2168,3809,-426,2282, + -13315,2079,18,-14525,2121,21,-14540,1971,479,-13335,1808,563,-730,-1279,481,-779,-1447,-1, + -9688,517,377,-12107,30,402,-12040,-185,15,-9604,325,9,-1219,2681,2077,-1720,2945,2160, + -12139,932,792,-13483,1308,760,-13705,649,461,-14768,1330,423,-14779,1235,21,-13715,468,19, + -503,-932,1319,3772,1217,2203,-4731,-518,1481,-3226,1641,2337,7059,126,1000,7214,208,-1, + 2739,1441,2096,-14930,1773,22,-14632,1693,589,-5639,-932,1281,9981,1754,775,-6270,4173,1, + -6551,3951,2,-564,2261,2017,-1078,4241,845,8439,280,1750,210,-4878,972,-1612,-497,1314, + -1250,519,1901,-7546,3385,4,-10111,2023,10,-9950,1778,387,-7404,3236,404,-4028,4750,1086, + -2935,3364,2402,-2377,4827,954,-7195,2893,553,1453,-238,1641,-4370,-1097,1284,252,-3351,601, + -74,-3357,1026,-4258,4831,-1,-2395,4830,1,10259,649,-1,10012,-16,-1,1783,1462,1983, + -12202,1613,536,10012,317,1579,-7098,-963,923,2327,2535,746,2411,2804,-1,-9809,1233,601, + 9873,1801,-1,-1915,-934,473,365,1863,1983,-6389,564,819,480,-4777,701,-280,128,1641, + 3480,2617,791,4593,2509,721,5506,2536,738,1838,-5810,1465,3628,2880,-1,6861,2714,-1, + 5665,2830,-1,4690,2840,-1,8899,2537,-1,-2571,-714,-12,-1994,-1054,-8,-1106,4306,0, + -12289,1901,15,832,78,-1988,2002,272,-2300,7929,-285,-1008,-6370,932,-1297,-6668,-497,-586, + 879,3001,-687,-172,3572,-784,-2887,422,-1950,-2491,453,-1916,-2507,-384,-1329,8972,898,-1832, + -7204,-3033,-392,4533,1690,-2168,3523,556,-2282,-13338,1808,-527,-14543,1971,-438,-728,-1333,-482, + -9688,518,-358,-12103,30,-372,-1829,2013,-2164,-1349,2203,-2084,-12143,932,-762,-13707,649,-424, + -13486,1307,-724,-14769,1330,-380,-577,-1020,-1322,3190,1704,-2204,-6027,-62,-1531,-3677,1183,-2352, + 6977,288,-1001,2446,1575,-2097,-14635,1693,-547,-6661,-493,-1338,9981,1754,-776,-1069,4053,-846, + -647,2031,-2022,8440,280,-1751,-1936,-2925,-972,-1456,296,-1912,-1844,-663,-1326,-7406,3236,-397, + -9973,1770,-367,-3901,4560,-1081,-2317,4593,-953,-2704,2632,-2387,-7198,2893,-547,540,-149,-1642, + -5694,-756,-1342,-928,-2015,-1026,-769,-2272,-600,1663,1466,-1984,-12205,1613,-506,10012,317,-1581, + -7838,-1205,-995,2273,2543,-747,-9812,1230,-582,-1990,-978,-488,506,1780,-1984,-7862,527,-883, + -1881,-3166,-701,-267,-5,-1644,3400,2704,-792,4438,2660,-722,5406,2686,-739,-3252,-3900,-1465, + -5357,585,574,-4053,651,240,-4389,142,398,6432,2271,948,8666,2140,1281,8690,2540,682, + -6264,4112,661,-6609,3466,750,-5832,3631,1818,858,-2265,294,517,-1498,259,1287,-1572,216, + -50,-1274,666,686,-800,1321,483,-1059,648,-3758,726,-5,-5455,1570,444,-5559,2136,0, + -2881,121,-150,-2769,307,256,-6068,-1372,660,-5656,-266,505,-5448,-1355,549,-6068,-1372,1261, + -5622,-311,1430,-6109,192,1316,-4895,-2080,694,-5007,-1261,654,-5415,-1368,1429,-4894,-2080,1234, + -4992,-1275,1270,-5099,-2507,605,-5014,-3303,632,5204,-1216,0,1770,-2171,581,2390,-1500,1, + -482,-2141,549,-783,-2001,1019,637,-1963,1765,209,-1205,1761,-518,-2053,1529,673,-3348,582, + 99,-3345,441,-242,-4700,631,182,-5283,642,72,-5525,550,2370,-1563,2112,1404,-2180,1528, + 4478,-1130,1456,5917,1259,1817,7372,862,1336,7721,1467,1369,-5711,3038,1801,-6299,2335,567, + -5914,2231,1541,-5553,1594,1435,-3722,1627,2256,-6115,2266,1,597,-5459,594,187,-5293,1300, + 74,-5531,1447,592,-5440,1447,-255,-4751,1351,106,-4661,1208,99,-3346,1525,539,-3336,1294, + 9238,2721,1119,9910,2818,1436,9880,1979,2003,10353,2840,1896,10023,2203,1028,10274,2703,1485, + 8852,-674,999,9117,-972,854,9431,-758,1019,-4959,-3239,1356,-5001,-3448,980,1376,-1473,13, + 17,-1154,12,613,-1464,14,-6624,2770,1,-7013,2593,429,-7015,2552,2,-3261,195,436, + -2156,184,478,-2527,657,882,-5555,599,746,9325,-1241,415,9017,-1108,0,-6605,3982,413, + -3828,-238,552,-6236,287,627,-5096,-2474,1381,-5457,-2558,1278,-5401,-3205,1515,-2573,1350,1772, + -3078,522,1420,111,-4621,758,-504,-3356,1489,-609,-4841,1285,4571,-220,1945,9971,-1111,0, + 10386,-771,0,9952,-1101,447,-4022,920,1852,-6517,2884,573,1337,-1514,2012,9446,1237,1879, + 10095,-521,785,-159,-5953,1280,-314,-5958,990,-108,-5952,705,672,-5877,549,898,-5843,1059, + 752,-5456,1062,-5458,-3511,956,-5551,-2443,954,-5454,-2561,652,9306,-1304,0,9345,-225,1562, + 9978,149,898,9850,-18,1460,10080,166,427,10276,859,731,10556,-449,-84,10552,-457,166, + -5979,755,-587,-5421,-146,-413,-4580,160,-253,6711,2401,-949,8689,2541,-683,8665,2140,-1282, + -6196,4099,-660,-5254,3533,-1815,-6614,3465,-747,1416,-1781,-292,1555,-1306,-215,937,-1145,-257, + 410,-862,-664,128,-968,-649,569,-616,-1321,-5306,1640,-447,-6605,-1763,-778,-6023,-1572,-632, + -6537,-595,-590,-6574,-1748,-1378,-7036,-226,-1421,-6492,-584,-1512,-5276,-2139,-751,-5622,-1353,-710, + -5962,-1553,-1509,-5590,-1338,-1325,-5260,-2105,-1290,-5379,-2588,-678,-5227,-3350,-715,2287,-1425,-579, + 255,-1182,-1017,431,-1411,-547,1291,-1319,-1763,426,-1336,-1527,747,-716,-1760,-129,-2296,-438, + 202,-2818,-580,-1555,-2484,-628,-2580,-2322,-548,-2426,-2604,-640,2942,-539,-2111,4636,-434,-1456, + 2049,-1342,-1526,5995,1543,-1818,7717,1489,-1370,7203,995,-1337,-4860,2963,-1798,-5326,2405,-1540, + -6078,2386,-566,-5400,1708,-1439,-3955,634,-2266,-3008,-2617,-591,-2445,-2590,-1298,-2991,-2631,-1444, + -2594,-2301,-1445,-1619,-2494,-1349,-1408,-2793,-1205,87,-2414,-1523,112,-2693,-1292,9238,2721,-1120, + 9910,2818,-1437,10353,2840,-1897,9880,1979,-2004,10023,2203,-1029,10274,2703,-1486,8852,-674,-1000, + 9431,-758,-1020,9117,-972,-855,-5227,-3544,-1068,-5196,-3344,-1439,-7015,2593,-425,-3927,-483,-447, + -2840,44,-892,-2260,-28,-485,-6166,866,-759,9325,-1241,-416,-6607,3982,-410,-4940,-731,-566, + -7215,-169,-736,-5338,-2576,-1453,-5623,-3296,-1619,-5656,-2668,-1368,-3930,-499,-1430,-3005,78,-1783, + -1354,-2780,-756,-237,-2045,-1487,-1718,-2196,-1282,5164,464,-1945,9952,-1101,-448,-4624,243,-1865, + -6463,2888,-571,1850,-659,-2011,9446,1237,-1880,10095,-521,-786,-2680,-1792,-1277,-2718,-1825,-702, + -2566,-1689,-987,-3480,-2434,-1057,-3302,-2301,-546,-3152,-2689,-1059,-5683,-3621,-1070,-5692,-2681,-744, + -5773,-2588,-1048,9346,-225,-1563,9978,149,-899,9850,-18,-1461,10276,859,-732,10080,166,-428, + 1892,4,1987,2111,-509,2301,8069,-301,1007,8019,-397,-1,8662,-802,-1,-5356,584,1312, + -5021,-580,578,972,3649,686,1154,3841,-1,-172,4428,-1,-227,4316,782,-1965,692,1318, + -2095,1945,1904,-2494,2335,1936,8979,986,1831,-5433,-3217,354,4339,878,2168,3367,-517,2283, + -13393,2002,18,-14599,2049,21,-14612,1899,480,-13408,1732,563,-636,-711,481,-647,-851,0, + -9735,799,376,-12061,4,402,-11968,-200,14,-9632,614,9,-1502,3610,2076,-2498,4222,2152, + -12202,890,792,-13547,1233,760,-13758,572,461,-14835,1256,423,-14845,1161,21,-13763,390,19, + -419,-329,1320,3544,1059,2204,-4406,148,1534,-3838,2730,2350,7098,34,1000,7305,112,-1, + 2708,1527,2096,-15000,1698,22,-14702,1620,589,-5024,-587,1329,9890,1946,775,-6327,4381,1, + -6619,4178,1,-623,3036,2017,-1316,4906,842,8516,315,1750,-883,-4852,975,-1446,248,1319, + -1173,1302,1905,-7639,3627,4,-10298,2279,10,-10124,2040,387,-7499,3477,404,-4431,5151,1080, + -4045,4383,2389,-2753,5416,950,-7296,3131,553,1290,166,1642,-3831,-243,1338,-504,-3356,603, + -822,-3293,1028,-4413,5108,-1,-2651,5347,0,10285,878,0,10110,190,0,1777,1742,1983, + -12349,1555,536,10075,521,1579,-6272,-1310,961,2707,2857,746,2863,3122,-1,-9932,1503,601, + 9777,1982,0,-1672,-243,479,374,2405,1983,-6374,380,863,-598,-4804,703,-247,699,1642, + 3728,2565,791,4728,2229,721,5427,2243,738,658,-5879,1468,4067,2815,-1,6716,2536,-1, + 5657,2504,-1,4966,2524,0,8730,2608,-1,-2259,-62,-4,-1731,-395,-2,-1249,4885,-1, + -12468,1828,16,1293,329,-1988,2532,246,-2300,8069,-301,-1008,-5974,757,-1324,-5923,-731,-630, + 972,3649,-687,-177,4198,-783,-2489,958,-1944,-2169,991,-1910,-2167,145,-1324,8979,987,-1832, + -5670,-3318,-460,4836,1406,-2168,4025,333,-2282,-13411,1732,-527,-14615,1899,-438,-571,-714,-482, + -9735,799,-358,-12055,4,-373,-1693,2716,-2154,-1246,2835,-2078,-12206,891,-762,-13760,572,-424, + -13551,1232,-724,-14837,1256,-380,-399,-392,-1322,3673,1537,-2204,-5424,-138,-1550,-3379,1573,-2355, + 7123,160,-1001,2825,1729,-2097,-14705,1620,-547,-5889,-717,-1381,9890,1946,-776,-1152,4634,-843, + -545,2667,-2019,8516,315,-1751,-1714,-1954,-972,-1235,880,-1909,-1581,-104,-1323,-7501,3476,-397, + -10139,2034,-367,-4057,4901,-1080,-2470,5091,-950,-2711,3199,-2386,-7298,3131,-547,800,295,-1642, + -4923,-727,-1352,-476,-1423,-1026,-413,-1715,-600,1979,1859,-1984,-12352,1555,-506,10075,521,-1580, + -6802,-1754,-1089,2696,2874,-747,-9935,1501,-582,-1722,-345,-484,618,2421,-1984,-7346,-107,-979, + -1758,-2191,-701,-93,628,-1643,3851,2663,-792,4784,2365,-722,5481,2368,-739,-3292,-2291,-1465, + -4918,504,592,-3663,850,256,-3869,284,416,6462,2447,948,8635,2349,1280,8655,2750,682, + -6279,4120,661,-6625,3495,750,-5951,3561,1817,1178,-2138,295,867,-1322,261,1639,-1479,219, + 268,-1039,666,802,-552,1321,583,-807,649,-3437,944,9,-5276,1467,454,-5566,2103,4, + -2620,437,-143,-2537,649,262,-4773,-1788,677,-4820,-608,523,-4208,-1533,566,-4773,-1788,1277, + -4786,-634,1448,-5428,-366,1335,-3423,-1969,710,-3837,-1276,670,-4177,-1530,1445,-3422,-1969,1249, + -3820,-1281,1286,-3435,-2434,620,-2940,-3058,645,5348,-1203,2,2086,-2045,582,2701,-1493,5, + -119,-1859,549,-393,-1664,1020,986,-1807,1765,543,-1000,1762,-141,-1767,1529,661,-3320,582, + 94,-3135,441,-689,-4295,631,-462,-5061,642,-659,-5259,550,2702,-1417,2113,1730,-2025,1528, + 4689,-1013,1457,5981,1427,1817,7373,1054,1336,7701,1666,1369,-5793,2899,1803,-6323,2269,567, + -5845,2046,1547,-5325,1476,1446,-3527,1891,2266,-6137,2252,2,-306,-5665,593,-462,-5073,1300, + -663,-5263,1447,-290,-5642,1446,-719,-4339,1351,-349,-4375,1207,94,-3136,1525,527,-3268,1294, + 9202,2936,1119,9873,3038,1436,9850,2200,2003,10316,3065,1896,9992,2425,1027,10238,2927,1485, + 8847,-463,999,9115,-758,854,9427,-541,1018,-2923,-2988,1370,-2851,-3175,993,1718,-1413,18, + 234,-781,17,950,-1289,17,-6677,2798,1,-7082,2653,429,-7088,2612,3,-2832,605,451, + -2024,503,480,-2333,1029,888,-5120,467,763,9325,-1025,415,9016,-895,-1,-6595,4013,412, + -3179,91,570,-5587,-328,645,-3446,-2408,1396,-3735,-2671,1292,-3322,-3196,1529,-2428,1786,1778, + -2704,961,1433,-331,-4339,758,-488,-2940,1489,-1083,-4303,1284,4779,-77,1946,9970,-890,-1, + 10382,-546,-1,9951,-880,447,-3671,1134,1866,-6564,2893,573,1676,-1346,2013,9424,1454,1878, + 10089,-299,784,-1112,-5399,1279,-1227,-5311,989,-1073,-5431,705,-620,-5968,548,-502,-6157,1059, + -241,-5828,1062,-3204,-3473,969,-3883,-2607,969,-3732,-2672,667,9307,-1088,-1,9336,-9,1562, + 9965,370,898,9839,203,1460,10068,389,427,10257,1084,731,10549,-222,-84,10545,-230,165, + -5606,600,-546,-4895,-186,-375,-4126,261,-226,6680,2591,-949,8655,2750,-684,8635,2349,-1282, + -6208,4120,-660,-5352,3580,-1814,-6630,3495,-747,1612,-1746,-283,1889,-1296,-208,1285,-1008,-249, + 794,-585,-656,357,-596,-644,815,-296,-1317,-5157,1586,-424,-5728,-2010,-767,-5199,-1706,-612, + -5902,-854,-569,-5694,-1983,-1366,-6456,-586,-1404,-5851,-825,-1490,-4351,-2105,-726,-4850,-1409,-683, + -5134,-1667,-1488,-4815,-1381,-1298,-4336,-2064,-1264,-4360,-2566,-657,-4042,-3276,-696,2557,-1587,-571, + 619,-883,-1008,731,-1148,-537,1583,-1266,-1754,745,-1075,-1518,1163,-506,-1751,-35,-1855,-429, + 152,-2444,-570,-1462,-1669,-619,-2418,-1265,-538,-2336,-1575,-630,3364,-858,-2102,4941,-739,-1450, + 2313,-1471,-1517,6152,1558,-1816,7707,1671,-1370,7305,1016,-1336,-4934,3001,-1787,-5308,2354,-1518, + -6112,2368,-563,-5226,1639,-1409,-3612,833,-2248,-2904,-1447,-582,-2351,-1557,-1288,-2892,-1465,-1435, + -2426,-1241,-1435,-1526,-1662,-1339,-1399,-2005,-1196,148,-2026,-1513,97,-2301,-1282,9202,2936,-1120, + 9873,3038,-1437,10316,3065,-1897,9850,2200,-2004,9992,2425,-1029,10238,2927,-1486,8847,-463,-1000, + 9427,-541,-1020,9115,-758,-856,-3995,-3461,-1051,-4005,-3256,-1420,-7084,2653,-424,-3414,-247,-425, + -2544,345,-885,-2091,268,-482,-5812,674,-718,9325,-1025,-417,-6597,4013,-410,-4315,-673,-531, + -6650,-573,-720,-4314,-2538,-1431,-4430,-3304,-1606,-4607,-2700,-1351,-3414,-276,-1408,-2645,404,-1777, + -1343,-2007,-746,-71,-1589,-1477,-1547,-1347,-1272,5429,332,-1941,9951,-880,-448,-4194,332,-1838, + -6510,2909,-571,2285,-760,-2001,9424,1454,-1880,10089,-299,-786,-2386,-727,-1268,-2432,-750,-693, + -2251,-655,-977,-3318,-1155,-1047,-3112,-1069,-536,-3062,-1482,-1050,-4422,-3637,-1059,-4647,-2726,-728, + -4744,-2649,-1032,9336,-9,-1564,9965,370,-899,9839,203,-1461,10257,1084,-732,10068,389,-428, + 2005,211,1988,2416,-347,2301,8061,-97,1007,8012,-193,-1,8659,-592,-1,-4918,506,1329, + -4135,-658,595,943,3877,686,1125,4071,-1,-227,4645,-1,-299,4549,781,-1890,1030,1320, + -2099,2313,1905,-2504,2748,1939,8959,1199,1831,-3342,-3217,368,4537,1025,2169,3659,-377,2284, + -13560,1649,18,-14756,1493,21,-14744,1342,479,-13529,1380,563,-593,-495,481,-603,-617,0, + -9784,1077,376,-11906,-93,401,-11779,-277,14,-9660,903,9,-1635,3924,2074,-2778,4573,2149, + -12198,755,792,-13582,865,760,-13678,179,461,-14855,671,423,-14849,576,21,-13652,-2,18, + -376,-95,1320,3733,1219,2204,-3886,289,1553,-3865,2945,2354,7115,222,1000,7292,309,-1, + 2823,1721,2097,-15093,1079,22,-14786,1052,589,-4135,-665,1347,9861,2167,775,-6319,4398,1, + -6602,4209,1,-689,3310,2016,-1447,5136,841,8502,523,1749,-1343,-4218,974,-1409,532,1320, + -1175,1594,1905,-7640,3726,4,-10499,2508,11,-10316,2274,387,-7510,3566,404,-4613,5214,1079, + -4378,4512,2385,-2947,5593,948,-7330,3208,553,1342,388,1642,-3180,88,1356,-488,-2940,603, + -765,-2780,1028,-4521,5186,-2,-2809,5539,-3,10266,1102,-1,10097,413,-1,1880,1954,1983, + -12459,1384,536,10059,743,1579,-4985,-1810,977,2722,3070,746,2870,3335,-1,-10070,1754,601, + 9748,2201,-1,-1583,42,480,417,2630,1983,-5750,-303,882,-1060,-4272,703,-197,938,1642, + 3795,2749,791,4783,2398,721,5453,2412,738,-627,-5956,1467,4092,2998,-1,6684,2726,-1, + 5646,2673,-1,5011,2679,0,8695,2818,-1,-2103,242,-1,-1630,-117,-1,-1344,5108,-2, + -12623,1633,16,1787,505,-1981,3053,138,-2292,8061,-97,-1008,-5609,600,-1283,-5275,-861,-601, + 943,3876,-687,-225,4440,-785,-2252,1345,-1944,-2000,1326,-1912,-1996,433,-1323,8959,1199,-1832, + -4484,-3345,-447,5126,1358,-2165,4447,105,-2276,-13532,1380,-527,-14747,1342,-438,-526,-468,-482, + -9785,1077,-359,-11900,-91,-373,-1683,3196,-2166,-1230,3186,-2085,-12202,755,-763,-13680,178,-424, + -13585,864,-724,-14857,671,-381,-329,-122,-1321,4118,1494,-2200,-4910,-183,-1512,-3210,1858,-2347, + 7162,264,-1000,3081,1883,-2095,-14788,1052,-547,-5238,-834,-1351,9861,2167,-777,-1228,4893,-847, + -540,2949,-2022,8502,523,-1751,-1482,-1113,-963,-1160,1152,-1910,-1481,146,-1323,-7512,3566,-397, + -10320,2273,-367,-4185,5036,-1083,-2596,5320,-954,-2815,3571,-2393,-7332,3208,-547,1120,594,-1638, + -4302,-664,-1316,-146,-922,-1016,-160,-1221,-591,2156,2079,-1982,-12462,1384,-506,10059,743,-1581, + -5919,-2038,-1081,2737,3085,-747,-10073,1754,-582,-1615,-71,-483,616,2650,-1984,-6788,-536,-964, + -1582,-1333,-691,-28,892,-1643,3959,2824,-791,4911,2476,-721,5557,2483,-738,-3100,-1062,-1455, + -4823,135,592,-3570,586,257,-3766,-47,416,6743,2961,948,8684,2806,1280,8726,3205,682, + -6241,4111,661,-6628,3532,749,-5968,3425,1817,2259,-1381,296,1993,-513,263,2720,-726,222, + 1376,-264,667,1309,-286,1321,1033,-558,650,-3316,859,12,-5241,1187,455,-5625,2007,6, + -2583,498,-143,-2527,653,262,-4065,-2406,677,-4265,-1241,523,-3538,-2079,566,-4065,-2406,1277, + -4250,-1247,1448,-4937,-1126,1335,-2711,-2394,710,-3204,-1775,670,-3512,-2071,1445,-2711,-2394,1249, + -3188,-1777,1286,-2656,-2846,620,-2009,-3305,645,5675,-612,4,3173,-1353,584,3607,-778,8, + 1110,-865,551,902,-605,1021,2156,-1032,1767,1725,-206,1763,1111,-773,1531,1305,-2469,584, + 840,-2071,443,-366,-2797,633,-698,-3657,644,-1002,-3700,552,3884,-826,2114,2858,-1322,1530, + 5367,-496,1458,6292,1970,1817,7432,1557,1336,7733,2168,1369,-5787,2639,1803,-6361,2187,567, + -5815,1746,1547,-5262,1144,1446,-3473,1586,2266,-6199,2215,4,-1387,-4050,596,-705,-3665,1302, + -1000,-3695,1449,-1351,-4061,1449,-412,-2824,1353,-92,-3013,1210,840,-2072,1527,1195,-2369,1296, + 9282,3361,1119,9958,3428,1436,9890,2592,2003,10401,3431,1896,10043,2809,1027,10316,3297,1485, + 8745,-14,999,8997,-323,854,9320,-123,1018,-2006,-3243,1370,-1890,-3392,993,2723,-624,22, + 807,-268,20,2002,-479,20,-6732,2836,1,-7149,2730,429,-7158,2690,2,-2755,485,451, + -2104,528,480,-2334,978,888,-5025,78,763,9193,-601,415,8891,-454,-1,-6553,4047,412, + -3004,-109,570,-5108,-1125,645,-2669,-2826,1396,-2901,-3162,1292,-2336,-3556,1529,-2428,1610,1779, + -2658,773,1433,-59,-2987,760,383,-1646,1491,-714,-2623,1286,5542,414,1946,9844,-500,-1, + 10274,-178,-1,9826,-489,447,-3598,818,1866,-6615,2907,573,2892,-630,2014,9424,1870,1878, + 9994,84,784,-1213,-3400,1282,-1199,-3301,991,-1224,-3437,707,-1693,-3756,551,-1929,-3850,1061, + -1602,-4123,1064,-2145,-3777,969,-3068,-3129,969,-2898,-3163,667,9171,-663,-1,9258,413,1562, + 9907,759,898,9772,598,1460,10010,772,427,10236,1455,731,10458,136,-84,10454,128,165, + -5406,424,-524,-4590,-251,-358,-3903,248,-216,6727,3174,-949,8725,3205,-684,8684,2806,-1282, + -6182,4124,-660,-5471,3555,-1811,-6632,3532,-747,2402,-867,-276,2796,-467,-203,2131,-144,-244, + 1565,242,-651,885,-117,-641,1275,129,-1314,-5141,1458,-410,-5056,-2193,-711,-4592,-1791,-570, + -5445,-1088,-524,-5038,-2169,-1311,-6054,-944,-1350,-5418,-1063,-1446,-3687,-2018,-696,-4308,-1434,-652, + -4551,-1753,-1448,-4289,-1410,-1267,-3689,-1984,-1235,-3603,-2470,-621,-3132,-3090,-657,3365,-768,-564, + 1397,51,-1001,1504,-220,-531,2362,-378,-1747,1521,-146,-1511,1915,327,-1745,727,-932,-422, + 908,-1523,-563,-696,-741,-612,-1693,-450,-531,-1575,-744,-623,4116,-58,-2095,5448,-53,-1446, + 3081,-618,-1510,6314,2180,-1815,7733,2180,-1370,7414,1558,-1335,-5079,2944,-1779,-5386,2230,-1504, + -6190,2322,-560,-5185,1513,-1392,-3475,935,-2241,-2158,-686,-575,-1590,-730,-1281,-2144,-703,-1428, + -1703,-430,-1428,-759,-735,-1332,-632,-1079,-1189,899,-1101,-1506,855,-1379,-1275,9282,3361,-1120, + 9958,3428,-1437,10401,3431,-1897,9890,2592,-2004,10043,2809,-1029,10316,3297,-1486,8745,-14,-1000, + 9320,-123,-1020,8997,-323,-856,-3051,-3264,-1010,-3112,-3074,-1382,-7151,2730,-425,-3169,-147,-422, + -2461,476,-885,-2144,349,-483,-5622,466,-693,9193,-600,-416,-6555,4047,-410,-3938,-637,-518, + -6235,-956,-663,-3576,-2445,-1396,-3519,-3222,-1559,-3830,-2671,-1310,-3154,-124,-1404,-2515,617,-1777, + -579,-1079,-739,683,-655,-1470,-792,-409,-1265,5721,990,-1939,9826,-489,-448,-3978,357,-1826, + -6571,2932,-570,3085,94,-1994,9424,1870,-1880,9994,84,-786,-1720,80,-1261,-1762,52,-686, + -1595,167,-970,-2600,-431,-1040,-2402,-329,-529,-2315,-733,-1043,-3426,-3533,-1008,-3853,-2694,-685, + -3972,-2643,-989,9259,414,-1564,9907,759,-899,9772,598,-1461,10236,1455,-732,10010,772,-428, + 2504,577,1988,3580,278,2302,7980,393,1007,7926,300,-1,8551,-133,-1,-4823,136,1329, + -3616,-1188,595,831,3764,686,1001,3958,-1,-270,4579,-1,-345,4477,781,-1989,1004,1320, + -2156,2225,1905,-2514,2571,1939,8946,1640,1831,-2350,-3578,368,5309,1565,2169,4733,151,2284, + -13677,1483,15,-14832,1132,18,-14797,985,476,-13605,1222,560,-647,-504,481,-689,-645,0, + -9858,1413,375,-11791,5,399,-11642,-162,11,-9723,1250,7,-1669,3849,2074,-2792,4386,2149, + -12189,811,790,-13575,704,757,-13561,10,458,-14798,305,419,-14777,212,18,-13507,-164,15, + -407,-102,1320,4427,1791,2205,-3772,-47,1553,-3830,2625,2354,7160,733,1000,7234,840,-1, + 3161,2123,2097,-15098,669,18,-14791,692,585,-3615,-1191,1347,9899,2558,775,-6256,4403,0, + -6544,4242,1,-760,3229,2016,-1459,5107,841,8453,989,1749,-898,-2420,977,-1553,474,1320, + -1291,1531,1905,-7621,3844,3,-10657,2797,9,-10475,2567,386,-7504,3674,403,-4655,5147,1079, + -4383,4233,2385,-2995,5569,948,-7352,3303,552,1605,538,1642,-3004,-111,1356,383,-1646,604, + 200,-1390,1030,-4580,5180,-2,-2882,5570,-3,10246,1473,-1,10041,794,-1,2162,2223,1984, + -12526,1404,534,10021,1126,1579,-4272,-2455,977,2427,3225,746,2486,3487,-1,-10198,2063,600, + 9787,2599,-1,-1707,27,480,545,2671,1983,-5272,-1149,882,-678,-2607,705,-125,990,1642, + 3760,3210,791,4820,3019,721,5603,3044,738,-1679,-3751,1470,3751,3455,0,6737,3309,-1, + 5621,3339,0,4853,3334,0,8769,3271,-1,-2173,295,-1,-1749,-119,-2,-1361,5111,-2, + -12721,1630,13,2388,1155,-1977,3739,892,-2287,7980,393,-1008,-5415,422,-1261,-4834,-976,-569, + 831,3764,-687,-276,4400,-786,-2211,1586,-1948,-2008,1508,-1915,-2036,554,-1325,8946,1640,-1832, + -3544,-3255,-399,5379,2048,-2163,4955,832,-2272,-13607,1222,-530,-14799,985,-441,-632,-510,-481, + -9860,1413,-360,-11785,8,-376,-1792,3474,-2174,-1284,3328,-2089,-12192,811,-765,-13563,10,-427, + -13578,703,-727,-14800,305,-384,-400,-136,-1321,4454,2178,-2197,-4615,-254,-1494,-3217,2003,-2343, + 7174,808,-1000,3162,2293,-2093,-14793,692,-550,-4815,-954,-1320,9899,2558,-777,-1261,4941,-848, + -613,2965,-2024,8453,990,-1751,-738,-163,-956,-1248,1198,-1912,-1586,179,-1325,-7505,3674,-398, + -10468,2571,-368,-4286,5065,-1085,-2684,5397,-956,-3027,3757,-2395,-7354,3303,-547,1514,993,-1635, + -3935,-631,-1304,621,1,-1009,606,-297,-584,2149,2283,-1981,-12529,1404,-508,10021,1126,-1581, + -5243,-2262,-1021,2426,3244,-747,-10200,2064,-583,-1726,-61,-484,507,2551,-1984,-6381,-951,-904, + -818,-403,-684,-101,869,-1643,3726,3291,-791,4808,3136,-720,5595,3153,-738,-2391,-321,-1448, + -4535,-577,592,-3381,80,252,-3469,-650,416,6806,3158,948,8599,2909,1280,8656,3307,682, + -6087,4122,660,-6579,3699,747,-6028,3109,1817,2871,-684,291,2675,165,256,3430,-29,214, + 1981,357,663,1559,-166,1320,1280,-474,647,-3211,609,-1,-5192,581,452,-5802,1808,-1, + -2567,411,-146,-2517,508,261,-3390,-3151,677,-3559,-1980,523,-2854,-2838,566,-3390,-3151,1277, + -3559,-1974,1448,-4250,-1915,1335,-2046,-3165,710,-2512,-2544,670,-2832,-2831,1445,-2046,-3165,1249, + -2499,-2545,1286,-1998,-3609,620,-1311,-4001,645,5869,-134,-1,3804,-692,578,4272,-68,-1, + 1792,-48,546,1630,237,1016,2782,-332,1762,2371,459,1759,1806,40,1526,1660,-1631,578, + 1286,-1139,437,-62,-1569,627,-782,-2213,639,-1087,-2103,547,4512,-258,2110,3457,-668,1525, + 5688,-92,1455,6370,2210,1816,7350,1715,1336,7636,2309,1369,-5832,2045,1803,-6473,2027,567, + -5786,1064,1547,-5111,419,1446,-3368,1021,2266,-6325,2172,-1,-1602,-2099,590,-790,-2217,1297, + -1077,-2103,1444,-1580,-2131,1443,-114,-1585,1348,151,-1844,1204,1286,-1140,1522,1570,-1510,1290, + 9217,3442,1119,9895,3484,1436,9796,2650,2003,10338,3470,1896,9957,2862,1027,10248,3340,1485, + 8556,89,999,8796,-229,854,9126,-41,1018,-1311,-3947,1370,-1184,-4075,993,3498,101,12, + 1287,-139,12,2697,174,12,-6781,3012,-1,-7195,2972,426,-7204,2929,-1,-2613,170,448, + -2179,408,481,-2324,774,887,-4725,-658,763,8981,-514,415,8685,-357,-1,-6395,4193,410, + -2690,-572,570,-4422,-1934,645,-2010,-3592,1396,-2229,-3951,1292,-1613,-4293,1529,-2403,1275,1779, + -2524,379,1433,190,-1825,755,934,-618,1486,-337,-1307,1281,5899,766,1944,9636,-437,-1, + 10078,-132,-1,9618,-426,447,-3410,232,1866,-6702,3033,571,3552,23,2009,9304,1946,1878, + 9808,141,784,-1071,-1779,1276,-1016,-1716,986,-1099,-1799,701,-1632,-1684,545,-1879,-1587,1056, + -1824,-2009,1059,-1400,-4483,969,-2404,-3923,969,-2227,-3952,667,8958,-575,-1,9084,497,1562, + 9745,818,898,9605,663,1460,9849,827,427,10100,1502,731,10273,176,-84,10268,168,165, + -5263,249,-593,-4388,-340,-418,-3750,152,-254,6644,3363,-949,8656,3307,-684,8599,2910,-1282, + -6029,4157,-661,-5561,3478,-1818,-6578,3701,-749,3445,-145,-292,3651,298,-216,2964,489,-258, + 2291,686,-664,1323,-21,-649,1632,224,-1321,-5181,1263,-453,-4513,-2303,-672,-4119,-1824,-561, + -5077,-1271,-523,-4510,-2305,-1272,-5715,-1267,-1337,-5073,-1283,-1448,-3194,-1899,-701,-3900,-1430,-665, + -4103,-1822,-1440,-3897,-1432,-1282,-3212,-1892,-1240,-3033,-2324,-609,-2438,-2829,-630,4333,118,-579, + 2279,530,-1017,2452,288,-547,3343,321,-1763,2451,366,-1527,2652,833,-1760,1894,-627,-439, + 2229,-1149,-580,477,-841,-628,-562,-921,-548,-349,-1153,-640,4936,955,-2111,5931,825,-1456, + 4097,237,-1526,6295,2607,-1818,7635,2341,-1370,7387,1927,-1337,-5197,2838,-1804,-5461,2079,-1548, + -6329,2272,-568,-5140,1367,-1447,-3357,971,-2267,-938,-1289,-591,-366,-1144,-1298,-919,-1300,-1444, + -578,-908,-1445,417,-853,-1349,635,-1149,-1205,2090,-742,-1523,2139,-1024,-1292,9217,3442,-1120, + 9895,3484,-1437,10338,3470,-1897,9796,2650,-2004,9957,2862,-1029,10248,3340,-1486,8556,89,-1000, + 9126,-41,-1020,8796,-229,-856,-2328,-2995,-977,-2434,-2844,-1355,-7194,2972,-428,-3005,-149,-449, + -2426,461,-888,-2207,280,-482,-5479,267,-765,8981,-514,-416,-6394,4193,-412,-3682,-631,-571, + -5880,-1274,-648,-3025,-2332,-1385,-2802,-3086,-1515,-3229,-2607,-1281,-2981,-58,-1434,-2436,701,-1780, + 685,-1134,-756,1756,-360,-1487,284,-546,-1282,5880,1627,-1945,9618,-425,-448,-3807,341,-1868, + -6660,3082,-573,3949,932,-2011,9304,1946,-1880,9808,141,-786,-754,-433,-1277,-784,-474,-702, + -666,-311,-987,-1440,-1168,-1057,-1277,-1018,-546,-1081,-1377,-1059,-2634,-3340,-954,-3236,-2604,-656, + -3372,-2591,-958,9085,497,-1564,9745,818,-899,9605,663,-1461,10100,1502,-732,9849,827,-428, + 2738,773,1987,4204,816,2298,7806,524,1007,7748,433,-1,8357,-23,-1,-4535,-577,1329, + -2933,-1925,595,726,3526,686,888,3717,-1,-326,4376,-1,-412,4269,781,-2076,841,1320, + -2231,1989,1905,-2556,2221,1939,8818,1734,1831,-1624,-4311,368,5677,1921,2167,5290,610,2281, + -13771,1773,-6,-14873,1276,-6,-14822,1135,452,-13669,1522,540,-729,-633,481,-785,-815,-1, + -9942,2007,364,-11758,494,383,-11597,337,-4,-9806,1847,-3,-1769,3618,2074,-2937,3986,2149, + -12211,1274,773,-13578,1009,737,-13483,321,437,-14740,460,396,-14706,370,-6,-13408,155,-5, + -474,-226,1320,4755,2152,2202,-3462,-653,1553,-3825,2007,2354,7066,907,1000,7077,999,-1, + 3289,2306,2096,-15081,785,-6,-14780,845,562,-2934,-1922,1347,9804,2617,775,-6021,4433,-1, + -6346,4375,-1,-859,3018,2016,-1518,4919,841,8301,1102,1749,-437,-1055,971,-1678,299,1320, + -1408,1345,1905,-7535,4128,-2,-10744,3374,-4,-10574,3141,373,-7438,3949,399,-4696,4886,1079, + -4527,3628,2385,-3074,5363,948,-7330,3563,548,1708,542,1641,-2691,-571,1356,934,-618,599, + 816,-329,1024,-4582,5016,0,-2964,5434,-1,10110,1520,-1,9881,849,-1,2272,2313,1983, + -12585,1846,516,9873,1181,1579,-3598,-3194,977,2179,3144,746,2183,3391,-1,-10298,2643,588, + 9694,2661,-1,-1821,-113,481,582,2580,1983,-4578,-1985,882,-298,-1304,700,-128,916,1641, + 3649,3362,791,4736,3273,721,5585,3301,738,-1617,-1691,1464,3442,3557,-1,6659,3497,-1, + 5490,3590,-1,4636,3591,-1,8701,3371,-1,-2243,202,-1,-1860,-249,-1,-1405,4957,-1, + -12795,2060,-5,2772,1560,-1988,4248,1651,-2300,7806,524,-1008,-5258,250,-1331,-4501,-1064,-595, + 726,3526,-687,-338,4219,-783,-2195,1681,-1940,-2035,1533,-1907,-2093,521,-1321,8818,1735,-1832, + -2799,-3069,-354,5411,2605,-2168,5318,1656,-2282,-13668,1522,-551,-14821,1135,-465,-736,-710,-482, + -9944,2006,-371,-11752,497,-392,-1882,3579,-2150,-1347,3303,-2075,-12210,1275,-782,-13482,320,-448, + -13577,1008,-748,-14739,460,-408,-483,-323,-1321,4512,2759,-2204,-4391,-338,-1554,-3202,2060,-2355, + 7089,1083,-1001,3078,2479,-2097,-14780,845,-574,-4498,-1071,-1346,9804,2617,-777,-1311,4827,-842, + -692,2836,-2017,8301,1103,-1751,258,-291,-972,-1339,1099,-1907,-1690,72,-1321,-7437,3949,-402, + -10558,3149,-381,-4331,4936,-1080,-2756,5300,-949,-3167,3813,-2386,-7330,3562,-551,1729,1086,-1642, + -3678,-635,-1358,1534,240,-1026,1603,-52,-600,2035,2271,-1984,-12585,1845,-526,9873,1181,-1581, + -4692,-2417,-972,2154,3165,-747,-10297,2645,-595,-1833,-181,-482,395,2325,-1984,-6029,-1304,-884, + 264,-550,-701,-197,683,-1642,3457,3443,-792,4599,3456,-722,5485,3478,-739,-1269,-1006,-1465, + -3791,-1512,592,-2903,-552,253,-2752,-1301,416,6913,3065,946,8710,2824,1280,8759,3223,682, + -5902,3573,660,-6404,3244,747,-6061,2276,1816,3071,-722,279,2840,93,246,3594,-64,205, + 2155,279,653,1753,-325,1316,1484,-671,645,-2861,-21,-1,-4820,-411,453,-5660,1195,2, + -2314,-164,-150,-2280,6,258,-2404,-3855,677,-2546,-2680,523,-1861,-3555,566,-2404,-3855,1277, + -2545,-2666,1448,-3233,-2653,1335,-1066,-3894,710,-1512,-3269,670,-1841,-3548,1445,-1066,-3894,1249, + -1500,-3270,1286,-1025,-4333,620,-319,-4685,645,6022,-150,-4,4000,-699,567,4423,-66,-8, + 1972,-110,533,1801,170,1004,2972,-366,1750,2540,400,1747,1983,-21,1514,1896,-1699,566, + 1505,-1221,425,172,-1706,615,-677,-2219,626,-966,-2040,534,4699,-250,2097,3655,-685,1513, + 5860,-114,1447,6496,2122,1814,7485,1616,1335,7760,2207,1369,-5794,912,1803,-6475,1381,566, + -5527,-124,1547,-4666,-696,1446,-3133,345,2266,-6296,1703,1,-1464,-1893,578,-684,-2222,1284, + -951,-2046,1431,-1450,-1930,1431,119,-1723,1335,391,-1972,1192,1505,-1222,1509,1802,-1581,1278, + 9317,3369,1119,9994,3425,1436,9912,2590,2003,10437,3420,1896,10069,2804,1027,10350,3288,1485, + 8723,4,999,8970,-309,854,9297,-115,1018,-320,-4637,1370,-189,-4753,993,3651,58,4, + 1457,-453,10,2854,79,3,-6768,2629,-2,-7198,2735,424,-7221,2703,-3,-2213,-261,447, + -1962,-49,479,-2138,336,886,-3948,-1636,763,9161,-591,415,8862,-439,-1,-6128,3700,410, + -2013,-1045,570,-3398,-2687,645,-1037,-4318,1396,-1250,-4689,1292,-606,-4999,1529,-2287,888,1779, + -2158,-17,1433,431,-1952,742,1134,-714,1473,-85,-1446,1268,6058,738,1936,9814,-500,-1, + 10250,-186,-1,9796,-489,447,-2955,-431,1866,-6633,2560,570,3732,8,1997,9434,1876,1878, + 9974,81,784,-873,-1738,1264,-809,-1693,973,-905,-1751,689,-1378,-1488,532,-1591,-1324,1043, + -1655,-1745,1046,-381,-5172,969,-1429,-4662,969,-1248,-4689,667,9139,-652,-1,9244,422,1562, + 9898,757,898,9761,599,1460,10002,768,427,10239,1448,731,10439,125,-84,10434,117,165, + -4804,-197,-578,-4024,-881,-411,-3302,-473,-252,6748,3239,-949,8759,3223,-684,8710,2825,-1282, + -5797,3639,-661,-5220,2954,-1812,-6401,3247,-750,3664,-242,-295,3786,247,-221,3079,327,-261, + 2363,406,-667,1479,-341,-650,1746,-71,-1322,-4671,749,-440,-4101,-2438,-672,-3837,-1877,-561, + -4901,-1581,-513,-4104,-2436,-1272,-5475,-1649,-1322,-4902,-1596,-1438,-2930,-1715,-708,-3722,-1440,-665, + -3830,-1867,-1440,-3724,-1439,-1281,-2952,-1711,-1247,-2664,-2084,-619,-1939,-2372,-649,4486,139,-582, + 2367,203,-1019,2592,-3,-549,3492,183,-1765,2575,76,-1529,2709,588,-1761,2273,-1054,-440, + 2723,-1481,-581,959,-1618,-630,-87,-1833,-549,156,-2044,-642,4964,1041,-2112,5981,943,-1457, + 4251,216,-1528,6330,2558,-1818,7750,2246,-1370,7461,1906,-1337,-4607,2335,-1791,-4853,1639,-1531, + -6210,1813,-565,-4537,912,-1431,-2814,274,-2268,-483,-2191,-593,138,-2037,-1300,-464,-2203,-1446, + -107,-1819,-1447,901,-1645,-1350,1186,-1876,-1207,2468,-1119,-1525,2607,-1379,-1293,9317,3369,-1120, + 9994,3425,-1437,10437,3420,-1897,9912,2590,-2004,10069,2804,-1029,10350,3288,-1486,8723,4,-1000, + 9297,-115,-1020,8970,-309,-856,-1788,-2491,-997,-1937,-2382,-1374,-7196,2735,-430,-2643,-852,-455, + -2168,-202,-895,-1966,-269,-486,-5019,-154,-747,9161,-590,-416,-6126,3700,-412,-3389,-1262,-571, + -5617,-1666,-632,-2662,-2086,-1396,-2210,-2732,-1532,-2776,-2420,-1293,-2610,-811,-1440,-2056,-82,-1789, + 1233,-1848,-757,2043,-815,-1489,713,-1383,-1284,5901,1642,-1946,9796,-489,-448,-3334,-286,-1865, + -6558,2647,-574,3994,875,-2012,9434,1876,-1880,9974,81,-786,-295,-1351,-1279,-324,-1392,-704, + -212,-1228,-989,-989,-2052,-1059,-817,-1913,-548,-637,-2272,-1061,-1967,-2916,-974,-2778,-2423,-667, + -2921,-2448,-968,9244,423,-1564,9898,757,-899,9761,599,-1461,10239,1448,-732,10002,768,-428, + 2901,652,1982,4362,794,2288,7965,424,1007,7909,331,-1,8527,-112,-1,-3790,-1511,1329, + -1921,-2618,595,712,3307,686,864,3511,-1,-404,4059,-1,-514,3946,781,-1917,454,1319, + -2240,1582,1905,-2633,1749,1939,8952,1654,1831,-616,-5016,368,5809,1878,2160,5454,609,2271, + -13987,2363,-16,-14883,1512,-17,-14792,1392,442,-13822,2148,530,-439,-944,480,-485,-1148,-1, + -10010,2699,359,-11935,1442,375,-11790,1269,-12,-9905,2521,-8,-1970,3177,2074,-3354,3326,2149, + -12326,2264,765,-13594,1671,727,-13315,1030,428,-14514,772,385,-14454,696,-16,-13207,892,-14, + -219,-507,1319,4882,2082,2196,-2736,-1306,1553,-3849,1156,2354,7218,806,999,7226,883,-1, + 3411,2184,2092,-14935,981,-17,-14667,1128,551,-1923,-2610,1347,9921,2556,775,-5785,3908,-1, + -6052,3872,-1,-922,2686,2016,-1699,4483,841,8449,1012,1749,-159,-1194,959,-1472,-49,1320, + -1312,1004,1905,-7168,3945,-3,-10539,4167,-9,-10432,3907,368,-7133,3742,397,-4802,4236,1079, + -4902,2589,2385,-3243,4804,948,-7148,3342,547,1878,372,1639,-2014,-1044,1356,1134,-714,587, + 1005,-430,1012,-4515,4477,0,-3009,4894,-1,10249,1465,-1,10033,790,-1,2390,2170,1979, + -12645,2873,508,10018,1123,1579,-2613,-3894,977,2277,2969,746,2274,3214,-1,-10254,3376,583, + 9810,2599,-1,-1566,-527,479,655,2362,1981,-3547,-2753,882,-47,-1446,687,48,662,1640, + 3753,3219,789,4845,3140,719,5691,3179,736,-1366,-1498,1451,3535,3387,-1,6760,3373,-1, + 5585,3448,-1,4722,3443,-1,8803,3288,-1,-2003,-300,-4,-1592,-688,-3,-1501,4523,-1, + -12846,3106,-13,2742,1340,-1989,4191,1601,-2301,7965,424,-1008,-4804,-192,-1315,-4396,-1268,-587, + 712,3307,-687,-372,3869,-784,-1750,886,-1949,-1732,839,-1915,-1839,-88,-1327,8952,1654,-1832, + -2203,-2720,-371,5392,2560,-2169,5283,1701,-2283,-13820,2148,-560,-14791,1392,-476,-446,-1054,-482, + -10012,2699,-376,-11933,1443,-399,-1320,2773,-2158,-1134,2698,-2081,-12323,2264,-790,-13313,1030,-457, + -13591,1671,-757,-14512,772,-418,-226,-673,-1322,4427,2684,-2204,-4041,-875,-1547,-2501,1334,-2355, + 7214,1015,-1001,3087,2309,-2097,-14665,1128,-585,-4398,-1274,-1339,9921,2556,-777,-1309,4353,-844, + -584,2398,-2020,8449,1012,-1751,641,-1145,-974,-1125,598,-1911,-1428,-434,-1325,-7132,3742,-404, + -10404,3914,-386,-4142,4364,-1079,-2658,4737,-950,-2366,3077,-2385,-7146,3342,-553,1777,804,-1643, + -3389,-1266,-1357,1708,-303,-1027,1850,-570,-602,2080,2065,-1984,-12643,2873,-534,10018,1123,-1581, + -4250,-2591,-972,2235,2983,-747,-10251,3378,-600,-1567,-643,-485,463,2080,-1984,-5751,-1692,-868, + 682,-1390,-702,-10,352,-1643,3513,3279,-792,4643,3320,-722,5541,3360,-739,-810,-1900,-1467, + -3790,-2234,592,-2901,-1244,253,-2761,-1978,416,7099,3147,946,8926,2959,1280,8958,3359,682, + -5759,2818,660,-6261,2526,746,-6054,1492,1816,3342,-724,276,3058,55,243,3812,-26,202, + 2384,217,650,2101,-414,1315,1873,-802,644,-2746,-756,-1,-4816,-1176,454,-5582,444,5, + -2008,-944,-154,-2019,-755,256,-2509,-4548,677,-2650,-3373,523,-1966,-4249,566,-2509,-4548,1277, + -2640,-3359,1448,-3336,-3331,1335,-1174,-4586,710,-1617,-3963,670,-1947,-4242,1445,-1174,-4586,1249, + -1605,-3964,1286,-1133,-5022,620,-416,-5350,645,6278,16,-5,4259,-642,563,4630,61,-10, + 2187,-189,530,1993,77,1000,3214,-378,1746,2749,357,1744,2192,-99,1510,2270,-1789,562, + 1834,-1355,421,559,-1985,611,-347,-2421,623,-624,-2201,531,4933,-174,2094,3914,-663,1509, + 6120,-3,1445,6714,2194,1813,7745,1708,1335,8000,2304,1369,-5847,110,1803,-6459,641,566, + -5560,-919,1547,-4694,-1464,1446,-3210,-356,2266,-6245,983,3,-1101,-1983,574,-353,-2423,1281, + -605,-2212,1428,-1092,-2020,1427,508,-2007,1332,802,-2226,1188,1834,-1356,1506,2166,-1681,1274, + 9509,3529,1119,10183,3613,1436,10136,2775,2003,10626,3627,1896,10284,2996,1027,10544,3491,1485, + 9056,141,999,9316,-162,854,9634,46,1018,-417,-5304,1370,-284,-5413,993,3864,115,2, + 1824,-721,9,3079,31,0,-6704,1971,-3,-7123,2164,423,-7153,2139,-4,-2131,-974,447, + -1566,-745,477,-1970,-402,885,-3943,-2362,763,9518,-434,415,9213,-296,-1,-5940,2952,410, + -2029,-1728,570,-3501,-3360,645,-1144,-5008,1396,-1351,-5386,1292,-692,-5676,1529,-2253,189,1779, + -2149,-704,1433,842,-2201,739,1412,-892,1470,304,-1751,1265,6287,841,1934,10167,-317,-1, + 10589,15,-1,10149,-307,447,-3002,-1117,1866,-6547,1855,570,3951,32,1993,9688,2041,1878, + 10303,271,784,-495,-1912,1260,-424,-1877,970,-527,-1920,685,-959,-1593,529,-1147,-1401,1040, + -1269,-1809,1042,-462,-5838,969,-1532,-5363,969,-1349,-5387,667,9499,-497,-1,9559,581,1562, + 10199,943,898,10068,779,1460,10302,959,427,10511,1648,731,10765,334,-84,10761,326,165, + -4470,-961,-557,-3699,-1647,-399,-3022,-1228,-247,6950,3291,-949,8957,3359,-684,8925,2959,-1282, + -5637,2887,-660,-5054,2201,-1806,-6256,2529,-750,4005,-154,-296,3985,334,-223,3289,269,-263, + 2558,182,-668,1831,-613,-651,2036,-309,-1323,-4407,-2,-424,-3568,-3177,-660,-3378,-2586,-549, + -4474,-2430,-497,-3572,-3175,-1261,-5034,-2571,-1304,-4479,-2442,-1422,-2510,-2301,-699,-3321,-2138,-653, + -3377,-2575,-1428,-3326,-2137,-1269,-2534,-2301,-1239,-2196,-2626,-612,-1423,-2742,-644,4705,374,-584, + 2614,-69,-1021,2895,-205,-550,3750,208,-1767,2856,-131,-1530,2875,425,-1763,2950,-1333,-442, + 3515,-1590,-583,1911,-2312,-632,863,-2618,-551,1126,-2820,-643,4988,1358,-2114,6040,1303,-1459, + 4484,412,-1530,6417,2687,-1818,7978,2352,-1370,7608,2099,-1338,-4349,1584,-1777,-4561,894,-1512, + -6123,1090,-561,-4212,165,-1411,-2489,-498,-2266,403,-2923,-594,1104,-2812,-1301,419,-2937,-1447, + 834,-2601,-1448,1862,-2358,-1352,2205,-2478,-1208,3130,-1339,-1526,3373,-1529,-1295,9509,3529,-1120, + 10183,3613,-1437,10626,3627,-1897,10136,2775,-2004,10283,2996,-1029,10544,3491,-1486,9056,141,-1000, + 9634,47,-1020,9316,-161,-856,-1254,-2821,-994,-1423,-2750,-1369,-7121,2164,-431,-2326,-1620,-458, + -1858,-998,-900,-1531,-984,-490,-4687,-923,-724,9518,-434,-416,-5938,2952,-413,-3048,-1995,-565, + -5171,-2602,-613,-2197,-2626,-1388,-1609,-3156,-1528,-2242,-2991,-1286,-2273,-1590,-1444,-1714,-885,-1798, + 2244,-2435,-759,2614,-1178,-1490,1633,-2174,-1285,5953,1854,-1946,10149,-306,-448,-3013,-1048,-1859, + -6456,1946,-573,4081,991,-2014,9688,2041,-1880,10303,271,-786,675,-2128,-1280,644,-2166,-706, + 762,-2013,-990,-91,-2713,-1060,105,-2605,-549,228,-2980,-1063,-1331,-3276,-970,-2241,-2994,-660, + -2384,-3047,-961,9559,581,-1564,10199,943,-899,10068,779,-1461,10511,1648,-732,10302,959,-428, + 3175,625,1981,4552,839,2285,8281,529,1007,8228,434,-1,8864,17,-1,-3789,-2233,1329, + -2014,-3308,595,683,3121,686,812,3347,-1,-535,3658,-1,-632,3533,781,-1586,-207,1319, + -2121,903,1905,-2656,1042,1939,9216,1800,1831,-701,-5692,368,5991,1962,2158,5660,707,2268, + -13887,2529,-20,-14709,1578,-22,-14608,1467,437,-13708,2327,525,166,-1258,480,151,-1486,-1, + -9874,2639,357,-11929,1668,372,-11806,1478,-15,-9797,2449,-11,-2022,2553,2074,-3513,2590,2149, + -12224,2526,761,-13447,1866,723,-13125,1247,424,-14277,873,381,-14211,802,-21,-13013,1120,-18, + 315,-799,1319,5059,2119,2195,-2748,-1984,1553,-3973,425,2354,7509,892,998,7523,957,-1, + 3608,2162,2091,-14715,1044,-22,-14460,1214,546,-2015,-3300,1347,10146,2742,775,-5616,3150,-1, + -5852,3116,-1,-875,2234,2016,-1861,3875,841,8740,1137,1749,238,-1509,955,-1006,-583,1320, + -1010,488,1905,-6879,3352,-4,-10177,4162,-11,-10114,3891,366,-6883,3145,397,-4798,3449,1079, + -5059,1795,2385,-3314,4050,948,-6968,2753,546,2197,288,1638,-2029,-1728,1356,1411,-892,583, + 1252,-622,1008,-4447,3701,0,-3042,4141,-2,10520,1666,-1,10332,982,-1,2585,2112,1978, + -12465,3169,504,10303,1313,1579,-2718,-4587,977,2461,2898,746,2447,3144,-1,-10018,3341,581, + 10034,2779,-1,-1041,-1099,477,787,2166,1980,-3655,-3422,882,341,-1752,684,416,398,1639, + 3934,3191,788,5036,3137,719,5881,3204,736,-949,-1605,1448,3721,3337,-1,6957,3426,-1, + 5773,3457,-1,4894,3426,-1,8999,3426,-1,-1565,-1009,-7,-1044,-1267,-5,-1646,3918,-2, + -12642,3422,-17,2780,1189,-1990,4136,1691,-2302,8281,529,-1008,-4477,-954,-1294,-4026,-2055,-573, + 683,3121,-687,-467,3450,-785,-1439,94,-1959,-1411,96,-1923,-1407,-796,-1333,9216,1800,-1832, + -1600,-3145,-367,5395,2670,-2169,5239,1935,-2284,-13706,2327,-565,-14606,1467,-480,175,-1401,-482, + -9876,2639,-378,-11929,1666,-403,-1094,1993,-2168,-1023,2041,-2088,-12220,2526,-794,-13123,1247,-461, + -13444,1866,-761,-14275,873,-423,325,-1013,-1323,4342,2727,-2205,-3727,-1638,-1536,-2163,562,-2354, + 7459,1150,-1001,3163,2232,-2098,-14458,1214,-589,-4031,-2060,-1324,10146,2742,-777,-1409,3738,-846, + -469,1927,-2024,8740,1137,-1751,1520,-1978,-975,-752,51,-1915,-906,-998,-1329,-6881,3145,-404, + -10082,3894,-388,-4039,3585,-1079,-2642,3980,-953,-2056,2294,-2384,-6965,2753,-554,1957,568,-1644, + -3054,-1997,-1352,2164,-818,-1029,2391,-1021,-604,2217,1939,-1984,-12463,3169,-538,10303,1313,-1581, + -3696,-3347,-960,2400,2902,-747,-10014,3344,-603,-1026,-1224,-488,612,1861,-1984,-5300,-2645,-848, + 1579,-2192,-704,387,36,-1644,3645,3226,-792,4754,3313,-723,5668,3391,-739,113,-2592,-1468, + -4644,-2897,592,-3423,-2093,253,-3571,-2871,416,7172,3342,946,9026,3219,1280,9039,3620,682, + -5805,2120,660,-6319,1813,746,-6079,971,1816,3577,-621,278,3290,96,245,4088,115,203, + 2508,234,652,2348,-409,1316,2242,-849,644,-2941,-1448,-1,-5265,-1686,455,-5646,-167,7, + -1823,-1699,-157,-1878,-1599,254,-3554,-5244,677,-3927,-4123,523,-3082,-4842,566,-3554,-5244,1277, + -3906,-4127,1448,-4605,-4105,1335,-2245,-5010,710,-2797,-4492,670,-3067,-4832,1445,-2245,-5010,1249, + -2787,-4490,1286,-2117,-5424,620,-1340,-5551,645,6486,334,-4,4479,-477,566,4914,317,-10, + 2300,-211,532,2066,28,1003,3368,-295,1749,2858,397,1747,2292,-119,1513,2689,-1792,565, + 2181,-1457,424,1076,-2353,614,187,-2832,625,-104,-2616,533,5074,3,2096,4089,-545,1512, + 6292,225,1447,6822,2381,1814,7900,1922,1335,8131,2522,1369,-6055,-267,1803,-6531,45,566, + -5969,-1292,1547,-5278,-1950,1446,-3590,-1208,2266,-6273,310,4,-592,-2409,576,183,-2834,1283, + -81,-2628,1430,-583,-2446,1430,1030,-2385,1334,1363,-2534,1191,2181,-1458,1508,2568,-1707,1277, + 9581,3816,1119,10250,3932,1436,10244,3093,2003,10692,3968,1896,10380,3321,1027,10617,3828,1485, + 9291,410,999,9566,120,854,9873,344,1018,-1351,-5511,1370,-1196,-5577,993,4240,280,2, + 2517,-987,7,3395,56,1,-6797,1302,-3,-7214,1549,423,-7248,1531,-4,-2437,-2025,446, + -1208,-1468,475,-1910,-1325,884,-4831,-2990,763,9781,-142,415,9469,-19,-1,-5987,2220,410, + -2817,-2810,570,-4773,-4128,645,-2129,-5414,1396,-2240,-5835,1292,-1520,-5943,1529,-2311,-837,1779, + -2522,-1815,1433,1398,-2501,741,1673,-1097,1472,800,-2177,1267,6413,1055,1936,10424,6,-1, + 10829,359,-1,10405,16,447,-3577,-1991,1866,-6616,1176,570,4079,146,1996,9832,2339,1878, + 10531,600,784,16,-2319,1263,88,-2282,972,-17,-2327,688,-461,-2015,531,-653,-1829,1042, + -764,-2240,1045,-1255,-6034,969,-2425,-5854,969,-2239,-5835,667,9764,-206,-1,9773,874,1562, + 10395,1267,898,10272,1097,1460,10497,1287,427,10672,1985,731,10990,686,-84,10986,677,165, + -4442,-1483,-551,-3645,-2139,-399,-3085,-1743,-247,7040,3456,-949,9039,3620,-684,9026,3219,-1282, + -5689,2176,-660,-5084,1569,-1803,-6316,1815,-750,4855,119,-298,4481,536,-223,3932,258,-264, + 3277,-91,-670,2509,-895,-653,2611,-572,-1324,-4471,-530,-418,-2914,-3937,-666,-2737,-3342,-555, + -3846,-3203,-504,-2918,-3936,-1266,-4435,-3454,-1311,-3862,-3204,-1429,-1890,-3032,-703,-2691,-2894,-658, + -2736,-3331,-1434,-2695,-2892,-1275,-1911,-3033,-1243,-1567,-3341,-616,-783,-3343,-646,5280,816,-585, + 3589,-384,-1024,3909,-387,-553,4565,348,-1769,3842,-334,-1533,3565,229,-1765,4515,-1381,-445, + 5133,-1341,-586,4089,-2740,-636,3134,-3284,-556,3439,-3426,-648,5272,1836,-2115,6122,1849,-1459, + 5176,805,-1531,6387,2907,-1818,8094,2578,-1370,7628,2419,-1337,-4409,1037,-1770,-4607,363,-1503, + -6150,430,-558,-4243,-334,-1403,-2502,-945,-2267,2651,-3604,-599,3415,-3424,-1306,2668,-3618,-1452, + 3094,-3274,-1453,4066,-2805,-1356,4420,-2743,-1213,4653,-1321,-1529,4981,-1352,-1298,9581,3816,-1120, + 10250,3932,-1437,10692,3968,-1897,10244,3093,-2004,10380,3321,-1029,10617,3828,-1486,9292,410,-1000, + 9874,344,-1020,9566,121,-856,-610,-3392,-995,-782,-3349,-1371,-7213,1549,-431,-2292,-2121,-461, + -1682,-1643,-905,-1150,-1615,-493,-4657,-1465,-718,9781,-142,-417,-5985,2220,-412,-2933,-2322,-568, + -4588,-3511,-620,-1566,-3341,-1392,-905,-3782,-1530,-1570,-3725,-1290,-2213,-2005,-1448,-1588,-1394,-1804, + 4438,-2685,-763,4103,-1415,-1493,3812,-2748,-1289,5950,2174,-1946,10405,16,-448,-3016,-1502,-1859, + -6522,1252,-573,4591,1192,-2015,9832,2339,-1880,10531,600,-786,2901,-2831,-1285,2874,-2870,-710, + 2974,-2715,-994,2148,-3385,-1065,2353,-3287,-554,2462,-3657,-1068,-617,-3853,-972,-1569,-3728,-664, + -1715,-3797,-965,9773,874,-1564,10394,1267,-899,10272,1097,-1461,10672,1985,-732,10497,1287,-428, + 3348,692,1982,4642,978,2287,8498,760,1007,8450,663,-1,9106,277,-1,-4644,-2896,1329, + -3285,-3971,595,561,2965,686,662,3210,-1,-721,3263,-1,-767,3121,781,-1260,-970,1319, + -1931,66,1905,-2602,117,1939,9372,2075,1831,-1525,-5959,368,6061,2153,2160,5763,912,2270, + -13912,2676,-18,-14783,1756,-20,-14688,1640,439,-13745,2464,527,714,-1436,480,761,-1693,-1, + -9910,2341,358,-12078,1683,373,-11984,1479,-14,-9860,2141,-10,-1913,1881,2074,-3321,1855,2149, + -12254,2571,763,-13511,1989,725,-13225,1355,426,-14387,1030,383,-14325,956,-19,-13121,1223,-16, + 792,-987,1319,5127,2256,2196,-3579,-2872,1553,-4163,-265,2354,7700,1097,998,7720,1151,-1, + 3695,2231,2092,-14816,1224,-20,-14553,1380,548,-3284,-3974,1347,10255,3061,775,-5663,2430,-1, + -5891,2381,-1,-787,1769,2016,-1954,3266,841,8928,1390,1749,713,-1956,958,-547,-1126,1320, + -687,-55,1905,-6836,2704,-3,-10002,3896,-10,-9974,3617,367,-6865,2496,397,-4707,2791,1079, + -4942,1307,2385,-3265,3346,948,-6994,2117,546,2420,299,1639,-2816,-2811,1356,1673,-1098,586, + 1457,-867,1011,-4415,2985,0,-3041,3437,-2,10680,2004,-1,10526,1312,-1,2671,2138,1979, + -12405,3240,506,10481,1642,1579,-3751,-5324,977,2535,2910,746,2509,3154,-1,-9954,3058,582, + 10141,3093,-1,-544,-1628,476,830,2031,1981,-4933,-4173,882,836,-2173,686,723,212,1639, + 4002,3256,789,5117,3236,719,5959,3337,736,-450,-2026,1450,3799,3373,-1,7042,3591,-1, + 5849,3570,-1,4957,3500,-1,9077,3688,-1,-1176,-1687,-9,-529,-1776,-7,-1796,3323,-2, + -12547,3514,-15,3108,951,-1991,4286,1784,-2303,8498,760,-1008,-4455,-1473,-1288,-3444,-2797,-579, + 562,2965,-687,-628,3058,-785,-1365,-407,-1965,-1226,-431,-1927,-1045,-1338,-1337,9372,2075,-1832, + -899,-3772,-369,5319,2827,-2169,5234,2250,-2284,-13743,2465,-563,-14686,1640,-478,790,-1627,-482, + -9912,2341,-377,-12080,1681,-402,-1149,1495,-2172,-1036,1530,-2091,-12251,2571,-792,-13223,1355,-459, + -13508,1990,-759,-14385,1030,-421,875,-1246,-1324,4238,2745,-2205,-3673,-2128,-1535,-2211,131,-2354, + 7599,1416,-1001,3218,2148,-2098,-14550,1380,-587,-3447,-2799,-1331,10255,3061,-777,-1569,3164,-847, + -439,1550,-2026,8928,1390,-1751,3653,-2628,-980,-448,-350,-1918,-435,-1399,-1331,-6863,2497,-404, + -9944,3616,-387,-4027,2903,-1079,-2682,3296,-953,-2131,1823,-2383,-6991,2118,-553,2345,271,-1645, + -2943,-2318,-1354,3580,-1311,-1032,3880,-1377,-607,2326,1816,-1985,-12402,3241,-536,10481,1642,-1581, + -3038,-4111,-966,2464,2886,-747,-9950,3060,-601,-522,-1710,-490,684,1697,-1984,-4710,-3604,-855, + 3748,-2796,-708,758,-197,-1645,3682,3227,-792,4762,3366,-723,5681,3505,-739,2359,-3272,-1473, + -6154,-3069,592,-4520,-2781,253,-5165,-3491,416,7127,3509,948,8988,3419,1280,8986,3821,682, + -5874,1684,660,-6383,1314,747,-6040,793,1817,3753,-403,291,3494,245,256,4346,356,214, + 2541,320,663,2422,-383,1320,2468,-841,647,-3388,-2021,0,-5977,-1753,455,-5676,-467,6, + -1926,-2327,-155,-2090,-2348,255,-5751,-5466,677,-6067,-4329,523,-5259,-5088,566,-5751,-5466,1277, + -6047,-4357,1448,-6692,-4196,1335,-4452,-5290,710,-4957,-4754,670,-5250,-5081,1445,-4452,-5290,1249, + -4950,-4752,1286,-4344,-5694,620,-3561,-5708,645,6563,621,-1,4643,-236,578,5167,626,-1, + 2347,-162,546,2071,30,1016,3457,-110,1762,2897,518,1758,2324,-70,1526,3161,-1608,578, + 2592,-1440,437,1842,-2646,627,1067,-3292,639,735,-3132,546,5155,244,2110,4200,-341,1525, + 6363,465,1454,6810,2549,1816,7908,2094,1336,8119,2693,1369,-6254,-216,1803,-6594,-212,567, + -6531,-1147,1547,-6240,-1948,1446,-4426,-1994,2266,-6291,-88,4,211,-3027,590,1065,-3295,1296, + 763,-3140,1443,227,-3062,1443,1808,-2691,1347,2169,-2726,1204,2592,-1441,1522,3027,-1560,1290, + 9521,4037,1119,10185,4178,1436,10209,3339,2003,10625,4229,1896,10337,3572,1027,10555,4087,1485, + 9355,622,999,9639,343,854,9939,577,1018,-3569,-5682,1370,-3415,-5714,993,4624,549,12, + 3251,-1071,12,3707,200,12,-6883,855,-1,-7293,1152,425,-7332,1143,-2,-3381,-3065,446, + -1183,-2073,476,-2229,-2192,884,-6369,-3076,763,9864,88,415,9548,200,-1,-6059,1708,410, + -4478,-3752,570,-6854,-4179,645,-4353,-5687,1396,-4437,-6128,1292,-3683,-6137,1529,-2810,-1938,1778, + -3572,-2950,1433,2193,-2683,754,2005,-1264,1485,1542,-2564,1281,6446,1285,1944,10501,260,-1, + 10894,627,-1,10482,269,447,-4767,-2709,1866,-6657,727,571,4154,338,2009,9825,2570,1878, + 10587,858,784,794,-2811,1276,860,-2760,985,763,-2826,701,260,-2614,544,36,-2471,1055, + 11,-2896,1058,-3409,-6175,969,-4628,-6157,969,-4435,-6128,667,9850,24,-1,9819,1103,1562, + 10426,1519,898,10310,1345,1460,10528,1543,427,10678,2247,731,11042,960,-84,11039,952,165, + -4431,-1761,-552,-3654,-2443,-398,-3255,-2119,-245,6999,3586,-949,8986,3821,-684,8987,3420,-1282, + -5770,1718,-660,-5056,1184,-1805,-6381,1316,-749,5739,582,-292,5022,842,-216,4695,408,-258, + 4199,-166,-665,3237,-1004,-649,3244,-709,-1321,-4502,-818,-420,-2701,-4439,-663,-2431,-3880,-554, + -3510,-3560,-505,-2705,-4440,-1263,-4136,-3804,-1312,-3536,-3559,-1430,-1564,-3703,-703,-2312,-3446,-660, + -2430,-3875,-1433,-2315,-3446,-1276,-1581,-3705,-1243,-1292,-4047,-614,-508,-4036,-644,5863,1342,-580, + 4841,-419,-1017,5140,-286,-547,5489,671,-1763,5054,-264,-1527,4456,204,-1760,6172,-899,-439, + 6698,-579,-580,6454,-2291,-628,5744,-3141,-548,6080,-3167,-640,5599,2280,-2111,6132,2288,-1456, + 5908,1312,-1526,6269,3031,-1818,8068,2751,-1370,7518,2636,-1337,-4349,699,-1774,-4548,54,-1506, + -6146,38,-559,-4217,-626,-1406,-2493,-1300,-2265,5305,-3544,-591,6058,-3174,-1298,5322,-3555,-1444, + 5699,-3147,-1445,6464,-2361,-1349,6743,-2137,-1205,6249,-814,-1523,6568,-657,-1292,9521,4037,-1120, + 10185,4178,-1437,10625,4229,-1897,10209,3339,-2005,10337,3572,-1029,10555,4087,-1487,9355,622,-1000, + 9939,577,-1020,9639,343,-856,-341,-4078,-992,-507,-4044,-1369,-7293,1153,-429,-2449,-2553,-457, + -1684,-2135,-902,-1046,-2087,-491,-4640,-1740,-720,9864,88,-417,-6058,1708,-412,-2944,-2563,-565, + -4305,-3861,-621,-1292,-4050,-1390,-621,-4485,-1526,-1309,-4455,-1286,-2235,-2344,-1444,-1538,-1792,-1800, + 6733,-2076,-756,5783,-1153,-1487,6230,-2424,-1282,5879,2384,-1945,10482,269,-448,-3031,-1833,-1857, + -6561,773,-572,5229,1469,-2011,9825,2570,-1880,10587,858,-786,5429,-2780,-1277,5412,-2822,-702, + 5469,-2658,-987,4763,-3390,-1057,4966,-3273,-546,5108,-3623,-1059,-337,-4539,-968,-1309,-4455,-660, + -1469,-4519,-961,9819,1104,-1564,10426,1519,-900,10310,1345,-1461,10678,2247,-733,10527,1543,-428, + 3394,774,1986,4674,1174,2298,8549,943,1007,8504,844,-1,9174,482,-1,-6154,-3067,1329, + -5415,-4257,595,433,2851,686,516,3105,-1,-863,3010,-1,-859,2824,781,-1267,-1660,1319, + -1982,-770,1905,-2799,-898,1939,9375,2289,1831,-3686,-6148,368,6047,2356,2167,5799,1155,2281, + -13799,3458,-6,-14780,2631,-7,-14698,2505,452,-13660,3228,539,965,-1508,481,1075,-1783,-1, + -9960,2274,364,-12228,2232,382,-12190,2011,-5,-9957,2065,-3,-1802,1242,2074,-3030,1029,2149, + -12176,3130,773,-13488,2728,737,-13286,2064,437,-14471,1864,395,-14418,1783,-6,-13204,1924,-5, + 993,-1098,1320,5109,2414,2202,-5187,-3481,1553,-4551,-900,2354,7737,1268,999,7756,1305,-1, + 3678,2312,2095,-14874,2106,-7,-14594,2231,561,-5415,-4268,1347,10222,3307,775,-5764,1969,-1, + -5966,1870,-1,-747,1363,2016,-1962,2824,841,8956,1588,1749,1410,-2381,971,-410,-1574,1320, + -589,-529,1905,-6780,2257,-1,-9702,3832,-3,-9725,3547,374,-6836,2052,399,-4563,2426,1079, + -4645,915,2385,-3144,2877,948,-7008,1691,548,2487,327,1641,-4477,-3755,1356,2005,-1264,599, + 1724,-1111,1024,-4405,2595,0,-2995,2995,-2,10685,2266,-1,10556,1569,-1,2653,2181,1983, + -12152,3814,516,10499,1897,1579,-5952,-5536,977,2495,2917,746,2459,3158,-1,-9830,2989,588, + 10107,3335,-1,-383,-2042,477,795,1956,1983,-7010,-4174,882,1574,-2552,699,836,93,1641, + 3958,3322,791,5080,3333,721,5918,3455,738,273,-2622,1464,3758,3397,-1,6996,3721,-1, + 5800,3657,-1,4906,3555,-1,9022,3891,-1,-1106,-2223,-8,-348,-2156,-6,-1877,2936,-2, + -12224,4112,-5,3610,770,-1988,4560,1844,-2300,8549,943,-1008,-4445,-1752,-1290,-3074,-3219,-581, + 433,2851,-687,-752,2799,-785,-1310,-813,-1961,-1133,-806,-1925,-895,-1706,-1335,9375,2289,-1832, + -615,-4473,-365,5212,2881,-2168,5242,2455,-2282,-13659,3228,-551,-14698,2505,-465,1127,-1732,-482, + -9962,2275,-371,-12234,2231,-392,-1101,1069,-2170,-1036,1162,-2090,-12175,3130,-782,-13286,2064,-448, + -13488,2728,-748,-14470,1864,-408,1190,-1369,-1323,4190,2665,-2204,-3676,-2432,-1534,-2166,-232,-2353, + 7587,1616,-1001,3268,2032,-2097,-14594,2231,-574,-3076,-3220,-1333,10222,3307,-777,-1661,2793,-847, + -451,1293,-2025,8956,1588,-1751,6050,-2390,-972,-316,-603,-1917,-219,-1633,-1330,-6835,2052,-402, + -9704,3542,-380,-3999,2511,-1080,-2683,2876,-953,-2037,1435,-2384,-7007,1692,-551,2813,57,-1642, + -2955,-2558,-1351,5317,-1284,-1026,5615,-1197,-600,2403,1688,-1984,-12150,3814,-526,10499,1897,-1581, + -2852,-4592,-962,2430,2862,-747,-9828,2990,-595,-332,-2058,-489,671,1586,-1984,-4425,-3970,-856, + 6185,-2503,-701,959,-340,-1645,3637,3202,-792,4690,3375,-722,5596,3567,-739,4968,-3257,-1465, + -7063,-2574,592,-5303,-2771,253,-6259,-3289,416,7172,3544,949,8965,3441,1280,8958,3842,682, + -5810,1670,660,-6332,1290,747,-5920,894,1817,4271,-214,299,3967,301,263,4849,419,220, + 2896,353,670,2541,-432,1322,2655,-886,649,-3652,-2194,0,-6345,-1406,454,-5640,-444,4, + -2085,-2582,-151,-2370,-2627,257,-7391,-5109,677,-7311,-3931,523,-6802,-4916,566,-7391,-5109,1277, + -7303,-3955,1448,-7880,-3608,1335,-6149,-5353,710,-6406,-4701,670,-6803,-4917,1445,-6149,-5353,1249, + -6406,-4701,1286,-6179,-5736,620,-5404,-5687,645,6744,624,1,5173,-104,586,5626,621,4, + 2831,-188,554,2524,-92,1024,3939,71,1770,3305,610,1766,2786,-105,1534,4207,-1118,587, + 3673,-1254,446,3727,-2673,636,3376,-3616,647,3006,-3641,555,5663,427,2118,4723,-161,1533, + 6673,577,1459,6899,2595,1818,7929,2115,1336,8111,2706,1369,-6239,87,1803,-6573,-129,567, + -6747,-683,1547,-6758,-1482,1446,-5075,-2094,2266,-6251,-129,2,2500,-3823,599,3375,-3619,1305, + 3035,-3633,1452,2531,-3845,1452,3723,-2729,1356,4043,-2561,1213,3673,-1254,1530,4082,-1136,1299, + 9489,4066,1119,10151,4216,1435,10187,3377,2003,10590,4273,1895,10312,3612,1027,10523,4130,1485, + 9369,648,999,9657,373,854,9954,611,1018,-5404,-5690,1370,-5258,-5681,993,5140,519,16, + 3682,-1275,13,4196,197,17,-6833,828,-1,-7241,1122,425,-7280,1113,-2,-4175,-3303,447, + -1340,-2316,478,-2610,-2499,885,-7270,-2516,763,9886,121,415,9568,229,-1,-6008,1685,410, + -5659,-3738,570,-8034,-3544,645,-6178,-5737,1396,-6302,-6182,1292,-5500,-6155,1529,-3362,-2333,1778, + -4477,-3198,1433,4039,-2511,763,3105,-1426,1494,3438,-2763,1289,6738,1413,1949,10521,303,-1, + 10908,676,-1,10501,311,447,-5636,-2669,1866,-6602,722,571,4667,496,2018,9813,2604,1878, + 10598,902,784,2889,-3326,1285,2923,-3247,994,2869,-3356,710,2328,-3442,553,2064,-3437,1064, + 2263,-3814,1067,-5218,-6141,969,-6506,-6185,969,-6302,-6182,667,9872,57,-1,9827,1137,1562, + 10429,1561,898,10315,1385,1460,10530,1586,427,10670,2292,731,11052,1011,-84,11049,1002,165, + -4229,-1924,-566,-3487,-2670,-403,-3206,-2338,-247,6978,3583,-949,8957,3842,-684,8964,3442,-1282, + -5711,1686,-661,-4915,1083,-1810,-6331,1292,-749,6324,385,-292,5554,689,-213,5271,157,-256, + 4800,-514,-664,3651,-1226,-648,3605,-960,-1321,-4344,-944,-432,-3271,-4939,-658,-2809,-4524,-550, + -3686,-3824,-506,-3272,-4941,-1258,-4364,-3835,-1315,-3704,-3822,-1431,-1974,-4666,-696,-2535,-4166,-656, + -2808,-4524,-1429,-2537,-4169,-1273,-1984,-4667,-1235,-1843,-5065,-605,-1060,-5097,-632,6372,1160,-579, + 5604,-837,-1018,5877,-646,-547,6092,384,-1763,5786,-639,-1527,5040,-162,-1760,7062,-993,-439, + 7489,-557,-580,7707,-2253,-628,7283,-3305,-547,7610,-3225,-640,5995,2005,-2111,6336,2140,-1456, + 6440,1070,-1526,6283,2944,-1818,8054,2754,-1370,7520,2592,-1337,-4103,527,-1785,-4297,-117,-1521, + -6078,-16,-562,-3969,-825,-1420,-2266,-1593,-2265,6904,-3767,-591,7595,-3235,-1298,6922,-3776,-1444, + 7242,-3325,-1445,7736,-2317,-1349,7946,-2027,-1205,7103,-927,-1523,7383,-662,-1292,9488,4066,-1120, + 10151,4216,-1437,10590,4273,-1897,10186,3377,-2005,10312,3612,-1029,10522,4130,-1487,9369,648,-1000, + 9954,612,-1020,9657,373,-856,-907,-5134,-980,-1060,-5104,-1357,-7241,1123,-429,-2437,-2858,-454, + -1694,-2374,-896,-1098,-2290,-488,-4437,-1880,-735,9886,121,-417,-6008,1685,-412,-2777,-2919,-566, + -4546,-3840,-625,-1843,-5069,-1381,-1198,-5548,-1514,-1930,-5497,-1276,-2066,-2654,-1439,-1399,-2068,-1792, + 7919,-1971,-756,6713,-1372,-1487,7521,-2446,-1282,5973,2240,-1945,10501,311,-448,-2831,-2099,-1860, + -6504,742,-572,5752,1146,-2011,9813,2604,-1880,10598,902,-786,6932,-3041,-1277,6925,-3082,-702, + 6942,-2924,-987,6336,-3677,-1057,6539,-3541,-546,6701,-3875,-1059,-922,-5595,-955,-1930,-5495,-650, + -2117,-5528,-951,9827,1137,-1564,10429,1561,-900,10315,1385,-1461,10670,2292,-733,10530,1586,-428, + 3560,796,1989,5093,1318,2306,8558,958,1006,8515,858,-1,9190,506,-1,-7063,-2574,1329, + -6677,-4066,595,421,2789,686,502,3043,-1,-872,2935,-1,-852,2711,781,-1467,-1940,1319, + -2189,-1127,1905,-3111,-1325,1939,9367,2316,1831,-5499,-6153,368,6288,2466,2171,6202,1320,2288, + -13641,4001,-6,-14734,3330,-7,-14673,3193,452,-13538,3753,539,1021,-1559,481,1140,-1850,0, + -10002,2246,364,-12270,2554,382,-12266,2330,-5,-10022,2036,-3,-1786,933,2074,-2925,586,2149, + -12086,3435,773,-13443,3233,736,-13343,2546,437,-14543,2525,395,-14504,2438,-6,-13282,2396,-6, + 1032,-1171,1321,5328,2498,2207,-6274,-3276,1553,-4852,-1024,2354,7780,1293,1000,7759,1310,-1, + 3803,2334,2098,-14907,2825,-7,-14611,2907,561,-6680,-4075,1347,10200,3345,775,-5716,1945,-1, + -5917,1847,-1,-749,1168,2016,-1916,2663,841,8957,1609,1749,3236,-2676,980,-504,-1768,1320, + -661,-746,1905,-6735,2230,-1,-9584,3784,-3,-9624,3495,374,-6789,2025,399,-4431,2349,1079, + -4418,770,2385,-3041,2722,948,-6960,1663,548,2575,308,1642,-5660,-3740,1356,3105,-1427,608, + 2778,-1447,1033,-4356,2529,0,-2922,2869,-2,10677,2311,-1,10557,1612,-1,2762,2181,1985, + -11959,4107,516,10497,1939,1579,-7604,-5107,977,2491,2878,746,2444,3113,-1,-9787,2946,588, + 10084,3372,-1,-456,-2226,478,848,1915,1984,-8186,-3497,882,3458,-2737,708,891,22,1642, + 4005,3321,792,5125,3344,722,5950,3469,739,2343,-3443,1473,3744,3355,-1,6974,3718,-1, + 5781,3637,-1,4905,3522,-1,8992,3913,-1,-1215,-2442,-5,-401,-2326,-4,-1855,2822,-2, + -11987,4413,-5,4012,422,-1988,4909,1517,-2300,8559,958,-1008,-4237,-1919,-1303,-3133,-3678,-582, + 421,2789,-687,-748,2710,-784,-1142,-1098,-1953,-1035,-1017,-1918,-862,-1882,-1330,9367,2316,-1833, + -1194,-5538,-353,5279,2720,-2168,5443,2217,-2282,-13537,3753,-551,-14672,3193,-465,1196,-1811,-482, + -10003,2247,-371,-12276,2554,-392,-877,768,-2163,-950,980,-2084,-12085,3435,-782,-13342,2547,-448, + -13442,3233,-748,-14543,2525,-408,1272,-1466,-1323,4329,2413,-2204,-3500,-2663,-1539,-1880,-546,-2354, + 7599,1598,-1001,3370,1857,-2097,-14610,2907,-574,-3134,-3681,-1333,10200,3345,-777,-1628,2674,-845, + -420,1175,-2022,8958,1609,-1751,7331,-2466,-972,-276,-726,-1913,-182,-1746,-1327,-6788,2025,-402, + -9612,3492,-380,-3906,2407,-1080,-2611,2750,-952,-1717,1133,-2385,-6958,1664,-551,3117,-203,-1643, + -2784,-2916,-1352,6340,-1588,-1026,6607,-1423,-600,2487,1543,-1984,-11958,4107,-526,10497,1939,-1581, + -3467,-5025,-957,2429,2798,-747,-9785,2947,-595,-361,-2218,-486,677,1519,-1984,-4695,-3902,-860, + 7503,-2530,-701,1020,-440,-1644,3647,3118,-792,4699,3290,-722,5591,3508,-739,6537,-3525,-1465, + -7521,-2141,592,-5763,-2650,253,-6886,-3007,416,7286,3523,949,8968,3387,1280,8960,3788,682, + -5779,1674,660,-6313,1291,747,-5819,944,1817,5106,-213,297,4726,156,262,5608,282,220, + 3593,192,669,2786,-567,1322,2892,-994,649,-3787,-2272,-1,-6491,-1137,452,-5588,-423,1, + -2196,-2727,-148,-2571,-2762,259,-8388,-4757,677,-7956,-3657,523,-7768,-4751,566,-8388,-4757,1277, + -7956,-3666,1448,-8458,-3197,1335,-7318,-5348,710,-7325,-4664,670,-7781,-4756,1445,-7318,-5348,1249, + -7334,-4664,1286,-7463,-5671,620,-6700,-5549,645,7029,513,1,6059,-121,585,6301,437,3, + 3805,-415,553,3528,-430,1023,4782,59,1769,4056,512,1765,3744,-354,1533,5367,-489,585, + 5066,-883,444,6007,-1963,634,6274,-2930,646,5986,-3166,553,6540,492,2117,5622,-131,1532, + 7199,606,1459,7069,2586,1818,7979,2073,1336,8126,2653,1369,-6163,264,1803,-6540,-69,567, + -6785,-371,1547,-6983,-1121,1446,-5480,-2068,2266,-6217,-143,0,5693,-3620,597,6275,-2932,1304, + 6005,-3142,1451,5731,-3619,1450,6039,-2009,1354,6186,-1680,1211,5066,-884,1529,5293,-563,1297, + 9490,4015,1119,10152,4166,1435,10190,3328,2003,10591,4225,1895,10314,3563,1027,10523,4082,1485, + 9380,595,999,9669,321,854,9965,560,1018,-6697,-5581,1370,-6554,-5531,993,5844,273,16, + 4049,-1583,12,4912,-1,17,-6810,821,-1,-7220,1106,425,-7258,1096,-2,-4678,-3319,448, + -1470,-2446,479,-2885,-2634,886,-7711,-2045,763,9898,70,415,9580,176,-1,-5991,1689,410, + -6340,-3550,570,-8600,-3101,645,-7455,-5681,1396,-7605,-6114,1292,-6768,-6050,1529,-3762,-2499,1778, + -5061,-3202,1433,6153,-1643,761,4747,-1366,1493,5837,-2206,1288,7238,1469,1948,10533,254,-1, + 10920,628,-1,10514,262,447,-6154,-2517,1866,-6573,730,571,5553,511,2016,9818,2553,1878, + 10609,854,784,5696,-2980,1283,5676,-2893,993,5698,-3017,708,5323,-3422,552,5113,-3578,1062, + 5500,-3755,1065,-6475,-5985,969,-7810,-6094,969,-7606,-6113,667,9885,6,-1,9837,1085,1562, + 10437,1512,898,10324,1336,1460,10538,1538,426,10677,2244,731,11063,964,-84,11059,955,165, + -4015,-2026,-585,-3325,-2840,-414,-3114,-2494,-253,6986,3526,-949,8960,3788,-684,8967,3388,-1282, + -5679,1681,-661,-4780,1015,-1815,-6312,1292,-749,6880,-106,-292,6186,302,-214,5851,-311,-256, + 5323,-1032,-664,3987,-1541,-648,3889,-1286,-1321,-4163,-1031,-446,-4127,-5023,-670,-3534,-4843,-559, + -4025,-3843,-519,-4126,-5025,-1270,-4647,-3564,-1331,-4030,-3842,-1444,-2879,-5299,-701,-3133,-4637,-663, + -3532,-4845,-1438,-3132,-4639,-1280,-2879,-5300,-1240,-2918,-5689,-610,-2141,-5739,-635,6931,690,-579, + 6231,-1465,-1018,6483,-1244,-547,6631,-174,-1763,6390,-1246,-1527,5542,-709,-1760,7739,-1396,-439, + 8084,-900,-580,8607,-2509,-629,8495,-3662,-548,8784,-3490,-640,6454,1468,-2111,6647,1791,-1456, + 6955,526,-1527,6358,2787,-1818,8065,2690,-1370,7577,2458,-1337,-3853,399,-1797,-4040,-239,-1539, + -6016,-41,-566,-3713,-969,-1438,-2037,-1832,-2268,8197,-4172,-591,8774,-3500,-1298,8215,-4179,-1444, + 8461,-3691,-1445,8647,-2565,-1349,8801,-2239,-1205,7757,-1357,-1523,7996,-1017,-1292,9490,4015,-1120, + 10152,4166,-1437,10591,4225,-1897,10190,3328,-2005,10314,3563,-1029,10523,4082,-1487,9380,595,-1000, + 9965,561,-1020,9669,321,-856,-1995,-5757,-982,-2140,-5742,-1359,-7220,1108,-429,-2401,-3109,-452, + -1692,-2538,-892,-1149,-2417,-484,-4216,-1953,-756,9898,70,-417,-5991,1689,-412,-2671,-3259,-571, + -4817,-3507,-641,-2917,-5691,-1386,-2292,-6190,-1517,-3079,-6121,-1282,-1910,-2933,-1438,-1268,-2293,-1785, + 8763,-2191,-756,7427,-1862,-1487,8451,-2745,-1282,6139,1978,-1945,10514,262,-449,-2628,-2307,-1866, + -6472,735,-573,6270,575,-2011,9818,2553,-1880,10609,854,-786,8130,-3489,-1277,8131,-3527,-703, + 8114,-3384,-987,7614,-4166,-1057,7808,-4002,-546,8001,-4316,-1060,-2014,-6217,-957,-3080,-6120,-656, + -3282,-6108,-958,9837,1086,-1564,10437,1512,-900,10324,1336,-1461,10676,2244,-733,10538,1538,-428, + 3837,750,1989,5813,1343,2304,8568,902,1006,8524,802,-1,9201,452,-1,-7521,-2141,1329, + -7394,-3951,595,428,2741,686,510,2996,-1,-861,2890,-1,-841,2638,781,-1636,-2083,1320, + -2370,-1320,1905,-3365,-1551,1939,9373,2264,1831,-6763,-6038,368,6710,2504,2171,6894,1390,2287, + -13527,4316,-6,-14681,3756,-7,-14633,3614,452,-13448,4058,539,1073,-1618,481,1178,-1918,-1, + -10064,2202,364,-12305,2741,382,-12322,2517,-5,-10098,1993,-4,-1810,734,2074,-2915,278,2149, + -12035,3599,773,-13406,3531,736,-13373,2839,437,-14570,2936,395,-14539,2845,-7,-13328,2683,-6, + 1077,-1243,1321,5710,2508,2206,-6891,-3000,1553,-5042,-1073,2354,7854,1255,1000,7765,1251,-1, + 4026,2300,2098,-14902,3271,-7,-14600,3323,561,-7396,-3955,1347,10203,3296,775,-5698,1948,-1, + -5901,1852,-1,-766,1043,2016,-1888,2561,841,8966,1555,1749,5628,-2254,978,-599,-1876,1320, + -738,-872,1905,-6733,2223,-1,-9548,3716,-3,-9598,3426,374,-6784,2017,399,-4349,2292,1079, + -4280,623,2385,-2982,2620,948,-6948,1652,548,2713,251,1642,-6340,-3551,1356,4747,-1366,606, + 4495,-1578,1032,-4317,2498,0,-2868,2791,-1,10684,2263,-1,10566,1564,-1,2954,2132,1984, + -11843,4255,516,10504,1891,1579,-8591,-4692,977,2513,2828,746,2449,3063,-1,-9796,2886,588, + 10087,3322,-1,-533,-2334,480,945,1857,1983,-8748,-3032,882,5838,-2173,706,977,-57,1642, + 4112,3285,792,5232,3314,722,6041,3438,738,5336,-3414,1471,3757,3295,-1,6983,3661,-1, + 5793,3577,-1,4943,3457,-1,8995,3859,-1,-1309,-2568,-2,-462,-2431,-2,-1828,2753,-1, + -11840,4563,-5,4382,-20,-1988,5295,1035,-2300,8568,902,-1008,-4017,-2023,-1322,-3432,-3958,-591, + 429,2741,-687,-728,2650,-783,-980,-1334,-1945,-946,-1185,-1911,-844,-2011,-1325,9373,2264,-1833, + -2293,-6187,-356,5414,2487,-2168,5726,1827,-2282,-13447,4059,-551,-14633,3614,-465,1225,-1887,-482, + -10064,2203,-371,-12311,2741,-392,-652,513,-2154,-862,837,-2078,-12034,3598,-782,-13373,2839,-448, + -13405,3532,-748,-14570,2936,-408,1314,-1560,-1322,4532,2100,-2204,-3329,-2836,-1551,-1591,-809,-2355, + 7635,1500,-1001,3492,1660,-2097,-14599,3323,-574,-3431,-3960,-1342,10203,3296,-777,-1583,2596,-843, + -385,1086,-2019,8966,1555,-1751,8258,-2811,-972,-250,-822,-1909,-168,-1833,-1324,-6782,2018,-402, + -9592,3424,-380,-3821,2342,-1080,-2540,2669,-950,-1393,875,-2385,-6946,1653,-551,3368,-508,-1643, + -2673,-3258,-1358,7138,-2109,-1026,7372,-1897,-601,2575,1392,-1984,-11842,4255,-526,10504,1891,-1581, + -4340,-5018,-970,2448,2730,-747,-9794,2887,-595,-403,-2322,-484,688,1463,-1984,-4979,-3496,-877, + 8456,-2821,-701,1058,-531,-1643,3688,3017,-792,4749,3173,-722,5629,3407,-739,7802,-3987,-1465, + -8163,-2170,592,-6486,-2859,252,-7713,-3141,416,7348,3551,947,8978,3349,1280,8971,3750,682, + -5780,1667,660,-6308,1285,748,-5827,826,1817,6120,-492,288,5593,-232,254,6449,3,213, + 4408,-284,660,3078,-788,1319,3149,-1187,647,-4286,-2448,-1,-6848,-1220,451,-5642,-448,-1, + -2537,-2874,-146,-2967,-2950,261,-9789,-4496,677,-8897,-3720,523,-9236,-4777,566,-9789,-4496,1277, + -8894,-3717,1448,-9208,-3098,1335,-9123,-5508,710,-8804,-4905,670,-9254,-4777,1445,-9123,-5508,1249, + -8813,-4901,1286,-9399,-5720,620,-8645,-5885,645,7290,474,-1,7070,-236,575,6993,261,-2, + 5006,-944,543,4773,-1036,1013,5793,-270,1759,4893,82,1756,4935,-906,1523,6399,-408,575, + 6319,-875,434,7650,-1427,624,8258,-2219,636,8078,-2547,543,7452,499,2106,6670,-287,1522, + 7718,703,1452,7181,2627,1815,8022,2064,1335,8143,2621,1369,-6291,80,1803,-6543,-99,567, + -7027,-477,1547,-7431,-1222,1446,-6154,-2443,2266,-6222,-147,-1,7986,-3083,587,8260,-2220,1293, + 8087,-2516,1440,8021,-3067,1440,7698,-1455,1344,7685,-1096,1201,6318,-876,1518,6371,-498,1287, + 9499,3977,1119,10161,4129,1435,10199,3291,2003,10600,4188,1895,10324,3526,1027,10533,4045,1485, + 9391,556,999,9680,282,854,9977,522,1018,-8659,-5923,1370,-8503,-5922,993,6596,-35,11, + 4457,-1866,13,5717,-380,10,-6798,808,-1,-7211,1087,426,-7249,1076,-1,-5436,-3592,448, + -1711,-2585,481,-3326,-2873,887,-8327,-2043,763,9910,31,415,9591,137,-1,-5995,1689,410, + -7262,-3780,570,-9314,-2962,645,-9395,-5735,1396,-9705,-6067,1292,-8900,-6329,1529,-4371,-2898,1778, + -5866,-3531,1433,7639,-1076,751,6255,-1447,1482,7604,-1710,1278,7660,1588,1941,10545,216,-1, + 10932,592,-1,10525,225,447,-6910,-2774,1866,-6556,712,571,6493,332,2006,9829,2516,1878, + 10621,817,784,7733,-2486,1273,7680,-2411,982,7748,-2520,698,7567,-3045,542,7437,-3269,1053, + 7862,-3280,1055,-8596,-6374,969,-9883,-5964,969,-9706,-6066,667,9896,-33,-1,9848,1047,1562, + 10448,1475,898,10335,1299,1460,10549,1501,426,10687,2208,731,11074,927,-84,11071,919,165, + -4410,-1985,-596,-3779,-2850,-420,-3577,-2555,-255,7004,3491,-949,8971,3750,-684,8978,3350,-1282, + -5688,1685,-661,-4836,998,-1818,-6307,1286,-749,7430,-448,-290,6819,-39,-215,6444,-711,-256, + 5878,-1455,-662,4358,-1809,-648,4197,-1537,-1320,-4442,-1029,-455,-5081,-4872,-680,-4461,-4868,-567, + -4647,-3770,-527,-5079,-4873,-1281,-5185,-3344,-1341,-4647,-3768,-1452,-4005,-5469,-709,-4018,-4784,-670, + -4462,-4871,-1446,-4016,-4785,-1287,-3998,-5473,-1248,-4145,-5805,-618,-3377,-5767,-641,7448,369,-578, + 6905,-1916,-1015,7123,-1660,-545,7152,-563,-1761,7030,-1675,-1525,6056,-1132,-1757,8406,-1583,-436, + 8652,-1036,-577,9478,-2497,-626,9710,-3642,-545,9934,-3392,-637,6815,1081,-2108,6861,1551,-1454, + 7417,166,-1524,6401,2659,-1818,8077,2644,-1370,7601,2355,-1337,-4019,378,-1805,-4269,-232,-1550, + -6039,-44,-569,-4037,-958,-1450,-2426,-1915,-2268,9566,-4208,-589,9927,-3403,-1295,9586,-4210,-1442, + 9686,-3677,-1442,9529,-2542,-1346,9615,-2191,-1203,8412,-1568,-1520,8586,-1165,-1289,9499,3977,-1120, + 10161,4129,-1437,10600,4188,-1897,10199,3291,-2005,10324,3526,-1029,10532,4045,-1487,9391,556,-1001, + 9977,522,-1020,9680,282,-856,-3230,-5745,-989,-3375,-5765,-1366,-7211,1088,-428,-2873,-3230,-450, + -1973,-2642,-888,-1340,-2514,-482,-4602,-1896,-768,9910,31,-417,-5994,1689,-412,-3171,-3342,-573, + -5342,-3255,-652,-4144,-5805,-1394,-3480,-6231,-1524,-4318,-6234,-1291,-2325,-3027,-1434,-1583,-2396,-1780, + 9568,-2154,-753,8164,-2126,-1484,9369,-2771,-1279,6241,1774,-1944,10525,225,-449,-3051,-2354,-1869, + -6462,725,-573,6735,154,-2008,9829,2516,-1880,10621,817,-786,9318,-3578,-1275,9330,-3614,-700, + 9273,-3485,-984,9003,-4365,-1054,9147,-4153,-543,9416,-4403,-1057,-3192,-6204,-964,-4319,-6233,-665, + -4523,-6204,-968,9848,1048,-1564,10448,1475,-900,10335,1299,-1462,10687,2208,-733,10549,1501,-428, + 4080,607,1985,6459,1238,2295,8578,861,1006,8534,761,-1,9212,412,-1,-8163,-2171,1329, + -8517,-4218,595,437,2731,686,519,2985,-1,-853,2883,-1,-846,2592,781,-1897,-2268,1320, + -2692,-1627,1905,-3850,-2016,1939,9384,2226,1831,-8888,-6319,368,6982,2570,2165,7498,1466,2278, + -13461,4456,-3,-14644,3960,-4,-14604,3815,455,-13396,4194,542,1126,-1693,480,1219,-1971,-1, + -10106,2157,365,-12324,2815,384,-12354,2593,-3,-10147,1948,-2,-1951,462,2074,-3180,-292,2149, + -12010,3658,775,-13383,3666,739,-13388,2972,439,-14578,3136,398,-14552,3043,-4,-13351,2814,-3, + 1112,-1339,1320,5947,2485,2200,-7712,-3146,1553,-5550,-1560,2354,7920,1255,999,7772,1210,-1, + 4155,2206,2094,-14891,3488,-4,-14586,3523,564,-8515,-4216,1347,10213,3259,775,-5708,1956,-1, + -5908,1853,-1,-832,916,2016,-1908,2458,841,8977,1516,1749,7438,-1833,968,-736,-1982,1320, + -867,-1005,1905,-6740,2211,-1,-9543,3655,-2,-9597,3365,375,-6788,2004,400,-4353,2176,1079, + -4473,68,2385,-3000,2491,948,-6946,1637,549,2832,142,1640,-7262,-3780,1356,6255,-1448,596, + 6118,-1746,1021,-4326,2498,-1,-2880,2730,-1,10694,2227,-1,10577,1527,-1,3062,2000,1981, + -11784,4304,518,10515,1854,1579,-9939,-4345,977,2525,2803,746,2457,3047,-1,-9811,2831,590, + 10097,3285,-1,-677,-2425,481,989,1743,1982,-9438,-2854,882,7591,-1681,696,1035,-198,1641, + 4153,3249,790,5281,3305,720,6089,3440,737,7575,-3032,1461,3770,3268,-1,7004,3627,-1, + 5813,3546,-1,4968,3421,-1,9005,3820,-1,-1527,-2682,-1,-596,-2511,-1,-1834,2716,-1, + -11766,4611,-3,4743,-395,-1986,5619,643,-2298,8578,862,-1008,-4409,-1985,-1333,-4099,-4045,-597, + 437,2731,-687,-729,2632,-783,-1249,-1456,-1939,-1117,-1270,-1906,-991,-2081,-1321,9384,2226,-1833, + -3486,-6233,-364,5502,2291,-2167,5924,1518,-2280,-13396,4195,-548,-14603,3815,-462,1258,-1924,-482, + -10106,2157,-370,-12331,2816,-390,-844,365,-2149,-949,760,-2075,-12009,3658,-780,-13388,2973,-446, + -13382,3666,-745,-14578,3136,-405,1352,-1613,-1321,4684,1823,-2203,-3777,-2850,-1556,-1926,-920,-2356, + 7653,1428,-1001,3599,1492,-2096,-14586,3523,-571,-4096,-4046,-1349,10212,3259,-777,-1600,2567,-842, + -420,1045,-2017,8977,1516,-1751,9188,-2887,-970,-314,-864,-1906,-234,-1869,-1321,-6787,2005,-402, + -9594,3364,-379,-3850,2320,-1080,-2565,2637,-949,-1600,746,-2386,-6945,1638,-551,3633,-763,-1641, + -3170,-3341,-1359,7957,-2398,-1023,8147,-2141,-598,2662,1270,-1984,-11783,4304,-524,10515,1854,-1581, + -5282,-4806,-981,2467,2695,-747,-9810,2831,-594,-524,-2393,-482,700,1444,-1984,-5498,-3209,-889, + 9391,-2835,-698,1087,-583,-1642,3724,2945,-792,4788,3085,-722,5661,3333,-739,9136,-4140,-1462, + -9425,-2187,592,-7910,-2997,253,-9171,-3216,416,7278,3467,947,9025,3138,1280,9022,3537,682, + -5827,1679,660,-6308,1283,748,-6053,707,1817,7298,-911,286,6555,-852,254,7288,-354,214, + 5385,-1150,659,3420,-1210,1318,3436,-1564,647,-5653,-2529,0,-7755,-1284,452,-5934,-432,0, + -3453,-2961,-145,-3856,-3079,261,-11668,-3838,677,-10505,-3615,523,-11331,-4359,566,-11668,-3838,1277, + -10492,-3608,1448,-10541,-2929,1335,-11598,-5054,710,-11022,-4687,670,-11344,-4350,1445,-11598,-5054,1249, + -11027,-4680,1286,-11946,-5102,620,-11414,-5662,645,7425,450,0,8070,-283,574,7579,145,1, + 6533,-1754,540,6370,-1934,1011,6953,-820,1757,5821,-737,1754,6455,-1751,1521,7511,-650,573, + 7661,-1095,432,9099,-959,622,9975,-1427,633,9945,-1801,541,8132,578,2104,7760,-468,1520, + 7981,916,1452,7115,2583,1815,8035,1924,1335,8177,2430,1369,-6879,-124,1803,-6637,-125,567, + -7785,-603,1547,-8480,-1324,1446,-7488,-2768,2266,-6310,-120,0,10088,-2328,584,9977,-1427,1291, + 9940,-1768,1438,10113,-2300,1437,9155,-962,1342,8975,-652,1198,7661,-1096,1516,7530,-740,1285, + 9549,3763,1119,10212,3911,1435,10244,3072,2003,10651,3966,1895,10370,3306,1027,10583,3824,1485, + 9413,338,999,9701,63,854,10000,301,1018,-11442,-5683,1370,-11317,-5772,993,7329,-368,14, + 4895,-2184,17,6597,-936,11,-6786,803,0,-7205,1080,427,-7245,1071,0,-6859,-3784,448, + -2244,-2695,481,-4189,-3051,887,-9557,-2037,763,9929,-189,415,9610,-82,-1,-6016,1696,411, + -8851,-3934,570,-10590,-2769,645,-11950,-5115,1396,-12393,-5229,1292,-11869,-5888,1529,-5477,-3227,1778, + -7258,-3787,1433,8925,-655,749,7874,-1630,1480,9186,-1223,1275,7708,1754,1940,10567,-7,-1, + 10957,368,-1,10547,1,447,-8312,-2975,1866,-6549,695,572,7344,23,2004,9868,2299,1878, + 10648,595,784,9602,-1892,1270,9522,-1843,980,9631,-1916,696,9691,-2470,539,9668,-2727,1050, + 10058,-2558,1053,-11646,-6094,969,-12489,-5047,969,-12393,-5228,667,9915,-254,-1,9876,829,1562, + 10480,1255,898,10366,1079,1460,10581,1280,426,10724,1985,731,11102,702,-84,11099,694,165, + -6192,-1699,-590,-5669,-2635,-414,-5320,-2444,-251,7074,3306,-949,9022,3538,-684,9024,3138,-1282, + -5756,1714,-661,-5267,1038,-1818,-6308,1284,-748,7931,-657,-281,7397,-330,-209,7022,-1069,-249, + 6441,-1815,-655,4744,-2088,-643,4505,-1773,-1316,-5728,-855,-451,-7309,-4483,-675,-6708,-4636,-565, + -6611,-3524,-520,-7311,-4482,-1275,-7061,-3001,-1331,-6616,-3515,-1446,-6445,-5321,-711,-6258,-4666,-671, + -6716,-4636,-1444,-6259,-4665,-1287,-6438,-5326,-1250,-6661,-5594,-621,-5899,-5571,-648,7864,181,-569, + 7588,-2203,-1005,7758,-1913,-535,7617,-813,-1751,7668,-1943,-1515,6552,-1466,-1749,9012,-1578,-426, + 9140,-997,-567,10253,-2240,-616,10789,-3281,-535,10936,-2980,-628,7040,787,-2099,6931,1331,-1449, + 7781,-53,-1514,6431,2409,-1816,8115,2433,-1370,7607,2111,-1336,-5033,484,-1804,-5518,-46,-1547, + -6238,-2,-568,-5620,-732,-1445,-4202,-1852,-2265,10824,-3860,-579,10932,-2992,-1286,10843,-3855,-1432, + 10777,-3319,-1433,10312,-2273,-1337,10322,-1911,-1193,9013,-1580,-1511,9101,-1134,-1279,9549,3763,-1120, + 10212,3911,-1437,10651,3966,-1897,10244,3072,-2005,10370,3306,-1029,10583,3824,-1487,9413,338,-1001, + 10000,302,-1020,9701,63,-856,-5749,-5538,-996,-5899,-5563,-1373,-7206,1081,-427,-4521,-3200,-447, + -2946,-2688,-887,-1898,-2587,-482,-6370,-1590,-761,9929,-189,-417,-6016,1695,-411,-5113,-3187,-568, + -7199,-2887,-642,-6664,-5590,-1397,-6019,-6027,-1532,-6877,-5995,-1294,-4041,-2966,-1432,-2938,-2430,-1779, + 10268,-1886,-744,8874,-2179,-1475,10205,-2539,-1270,6272,1499,-1941,10547,1,-449,-4899,-2214,-1865, + -6475,722,-572,7109,-157,-1998,9868,2299,-1880,10648,595,-786,10390,-3333,-1265,10413,-3364,-690, + 10318,-3257,-975,10337,-4181,-1045,10408,-3936,-534,10742,-4090,-1047,-5718,-5998,-973,-6873,-5997,-668, + -7071,-5940,-969,9877,829,-1564,10480,1255,-900,10366,1079,-1462,10724,1985,-733,10581,1280,-428, + 4249,237,1985,6807,956,2294,8601,649,1006,8554,548,-1,9232,195,-1,-9425,-2187,1329, + -10404,-4223,595,478,2693,686,565,2945,-1,-812,2873,-1,-850,2543,781,-2395,-2422,1320, + -3320,-1893,1905,-4833,-2426,1939,9422,2011,1831,-11855,-5886,368,6827,2547,2164,7677,1558,2276, + -13421,4529,4,-14617,4068,4,-14580,3922,463,-13363,4267,549,1163,-1874,480,1232,-2085,-1, + -10118,2148,369,-12325,2857,390,-12360,2636,3,-10163,1940,2,-2300,212,2074,-3953,-836,2149, + -11991,3693,780,-13364,3738,746,-13388,3044,446,-14574,3242,406,-14551,3148,4,-13356,2885,4, + 1123,-1546,1320,5831,2274,2200,-9173,-3235,1553,-6759,-2022,2354,7938,1144,999,7790,1004,-1, + 4114,1883,2094,-14877,3602,5,-14571,3629,572,-10396,-4220,1347,10257,3040,775,-5754,1988,0, + -5938,1863,0,-976,790,2016,-1979,2370,841,9008,1303,1749,9093,-1400,965,-993,-2083,1320, + -1117,-1132,1905,-6740,2207,0,-9534,3638,2,-9590,3348,379,-6787,1999,401,-4484,2087,1079, + -5302,-494,2385,-3114,2386,948,-6944,1631,550,2925,-143,1640,-8850,-3933,1356,7874,-1630,594, + 7892,-1957,1019,-4393,2536,-1,-2983,2688,-1,10731,2004,-1,10609,1306,-1,3047,1608,1981, + -11751,4334,524,10550,1633,1579,-11721,-3632,977,2550,2696,746,2502,2970,-1,-9811,2816,593, + 10142,3067,-1,-1015,-2513,481,987,1435,1982,-10672,-2630,882,9163,-1203,694,1067,-511,1640, + 4089,3054,790,5222,3162,720,6055,3316,737,9693,-2455,1458,3825,3156,-1,7080,3442,-1, + 5885,3392,-1,5005,3275,-1,9057,3608,-1,-2071,-2769,0,-940,-2589,-1,-1867,2690,-1, + -11726,4640,3,5080,-760,-1980,5860,273,-2290,8601,649,-1008,-6193,-1698,-1327,-6150,-3918,-595, + 478,2693,-687,-747,2606,-783,-2492,-1536,-1939,-1886,-1338,-1906,-1553,-2137,-1321,9421,2012,-1833, + -6021,-6035,-371,5555,2004,-2164,6020,1193,-2275,-13364,4267,-541,-14581,3922,-454,1255,-1991,-482, + -10119,2148,-366,-12332,2858,-384,-1913,218,-2151,-1407,677,-2075,-11992,3693,-774,-13389,3045,-439, + -13364,3738,-739,-14574,3241,-397,1336,-1695,-1321,4793,1479,-2199,-5668,-2636,-1551,-3616,-919,-2354, + 7661,1209,-1000,3707,1255,-2094,-14572,3629,-563,-6150,-3916,-1346,10257,3040,-777,-1709,2541,-842, + -603,987,-2017,9008,1303,-1751,10053,-2699,-960,-603,-920,-1907,-520,-1915,-1321,-6787,2000,-400, + -9589,3347,-375,-4075,2331,-1080,-2746,2622,-950,-2894,667,-2387,-6944,1632,-550,3898,-1032,-1637, + -5113,-3185,-1355,8744,-2467,-1013,8877,-2176,-588,2759,1090,-1982,-11751,4334,-518,10549,1633,-1581, + -7489,-4367,-974,2518,2597,-747,-9812,2817,-590,-871,-2465,-482,723,1394,-1984,-7347,-2819,-878, + 10240,-2591,-688,1072,-667,-1642,3789,2789,-791,4850,2895,-721,5725,3140,-738,10394,-3927,-1453, + -9790,-1426,592,-8455,-2426,253,-9627,-2464,416,7041,3274,946,9087,2804,1280,9092,3202,682, + -5826,1768,660,-6306,1367,748,-6104,920,1817,7578,-431,286,6767,-745,253,7357,-88,213, + 5653,-1234,658,3484,-1364,1318,3551,-1762,647,-6481,-2447,0,-8055,-761,452,-6100,-349,0, + -4112,-3000,-145,-4389,-2986,261,-12208,-2638,677,-11029,-2761,523,-12036,-3234,566,-12208,-2638,1277, + -11010,-2753,1448,-10914,-2098,1335,-12527,-3921,710,-11835,-3638,670,-12033,-3249,1445,-12527,-3921,1249, + -11836,-3647,1286,-12951,-3870,620,-13331,-4558,645,7225,486,0,7975,399,573,7488,406,0, + 7017,-1539,540,6845,-1788,1010,7169,-427,1756,5988,-768,1753,6927,-1553,1520,8180,-408,572, + 8233,-927,431,9649,-1062,622,10580,-1443,634,10592,-1809,542,7600,1282,2103,7737,194,1519, + 7428,1382,1451,6777,2478,1815,7972,1678,1335,8205,2119,1369,-7024,317,1803,-6667,34,567, + -7976,-39,1547,-8763,-643,1446,-7903,-2144,2266,-6378,-87,0,10817,-2299,585,10584,-1444,1292, + 10586,-1783,1439,10837,-2267,1438,9704,-1073,1342,9582,-732,1199,8234,-927,1516,8177,-522,1284, + 9616,3426,1119,10280,3566,1435,10304,2728,2003,10720,3618,1895,10432,2960,1027,10650,3476,1485, + 9439,-5,999,9725,-283,854,10028,-46,1018,-13295,-4562,1370,-13403,-4685,993,7453,-246,14, + 5150,-2412,17,6831,-891,11,-6794,898,0,-7208,1190,427,-7248,1182,0,-7461,-3326,448, + -2584,-2660,481,-4594,-2836,887,-9908,-1270,763,9950,-537,415,9631,-427,-1,-6009,1775,411, + -9374,-3208,570,-10931,-1937,645,-12939,-3864,1396,-13296,-3603,1292,-13645,-4254,1529,-5858,-2805,1778, + -7720,-3195,1433,9533,-729,749,8329,-1504,1480,9705,-1372,1275,6955,2084,1939,10592,-358,-1, + 10988,16,-1,10573,-350,447,-8745,-2281,1866,-6558,797,572,7133,438,2003,9920,1959,1878, + 10681,247,784,10262,-1952,1271,10174,-1918,981,10294,-1972,696,10451,-2507,540,10472,-2764,1051, + 10827,-2530,1054,-13814,-4475,969,-13243,-3421,969,-13296,-3604,667,9935,-601,-1,9911,484,1562, + 10521,908,898,10405,733,1460,10622,932,426,10772,1636,731,11137,349,-84,11133,340,165, + -7575,-1837,-589,-7269,-2865,-414,-6691,-2679,-251,7177,3011,-949,9091,3202,-684,9087,2804,-1282, + -5771,1765,-661,-5504,880,-1818,-6307,1369,-749,8231,-837,-281,7578,-391,-209,7328,-1286,-249, + 6803,-2107,-654,4974,-2316,-643,4697,-2000,-1316,-6619,-1002,-451,-9462,-4068,-675,-8956,-4427,-567, + -8466,-3426,-521,-9464,-4066,-1276,-8710,-2778,-1331,-8466,-3417,-1446,-8929,-5190,-714,-8547,-4616,-673, + -8960,-4423,-1446,-8548,-4614,-1289,-8928,-5192,-1253,-9243,-5387,-624,-8639,-5868,-652,8060,51,-569, + 8048,-2478,-1005,8187,-2172,-535,7945,-1084,-1751,8101,-2211,-1515,6897,-1778,-1749,9400,-1692,-426, + 9459,-1103,-567,10712,-2196,-616,11443,-3107,-535,11527,-2783,-627,7223,475,-2099,6934,1087,-1449, + 8047,-313,-1514,6489,2089,-1816,8169,2103,-1370,7640,1769,-1335,-5654,166,-1803,-6331,-295,-1547, + -6366,-6,-568,-6745,-985,-1445,-5649,-2386,-2265,11640,-3648,-579,11525,-2796,-1285,11656,-3638,-1432, + 11438,-3145,-1432,10774,-2221,-1336,10740,-1860,-1193,9401,-1705,-1510,9436,-1243,-1279,9616,3426,-1120, + 10280,3566,-1437,10720,3618,-1897,10304,2728,-2005,10432,2960,-1029,10650,3476,-1487,9439,-5,-1001, + 10028,-46,-1020,9725,-283,-856,-8519,-5950,-1001,-8639,-5863,-1377,-7209,1191,-427,-5902,-3499,-447, + -3768,-2904,-888,-2356,-2675,-482,-7722,-1691,-761,9950,-537,-417,-6009,1775,-412,-6876,-3543,-568, + -8803,-2629,-642,-9244,-5383,-1400,-9025,-6143,-1536,-9651,-5605,-1296,-5571,-3430,-1432,-4149,-2927,-1779, + 10684,-1842,-743,9328,-2317,-1474,10704,-2507,-1269,6317,1172,-1940,10573,-350,-449,-6425,-2607,-1865, + -6501,791,-572,7386,-472,-1998,9919,1959,-1880,10681,247,-786,11052,-3263,-1265,11083,-3287,-690, + 10959,-3203,-974,11268,-4094,-1044,11261,-3843,-533,11627,-3885,-1047,-8795,-6319,-978,-9650,-5608,-671, + -9792,-5457,-972,9911,485,-1564,10521,908,-900,10404,733,-1462,10772,1636,-733,10622,932,-428, + 4149,38,1984,6258,1115,2292,8626,313,1006,8576,211,-1,9255,-147,-1,-9790,-1427,1329, + -11095,-3364,595,539,2642,686,634,2892,-1,-750,2867,-1,-808,2545,781,-2635,-2307,1320, + -3551,-1681,1905,-5165,-2061,1939,9470,1673,1831,-13657,-4245,368,6039,2584,2162,6838,1981,2275, + -13290,4859,3,-14518,4527,3,-14497,4378,462,-13258,4591,548,1150,-2005,480,1194,-2185,-1, + -10083,2331,369,-12290,3061,390,-12328,2840,2,-10131,2124,1,-2406,326,2074,-4191,-552,2149, + -11938,3894,780,-13309,4064,745,-13400,3374,445,-14559,3701,405,-14546,3606,3,-13383,3210,3, + 1106,-1680,1319,5218,2143,2198,-9635,-2491,1553,-7122,-1452,2354,7828,945,999,7807,677,-1, + 3852,1608,2093,-14824,4091,4,-14517,4086,571,-11082,-3361,1347,10317,2695,775,-5747,2063,-1, + -5929,1941,0,-1014,813,2016,-1965,2420,841,9047,966,1749,9598,-1574,966,-1136,-2042,1320, + -1233,-1086,1905,-6713,2304,0,-9476,3812,1,-9538,3523,378,-6766,2097,401,-4509,2196,1079, + -5532,-86,2385,-3131,2472,948,-6932,1733,550,2905,-346,1640,-9373,-3207,1356,8330,-1504,593, + 8299,-1832,1019,-4417,2544,-1,-3021,2684,-1,10780,1655,-1,10650,958,-1,2887,1277,1980, + -11687,4532,523,10594,1286,1579,-12199,-2426,977,2579,2557,746,2570,2856,-1,-9767,2996,593, + 10201,2724,-1,-1268,-2532,481,957,1163,1981,-10987,-1791,882,9680,-1349,694,1072,-723,1640, + 3926,2823,789,5043,2964,720,5919,3114,737,10450,-2492,1459,3906,2997,-1,7194,3148,-1, + 5995,3156,-1,5044,3070,-1,9127,3272,-1,-2473,-2791,0,-1221,-2633,-1,-1854,2688,-1, + -11654,4838,2,5321,-1088,-1979,6050,-77,-2290,8626,313,-1008,-7576,-1836,-1327,-8166,-3963,-596, + 539,2642,-687,-734,2548,-783,-3556,-2119,-1940,-2578,-1709,-1907,-2079,-2335,-1321,9470,1674,-1833, + -9026,-6149,-375,5627,1688,-2164,6112,855,-2274,-13258,4591,-542,-14497,4378,-455,1204,-2070,-482, + -10083,2331,-366,-12295,3062,-385,-2720,-509,-2151,-1776,348,-2076,-11939,3893,-775,-13400,3375,-439, + -13309,4064,-739,-14560,3701,-398,1274,-1801,-1321,4903,1150,-2198,-7269,-2867,-1551,-4903,-1607,-2354, + 7677,879,-1000,3803,1017,-2094,-14518,4086,-564,-8166,-3960,-1347,10316,2695,-777,-1767,2438,-843, + -755,824,-2017,9047,966,-1751,10573,-2693,-960,-890,-1090,-1907,-826,-2053,-1321,-6765,2098,-401, + -9536,3522,-376,-4192,2208,-1080,-2853,2494,-950,-3748,-105,-2387,-6932,1735,-550,4074,-1279,-1637, + -6876,-3540,-1355,9243,-2607,-1013,9339,-2301,-588,2841,907,-1982,-11687,4532,-519,10594,1286,-1581, + -9589,-3896,-975,2585,2469,-747,-9768,2996,-590,-1174,-2537,-482,750,1331,-1984,-8919,-2515,-877, + 10742,-2547,-688,1027,-776,-1642,3874,2600,-791,4939,2664,-721,5826,2881,-738,11245,-3838,-1452, + -9817,-938,592,-8635,-2032,253,-9671,-1977,416,6898,2905,947,9131,2485,1280,9141,2881,682, + -5831,1856,660,-6335,1474,748,-6113,1092,1817,7868,-430,285,7059,-993,251,7488,-273,210, + 6062,-1553,658,3612,-1575,1318,3692,-1994,646,-7071,-2331,0,-8165,-410,452,-6312,-276,-1, + -4562,-2965,-146,-4698,-2878,261,-12255,-2054,677,-11087,-2252,523,-12122,-2659,566,-12255,-2054,1277, + -11066,-2245,1448,-10941,-1600,1335,-12732,-3339,710,-11947,-3075,670,-12130,-2695,1445,-12733,-3339,1249, + -11959,-3093,1286,-13191,-3216,620,-13960,-3436,645,7076,286,-2,7910,442,572,7458,227,-4, + 7527,-1640,539,7359,-1927,1010,7459,-508,1756,6334,-1123,1753,7435,-1668,1519,8827,-592,572, + 8778,-1161,431,10120,-1604,621,11041,-2003,632,11049,-2360,540,7227,1198,2103,7759,233,1519, + 6978,1271,1451,6549,2137,1815,7919,1367,1335,8225,1801,1369,-7033,647,1803,-6694,159,567, + -7988,350,1547,-8776,-188,1446,-7941,-1687,2266,-6499,-45,-1,11288,-2838,583,11046,-2006,1290, + 11045,-2339,1437,11307,-2805,1436,10172,-1626,1341,10122,-1264,1197,8779,-1162,1515,8804,-724,1284, + 9660,3107,1119,10325,3243,1435,10344,2404,2003,10766,3292,1895,10474,2636,1027,10695,3150,1485, + 9458,-332,999,9742,-611,854,10049,-374,1018,-13888,-3480,1370,-14102,-3474,993,7605,-495,8, + 5265,-2703,11,7105,-1158,8,-6832,1009,-1,-7242,1311,426,-7282,1303,-1,-7677,-2990,448, + -2780,-2611,481,-4765,-2657,887,-9931,-781,763,9966,-866,415,9646,-755,-1,-6021,1875,411, + -9432,-2724,570,-10951,-1440,645,-13158,-3214,1396,-13303,-2875,1292,-13930,-3039,1529,-5914,-2480,1778, + -7778,-2776,1433,10076,-1252,748,8741,-1763,1479,10124,-1941,1274,6406,1803,1940,10613,-688,-1, + 11013,-311,-1,10593,-679,447,-8784,-1805,1866,-6604,915,572,7091,238,2003,9956,1638,1878, + 10707,-79,784,10719,-2509,1269,10630,-2477,979,10752,-2528,695,10928,-3058,538,10958,-3314,1049, + 11306,-3068,1052,-14242,-3035,969,-13207,-2763,969,-13305,-2878,667,9950,-930,-1,9937,159,1562, + 10551,583,898,10433,409,1460,10652,607,426,10806,1310,731,11163,21,-84,11160,12,165, + -8891,-1962,-591,-8658,-3006,-416,-7865,-2777,-253,7269,2723,-949,9140,2881,-684,9130,2485,-1282, + -5809,1832,-661,-5883,830,-1818,-6334,1476,-749,8443,-1243,-292,7747,-723,-217,7532,-1670,-258, + 6965,-2489,-665,5054,-2594,-649,4741,-2251,-1321,-7580,-1086,-452,-11131,-3582,-678,-10742,-4066,-569, + -9995,-3242,-523,-11131,-3579,-1278,-10057,-2547,-1333,-9991,-3238,-1448,-10928,-4884,-717,-10401,-4360,-676, + -10740,-4065,-1449,-10401,-4357,-1292,-10927,-4874,-1257,-11329,-4994,-627,-11355,-5777,-657,8262,-322,-580, + 8218,-2916,-1017,8364,-2614,-547,8152,-1517,-1763,8277,-2650,-1527,7057,-2162,-1760,9588,-2153,-438, + 9655,-1566,-579,10892,-2674,-628,11728,-3471,-547,11768,-3142,-640,7467,73,-2111,7060,751,-1456, + 8279,-749,-1526,6585,1806,-1818,8214,1786,-1370,7717,1448,-1337,-6497,24,-1804,-7337,-434,-1547, + -6580,15,-568,-7956,-1139,-1446,-6988,-2606,-2266,12073,-3919,-591,11766,-3156,-1298,12086,-3904,-1444, + 11728,-3505,-1445,10953,-2698,-1349,10923,-2335,-1205,9588,-2170,-1523,9631,-1705,-1291,9660,3107,-1120, + 10325,3243,-1437,10766,3292,-1897,10344,2404,-2005,10474,2636,-1029,10695,3150,-1487,9458,-332,-1001, + 10049,-374,-1020,9742,-611,-856,-11386,-5928,-1005,-11360,-5775,-1381,-7242,1312,-428,-7008,-3615,-448, + -4400,-2961,-888,-2679,-2690,-482,-9021,-1804,-763,9966,-866,-417,-6021,1875,-412,-8378,-3731,-570, + -10110,-2388,-644,-11331,-4992,-1403,-11806,-5640,-1539,-11763,-4899,-1299,-6851,-3635,-1433,-5166,-3106,-1779, + 10869,-2318,-756,9503,-2781,-1487,10885,-3000,-1282,6431,865,-1945,10593,-679,-449,-7794,-2792,-1866, + -6582,888,-573,7615,-886,-2010,9955,1638,-1880,10707,-79,-786,11359,-3728,-1277,11398,-3745,-702, + 11246,-3688,-987,11860,-4451,-1057,11770,-4226,-546,12132,-4137,-1059,-11847,-5917,-981,-11763,-4901,-674, + -11790,-4710,-975,9937,159,-1564,10551,583,-900,10433,409,-1462,10806,1310,-733,10652,607,-428, + 4185,-301,1984,6004,680,2293,8642,-11,1006,8588,-115,-1,9269,-475,-1,-9816,-939,1329, + -11188,-2847,595,589,2581,686,690,2827,-1,-699,2843,-1,-763,2533,781,-2731,-2213,1320, + -3595,-1504,1905,-5208,-1756,1939,9505,1352,1831,-13946,-3009,368,5548,2100,2163,6275,1661,2275, + -13349,4908,-2,-14570,4566,-3,-14548,4417,456,-13314,4640,543,1166,-2130,480,1160,-2286,-1, + -10073,2477,366,-12304,3115,385,-12333,2892,-2,-10116,2269,-2,-2406,437,2074,-4205,-268,2149, + -11983,3960,775,-13358,4112,740,-13440,3421,440,-14603,3739,399,-14589,3644,-2,-13421,3256,-2, + 1134,-1811,1319,4901,1615,2199,-9681,-2005,1553,-7149,-1001,2354,7728,653,999,7809,360,-1, + 3784,1168,2094,-14872,4126,-3,-14566,4124,565,-11174,-2845,1347,10357,2372,775,-5739,2139,-1, + -5934,2037,-1,-1007,831,2016,-1929,2460,841,9075,643,1749,9990,-2145,964,-1202,-2020,1320, + -1273,-1055,1905,-6731,2418,-1,-9501,3971,-1,-9560,3683,376,-6787,2212,400,-4498,2281,1079, + -5539,286,2385,-3113,2544,948,-6958,1851,549,2958,-590,1640,-9431,-2723,1356,8741,-1763,593, + 8653,-2075,1018,-4456,2549,-1,-3064,2704,-1,10814,1329,-1,10681,632,-1,2907,844,1981, + -11756,4608,519,10626,961,1579,-12232,-1842,977,2617,2405,746,2622,2742,-1,-9777,3150,590, + 10242,2401,-1,-1427,-2536,481,1036,869,1982,-11000,-1293,882,10100,-1914,693,1160,-935,1640, + 3877,2492,790,4964,2648,720,5862,2802,737,10927,-3043,1457,3970,2851,-1,7299,2860,-1, + 6098,2934,-1,5095,2862,-1,9177,2951,-1,-2734,-2774,-1,-1408,-2650,-1,-1846,2691,-1, + -11733,4916,-2,5445,-1386,-1988,6233,-412,-2299,8642,-11,-1008,-8893,-1960,-1329,-9840,-3852,-598, + 589,2581,-687,-733,2508,-783,-4513,-2322,-1940,-3173,-1838,-1907,-2486,-2405,-1321,9505,1352,-1833, + -11800,-5646,-378,5740,1430,-2168,6278,536,-2282,-13313,4640,-547,-14548,4417,-461,1139,-2155,-482, + -10073,2478,-369,-12308,3116,-389,-3588,-752,-2150,-2140,235,-2075,-11983,3960,-779,-13440,3421,-445, + -13357,4112,-744,-14603,3739,-404,1196,-1898,-1321,5031,909,-2204,-8659,-3008,-1552,-6221,-1873,-2355, + 7709,558,-1001,3868,835,-2097,-14565,4124,-570,-9838,-3848,-1350,10357,2372,-777,-1847,2401,-842, + -897,750,-2017,9075,644,-1751,10757,-3201,-972,-1124,-1166,-1907,-1064,-2121,-1321,-6786,2213,-401, + -9555,3681,-378,-4373,2167,-1080,-3000,2456,-949,-4820,-384,-2387,-6958,1852,-551,4127,-1503,-1642, + -8377,-3728,-1356,9418,-3065,-1025,9518,-2760,-600,2883,762,-1984,-11755,4608,-523,10626,961,-1581, + -11205,-3381,-977,2634,2351,-747,-9777,3151,-593,-1385,-2567,-482,767,1263,-1984,-10191,-2243,-879, + 10919,-3028,-701,970,-867,-1643,3944,2445,-792,5027,2476,-722,5933,2655,-739,11754,-4224,-1465, + -9519,-379,592,-8458,-1514,253,-9407,-1426,416,6801,2671,948,9095,2394,1280,9108,2788,682, + -5947,2281,660,-6491,1903,747,-6056,1514,1817,7842,-918,291,7033,-1530,256,7316,-784,214, + 6204,-2092,663,3633,-1761,1320,3670,-2156,647,-7164,-1845,0,-7992,87,452,-6435,165,-1, + -4718,-2643,-146,-4786,-2562,261,-12002,-1398,677,-10829,-1563,523,-11852,-1999,566,-12002,-1398,1277, + -10811,-1562,1448,-10674,-904,1335,-12441,-2696,710,-11666,-2410,670,-11859,-2035,1445,-12441,-2696,1249, + -11677,-2428,1286,-12902,-2588,620,-13660,-2845,645,6804,82,-1,7702,-55,578,7156,-190,-1, + 7690,-2167,546,7545,-2470,1016,7484,-1065,1762,6438,-1737,1759,7602,-2204,1526,8961,-1090,578, + 8910,-1674,437,10239,-2145,627,11196,-2330,639,11278,-2662,547,6931,569,2110,7637,-295,1525, + 6633,870,1454,6389,1879,1816,7836,1232,1336,8180,1697,1369,-6832,1098,1803,-6753,599,567, + -7748,835,1547,-8484,324,1446,-7677,-1202,2266,-6643,393,-1,11724,-2957,590,11204,-2332,1297, + 11273,-2648,1444,11728,-2921,1443,10290,-2167,1348,10245,-1802,1204,8911,-1674,1522,8939,-1228,1290, + 9622,3020,1119,10287,3156,1436,10306,2317,2003,10727,3205,1896,10436,2550,1027,10657,3064,1485, + 9414,-428,999,9699,-706,854,10008,-468,1018,-13587,-2886,1370,-13799,-2890,993,7360,-950,12, + 5014,-2887,12,7016,-1640,12,-6970,1412,-1,-7395,1680,426,-7432,1667,-1,-7581,-2528,448, + -2886,-2402,481,-4791,-2351,887,-9627,-215,763,9923,-961,415,9602,-851,-1,-6184,2314,410, + -9211,-2184,570,-10679,-741,645,-12870,-2584,1396,-13030,-2250,1292,-13650,-2446,1529,-5787,-2107,1778, + -7596,-2320,1433,10200,-1791,755,8862,-2281,1486,10257,-2502,1281,6060,1297,1944,10574,-779,-1, + 10977,-398,-1,10554,-770,447,-8517,-1289,1866,-6739,1341,571,6972,-413,2009,9918,1551,1878, + 10671,-165,784,11006,-2968,1276,10893,-2970,986,11049,-2973,701,11518,-3334,545,11671,-3524,1056, + 11844,-3136,1058,-13961,-2458,969,-12938,-2132,969,-13033,-2252,667,9907,-1025,-1,9898,67,1562, + 10514,497,898,10397,323,1460,10615,520,427,10769,1223,731,11127,-65,-84,11123,-74,165, + -9243,-1325,-592,-9060,-2374,-417,-8181,-2199,-253,7283,2652,-949,9108,2788,-684,9095,2394,-1282, + -5944,2259,-661,-6029,1281,-1818,-6491,1905,-749,8009,-1562,-292,7399,-1104,-216,7151,-1996,-257, + 6540,-2743,-664,4780,-2752,-649,4450,-2361,-1321,-7850,-529,-452,-11623,-2386,-678,-11341,-2939,-569, + -10441,-2289,-524,-11623,-2384,-1279,-10355,-1593,-1334,-10435,-2293,-1449,-11732,-3743,-716,-11066,-3297,-675, + -11339,-2946,-1448,-11066,-3295,-1291,-11723,-3728,-1255,-12175,-3747,-626,-12695,-4334,-654,7843,-648,-579, + 7675,-3202,-1017,7838,-2908,-547,7672,-1810,-1763,7749,-2940,-1527,6598,-2401,-1760,9083,-2538,-438, + 9195,-1954,-579,10340,-3163,-628,11288,-3735,-547,11252,-3417,-640,7045,-191,-2111,6757,535,-1456, + 7820,-1047,-1526,6497,1724,-1818,8175,1691,-1370,7633,1335,-1337,-6744,539,-1804,-7625,124,-1548, + -6741,470,-568,-8286,-543,-1447,-7368,-2036,-2267,11803,-3915,-591,11254,-3430,-1298,11805,-3895,-1444, + 11294,-3758,-1445,10396,-3190,-1348,10392,-2822,-1205,9084,-2543,-1523,9161,-2093,-1291,9622,3020,-1120, + 10287,3156,-1437,10727,3205,-1897,10306,2317,-2005,10436,2550,-1029,10657,3064,-1487,9414,-428,-1000, + 10008,-468,-1020,9699,-706,-856,-12843,-4430,-1002,-12704,-4331,-1378,-7395,1681,-428,-7347,-3112,-449, + -4645,-2628,-888,-2843,-2474,-482,-9360,-1161,-763,9923,-961,-417,-6184,2314,-412,-8880,-3116,-570, + -10374,-1433,-645,-12179,-3747,-1402,-12951,-3951,-1536,-12466,-3476,-1298,-7265,-3118,-1433,-5499,-2675,-1779, + 10343,-2804,-755,8961,-3145,-1487,10324,-3518,-1282,6236,740,-1945,10554,-770,-448,-8184,-2192,-1867, + -6736,1317,-573,7150,-1165,-2010,9918,1551,-1880,10671,-165,-786,11038,-4148,-1277,11084,-4147,-702, + 10903,-4159,-987,11907,-4459,-1056,11711,-4341,-546,11956,-4047,-1059,-13186,-4122,-977,-12466,-3475,-672, + -12414,-3316,-973,9898,67,-1564,10514,497,-900,10397,323,-1461,10769,1223,-733,10615,520,-428, + 4162,-588,1986,5822,-22,2298,8594,-109,1007,8535,-214,-1,9223,-572,-1,-9518,-380,1329, + -10917,-2172,595,582,2634,686,688,2878,-1,-682,2926,-1,-744,2625,781,-2780,-2011,1320, + -3553,-1254,1905,-5065,-1409,1939,9468,1262,1831,-13667,-2417,368,5267,1546,2167,5923,996,2281, + -13725,4857,-5,-14925,4450,-5,-14894,4302,453,-13675,4591,540,1097,-2140,481,1047,-2270,-1, + -10297,2645,364,-12569,3122,383,-12582,2898,-4,-10325,2435,-3,-2357,624,2074,-4021,42,2149, + -12307,3988,773,-13690,4062,737,-13734,3367,438,-14913,3623,396,-14893,3529,-5,-13705,3203,-5, + 1080,-1826,1320,4714,1098,2202,-9416,-1449,1553,-6856,-539,2354,7610,508,999,7744,265,-1, + 3718,844,2095,-15202,3994,-6,-14896,4009,562,-10905,-2170,1347,10319,2285,775,-5865,2564,-1, + -6094,2478,-1,-1013,949,2016,-1871,2597,841,9036,550,1749,10139,-2732,971,-1283,-1893,1320, + -1321,-922,1905,-6960,2817,-1,-9832,4176,-3,-9871,3884,374,-7001,2609,399,-4396,2591,1079, + -5260,678,2385,-2999,2744,948,-7148,2237,549,2943,-724,1641,-9210,-2183,1356,8862,-2281,599, + 8771,-2590,1024,-4410,2857,-1,-2978,2901,-1,10777,1242,-1,10644,546,-1,2891,551,1983, + -12126,4650,517,10589,874,1579,-11986,-1185,977,2575,2397,746,2582,2769,-1,-10050,3337,589, + 10204,2314,-1,-1546,-2390,481,1052,732,1983,-10724,-588,882,10229,-2476,700,1156,-1016,1641, + 3806,2322,791,4880,2493,721,5806,2656,738,11510,-3323,1464,3927,2879,-1,7324,2787,-1, + 6111,2924,-1,5052,2846,-1,9145,2858,-1,-2872,-2562,-1,-1542,-2502,-1,-1809,2821,-1, + -12125,4959,-4,5090,-1546,-1988,5867,-602,-2300,8594,-109,-1008,-9245,-1324,-1329,-10401,-2931,-598, + 582,2634,-687,-735,2603,-783,-4814,-1917,-1940,-3394,-1554,-1907,-2679,-2185,-1321,9468,1262,-1832, + -12942,-3962,-375,5589,1360,-2168,5986,378,-2282,-13675,4591,-550,-14894,4302,-464,999,-2123,-482, + -10297,2646,-370,-12573,3123,-391,-3835,-383,-2150,-2268,447,-2075,-12307,3988,-781,-13733,3367,-447, + -13689,4062,-747,-14912,3623,-407,1052,-1864,-1321,4815,837,-2204,-9062,-2375,-1553,-6581,-1324,-2355, + 7623,451,-1001,3718,807,-2097,-14896,4009,-573,-10400,-2928,-1350,10319,2285,-777,-1847,2546,-842, + -974,878,-2017,9036,550,-1751,10197,-3741,-972,-1261,-1022,-1907,-1222,-1984,-1321,-7000,2610,-402, + -9865,3882,-380,-4374,2492,-1080,-2970,2668,-949,-5098,86,-2386,-7147,2238,-551,3865,-1593,-1642, + -8880,-3114,-1357,8841,-3435,-1025,8965,-3139,-600,2763,753,-1984,-12125,4650,-525,10589,874,-1581, + -11654,-2175,-978,2580,2373,-747,-10049,3338,-594,-1527,-2418,-482,725,1310,-1984,-10420,-1271,-881, + 10341,-3525,-700,853,-824,-1642,3872,2461,-792,4961,2482,-722,5911,2634,-739,11695,-4345,-1465, +}; +static const signed char g_rcatN[] = { + 15,-27,123,24,2,125,13,2,126,-2,-101,-76,32,-60,-107,-11,-125,-19,31,-105,-64,56,-56,-100,29,-75,-98,-18,-6,126, + -42,74,94,-45,-12,118,4,122,-36,6,86,-94,4,101,-77,108,66,13,45,-62,101,111,-62,-1,19,122,-28,17,120,37, + 40,-45,112,9,-11,126,4,7,127,42,-51,-109,9,-8,-126,18,-67,-106,-41,87,82,-44,108,49,1,8,-127,-44,105,-56, + -47,108,-47,-7,8,127,-11,113,56,-81,90,38,-68,-92,55,-94,-15,84,3,62,111,28,124,7,-29,1,-124,10,70,-106, + 11,75,-102,-73,-94,44,-14,7,126,-6,2,127,-24,-117,43,9,13,126,-65,-47,-98,-83,-82,-50,-94,-83,-20,-18,-63,-109, + 3,37,-121,6,-39,-121,53,-50,-104,125,-22,-9,68,-81,-70,59,-77,-81,25,18,-123,107,69,-1,-37,-86,86,-17,-116,-49, + 13,9,-126,-26,-82,-93,-5,-19,-125,-46,-79,-88,-22,-4,-125,-64,-82,-73,32,-122,-17,46,-116,-22,-22,-11,-125,-5,-71,-105, + -102,-21,73,-122,18,29,41,101,-66,56,104,-48,-38,84,-88,10,94,-84,55,114,12,-81,6,97,-3,121,-38,-77,99,17, + 98,81,-1,22,66,-106,30,123,0,18,112,56,3,120,-41,0,124,-28,30,-123,5,-9,119,-45,72,104,0,-4,-104,-72, + 29,87,88,24,-57,111,-1,0,-127,60,-96,-57,48,-37,-112,-5,89,-91,15,108,-66,-63,-94,58,-3,43,-119,18,78,-99, + -46,20,-117,-54,115,0,-105,71,0,-67,89,-61,40,9,-120,99,-5,-80,-19,27,-123,-62,-14,-110,-100,31,-71,-3,94,-85, + 27,124,7,-23,111,58,-69,30,102,-94,85,-1,-56,-114,0,107,-68,-4,95,-84,0,57,-99,57,-8,127,0,0,64,-110, + -124,19,-20,-59,44,-104,-123,-10,-29,-119,-25,-36,-114,-32,46,-113,-32,-49,15,-26,-123,14,3,-126,26,3,-124,-1,-99,80, + -13,-125,15,34,-59,107,33,-106,61,32,-79,95,57,-57,98,-25,-1,-124,-120,7,-40,-45,78,-89,17,126,-2,23,119,39, + 19,86,92,48,-60,-101,41,-43,-112,4,7,-127,9,-11,-126,44,-50,108,16,-67,107,9,-8,126,-47,85,-82,-47,108,-48, + 1,8,127,-50,106,49,-45,103,60,-7,8,-127,-11,115,-54,-112,-60,9,50,117,-8,13,55,-114,-30,2,123,22,71,103, + 17,77,100,-13,9,-126,-92,-82,-30,-6,3,-127,11,10,-126,-51,-109,-41,-69,-19,105,-106,13,69,-92,-53,70,-19,-53,114, + 6,-34,122,-4,37,122,55,-51,103,72,-79,69,125,-23,5,64,-74,81,25,19,123,-60,-73,-84,-45,-108,49,-49,-72,93, + 14,6,126,-9,-18,125,-60,-72,86,-23,-2,125,-74,-74,71,32,-122,17,46,-116,22,-5,-71,105,-22,-11,125,-102,-21,-73, + -122,18,-29,41,101,66,-38,84,88,56,104,48,54,115,-1,12,95,83,21,66,106,19,115,-51,0,123,30,4,120,43, + 30,-123,-7,-9,119,45,-4,-104,72,29,88,-87,24,-57,-111,-1,1,127,47,-42,110,59,-99,54,13,106,69,-5,86,94, + -77,-83,-58,2,42,120,31,72,100,-51,33,112,-67,89,61,40,12,120,100,-7,78,-26,23,122,-62,-14,110,-102,29,70, + 21,93,84,8,113,-58,52,116,-5,-65,109,0,-57,48,-103,-85,-94,0,106,-71,-1,56,-99,-57,99,-80,0,0,64,110, + -124,19,20,-59,44,104,-119,-25,36,-123,-10,29,13,36,-121,2,19,-126,55,94,-65,64,110,0,62,111,0,33,-54,-110, + 15,94,84,-12,-85,-93,-13,-126,1,-6,-127,0,-3,-89,-90,-7,98,-81,-8,35,-122,-12,32,-122,12,-10,-126,41,-30,116, + 14,-83,-95,-28,-41,-117,20,-125,0,59,-113,0,40,-88,-82,6,-84,-95,6,115,-53,15,126,0,16,78,-99,16,77,-100, + 28,124,0,25,125,0,2,-36,-122,-19,-26,-123,0,15,-126,12,9,-126,20,79,-97,45,71,-95,52,116,0,36,122,0, + 3,96,-83,22,-59,-110,18,0,-126,11,-2,-127,6,103,-74,36,122,-3,18,-39,-119,127,-5,0,56,5,-114,-1,88,-92, + -116,-53,3,22,-125,-2,-17,-126,0,-5,-35,-122,-9,-80,-98,38,34,-116,29,123,4,0,94,-85,-5,34,-122,-43,-120,0, + -44,-119,0,-28,-71,-101,-24,-81,-95,-11,-84,-95,-10,-37,-121,-23,-79,-96,1,-14,-126,10,41,-120,29,108,-60,8,62,111, + 26,124,2,-5,-127,-3,-29,-124,-3,-124,-29,0,-121,-39,-1,11,-25,-124,-22,-73,-101,-111,-3,-62,72,-104,-10,-4,-84,-95, + 0,-127,-4,-5,-1,-127,-78,-100,0,1,119,-45,-8,-26,-124,34,-119,-29,19,73,102,-5,39,-121,8,-92,-87,19,-98,-79, + 8,-108,-66,-62,78,-79,5,-126,-11,-1,-127,-1,-6,-127,-1,7,-127,-1,-23,-125,0,8,127,2,0,127,1,-11,-127,-2, + -17,-126,0,22,30,121,7,14,126,52,95,66,34,-52,111,17,94,-84,-7,-96,82,-4,-90,89,-12,29,123,-7,33,122, + -6,96,83,12,-10,126,40,-28,-117,7,-72,105,-35,-11,122,6,-84,95,40,-88,82,15,121,37,16,78,99,16,77,100, + -20,-26,123,0,-36,122,0,15,126,20,79,97,12,9,126,45,71,95,16,92,87,15,-59,111,18,1,126,10,-2,127, + -8,107,68,19,-37,120,56,5,114,0,87,93,-116,-53,-3,-12,-82,96,0,-37,122,35,34,117,50,117,-2,-8,33,122, + -1,92,87,-24,-81,95,-28,-71,101,-10,-87,92,-24,-83,93,-9,-39,121,0,-14,126,19,34,121,29,108,60,43,120,2, + 17,60,-111,8,-19,125,-22,-73,101,-111,-3,62,76,-101,10,-7,-91,89,-5,-1,127,1,118,47,-5,-23,125,33,-119,28, + 32,68,-102,0,35,122,6,-91,88,13,-97,81,6,-107,68,-37,92,79,15,-27,123,25,1,125,13,2,126,1,-101,-77, + 37,-57,-107,-2,-126,-18,24,-106,-66,50,-58,-101,26,-76,-98,-17,-8,126,-43,70,97,-39,-15,120,-8,122,-33,0,87,-93, + -2,102,-76,108,64,18,46,-62,101,114,-56,2,27,122,-24,23,118,40,39,-45,112,9,-11,126,4,7,127,41,-51,-109, + 9,-9,-126,17,-67,-106,-40,88,82,-43,109,49,1,8,-127,-42,106,-56,-46,109,-47,-7,8,127,-10,113,56,-62,103,41, + -53,-101,57,-84,-22,92,0,62,111,20,125,7,-29,-2,-124,3,72,-105,6,76,-101,-68,-98,44,-14,6,126,-6,2,127, + -30,-116,43,10,12,126,-59,-59,-96,-73,-93,-47,-66,-108,-8,-18,-64,-108,3,39,-121,8,-38,-121,53,-48,-105,125,-14,-18, + 67,-78,-74,59,-77,-82,26,18,-123,109,65,2,-41,-84,86,-23,-115,-49,13,9,-126,-31,-80,-93,-6,-19,-125,-48,-78,-88, + -22,-5,-125,-62,-84,-72,43,-118,-17,56,-112,-22,-21,-12,-125,1,-71,-105,-99,-30,73,-123,8,29,31,104,-66,46,108,-48, + -46,80,-88,11,94,-84,56,113,12,-68,2,107,-10,122,-32,-75,94,40,101,77,0,19,66,-107,26,124,0,22,111,58, + 10,120,-41,5,124,-27,28,-124,5,-19,117,-45,62,111,0,-9,-104,-72,30,87,88,23,-58,111,-1,0,-127,59,-97,-57, + 47,-38,-112,-1,88,-91,19,107,-66,-65,-92,58,-3,43,-119,20,77,-99,-42,23,-118,-64,110,0,-111,62,0,-75,83,-61, + 40,9,-120,97,-3,-82,-22,25,-123,-61,-19,-110,-102,23,-72,1,94,-85,33,123,7,-17,112,58,-68,34,102,-90,90,-1, + -61,-111,0,106,-70,-5,94,-85,0,55,-99,57,-20,125,0,-6,63,-110,-125,9,-20,-63,38,-104,-122,-20,-29,-117,-35,-36, + -111,-42,46,-110,-41,-49,14,-27,-123,14,2,-126,26,2,-124,-1,-100,78,-4,-126,14,40,-56,107,25,-108,62,27,-81,94, + 51,-59,100,-27,2,-124,-123,15,-30,-51,88,-76,14,126,1,16,119,41,13,83,95,45,-61,-102,35,-48,-112,5,6,-127, + 8,-12,-126,37,-55,108,11,-68,107,8,-9,126,-35,90,-82,-32,113,-48,2,8,127,-36,112,48,-31,108,60,-6,8,-127, + 5,115,-54,-116,-50,10,64,109,-8,20,52,-114,-30,6,123,29,67,104,16,74,102,-11,10,-126,-104,-68,-27,-5,4,-127, + 12,9,-126,-57,-106,-41,-70,-12,105,-107,18,66,-98,-41,70,-22,-54,113,8,-34,122,-4,37,122,53,-49,104,67,-79,74, + 125,-13,18,61,-76,81,26,18,123,-64,-70,-84,-51,-106,49,-53,-70,92,14,5,126,-10,-17,125,-67,-64,87,-23,1,125, + -84,-63,71,43,-118,17,56,-112,22,1,-71,105,-21,-12,125,-99,-30,-73,-123,8,-29,31,104,66,-46,80,88,46,108,48, + 69,107,-1,24,93,83,20,66,107,26,114,-49,8,123,33,12,118,44,24,-124,-7,-19,117,45,-9,-104,72,34,86,-87, + 19,-59,-111,-1,2,127,41,-47,111,46,-105,54,19,104,71,0,85,95,-87,-71,-59,9,41,120,38,68,100,-49,30,113, + -75,83,61,40,10,120,97,-2,83,-24,26,122,-61,-19,110,-104,21,70,26,92,83,15,112,-57,56,114,-5,-58,113,0, + -54,52,-103,-90,-90,0,95,-84,-1,42,-106,-57,87,-92,0,-6,63,110,-125,9,20,-63,38,104,-117,-35,36,-122,-20,29, + 10,38,-121,1,19,-126,47,98,-67,54,115,0,51,116,0,33,-55,-110,15,94,84,-15,-85,-93,-15,-126,0,-15,-126,0, + -10,-89,-90,-2,98,-81,-6,34,-122,-11,31,-123,13,-9,-126,41,-31,116,15,-85,-93,-23,-48,-115,20,-125,0,57,-113,0, + 39,-88,-83,7,-84,-95,7,115,-53,14,126,-2,8,79,-99,9,78,-100,12,126,0,14,126,0,-2,-36,-122,-21,-26,-122, + -2,16,-126,12,10,-126,20,79,-98,46,71,-95,54,115,0,35,122,0,2,95,-84,26,-58,-110,18,0,-126,10,-2,-127, + 12,103,-73,34,122,-2,21,-38,-120,127,-6,0,56,5,-114,0,88,-92,-111,-62,3,13,-126,-2,-21,-125,0,-7,-35,-122, + -15,-81,-97,35,37,-116,33,123,4,4,94,-85,-3,34,-122,-39,-121,0,-31,-123,0,-23,-73,-101,-22,-82,-95,-14,-86,-92, + -11,-39,-120,-24,-81,-95,1,-15,-126,8,42,-119,31,108,-60,9,62,111,27,124,2,-9,-127,-3,-32,-123,-3,-121,-39,0, + -117,-49,-1,11,-24,-124,-14,-75,-101,-110,-13,-62,71,-105,-10,-1,-84,-95,7,-127,-8,-5,-2,-127,-69,-107,0,8,119,-45, + -8,-25,-124,32,-119,-29,21,72,102,-4,40,-121,14,-91,-88,21,-96,-80,5,-107,-68,-58,81,-79,14,-125,-18,1,-127,-3, + -10,-126,-7,8,-126,-8,-14,-126,0,16,126,3,6,127,0,-20,-125,-2,-7,-127,0,20,29,122,6,14,126,45,97,68, + 32,-54,110,23,93,-83,-10,-96,82,-12,-90,89,-10,30,123,-5,34,122,0,95,84,13,-9,126,36,-33,-117,2,-74,103, + -33,-11,122,7,-84,95,39,-88,82,14,121,37,9,79,99,9,78,100,-22,-27,122,-4,-35,122,-2,16,126,20,79,98, + 12,10,126,46,71,95,14,91,88,18,-60,111,18,0,126,10,-2,127,-3,106,70,24,-37,119,56,5,114,8,87,92, + -111,-62,-3,-18,-82,95,-3,-37,122,33,36,117,62,111,-2,-6,33,122,4,92,88,-22,-82,95,-23,-73,101,-13,-90,89, + -25,-84,92,-11,-41,120,1,-15,126,16,33,121,34,106,61,61,112,3,26,57,-111,9,-20,125,-13,-75,101,-110,-13,62, + 63,-110,10,-2,-92,87,-4,-2,127,8,118,47,-5,-22,125,22,-122,27,38,64,-102,0,34,122,16,-94,84,14,-100,77, + 1,-109,66,-31,95,79,16,-27,123,28,2,124,14,2,126,-1,-101,-76,40,-54,-108,2,-126,-15,17,-108,-65,44,-62,-102, + 24,-78,-98,-16,-7,126,-31,63,106,-35,-11,122,-3,126,-18,3,88,-92,2,108,-67,110,63,-12,55,-61,97,121,-37,-14, + 34,120,-26,30,117,37,36,-47,112,9,-11,126,5,7,127,38,-53,-109,8,-9,-126,16,-68,-106,-35,89,83,-36,111,49, + 1,8,-127,-42,105,-58,-40,111,-47,-7,7,127,-14,112,59,-39,114,39,-52,-99,60,-76,1,102,0,60,112,20,125,7, + -29,-1,-124,5,71,-105,6,76,-101,-66,-98,47,-14,6,126,-6,2,127,-26,-117,43,9,13,126,-61,-59,-95,-74,-92,-46, + -61,-111,0,-22,-65,-107,3,39,-121,9,-37,-121,53,-41,-108,122,-9,-34,69,-66,-83,63,-72,-84,27,18,-123,116,53,-2, + -38,-85,86,-19,-116,-49,13,9,-126,-28,-81,-93,-5,-19,-125,-45,-80,-88,-22,-5,-125,-61,-85,-72,50,-116,-16,61,-109,-22, + -20,-13,-125,5,-71,-105,-98,-34,73,-124,2,29,26,105,-67,40,110,-49,-50,77,-88,7,93,-86,50,116,11,-50,8,117, + -8,127,-5,-48,78,88,109,66,-2,14,67,-107,16,126,0,25,111,57,20,118,-42,10,123,-28,32,-123,3,-26,116,-45, + 56,114,0,-13,-104,-71,29,87,88,23,-58,111,-2,-1,-127,61,-94,-59,50,-34,-112,2,86,-93,21,105,-69,-61,-95,58, + -5,43,-119,17,78,-99,-42,22,-118,-69,106,0,-114,57,0,-79,79,-61,41,9,-120,94,-2,-85,-21,26,-123,-60,-22,-110, + -103,18,-72,-2,94,-85,29,124,7,-21,111,58,-69,31,102,-93,87,-1,-58,-113,0,111,-61,-4,89,-91,0,59,-96,59, + -26,124,0,-10,62,-110,-125,3,-20,-64,35,-104,-121,-26,-29,-115,-41,-36,-109,-47,46,-108,-46,-49,14,-27,-123,13,2,-126, + 26,2,-124,-5,-102,76,2,-126,13,43,-53,107,19,-110,61,24,-84,92,45,-64,100,-27,11,-124,-115,53,6,-50,115,-18, + 25,124,9,16,117,46,12,78,100,45,-59,-103,28,-53,-112,6,6,-127,7,-12,-126,28,-60,108,6,-69,107,7,-10,126, + -21,95,-82,-15,117,-48,3,8,127,-18,116,49,-14,111,60,-4,9,-127,22,113,-53,-126,-4,16,87,92,-7,31,45,-115, + -27,15,123,44,57,104,23,65,106,-9,12,-126,-118,-38,-26,-3,4,-127,12,9,-126,-65,-102,-40,-69,14,106,-93,56,66, + -104,-5,73,-30,-56,110,9,-35,122,-3,37,121,53,-50,104,65,-76,78,124,-18,22,61,-76,81,27,18,123,-71,-65,-83, + -58,-102,47,-60,-65,91,15,4,126,-11,-15,126,-71,-54,90,-22,6,125,-95,-43,73,50,-116,16,61,-109,22,5,-71,105, + -20,-13,125,-98,-34,-73,-124,2,-29,26,105,67,-50,77,88,40,110,49,84,95,-1,38,88,84,14,67,107,30,113,-50, + 14,122,34,22,117,44,24,-124,-8,-26,116,45,-13,-105,71,35,86,-87,14,-62,-110,0,2,127,33,-53,110,29,-111,54, + 23,102,72,4,84,96,-96,-59,-59,17,41,119,45,66,99,-45,32,114,-79,79,61,41,9,120,97,-9,82,-18,33,121, + -60,-22,110,-104,16,70,33,91,82,26,111,-57,60,112,-6,-44,119,0,-48,57,-103,-98,-81,0,81,-98,-1,26,-111,-57, + 72,-105,0,-10,62,110,-125,3,20,-64,35,104,-115,-41,36,-121,-26,29,10,37,-121,1,19,-126,42,99,-68,48,118,0, + 45,119,0,34,-54,-110,15,95,83,-23,-84,-92,-24,-125,-1,-29,-124,0,-21,-88,-89,4,97,-82,-4,34,-122,-11,31,-123, + 13,-8,-126,43,-27,116,10,-87,-92,-24,-49,-115,16,-126,0,52,-116,0,34,-90,-83,5,-83,-96,13,116,-50,19,126,1, + 0,79,-99,5,79,-99,0,127,0,1,127,0,-7,-35,-122,-24,-28,-122,-3,16,-126,13,9,-126,22,78,-98,50,69,-94, + 60,112,0,37,122,0,7,95,-84,23,-59,-110,18,0,-126,11,-2,-127,17,102,-73,35,122,-1,20,-38,-120,126,-12,0, + 56,3,-114,1,88,-91,-108,-67,3,5,-127,-2,-24,-125,0,-11,-34,-122,-24,-82,-94,33,38,-117,28,124,4,12,93,-86, + 0,34,-122,-30,-123,0,-17,-126,0,-16,-75,-101,-16,-84,-94,-16,-90,-88,-12,-41,-119,-26,-84,-92,2,-15,-126,10,42,-119, + 30,107,-61,6,62,111,21,125,2,-11,-127,-2,-35,-122,-1,-119,-45,0,-115,-54,-1,10,-24,-124,-7,-76,-101,-109,-18,-62, + 65,-108,-10,-5,-84,-95,4,-126,-12,-4,-2,-127,-63,-110,0,17,117,-46,-10,-24,-124,30,-120,-29,19,73,102,-1,40,-121, + 11,-91,-88,16,-97,-80,-2,-107,-69,-61,79,-79,11,-124,-25,-2,-127,-3,-20,-125,-13,0,-126,-16,-9,-127,0,27,124,3, + 16,126,1,-32,-123,-1,-1,-127,0,21,25,123,7,13,126,43,97,70,32,-54,110,27,94,-81,-18,-96,82,-22,-88,89, + -9,30,123,-3,33,123,7,94,85,13,-8,126,31,-38,-117,-9,-76,101,-32,-3,123,5,-83,96,34,-90,83,19,119,39, + 0,79,99,5,79,99,-24,-27,122,-8,-35,122,-3,16,126,22,78,98,13,9,126,50,69,94,16,89,89,14,-65,108, + 17,0,126,10,-2,127,2,103,74,25,-43,117,56,3,114,14,88,91,-108,-67,-3,-25,-82,94,-7,-36,121,34,38,117, + 73,104,-3,-4,34,122,10,91,88,-16,-84,94,-16,-75,101,-16,-92,86,-26,-86,90,-13,-43,119,2,-15,126,16,30,122, + 36,105,61,78,100,-2,35,52,-111,10,-23,124,-7,-76,101,-109,-18,62,46,-118,10,-3,-96,84,-4,-2,127,18,117,47, + -6,-23,125,12,-124,27,45,60,-103,0,34,122,18,-100,76,5,-107,69,-11,-111,61,-20,97,79,20,-24,123,31,6,123, + 13,4,126,1,-102,-76,40,-54,-108,2,-126,-13,22,-109,-61,48,-63,-99,29,-80,-94,-16,-5,126,-16,55,113,-31,-5,123, + 10,126,8,10,89,-90,10,120,-42,101,62,-45,71,-52,92,122,-16,-31,36,118,-30,34,118,32,40,-44,112,10,-10,126, + 4,7,127,42,-50,-109,9,-8,-126,23,-66,-106,-42,86,83,-45,108,49,1,8,-127,-48,103,-57,-49,107,-47,-7,7,127, + -17,112,57,-28,117,39,-67,-88,62,-58,41,105,5,58,113,29,124,7,-29,3,-124,11,69,-106,13,75,-102,-73,-92,48, + -13,6,126,-6,2,127,-26,-116,43,9,12,126,-67,-47,-97,-86,-81,-47,-90,-89,-9,-22,-65,-107,2,38,-121,9,-37,-121, + 57,-35,-108,120,-11,-39,77,-49,-89,73,-60,-85,27,20,-123,118,46,-5,-39,-85,86,-19,-116,-49,13,9,-126,-29,-81,-93, + -5,-19,-125,-46,-79,-88,-22,-4,-125,-65,-82,-73,50,-115,-16,62,-109,-22,-20,-13,-125,5,-71,-105,-98,-35,73,-124,1,29, + 25,105,-67,39,111,-49,-50,77,-88,5,94,-85,48,117,12,-36,12,121,-6,117,49,-21,52,114,113,57,-4,9,67,-107, + 7,127,0,22,113,54,26,116,-45,10,122,-32,48,-118,1,-27,116,-45,55,114,0,-8,-105,-71,21,89,88,28,-56,110, + -2,0,-127,64,-93,-58,50,-34,-112,0,82,-97,15,103,-73,-62,-95,58,-4,44,-119,18,78,-99,-47,16,-117,-70,106,0, + -114,56,0,-79,78,-61,41,13,-120,95,-4,-84,-18,28,-123,-59,-23,-110,-103,17,-72,-1,94,-85,29,124,7,-20,111,58, + -69,32,102,-92,87,-1,-59,-113,0,111,-61,-4,96,-83,0,61,-95,58,-27,124,0,-10,62,-110,-125,1,-20,-65,35,-104, + -121,-27,-29,-114,-42,-36,-108,-48,46,-107,-47,-49,15,-25,-124,13,3,-126,25,4,-125,-7,-104,73,3,-126,13,44,-53,107, + 23,-111,57,29,-88,87,49,-66,96,-25,22,-123,-74,82,62,-27,85,90,21,122,30,3,115,53,3,73,104,49,-53,-105, + 31,-51,-112,5,6,-127,8,-12,-126,32,-58,108,8,-68,107,7,-10,126,-27,94,-82,-21,116,-48,2,8,127,-25,115,49, + -19,111,60,-4,9,-127,19,114,-52,-108,60,31,108,66,-7,41,36,-115,-23,23,123,57,43,105,26,58,110,-6,14,-126, + -125,-3,-24,-2,5,-127,13,9,-126,-67,-100,-40,-55,44,105,-56,90,70,-95,35,76,-34,-59,107,9,-37,121,-2,37,121, + 56,-52,102,71,-71,77,123,-30,13,68,-70,81,26,19,123,-70,-65,-83,-60,-102,46,-60,-65,91,15,4,126,-12,-14,126, + -75,-42,94,-19,11,125,-101,-19,74,50,-115,16,62,-109,22,5,-71,105,-20,-13,125,-98,-35,-73,-124,1,-29,25,105,67, + -50,77,88,39,111,49,82,97,-1,36,90,83,9,67,107,27,112,-53,14,121,35,27,116,44,31,-123,-9,-27,116,45, + -8,-105,70,31,87,-87,16,-61,-110,0,2,127,34,-53,110,32,-111,52,19,102,74,3,81,98,-104,-42,-59,25,37,119, + 54,61,98,-39,37,115,-79,78,61,40,12,120,100,-19,76,-13,41,120,-59,-23,110,-105,15,70,35,90,82,28,110,-57, + 63,110,-7,-43,119,0,-48,58,-102,-98,-81,0,82,-97,-1,29,-111,-56,79,-100,0,-10,62,110,-125,1,20,-65,35,104, + -114,-42,36,-121,-27,29,13,35,-121,3,18,-126,42,99,-67,47,118,0,44,119,0,41,-48,-110,6,96,83,-29,-84,-91, + -30,-123,-3,-38,-121,1,-30,-88,-86,8,94,-85,-3,32,-123,-12,29,-123,13,-8,-126,43,-27,116,7,-86,-93,-28,-43,-116, + 11,-127,0,47,-118,0,30,-91,-83,2,-83,-96,17,119,-40,17,125,13,-3,79,-99,5,79,-99,-1,127,0,-6,127,0, + -13,-35,-122,-28,-30,-120,-3,16,-126,13,9,-126,25,77,-98,53,66,-94,65,109,0,40,121,0,13,96,-83,20,-60,-110, + 17,3,-126,11,0,-127,11,102,-74,33,123,-1,17,-39,-120,126,-17,0,56,0,-114,-7,88,-91,-107,-68,3,8,-127,-1, + -18,-126,0,-15,-34,-121,-33,-84,-89,33,38,-116,29,124,4,18,90,-88,2,33,-123,-22,-125,0,-12,-126,0,-11,-76,-101, + -11,-85,-94,-14,-95,-83,-10,-45,-119,-28,-89,-86,3,-15,-126,13,40,-120,19,109,-62,7,62,111,24,125,1,-10,-127,0, + -37,-121,1,-118,-46,0,-114,-56,-1,9,-25,-124,-5,-77,-101,-109,-19,-62,75,-102,-10,-10,-84,-95,-3,-126,-16,-4,-2,-127, + -63,-111,0,25,115,-47,-12,-24,-124,41,-117,-28,19,73,102,1,39,-121,6,-92,-87,13,-98,-80,-2,-108,-66,-60,79,-79, + 5,-123,-31,0,-127,-2,-21,-124,-18,-4,-125,-23,-7,-127,0,33,123,1,22,125,4,-41,-120,1,-2,-127,0,22,22,123, + 9,12,126,43,96,71,36,-51,111,24,95,-81,-23,-95,81,-29,-87,87,-10,28,123,-1,32,123,10,93,86,13,-8,126, + 31,-38,-117,-18,-80,97,-28,10,123,2,-83,96,30,-91,83,16,118,44,-4,79,99,5,79,99,-26,-29,121,-12,-34,122, + -3,16,126,25,77,98,13,9,126,53,66,94,11,87,92,10,-75,102,17,2,126,10,-1,127,4,100,78,27,-55,111, + 56,0,114,11,88,91,-107,-68,-3,-32,-82,91,-10,-36,121,35,38,116,86,94,-4,-3,34,122,13,90,89,-11,-85,94, + -11,-76,101,-15,-95,83,-28,-88,87,-11,-45,118,3,-16,126,15,28,123,31,107,61,96,82,-5,44,44,-110,11,-31,123, + -6,-77,101,-109,-19,62,54,-115,10,-4,-100,78,-4,-2,127,23,116,47,-8,-26,124,16,-123,27,52,54,-103,-2,35,122, + 17,-109,63,-3,-114,57,-16,-114,53,-19,98,79,21,-22,123,34,9,122,14,6,126,-3,-103,-75,34,-58,-108,-9,-126,-14, + 33,-106,-62,60,-50,-100,37,-78,-93,-16,-3,126,-6,49,117,-29,1,124,27,120,31,20,88,-89,21,125,-10,95,62,-58, + 81,-43,88,119,17,-40,40,116,-34,40,118,26,51,-37,110,11,-9,126,3,8,127,53,-42,-107,10,-7,-126,26,-63,-107, + -59,84,75,-63,99,50,-1,8,-127,-40,110,-48,-66,99,-46,-2,15,126,15,118,44,-16,119,41,-86,-67,65,-31,69,102, + 15,54,114,49,117,7,-28,8,-124,19,67,-106,25,71,-102,-89,-77,48,-12,9,126,-5,3,127,-47,-110,43,11,11,126, + -72,-29,-100,-100,-61,-48,-116,-46,-26,-23,-64,-107,4,37,-121,6,-38,-121,58,-26,-110,115,16,-51,79,-33,-94,80,-49,-86, + 26,21,-122,105,72,-6,-55,-76,85,-40,-110,-48,14,6,-126,-44,-75,-93,-8,-18,-125,-59,-70,-88,-23,0,-125,-79,-68,-73, + 38,-120,-17,51,-114,-22,-21,-11,-125,-1,-71,-105,-101,-26,73,-123,13,29,36,102,-66,51,106,-48,-42,82,-88,35,98,-73, + 84,94,12,-27,16,123,-3,94,85,-9,39,120,98,80,-5,-3,68,-107,-15,126,0,21,115,51,34,113,-48,13,120,-38, + 53,-115,1,-15,118,-45,67,108,0,15,-104,-71,20,91,87,33,-53,111,2,6,-127,40,-113,-42,30,-55,-110,0,78,-100, + 12,100,-77,-78,-81,58,5,43,-119,31,73,-99,-48,17,-116,-59,112,0,-108,66,0,-71,86,-61,40,17,-119,89,16,-89, + -14,31,-122,-61,-17,-110,-102,26,-71,16,93,-84,48,117,7,2,113,57,-61,45,102,-72,105,-1,-79,-99,0,70,-106,-7, + 113,-59,0,32,-116,39,-14,126,0,-4,63,-110,-125,14,-20,-61,41,-104,-123,-15,-29,-118,-31,-36,-112,-37,46,-111,-37,-49, + 15,-24,-124,14,4,-126,25,6,-124,-11,-105,71,-8,-126,15,38,-57,107,34,-108,58,37,-88,83,63,-53,97,-18,31,-122, + -35,88,84,-11,53,115,-7,110,64,-19,111,59,-13,72,104,52,-48,-105,41,-45,-111,4,7,-127,8,-10,-126,44,-51,108, + 10,-68,107,9,-9,126,-46,88,-79,-43,109,-49,1,8,127,-47,108,48,-33,108,57,-4,11,-126,11,117,-48,-68,99,43, + 126,8,-8,51,13,-116,-17,29,122,67,16,107,26,52,113,1,15,-126,-117,46,-19,2,5,-127,15,7,-126,-78,-92,-40, + -33,67,102,-22,103,71,-75,67,77,-38,-60,105,6,-39,121,2,37,121,59,-48,102,76,-62,81,125,-11,19,75,-62,82, + 25,20,123,-70,-63,-86,-69,-97,45,-59,-63,93,16,3,126,-15,-8,126,-83,-5,96,-13,19,125,-101,25,72,38,-120,17, + 51,-114,22,-1,-71,105,-21,-11,125,-101,-26,-73,-123,13,-29,36,102,66,-42,82,88,51,106,48,76,101,-2,31,95,79, + -3,68,107,25,112,-54,15,120,37,34,114,44,29,-123,-11,-15,118,45,16,-104,71,32,88,-86,21,-59,-110,1,4,127, + 35,-53,110,41,-110,48,16,100,77,3,77,101,-112,13,-59,38,21,119,72,35,99,-32,44,115,-71,86,61,40,15,120, + 99,-2,80,-5,48,117,-61,-17,110,-103,24,70,43,85,84,34,108,-58,86,93,-10,-43,119,0,-48,60,-102,-99,-79,0, + 83,-96,-1,36,-111,-51,99,-80,0,-4,63,110,-125,14,20,-61,41,104,-118,-31,36,-123,-15,29,17,32,-122,5,17,-126, + 51,95,-66,58,113,0,56,114,0,44,-44,-111,2,95,84,-35,-83,-90,-38,-121,-4,-43,-119,1,-38,-89,-83,15,90,-89, + -1,30,-123,-13,27,-123,13,-9,-126,27,-48,115,5,-86,-93,-33,-33,-118,1,-127,0,37,-121,0,23,-93,-83,-4,-83,-96, + 22,122,-27,12,123,27,-9,79,-99,9,79,-99,3,127,0,-16,126,0,-19,-34,-121,-32,-34,-118,-2,17,-126,14,8,-126, + 31,75,-98,58,62,-94,73,104,0,48,118,0,20,95,-82,15,-62,-110,17,4,-126,11,2,-126,8,102,-75,38,121,0, + 13,-41,-119,124,-27,0,56,-4,-114,-14,86,-92,-113,-58,3,16,-126,-1,10,-127,0,-19,-34,-121,-43,-87,-82,37,36,-116, + 51,116,4,24,88,-89,4,32,-123,1,-127,0,-6,-127,0,-2,-76,-101,5,-85,-94,-10,-101,-76,-8,-49,-117,-32,-96,-77, + 6,-14,-126,17,38,-120,16,109,-63,19,60,111,48,118,1,-10,-127,0,-41,-120,2,-122,-35,0,-119,-44,-1,7,-26,-124, + -8,-76,-101,-110,-8,-62,92,-87,-10,-17,-84,-94,-16,-125,-18,-5,-2,-127,-73,-104,0,33,113,-48,-14,-24,-124,51,-113,-29, + 32,68,102,4,38,-121,-3,-94,-85,8,-100,-77,-1,-110,-63,-43,90,-79,-8,-122,-35,-4,-127,0,-21,-124,-20,-11,-124,-26, + -18,-126,0,41,120,-1,29,123,7,-48,-118,2,-8,-127,0,25,19,123,11,12,126,52,92,70,37,-47,112,25,93,-82, + -29,-95,80,-36,-88,85,-10,26,124,0,30,123,15,90,88,13,-9,126,32,-38,-117,-28,-84,91,-21,25,123,-4,-83,96, + 23,-93,83,11,116,50,-9,79,99,9,79,99,-29,-31,120,-16,-33,122,-2,17,126,31,75,98,14,8,126,58,62,94, + 3,86,93,2,-87,93,18,3,126,11,0,127,9,98,81,25,-73,101,56,-4,114,7,88,91,-113,-58,-3,-40,-84,87, + -13,-36,121,39,36,115,115,55,-5,-1,34,122,16,89,89,5,-85,94,-2,-76,101,-11,-101,77,-31,-93,80,-9,-49,117, + 6,-14,126,12,28,123,32,107,60,124,28,0,60,18,-111,9,-42,120,-8,-76,101,-110,-8,62,73,-103,9,-10,-104,72, + -5,-2,127,28,115,46,-12,-30,123,23,-122,26,68,31,-103,-5,37,121,8,-116,51,-15,-118,44,-21,-117,45,-19,99,78, + 22,-21,123,43,11,119,15,6,126,-13,-104,-72,28,-60,-109,-20,-125,-11,40,-99,-69,61,-33,-106,37,-63,-104,-17,1,126, + -3,53,115,-30,12,123,35,118,31,22,85,-92,24,125,-7,79,49,-86,106,-30,62,93,66,-56,41,113,-40,44,118,17, + 54,-33,110,11,-9,126,2,8,127,56,-38,-107,11,-6,-126,25,-64,-107,-66,79,75,-70,93,50,-2,8,-127,-50,106,-48, + -74,93,-45,-3,15,126,3,119,44,0,126,17,-105,-24,67,-11,113,56,21,53,113,63,110,6,-27,14,-123,24,63,-108, + 33,65,-104,-98,-63,50,-11,10,126,-5,3,127,-47,-110,42,11,11,126,-71,1,-105,-110,-29,-56,-95,46,-70,-30,-63,-106, + 10,38,-121,1,-38,-121,52,-2,-116,79,66,-74,73,-4,-104,81,-42,-88,28,21,-122,74,103,-6,-56,-76,85,-40,-110,-48, + 14,6,-126,-45,-74,-92,-8,-18,-126,-60,-68,-89,-22,2,-125,-86,-59,-72,29,-122,-17,43,-117,-22,-22,-10,-125,-7,-71,-105, + -102,-19,73,-122,21,29,43,100,-66,58,102,-48,-36,84,-88,25,101,-73,75,102,12,-29,28,120,-4,95,84,-9,46,118, + 69,106,-6,-10,69,-106,-27,124,0,22,116,46,36,111,-50,11,121,-38,50,-117,-3,-6,119,-45,75,103,0,31,-100,-72, + 26,91,85,34,-52,111,2,6,-127,50,-109,-43,35,-52,-110,-3,79,-99,11,100,-77,-81,-79,59,8,43,-119,33,73,-99, + -34,33,-118,-51,116,0,-103,74,0,-65,91,-61,41,16,-119,67,46,-98,-14,33,-122,-62,-12,-110,-100,33,-71,17,94,-84, + 46,118,7,4,113,57,-59,46,102,-69,106,-1,-81,-98,0,80,-98,-7,117,-49,0,43,-113,40,-5,127,0,1,63,-110, + -123,22,-20,-58,45,-104,-123,-6,-29,-120,-22,-36,-115,-29,46,-113,-29,-49,15,-24,-124,14,4,-126,23,4,-125,-16,-104,72, + -16,-125,15,34,-60,107,42,-102,62,39,-78,92,67,-37,101,-13,29,-123,-34,118,33,-8,64,109,-6,108,66,-27,111,55, + -17,73,102,50,-47,-107,45,-41,-111,4,7,-127,9,-10,-126,48,-48,107,14,-67,107,10,-8,126,-53,85,-79,-52,105,-49, + 0,8,127,-56,104,48,-40,106,57,-4,11,-126,6,118,-47,-52,113,26,125,-20,-8,52,3,-116,-12,32,122,69,0,107, + 35,41,115,3,15,-126,-107,64,-23,5,5,-127,16,8,-126,-71,-97,-40,-23,73,101,-13,107,66,-64,81,74,-40,-56,107, + 4,-39,121,3,37,121,56,-36,108,72,-54,90,106,48,51,72,-66,81,26,19,123,-61,-70,-87,-59,-103,44,-50,-70,94, + 17,4,126,-16,-6,126,-79,10,99,-9,21,125,-95,44,72,29,-122,17,43,-117,22,-7,-71,105,-22,-10,125,-102,-19,-73, + -122,21,-29,43,100,66,-36,84,88,58,102,48,72,105,-2,27,97,78,-10,69,106,26,110,-58,15,121,35,35,115,42, + 28,-124,-11,-6,119,45,32,-100,72,32,86,-87,25,-57,-111,1,4,127,36,-53,110,46,-109,46,17,101,76,3,79,99, + -107,35,-59,42,13,119,75,27,99,-28,48,114,-65,91,61,41,13,120,83,29,92,3,48,118,-62,-12,110,-101,32,70, + 38,87,84,24,111,-58,88,91,-11,-58,113,0,-54,54,-101,-89,-91,0,86,-93,-1,41,-109,-50,105,-72,0,1,63,110, + -123,22,20,-58,45,104,-120,-22,36,-124,-6,29,19,28,-122,6,16,-126,59,91,-66,66,108,0,65,109,0,42,-44,-112, + 7,94,85,-37,-84,-88,-42,-120,-3,-44,-119,2,-40,-89,-81,15,90,-89,-2,31,-123,-17,30,-122,12,-10,-126,31,-44,115, + -6,-94,-85,-36,-20,-120,-7,-127,0,29,-124,0,16,-95,-83,-10,-83,-96,23,122,-26,9,123,28,-12,80,-98,12,78,-99, + 8,127,0,-21,125,0,-23,-36,-119,-38,-40,-115,-2,17,-126,14,7,-126,35,74,-98,62,58,-94,80,99,0,55,115,0, + 22,93,-84,14,-72,-104,18,4,-126,12,3,-126,17,98,-79,44,119,-3,15,-48,-117,122,-34,0,56,-8,-114,-13,86,-93, + -117,-49,3,27,-124,-1,29,-124,0,-21,-35,-120,-47,-90,-77,40,33,-116,53,115,4,25,87,-89,3,32,-123,14,-126,0, + -4,-127,0,2,-76,-102,13,-84,-95,-5,-102,-75,-7,-49,-117,-31,-98,-75,7,-13,-126,18,34,-121,21,108,-63,22,59,110, + 54,115,1,-4,-127,1,-39,-121,4,-124,-26,0,-122,-36,-1,6,-29,-124,-12,-76,-101,-111,0,-62,98,-80,-10,-20,-87,-90, + -23,-124,-13,-6,-2,-127,-81,-98,0,34,112,-49,-15,-25,-124,51,-113,-29,33,67,103,3,37,-121,-6,-101,-77,0,-110,-64, + -11,-116,-51,-40,91,-79,-17,-123,-24,-12,-126,5,-28,-123,-11,-19,-124,-17,-26,-124,0,43,120,-4,29,123,8,-47,-118,4, + -14,-126,0,29,11,123,13,8,126,58,89,69,36,-49,112,21,93,-83,-30,-94,80,-35,-87,85,-11,28,123,-1,31,123, + 14,91,87,12,-10,126,33,-37,-117,-33,-79,94,-18,30,122,-9,-83,96,17,-95,83,8,117,49,-11,80,98,12,78,99, + -30,-33,119,-16,-34,121,-1,17,126,35,74,97,14,7,126,62,58,94,1,87,93,-6,-86,93,18,2,126,11,0,127, + 11,100,78,19,-78,98,56,-8,114,2,87,93,-117,-49,-3,-39,-84,87,-13,-37,121,41,33,116,120,41,-5,-2,34,122, + 15,89,89,14,-84,94,3,-76,102,-7,-100,78,-30,-92,82,-9,-49,117,7,-13,126,13,26,124,32,107,60,127,4,1, + 62,6,-111,7,-47,118,-11,-76,101,-111,0,62,81,-97,9,-17,-104,71,-6,-2,127,28,116,45,-13,-32,122,32,-120,27, + 71,24,-102,-7,38,121,-2,-117,50,-23,-116,45,-26,-115,46,-31,96,77,23,-20,123,48,14,117,16,6,126,-20,-103,-72, + 24,-61,-109,-30,-123,-10,38,-99,-70,59,-33,-107,37,-59,-107,-15,12,126,4,68,107,-25,37,119,52,114,22,17,79,-98, + 16,126,-10,74,48,-91,122,-6,33,82,84,-48,40,111,-46,47,118,7,57,-28,110,11,-8,126,2,8,127,60,-32,-107, + 12,-5,-126,27,-63,-107,-73,71,76,-80,85,50,-2,8,-127,-63,99,-49,-84,84,-45,-5,13,126,-13,118,46,-16,125,18, + -94,69,50,3,121,38,39,47,111,97,82,5,-19,27,-123,33,53,-110,54,47,-105,-114,-21,52,-8,13,126,-4,4,127, + -59,-104,43,12,9,126,-54,42,-107,-102,45,-61,-69,84,-65,-38,-58,-106,8,36,-122,-1,-37,-121,50,8,-117,72,70,-78, + 70,15,-105,84,-33,-90,28,22,-122,77,101,-8,-61,-72,86,-51,-106,-49,15,5,-126,-50,-71,-93,-11,-16,-126,-71,-56,-89, + -21,9,-125,-100,-31,-71,18,-124,-17,33,-120,-22,-23,-8,-125,-12,-70,-105,-103,-10,73,-120,31,29,52,96,-65,67,97,-48, + -29,87,-88,11,103,-74,60,111,13,-25,60,109,-10,106,70,-9,63,110,69,107,-7,-18,67,-106,-42,120,0,23,118,41, + 38,109,-53,9,120,-40,51,-116,-6,4,119,-45,83,96,0,34,-99,-71,29,91,84,37,-51,110,1,6,-127,65,-100,-44, + 43,-45,-111,-6,79,-99,9,99,-79,-94,-62,59,18,40,-119,45,66,-99,-42,32,-116,-41,120,0,-97,82,0,-57,96,-61, + 42,18,-119,62,50,-99,-4,41,-120,-63,-7,-110,-97,41,-71,25,92,-84,60,112,7,13,113,57,-57,50,102,-63,110,-1, + -87,-92,0,94,-85,-7,122,-37,0,58,-105,41,6,127,0,6,63,-110,-121,32,-20,-54,50,-104,-124,3,-29,-121,-12,-36, + -117,-20,46,-115,-19,-49,15,-24,-124,15,4,-126,24,5,-125,-19,-102,74,-26,-123,16,28,-62,107,38,-101,67,36,-69,100, + 63,-36,104,-8,27,-124,-16,121,-36,0,96,83,5,113,59,-36,113,46,-20,76,100,59,-40,-105,52,-34,-111,3,8,-127, + 10,-9,-126,56,-40,107,20,-66,107,11,-7,126,-67,74,-79,-70,94,-49,-2,8,127,-73,92,48,-57,98,57,-6,10,-126, + -13,117,-47,-43,118,20,119,-43,-9,52,-7,-115,-7,33,122,68,-13,107,42,28,116,6,14,-126,-95,80,-26,3,5,-127, + 15,-5,-126,-119,-9,-44,-19,72,103,-19,105,68,-56,90,70,-39,-51,110,2,-38,121,2,37,122,54,-20,113,74,-34,97, + 95,58,61,73,-63,83,27,18,123,-92,-1,-88,-114,-19,52,-83,-10,95,14,-9,126,-19,3,126,-80,40,90,-5,22,125, + -85,62,71,18,-124,17,33,-120,22,-12,-70,105,-23,-8,125,-103,-10,-73,-120,31,-29,52,96,65,-29,87,88,67,97,48, + 54,115,-2,11,100,78,-18,67,106,24,109,-61,11,123,30,35,116,39,29,-123,-7,4,119,45,35,-99,71,32,86,-88, + 31,-54,-111,0,4,127,44,-46,110,63,-100,46,16,102,74,0,82,97,-95,60,-59,42,4,120,78,-11,100,-32,47,114, + -57,96,61,41,12,119,76,36,95,10,44,119,-63,-7,110,-98,39,70,87,34,86,93,63,-59,127,3,-7,37,121,1, + -1,77,-101,-127,5,0,100,-79,-1,58,-102,-49,116,-53,0,6,63,110,-121,32,20,-54,50,104,-121,-12,36,-124,3,29, + 22,21,-123,9,12,-126,66,87,-65,75,102,0,74,103,0,43,-42,-112,9,94,85,-35,-90,-82,-41,-120,-1,-46,-119,2, + -43,-89,-80,16,88,-90,-2,31,-123,-21,32,-121,11,-11,-126,38,-39,115,-22,-87,-90,-36,2,-122,-2,-127,0,34,-122,0, + 20,-94,-83,-6,-83,-96,18,122,-30,3,126,18,-19,78,-98,8,79,-99,0,127,0,-33,123,0,-29,-38,-118,-43,-44,-111, + -3,17,-126,14,7,-126,32,75,-97,60,61,-94,76,102,0,49,117,0,17,89,-88,5,-74,-103,18,5,-126,12,4,-126, + 0,99,-80,38,121,-2,16,-53,-114,124,-29,0,56,-5,-114,-16,85,-93,-120,-40,3,25,-125,1,35,-122,0,-20,-38,-120, + -51,-92,-71,43,30,-116,72,104,4,22,86,-91,0,33,-123,29,-124,0,7,-127,0,10,-76,-101,23,-82,-94,-5,-104,-73, + -6,-50,-117,-30,-101,-71,8,-12,-126,17,28,-123,23,108,-63,36,52,110,81,98,1,-4,-127,4,-38,-121,7,-126,-16,0, + -124,-26,-1,7,-31,-123,-7,-77,-101,-110,9,-62,105,-71,-10,-16,-97,-80,-18,-126,-4,-6,-2,-127,-88,-91,0,33,112,-51, + -13,-27,-123,56,-110,-29,44,60,103,1,36,-122,-2,-109,-65,-9,-115,-54,-20,-117,-45,-35,93,-79,-15,-126,-7,-18,-125,8, + -30,-123,0,-19,-125,-3,-35,-122,0,44,119,-7,27,124,4,-48,-117,6,-8,-127,0,31,6,123,13,5,126,64,87,67, + 35,-49,112,18,93,-85,-30,-94,80,-36,-87,86,-14,30,123,-3,32,123,12,93,85,11,-11,126,39,-31,-117,-32,-73,99, + -19,29,122,-6,-83,96,20,-94,83,3,118,46,-19,78,98,8,79,99,-32,-34,118,-16,-35,121,-3,17,126,32,75,97, + 14,7,126,60,61,94,-4,88,92,-7,-80,98,18,2,126,11,1,127,-1,100,78,17,-76,101,56,-5,114,-6,85,94, + -120,-40,-3,-39,-84,87,-13,-37,121,42,30,116,126,-18,-3,-3,35,122,14,90,89,23,-82,94,11,-76,101,-8,-98,81, + -28,-91,84,-9,-47,118,9,-12,126,14,25,124,33,107,60,125,-24,3,62,-7,-111,7,-48,117,-7,-77,101,-110,9,62, + 95,-83,9,-15,-105,71,-6,-2,127,28,116,43,-13,-32,122,45,-116,27,74,-12,-102,-8,39,121,-3,-115,53,-22,-115,51, + -26,-113,51,45,91,76,25,-17,123,45,18,117,16,8,126,-16,-100,-76,23,-64,-107,-33,-122,-16,41,-97,-71,62,-33,-106, + 40,-54,-108,-7,22,125,9,104,72,-8,74,103,53,115,5,-3,75,-102,-10,124,-28,76,56,-85,122,16,32,89,87,-25, + 34,113,-48,39,121,7,60,-22,110,12,-7,126,1,8,127,63,-25,-108,12,-4,-126,29,-61,-107,-81,60,78,-89,75,51, + -4,8,-127,-78,87,-50,-94,73,-45,-7,10,126,-35,112,48,-36,120,23,-54,112,23,3,126,17,59,25,109,125,21,6, + -5,34,-122,43,33,-115,70,13,-105,-112,35,49,-2,16,126,-1,6,127,-81,-87,44,15,7,126,-30,65,-105,-63,93,-60, + -54,101,-55,-36,-49,-112,-3,35,-122,1,-38,-121,51,11,-116,83,63,-72,75,24,-100,89,-13,-89,26,27,-122,84,95,-1, + -69,-61,87,-72,-92,-50,16,2,-126,-57,-62,-95,-15,-10,-126,-85,-25,-91,-15,17,-125,-105,14,-71,9,-125,-18,25,-123,-22, + -24,-7,-125,-17,-69,-105,-104,-3,73,-117,39,29,59,92,-65,74,91,-47,-22,89,-88,-10,101,-76,36,121,13,-8,125,18, + -25,123,20,-23,118,42,69,106,-2,-29,64,-106,-59,112,0,-3,118,47,42,106,-56,-10,122,-35,65,-108,-10,14,118,-45, + 91,89,0,42,-97,-70,20,94,83,42,-49,110,-1,5,-127,82,-85,-47,51,-34,-111,-21,80,-97,-10,101,-76,-111,-21,59, + 33,29,-119,65,46,-99,-49,36,-111,-32,123,0,-91,88,0,-50,100,-61,40,25,-118,67,46,-98,12,43,-119,-63,-3,-110, + -94,47,-71,42,84,-85,89,90,8,29,109,58,-50,58,101,-50,117,-1,-99,-80,0,111,-61,-7,125,-23,-1,78,-90,44, + 16,126,0,12,63,-110,-119,41,-20,-50,53,-104,-123,12,-29,-122,-4,-36,-118,-12,46,-116,-11,-49,17,-23,-124,16,4,-126, + 24,6,-125,-17,-100,76,-32,-122,17,23,-64,107,37,-98,72,35,-60,106,62,-33,106,-8,24,-124,-12,92,-87,9,113,-58, + 93,85,16,-30,117,38,-10,73,104,69,-31,-102,62,-12,-110,0,8,-127,12,-7,-126,68,-15,106,35,-60,106,14,-3,126, + -91,41,-79,-103,55,-50,-5,7,127,-105,53,47,-91,68,56,-9,7,-126,-58,103,-47,-50,116,16,109,-64,-9,51,-16,-115, + -8,34,122,67,-22,106,57,12,113,9,12,-126,-87,88,-28,4,7,-127,4,-16,-126,-80,89,-42,-26,68,104,-34,99,71, + -61,91,65,-32,-47,114,0,-37,121,-1,36,122,54,-9,115,78,-17,99,93,58,64,75,-57,85,28,19,122,-48,78,-88, + -82,82,51,-51,65,96,-1,-17,126,-15,16,125,-57,82,79,-2,24,125,-75,77,67,9,-125,18,25,-123,22,-17,-69,105, + -24,-7,125,-104,-3,-73,-117,39,-29,59,92,65,-22,89,88,74,91,47,5,127,-3,-29,97,77,-28,64,106,18,110,-61, + 2,124,26,35,116,36,37,-122,-3,14,118,45,41,-97,70,33,87,-87,44,-43,-111,-2,4,127,59,-26,110,97,-68,46, + 11,104,72,-7,85,94,-67,92,-57,40,-5,120,56,-51,102,-37,44,113,-50,100,61,42,12,119,72,40,96,10,40,120, + -63,-3,110,-95,46,70,76,-53,87,105,-40,-59,68,-107,-5,121,38,1,62,43,-102,-63,110,1,122,-34,-1,94,-71,-48, + 127,-1,1,12,63,110,-119,41,20,-50,53,104,-122,-4,36,-123,12,29,28,12,-123,13,7,-126,70,85,-63,83,96,0, + 82,97,0,48,-33,-113,7,94,85,-34,-97,-75,-39,-121,0,-52,-116,3,-50,-88,-77,16,86,-92,-3,30,-123,-28,32,-120, + 11,-12,-126,45,-30,115,-25,-71,-102,-31,18,-122,-3,-127,0,34,-122,0,20,-94,-83,-6,-83,-96,13,119,-43,8,127,-5, + -27,76,-98,5,79,-99,-6,127,0,-46,118,0,-35,-39,-116,-41,-47,-111,-4,17,-126,14,8,-126,31,75,-97,60,61,-94, + 76,102,0,48,118,0,10,86,-93,-2,-70,-106,19,8,-125,12,7,-126,-33,104,-65,16,126,3,16,-59,-111,124,-29,0, + 56,-5,-114,-20,85,-92,-123,-32,3,26,-124,4,45,-119,0,-20,-40,-119,-55,-93,-67,45,28,-115,105,71,4,19,85,-93, + -2,33,-123,46,-118,0,18,-126,0,20,-74,-101,35,-78,-93,0,-102,-76,0,-45,-119,-28,-103,-69,10,-11,-126,15,24,-124, + 13,111,-60,54,33,110,114,56,0,-1,-127,8,-37,-121,11,-127,-7,0,-126,-17,-1,8,-35,-122,-4,-77,-101,-110,16,-62, + 111,-60,-10,-10,-105,-71,-6,-127,0,-7,-3,-127,-94,-85,0,33,111,-53,-11,-29,-123,63,-107,-29,63,41,103,-2,36,-122, + 3,-111,-62,-10,-112,-59,-14,-113,-57,-24,98,-77,-4,-127,1,-17,-126,2,-20,-125,0,-8,-127,-1,-41,-120,0,47,117,-11, + 29,124,-5,-53,-115,9,-4,-127,0,35,0,122,13,3,126,68,86,64,36,-49,111,12,91,-88,-31,-94,80,-40,-85,85, + -18,34,121,-5,34,122,9,95,84,11,-12,126,48,-14,-117,-21,-68,105,-23,27,122,-6,-83,96,20,-94,83,9,120,41, + -27,76,98,5,79,99,-33,-36,117,-19,-36,120,-3,17,126,32,76,97,14,8,126,60,61,94,2,88,92,0,-72,104, + 20,2,125,12,1,126,-21,98,78,22,-69,104,56,-5,114,-24,77,98,-123,-32,-3,-40,-84,87,-15,-37,120,42,28,116, + 96,-83,1,-4,35,122,17,90,88,35,-78,93,20,-74,101,-7,-95,84,-26,-90,86,-8,-45,119,11,-11,126,20,21,124, + 34,107,59,114,-55,12,57,-24,-111,11,-46,118,-4,-77,101,-110,16,62,119,-42,9,-9,-104,73,-6,-3,127,33,116,41, + -12,-31,123,73,-100,28,56,-51,-102,-6,39,121,6,-112,59,-9,-112,59,-14,-113,57,100,14,77,26,-12,124,32,22,121, + 14,12,126,-3,-97,-82,27,-65,-106,-23,-122,-25,60,-87,-70,69,-18,-105,52,-48,-105,-4,21,125,32,64,105,-4,63,110, + 83,-93,24,24,37,-119,14,116,-49,79,74,-67,107,21,64,87,93,5,18,115,-50,20,125,8,63,-8,110,13,-4,126, + 0,8,127,66,-12,-108,13,-2,-126,39,-57,-107,-91,39,79,-103,54,51,-5,6,-127,-99,61,-52,-108,50,-45,-10,6,126, + -68,95,51,-42,114,37,-49,113,32,-8,106,69,62,2,111,124,-28,8,2,32,-123,65,-46,-99,73,-14,-103,-101,61,46, + 2,16,126,1,6,127,-89,-79,45,16,5,126,-26,71,-102,-53,103,-52,-53,106,-46,-22,-39,-119,-12,37,-121,6,-39,-121, + 54,15,-114,85,77,-55,81,32,-93,92,7,-87,19,32,-121,59,112,10,-67,-61,89,-77,-87,-52,17,1,-126,-55,-63,-96, + -17,-6,-126,-87,-2,-93,-10,20,-125,-97,40,-71,11,-125,-18,28,-122,-22,-24,-7,-125,-16,-70,-105,-104,-5,73,-118,37,29, + 58,93,-64,73,93,-47,-23,89,-88,-41,91,-79,-7,126,13,0,114,56,-5,127,9,-12,115,54,32,123,5,-37,61,-105, + -72,105,0,-40,109,51,51,99,-61,-37,117,-31,86,-93,-4,12,118,-44,89,90,0,60,-86,-71,-2,97,82,51,-41,109, + -2,2,-127,103,-54,-51,59,-14,-112,-38,73,-96,-41,97,-72,-112,10,59,40,20,-119,74,30,-98,-44,40,-112,-34,122,0, + -93,87,0,-51,99,-61,33,34,-118,65,54,-95,25,24,-122,-63,-4,-110,-95,45,-71,46,81,-86,102,75,8,30,108,59, + -52,58,101,-53,115,-1,-98,-81,0,126,-17,-6,127,4,-1,101,-59,49,14,126,0,11,63,-109,-119,38,-20,-51,52,-104, + -123,9,-29,-122,-7,-36,-118,-14,46,-116,-14,-49,22,-19,-124,16,8,-126,22,8,-125,-10,-100,77,-28,-123,17,24,-64,107, + 51,-88,76,41,-48,110,64,-16,108,-10,27,-124,-11,116,-50,25,110,-58,94,-82,-25,-11,119,42,11,54,114,76,-23,-99, + 62,22,-109,-3,7,-127,16,-1,-126,66,21,107,54,-44,106,14,4,126,-98,-11,-80,-116,-10,-51,-9,2,127,-118,-11,46, + -114,12,55,-10,2,-127,-102,60,-48,-47,112,36,89,-90,-9,49,-29,-114,-10,38,121,66,-32,104,79,-22,97,12,9,-126, + -76,99,-25,7,6,-127,-2,-16,-126,-42,112,-42,-27,67,104,-34,95,77,-60,93,62,-25,-45,116,2,-36,122,-2,37,122, + 53,4,115,79,-5,99,74,81,65,81,-50,84,28,20,122,-17,90,-88,-47,107,51,-25,79,97,-7,-15,126,-8,22,125, + -26,99,75,3,26,124,-62,93,61,11,-125,18,28,-122,22,-16,-70,105,-24,-7,125,-104,-5,-73,-118,37,-29,58,93,64, + -23,89,88,73,93,47,-60,112,-3,-75,68,76,-36,61,105,-2,113,-58,-19,124,18,38,117,31,62,-111,-2,12,118,45, + 59,-87,72,21,92,-85,58,-21,-111,-5,2,127,62,7,111,118,-9,47,-4,107,68,-21,89,88,-33,109,-56,33,-16,122, + 32,-65,104,-35,47,113,-51,99,61,42,15,119,56,59,97,8,37,121,-63,-4,110,-96,44,70,52,-77,87,84,-74,-59, + 22,-125,-5,127,-6,1,73,18,-102,-20,125,1,123,32,1,117,-14,-46,109,65,2,11,63,109,-119,38,20,-51,52,104, + -122,-7,36,-123,9,29,43,-9,-119,18,-2,-126,64,91,-61,82,97,0,80,98,0,55,-19,-113,-6,95,84,-44,-92,-76, + -43,-119,0,-61,-111,4,-59,-86,-73,21,81,-95,-1,27,-124,-29,25,-121,11,-12,-126,52,-11,115,-16,-52,-115,-27,25,-122, + -13,-126,0,24,-125,0,12,-95,-83,-12,-83,-96,30,112,-52,34,122,-7,-33,74,-98,6,80,-99,-7,127,0,-56,114,0, + -36,-38,-116,-31,-48,-113,-4,17,-126,14,7,-126,36,74,-97,64,56,-94,83,96,0,54,115,0,21,83,-94,-8,-56,-114, + 18,14,-125,11,10,-126,-48,103,-56,-5,127,9,13,-61,-110,121,-39,0,55,-10,-114,-34,81,-92,-122,-34,3,48,-117,8, + 67,-108,0,-27,-37,-118,-54,-94,-66,42,30,-116,118,46,4,32,80,-94,2,32,-123,62,-111,0,25,-124,0,27,-71,-101, + 45,-72,-94,17,-100,-76,9,-41,-120,-19,-106,-67,11,-9,-126,23,15,-124,-12,113,-57,61,17,110,125,24,0,16,-125,13, + -30,-123,14,-127,-10,0,-125,-20,-1,7,-36,-122,-4,-78,-100,-110,14,-62,121,-37,-9,-14,-101,-76,-6,-127,-3,-7,-3,-127, + -93,-87,0,47,103,-57,-14,-27,-123,80,-95,-28,70,26,102,1,35,-122,-3,-104,-73,-9,-100,-78,1,-102,-76,-27,99,-75, + -2,-127,-9,-8,-126,-9,-7,-126,-12,-1,-126,-13,-37,-122,0,54,113,-17,43,119,-8,-56,-114,11,-8,-127,0,46,-17,117, + 14,-1,126,65,89,63,45,-45,110,-7,88,-92,-38,-92,79,-47,-83,84,-25,36,119,-9,35,122,3,98,81,11,-12,126, + 50,13,-116,-14,-66,108,-22,31,121,-12,-83,96,12,-95,83,30,117,40,-33,74,98,7,80,99,-29,-40,117,-23,-37,119, + -3,17,126,36,74,97,15,7,126,65,56,94,16,83,95,3,-70,106,21,4,125,14,2,126,-28,95,80,27,-73,101, + 56,-10,114,-51,53,104,-122,-34,-3,-40,-85,86,-20,-37,120,40,29,117,61,-111,1,-3,35,122,24,89,88,46,-72,94, + 28,-71,101,7,-92,88,-19,-89,88,-1,-43,120,12,-9,126,34,10,122,20,111,58,87,-89,23,45,-42,-111,13,-50,116, + -3,-78,100,-110,14,62,125,22,10,-10,-103,74,-7,-3,127,45,113,36,-14,-31,122,106,-64,29,35,-68,-101,-3,39,121, + 9,-112,60,-3,-111,61,-3,-113,58,99,-22,77,29,0,124,26,39,118,8,18,125,7,-98,-81,32,-62,-106,-10,-125,-22, + 72,-87,-58,78,-23,-98,77,-59,-82,-4,21,125,41,37,114,-2,60,112,93,-85,18,63,6,-110,67,88,-62,61,89,-67, + 93,46,74,77,99,20,6,106,-69,32,117,-38,58,22,111,13,2,126,-5,6,127,64,22,-108,13,5,-126,61,-32,-106, + -97,-11,81,-117,-4,50,-7,4,-127,-115,-2,-54,-118,-9,-46,-11,-2,127,-109,36,54,-42,117,25,-44,117,23,-3,107,68, + 58,-7,113,119,-44,8,2,30,-123,67,-47,-97,71,-22,-103,-94,74,42,5,15,126,3,5,127,-93,-71,49,18,4,126, + -22,70,-104,-51,103,-55,-46,104,-56,-18,-39,-120,-12,38,-121,9,-38,-121,62,16,-110,97,74,-34,78,46,-89,84,37,-88, + 8,36,-122,64,108,17,-61,-65,90,-78,-83,-56,18,1,-126,-48,-66,-97,-18,-3,-126,-83,18,-94,-7,21,-125,-89,55,-72, + 24,-123,-18,40,-118,-22,-23,-10,-125,-8,-71,-105,-103,-16,73,-121,24,29,48,99,-64,63,100,-47,-33,86,-88,-84,48,-82, + -81,97,13,6,105,71,51,114,25,22,94,82,44,118,11,-36,60,-106,-72,105,0,-53,112,27,61,90,-66,-13,109,-65, + 114,-56,-2,-1,119,-44,79,99,0,59,-88,-70,-34,93,80,63,-13,109,-2,0,-127,114,17,-54,54,27,-112,-29,59,-109, + -53,80,-83,-106,37,59,43,11,-119,80,16,-98,-40,40,-114,-47,118,0,-102,76,0,-62,93,-61,18,42,-118,82,48,-85, + 26,19,-123,-62,-11,-110,-99,35,-71,48,80,-86,111,61,8,25,110,59,-57,55,99,-63,110,-1,-90,-89,0,108,66,-5, + 110,64,0,114,12,54,0,127,0,4,64,-109,-123,25,-20,-56,46,-104,-124,-4,-29,-120,-20,-36,-115,-27,46,-114,-26,-49, + 30,-12,-123,17,13,-125,22,14,-124,1,-102,76,-14,-125,17,32,-61,107,55,-86,76,45,-45,110,64,-18,108,-6,31,-123, + 0,120,-40,56,113,-11,117,41,26,27,112,53,35,58,108,89,2,-90,57,41,-106,0,8,-127,20,5,-125,52,38,109, + 61,-29,108,7,7,127,-87,-39,-84,-105,-44,-56,-15,-2,126,-111,-47,41,-114,-24,50,-5,1,-127,-113,27,-52,-38,118,29, + 70,-106,-8,43,-38,-113,-3,42,120,60,-38,105,70,1,106,14,6,-126,-52,114,-24,9,3,-127,-9,-14,-126,6,120,-42, + -22,69,104,-35,99,72,-48,103,57,-24,-49,115,6,-37,121,-6,37,121,53,11,115,80,14,98,78,76,66,88,-27,87, + 23,26,122,21,89,-88,0,117,50,10,82,97,-13,-11,126,1,24,125,11,103,73,10,26,124,-36,107,58,24,-123,18, + 40,-118,22,-8,-71,105,-23,-10,125,-103,-16,-73,-121,24,-29,48,99,64,-33,86,88,63,100,47,-91,89,-5,-95,41,73, + -35,60,106,-27,104,-68,-40,121,3,38,120,20,90,-90,3,-1,119,45,57,-88,71,-3,94,-85,65,-2,-109,-11,-1,127, + 53,24,113,113,26,52,-28,107,63,-38,85,86,6,114,-55,26,-25,122,5,-71,105,-40,45,112,-62,93,61,37,24,119, + 58,59,96,15,35,121,-62,-11,110,-101,33,70,15,-91,87,47,-102,-59,-33,-122,-5,113,-57,1,74,-13,-102,33,123,1, + 107,68,6,118,23,-42,85,94,7,4,64,109,-123,25,20,-56,46,104,-120,-20,36,-124,-4,29,45,-17,-118,19,-5,-125, + 54,97,-62,71,105,0,69,106,0,58,1,-113,-46,84,84,-47,-89,-78,-50,-117,0,-54,-115,4,-55,-91,-70,37,71,-98, + 6,23,-125,-23,14,-124,12,-11,-126,46,24,116,-16,-50,-115,-24,26,-122,-14,-126,0,24,-125,0,12,-95,-83,-12,-83,-95, + 47,105,-53,51,116,3,-37,72,-98,0,80,-98,-20,125,0,-62,111,0,-32,-41,-116,-25,-56,-111,-5,17,-126,14,7,-126, + 34,75,-96,64,57,-94,83,96,0,49,117,0,37,78,-93,-17,-51,-115,11,19,-125,7,12,-126,-47,98,-66,-14,126,6, + -2,-59,-113,121,-38,0,55,-9,-114,-65,56,-93,-118,-47,3,54,-114,14,63,-110,0,-28,-37,-118,-49,-101,-59,39,34,-116, + 125,24,4,42,75,-94,8,30,-123,60,-112,0,36,-122,0,29,-71,-102,44,-73,-94,37,-109,-54,23,-48,-115,-7,-118,-46, + 11,-10,-126,30,9,-123,-51,100,-60,63,4,110,127,-1,1,28,-122,21,-19,-124,19,-125,-23,0,-123,-33,-1,1,-35,-122, + 5,-78,-100,-111,2,-62,124,25,-9,-33,-92,-81,-38,-121,-3,-6,-3,-127,-83,-96,0,57,96,-60,-19,-28,-122,115,-46,-28, + 75,13,102,6,33,-122,-31,-95,-78,-23,-96,-80,7,-104,-72,-36,98,-73,-37,-121,-12,7,-127,-6,1,-127,-11,-16,-125,-12, + -23,-125,0,59,109,-30,52,116,-7,-48,-117,12,2,-127,0,37,-9,121,13,0,126,56,95,63,53,-33,111,-26,82,-93, + -39,-92,79,-43,-87,82,-31,31,119,-12,33,122,-3,100,79,12,-11,126,49,29,-114,-18,-66,107,-22,30,121,-12,-83,95, + 12,-95,83,41,113,40,-37,72,98,0,80,98,-26,-44,116,-23,-40,118,-5,17,126,35,75,96,15,7,126,64,57,94, + 23,80,96,-12,-65,108,16,7,126,12,5,126,-36,96,75,4,-64,110,56,-9,114,-68,32,102,-118,-47,-3,-36,-90,82, + -19,-39,119,38,33,117,16,-126,2,-4,34,122,24,90,87,44,-73,94,29,-71,101,15,-92,86,-10,-92,86,4,-42,120, + 12,-10,126,31,12,123,-10,113,57,51,-113,28,28,-55,-111,2,-42,120,6,-78,100,-111,2,62,112,58,15,-30,-97,76, + -5,-3,127,49,114,28,-17,-32,122,118,-33,33,7,-76,-101,-2,37,121,-24,-106,66,-21,-108,63,3,-114,56,82,-60,77, + 29,9,123,17,47,117,3,18,126,8,-97,-81,33,-62,-106,-9,-125,-23,77,-84,-57,79,-26,-96,85,-55,-77,-3,21,125, + 47,39,112,1,63,110,104,-71,17,58,23,-111,57,100,-54,48,96,-68,82,67,70,67,105,23,1,105,-72,39,110,-50, + 45,42,111,12,6,126,-7,4,127,50,45,-108,10,10,-126,69,-4,-107,-84,-48,83,-105,-51,50,-8,1,-127,-100,-54,-57, + -105,-55,-46,-7,-8,127,-111,-23,57,-49,117,8,-44,118,15,-1,118,47,54,-17,113,110,-63,8,3,31,-123,66,-35,-103, + 67,-31,-103,-81,90,39,8,13,126,4,4,127,-120,-10,41,15,-5,126,-22,69,-104,-49,103,-55,-48,102,-58,-18,-40,-119, + -14,38,-121,9,-38,-121,62,22,-109,97,77,-29,70,60,-87,73,54,-89,2,37,-122,66,106,22,-94,8,85,-116,-19,-47, + 13,-9,-126,-87,-1,-93,-18,3,-126,-74,43,-94,-1,23,-125,-72,75,-73,25,-123,-18,42,-118,-22,-23,-10,-125,-7,-71,-105, + -102,-17,73,-122,23,29,47,99,-64,62,101,-47,-33,86,-88,-94,-1,-85,-120,41,12,19,107,66,45,114,34,37,87,84, + 52,115,13,-31,61,-107,-66,109,0,-76,99,24,72,83,-63,-6,106,-70,126,-15,-5,-2,119,-44,78,100,0,54,-91,-70, + -61,75,82,62,13,110,0,-1,-127,89,70,-58,31,52,-112,-34,52,-111,-69,66,-84,-88,70,59,44,-2,-119,79,-13,-99, + -43,39,-113,-48,118,0,-102,75,0,-62,92,-61,9,44,-119,88,46,-79,26,20,-123,-62,-12,-110,-99,34,-71,90,31,-83, + 126,13,7,100,53,57,9,75,102,55,114,-1,-126,17,0,54,115,-4,77,101,0,91,67,58,-1,127,0,3,64,-109, + -123,24,-20,-57,46,-104,-124,-5,-29,-120,-21,-36,-115,-28,46,-114,-27,-49,32,-5,-123,15,16,-125,19,17,-124,6,-99,79, + -13,-125,17,33,-61,107,60,-85,73,50,-44,108,65,-21,107,2,32,-123,12,108,-65,76,102,-3,111,51,36,17,115,50, + 31,63,106,88,21,-89,48,53,-105,0,9,-127,20,10,-125,42,47,110,65,-17,108,4,7,127,-76,-55,-85,-93,-65,-58, + -15,-7,126,-99,-69,39,-107,-48,49,-4,1,-127,-115,2,-54,-30,123,14,46,-118,-8,33,-45,-114,8,41,120,51,-48,106, + 65,2,109,15,2,-126,-22,123,-24,9,1,-127,-12,-12,-126,35,115,-42,-17,70,105,-42,102,62,-31,111,53,-23,-45,116, + 8,-35,122,-11,36,121,52,17,115,77,29,96,78,76,66,92,-8,87,21,30,122,42,81,-88,28,113,50,29,77,97, + -15,-8,126,6,23,125,36,97,73,17,23,124,-7,113,57,25,-123,18,42,-118,22,-7,-71,105,-23,-10,125,-102,-17,-73, + -122,23,-29,47,99,64,-33,86,88,62,101,47,-108,66,-6,-103,19,72,-30,61,107,-45,98,-68,-53,115,2,48,116,19, + 106,-70,4,-2,119,45,53,-91,71,-22,92,-85,65,12,-108,-12,-5,126,45,34,114,103,50,54,-43,101,64,-47,79,88, + 35,109,-55,18,-30,122,-13,-70,105,-48,42,110,-62,92,61,34,31,118,60,58,95,22,30,121,-62,-12,110,-101,32,70, + -7,-92,87,20,-110,-59,-61,-111,-5,96,-83,1,69,-31,-102,61,111,1,89,90,8,110,49,-40,64,109,9,3,64,109, + -123,24,20,-57,46,104,-120,-21,36,-124,-5,29,42,-12,-119,18,-3,-126,53,98,-62,70,106,0,68,107,0,58,14,-112, + -73,58,86,-46,-90,-77,-50,-117,-1,-53,-115,3,-54,-93,-68,48,68,-96,8,23,-125,-24,10,-124,12,-10,-126,25,45,116, + -17,-51,-115,-25,25,-122,8,-127,0,44,-119,0,28,-92,-83,2,-84,-95,47,107,-49,43,120,6,-41,69,-98,-14,79,-98, + -41,120,0,-67,108,0,-32,-44,-115,-21,-61,-110,-8,16,-126,13,9,-126,21,80,-96,53,67,-94,65,109,0,29,124,0, + 36,78,-93,-18,-51,-115,6,19,-125,5,13,-126,-53,94,-67,-23,125,1,-3,-57,-114,126,-17,0,56,0,-114,-80,24,-95, + -117,-48,3,60,-111,14,58,-113,0,-28,-39,-118,-46,-104,-57,39,34,-116,125,-23,4,47,73,-93,9,30,-123,53,-116,0, + 48,-118,0,29,-71,-101,39,-76,-94,46,-107,-49,30,-46,-115,0,-120,-41,11,-11,-126,29,11,-123,-79,78,-62,60,-16,111, + 120,-41,1,35,-121,19,-14,-125,16,-125,-24,0,-122,-34,-1,0,-35,-122,19,-75,-101,-111,1,-62,104,72,-9,-33,-92,-81, + -39,-121,-3,-4,-4,-127,-82,-97,0,65,93,-57,-19,-29,-122,124,5,-28,73,-15,103,7,34,-122,-33,-95,-78,-25,-96,-79, + 8,-104,-72,57,82,-78,-40,-120,-10,9,-127,-6,4,-127,-6,-17,-126,-8,-21,-125,0,69,103,-30,59,112,-6,-46,-118,9, + 25,-124,0,33,-4,122,13,1,126,54,98,61,59,-23,110,-40,75,-94,-40,-92,77,-45,-89,78,-32,27,120,-11,30,123, + 1,97,82,12,-10,126,42,40,-113,-20,-61,110,-24,27,122,2,-84,95,28,-92,83,34,115,41,-41,69,99,-13,79,98, + -25,-48,115,-27,-41,117,-8,16,126,22,80,96,13,9,126,54,67,94,17,83,95,-15,-59,111,14,10,126,12,8,126, + -51,97,64,-1,-58,113,56,0,114,-74,16,102,-117,-48,-3,-37,-94,76,-21,-40,119,37,34,117,-16,-126,2,-4,33,122, + 26,88,87,39,-76,94,30,-71,101,23,-93,83,-6,-97,82,10,-41,120,11,-10,126,26,16,123,-30,110,56,19,-122,29, + 13,-60,-111,1,-39,121,19,-75,101,-111,1,62,98,79,17,-31,-98,75,-4,-4,127,55,111,26,-18,-32,122,122,-9,35, + -12,-76,-101,-2,37,121,-30,-103,69,-24,-105,67,7,-110,63,64,-78,77,29,13,123,12,40,120,1,16,126,3,-97,-82, + 32,-63,-105,-15,-124,-24,75,-84,-59,77,-32,-96,81,-51,-83,0,22,125,42,65,101,5,59,112,118,17,43,10,69,-106, + -5,126,-18,48,113,-34,77,62,79,69,102,30,-3,111,-61,30,119,-32,37,47,112,11,9,126,-7,3,127,43,51,-108, + 8,11,-126,68,14,-106,-76,-58,84,-96,-67,50,-8,-1,-127,-87,-72,-59,-96,-69,-47,-3,-9,127,-100,-49,60,-54,114,18, + -41,118,23,7,112,59,48,-30,114,89,-90,8,8,32,-123,54,-3,-115,57,-43,-105,-55,108,38,12,9,126,4,3,127, + -78,94,34,6,-13,126,-20,73,-102,-41,109,-51,-56,105,-45,-24,-43,-117,-17,36,-121,9,-38,-121,63,22,-108,102,72,-24, + 75,60,-83,74,54,-88,0,37,-122,75,100,23,-17,106,68,-77,94,-39,-1,-13,-126,-22,99,-76,-15,12,-126,-48,76,-90, + 7,22,-125,-44,95,-72,19,-124,-18,35,-120,-22,-23,-9,-125,-11,-70,-105,-103,-12,73,-120,29,29,52,97,-64,67,97,-47, + -29,87,-88,-89,-24,-88,-126,9,11,25,102,72,-14,121,36,18,99,77,64,109,12,-26,63,-107,-57,113,0,-92,79,38, + 81,80,-56,-32,108,-59,126,10,-6,4,119,-44,83,96,0,46,-95,-70,-78,51,87,56,24,112,2,-2,-127,69,88,-60, + 15,58,-112,-47,50,-107,-81,59,-78,-49,101,59,39,-19,-119,66,-49,-97,-57,37,-107,-41,120,0,-98,81,0,-57,95,-61, + 6,44,-119,93,41,-76,26,29,-121,-63,-8,-110,-97,39,-71,77,-76,-67,115,-54,5,75,-93,43,75,-5,103,111,-62,-1, + -10,127,1,19,126,-3,64,110,0,71,86,61,6,127,0,7,64,-109,-122,30,-20,-54,49,-104,-124,2,-29,-121,-14,-36, + -116,-22,46,-115,-21,-49,31,-1,-123,11,17,-125,17,21,-124,-1,-100,79,-20,-124,17,29,-62,107,63,-85,70,55,-45,106, + 66,-27,105,3,32,-123,22,110,-59,50,115,20,19,119,39,-32,119,30,-11,83,96,88,34,-85,36,59,-107,-4,7,-127, + 16,12,-125,34,56,109,69,-4,107,5,9,127,-65,-70,-84,-79,-82,-55,-11,-8,126,-83,-86,42,-94,-68,52,-5,-2,-127, + -113,-24,-52,-25,123,19,34,-122,-9,29,-46,-115,8,42,120,47,-50,107,48,23,115,15,2,-126,-18,124,-23,8,1,-127, + -10,-13,-126,23,117,-43,-15,71,104,-43,101,64,-26,111,55,-28,-47,115,6,-37,122,-9,36,121,53,20,114,76,38,95, + 83,72,63,92,7,87,18,33,121,33,86,-88,16,115,51,21,80,96,-14,-10,126,5,22,125,32,98,74,17,22,124, + -5,113,58,19,-124,18,35,-120,22,-11,-70,105,-23,-9,125,-103,-12,-73,-120,29,-29,52,97,64,-29,87,88,67,97,47, + -120,41,-5,-102,-3,75,-25,63,107,-59,95,-60,-63,110,8,66,105,27,116,-50,5,4,119,45,46,-95,71,-36,89,-84, + 60,22,-110,-8,-5,127,37,45,113,90,72,53,-55,94,66,-53,73,90,33,109,-56,18,-31,122,-9,-71,105,-53,38,109, + -57,95,61,30,35,118,67,54,94,22,35,120,-63,-8,110,-99,38,70,4,-93,87,32,-108,-59,-43,-119,-5,104,-73,1, + 71,-23,-102,49,117,1,66,109,5,95,71,-44,42,120,6,7,64,109,-122,30,20,-54,49,104,-121,-14,36,-124,2,29, + 33,2,-123,15,2,-126,59,94,-61,75,102,0,74,103,0,58,22,-111,-87,28,88,-44,-95,-72,-44,-119,-1,-59,-112,2, + -57,-90,-69,50,68,-95,7,23,-125,-26,10,-124,12,-11,-126,11,50,116,-20,-55,-113,-28,23,-122,29,-124,0,63,-110,0, + 42,-86,-83,16,-82,-95,27,118,-38,7,127,-1,-43,67,-99,-25,77,-98,-56,114,0,-68,107,0,-32,-44,-115,-18,-58,-112, + -10,15,-126,11,11,-126,8,82,-96,42,74,-94,47,118,0,10,127,0,18,82,-96,-12,-58,-112,4,18,-126,5,13,-126, + -65,95,-53,-22,125,3,6,-58,-113,127,4,0,55,9,-114,-83,0,-96,-120,-42,3,61,-111,10,48,-118,0,-27,-41,-117, + -45,-102,-61,41,32,-116,102,-75,4,43,73,-94,7,30,-123,44,-119,0,54,-115,0,28,-72,-101,33,-79,-94,46,-103,-59, + 28,-41,-117,1,-116,-51,10,-12,-126,20,21,-124,-96,54,-62,48,-39,111,93,-87,2,37,-121,13,-14,-126,10,-126,-18,0, + -124,-28,-1,5,-35,-122,29,-72,-101,-110,7,-62,91,88,-9,-19,-103,-72,-14,-126,-3,-3,-4,-127,-87,-93,0,68,93,-52, + -14,-29,-123,119,33,-29,60,-49,100,3,35,-122,-11,-105,-71,-15,-101,-75,4,-102,-76,78,-60,-80,-13,-126,-7,1,-127,-10, + -2,-127,-9,-4,-127,-10,-29,-124,0,81,96,-21,62,111,-7,-50,-117,5,43,-120,0,31,8,123,14,4,126,61,93,61, + 63,-13,109,-54,65,-95,-41,-93,76,-54,-88,74,-33,23,120,-8,28,124,15,90,88,12,-11,126,30,47,-114,-22,-66,106, + -27,24,122,16,-82,95,43,-86,83,9,120,40,-43,67,99,-24,77,98,-24,-50,114,-33,-41,115,-10,15,126,9,82,96, + 12,11,126,42,74,94,-4,91,89,-7,-68,107,15,13,125,12,10,126,-55,95,64,11,-66,108,56,9,114,-73,2,104, + -120,-42,-3,-43,-97,70,-26,-39,118,41,32,116,-9,-127,1,0,33,123,32,85,89,33,-79,94,28,-72,101,27,-95,80, + -4,-101,76,14,-40,120,10,-12,126,13,27,123,-46,104,58,19,-123,27,13,-60,-111,6,-44,119,29,-72,101,-110,7,62, + 81,96,14,-14,-104,72,-2,-4,127,64,106,30,-16,-31,122,122,13,33,-8,-76,-101,-6,39,121,-5,-111,62,-12,-111,61, + 2,-111,62,72,-71,77,27,19,123,6,35,122,-2,14,126,-2,-98,-81,30,-64,-105,-21,-123,-23,63,-88,-67,66,-45,-98, + 76,-48,-89,1,23,125,21,89,88,5,55,114,-80,98,13,-21,82,-94,-32,122,-11,42,116,32,68,61,88,73,92,48, + -7,118,-46,6,127,-2,37,46,112,11,10,126,-7,3,127,43,50,-109,7,11,-126,66,26,-105,-77,-55,85,-98,-65,49, + -8,-1,-127,-84,-73,-61,-97,-66,-48,-1,-10,127,-96,-56,62,-48,113,32,-33,119,29,16,99,78,40,-37,115,66,-108,9, + 10,34,-122,24,40,-118,48,-50,-106,-38,116,36,14,6,126,5,3,127,-18,121,33,-1,-14,126,-15,76,-101,-31,113,-50, + -54,109,-36,-32,-47,-114,-16,35,-121,8,-39,-121,68,22,-105,113,57,-14,80,64,-75,69,61,-87,-4,36,-122,96,78,29, + 54,98,60,-14,121,-36,-7,-9,-126,47,96,-68,-10,18,-125,-23,90,-86,11,21,-125,-26,102,-71,14,-125,-18,31,-121,-22, + -24,-8,-125,-14,-70,-105,-104,-8,73,-119,34,29,56,95,-64,71,95,-47,-25,88,-88,-85,-29,-89,-127,-1,10,18,98,79, + -39,118,25,-12,110,63,91,87,14,-17,66,-107,-44,119,0,-107,43,52,88,76,-50,-82,89,-37,121,38,-7,9,119,-44, + 87,93,0,26,-103,-70,-88,23,89,53,30,112,3,-2,-127,63,91,-62,9,60,-112,-67,39,-100,-98,41,-70,-21,111,58, + 33,-27,-120,49,-69,-95,-67,37,-101,-37,121,0,-95,84,0,-54,97,-61,1,43,-119,104,28,-68,25,39,-118,-63,-6,-110, + -96,43,-71,19,-111,-59,83,-96,3,-4,-122,36,51,-55,102,35,-122,-1,70,106,1,6,127,-3,67,108,0,64,89,63, + 11,127,0,9,64,-109,-120,35,-20,-53,51,-104,-123,6,-29,-121,-10,-36,-117,-17,46,-116,-17,-49,32,2,-123,8,17,-126, + 19,28,-122,-10,-103,73,-25,-123,17,26,-64,107,57,-92,66,56,-50,102,61,-42,103,-3,31,-123,19,122,-28,8,121,37, + -38,118,28,-45,116,26,-29,86,88,90,54,-71,23,59,-110,-7,0,-127,12,8,-126,26,65,106,70,11,105,6,16,126, + -54,-83,-80,-65,-97,-49,-8,-3,127,-67,-97,48,-78,-82,58,-6,-9,-126,-106,-49,-49,-34,119,29,67,-107,-9,41,-35,-115, + -2,43,119,55,-32,110,18,58,112,14,6,-126,-48,115,-25,7,3,-127,-5,-15,-126,-18,118,-42,-20,70,104,-38,98,72, + -45,103,59,-36,-58,107,3,-41,120,-2,35,122,54,18,114,73,42,95,94,57,63,88,18,90,13,34,122,5,93,-87, + -23,114,51,-5,84,95,-9,-13,126,-3,22,125,0,102,76,10,25,124,-33,107,59,14,-125,18,31,-121,22,-14,-70,105, + -24,-8,125,-104,-8,-73,-119,34,-29,56,95,64,-25,88,88,71,95,47,-126,14,-3,-97,-21,79,-18,66,107,-67,96,-49, + -67,107,15,80,93,32,122,-33,9,9,119,45,27,-103,70,-43,87,-82,54,25,-112,-4,-1,127,27,58,110,74,91,49, + -63,87,68,-57,67,91,0,114,-56,26,-25,122,14,-70,105,-54,32,111,-54,97,61,25,36,119,79,42,90,12,47,117, + -63,-6,110,-98,41,70,34,-87,86,63,-93,-59,4,-127,-5,121,-40,0,75,-1,-103,11,127,0,39,121,-2,77,87,-52, + 22,125,0,9,64,109,-120,35,20,-53,51,104,-121,-10,36,-124,6,29,28,12,-123,14,5,-126,64,91,-61,79,99,0, + 78,100,0,56,33,-109,-93,4,86,-42,-98,-69,-40,-120,-1,-64,-110,0,-59,-88,-71,50,69,-94,4,22,-125,-29,7,-123, + 12,-11,-126,5,50,116,-24,-63,-108,-30,23,-121,44,-119,0,76,-101,0,53,-80,-83,26,-80,-96,15,121,-35,-4,127,-10, + -41,68,-99,-31,74,-98,-65,109,0,-65,109,0,-30,-44,-115,-14,-54,-114,-11,14,-126,10,12,-126,-1,82,-97,33,79,-94, + 32,123,0,-4,127,0,8,83,-96,-8,-66,-108,2,18,-126,5,13,-126,-67,98,-47,-5,127,8,13,-60,-111,126,19,0, + 54,16,-114,-85,-13,-94,-121,-38,3,52,-116,4,25,-124,0,-26,-42,-117,-41,-99,-68,43,30,-115,77,-101,5,41,74,-95, + 6,31,-123,30,-123,0,54,-115,0,24,-74,-101,24,-83,-93,42,-97,-70,27,-35,-119,2,-111,-62,10,-14,-126,13,27,-123, + -109,25,-60,38,-49,111,71,-106,2,37,-121,5,-13,-126,3,-126,-13,0,-125,-23,-1,8,-36,-122,34,-70,-101,-110,11,-62, + 90,89,-10,-7,-109,-65,4,-127,-5,-1,-4,-127,-90,-89,0,71,93,-49,-11,-30,-123,114,48,-30,44,-67,99,1,36,-122, + 3,-109,-66,-9,-106,-69,-1,-104,-73,12,-98,-79,5,-127,-10,-6,-126,-10,-6,-126,-15,3,-126,-16,-34,-122,0,91,88,-14, + 66,108,-9,-53,-115,0,53,-115,0,23,24,123,13,11,126,68,86,64,64,-4,110,-65,52,-96,-43,-93,76,-60,-87,71, + -35,20,120,-3,25,124,30,82,92,11,-11,126,17,46,-117,-26,-82,93,-30,17,122,26,-80,96,53,-80,83,0,121,38, + -41,68,99,-31,74,98,-25,-53,113,-37,-41,114,-11,14,126,-1,82,97,10,12,126,33,79,94,-11,93,86,4,-82,97, + 14,17,125,9,10,126,-42,93,76,25,-71,102,54,16,114,-70,-5,106,-121,-38,-3,-47,-99,65,-29,-39,118,46,30,114, + 31,-123,0,4,32,123,39,81,90,24,-83,94,24,-74,101,28,-98,76,-3,-105,71,15,-42,119,9,-13,126,4,34,122, + -57,98,58,53,-113,25,29,-54,-111,12,-46,118,34,-70,101,-110,11,62,65,109,8,0,-107,68,-1,-4,127,72,100,32, + -14,-30,123,119,33,29,16,-74,-102,-7,39,121,17,-114,53,-3,-118,46,-6,-116,51,89,-47,77,23,25,122,-1,34,122, + -5,13,126,-1,-98,-81,31,-63,-106,-19,-124,-22,63,-85,-69,59,-51,-100,83,-41,-87,0,24,125,10,100,77,3,56,114, + -96,83,-1,-29,81,-93,-41,119,-19,33,102,68,56,64,94,62,83,74,-28,108,-60,0,125,-23,37,45,113,9,12,126, + -7,3,127,43,48,-109,7,10,-126,62,35,-105,-78,-53,85,-100,-62,49,-8,0,-127,-83,-74,-62,-99,-64,-48,0,-10,127, + -93,-59,64,-43,113,38,-34,119,30,14,99,78,41,-36,115,68,-107,9,9,34,-122,15,45,-118,49,-49,-106,-41,114,36, + 13,7,126,6,5,127,5,123,32,-3,-13,126,-15,75,-101,-33,112,-50,-53,109,-36,-31,-48,-113,-15,35,-121,8,-38,-121, + 74,28,-99,113,58,4,81,79,-59,54,78,-85,-12,34,-122,93,72,49,78,82,57,11,122,-35,-9,-7,-126,71,82,-66, + -8,20,-125,-18,94,-84,10,21,-125,-30,101,-71,17,-125,-18,33,-121,-22,-23,-8,-125,-12,-70,-105,-103,-10,73,-120,31,29, + 54,96,-64,69,96,-47,-27,88,-88,-83,-32,-90,-127,-6,9,11,105,70,-48,117,14,-27,117,41,99,76,22,6,70,-106, + -3,127,0,-115,2,53,81,82,-54,-105,61,-37,107,69,-3,6,119,-44,85,94,0,2,-104,-73,-91,-3,88,51,36,111, + 4,-2,-127,60,93,-63,6,60,-112,-75,21,-100,-107,9,-68,-21,111,57,34,-26,-119,47,-72,-94,-66,36,-102,-39,121,0, + -97,82,0,-56,96,-61,-11,41,-120,109,19,-62,24,42,-117,-63,-7,-110,-97,41,-71,-10,-113,-58,61,-111,3,-37,-116,34, + 34,-68,102,-2,-127,-1,95,85,1,-1,127,-3,69,107,0,61,91,64,8,127,0,8,64,-109,-121,32,-20,-54,50,-104, + -124,4,-29,-121,-12,-36,-117,-20,46,-115,-19,-49,32,-2,-123,12,19,-125,21,28,-122,-10,-104,72,-23,-124,17,27,-63,107, + 53,-93,69,47,-44,109,52,-47,106,-8,28,-124,5,122,-36,-6,126,11,-42,117,26,-50,113,27,-33,84,90,97,68,-46, + 9,64,-109,-8,-2,-127,10,9,-126,12,70,105,72,8,104,5,21,125,-32,-93,-81,-36,-111,-49,-5,-5,127,-38,-111,48, + -51,-100,59,-3,-10,-127,-86,-78,-51,-45,115,30,95,-84,-8,49,-23,-115,-9,41,120,61,-20,110,15,59,112,12,9,-126, + -70,102,-28,7,5,-127,-4,-15,-126,-28,117,-41,-26,68,104,-36,96,75,-60,94,60,-35,-62,105,3,-42,120,0,35,122, + 48,23,115,65,46,99,80,58,79,88,11,91,19,32,121,4,94,-85,-31,113,49,-4,85,94,-9,-13,126,-7,22,125, + -20,101,75,5,26,124,-55,97,61,17,-125,18,33,-121,22,-12,-70,105,-23,-8,125,-103,-10,-73,-120,31,-29,54,96,64, + -27,88,88,69,96,47,-124,-25,-1,-84,-49,81,4,69,106,-67,94,-52,-86,93,-3,65,106,25,118,-47,9,6,119,45, + 5,-105,71,-18,101,-75,54,29,-111,-1,-2,127,8,63,110,43,108,51,-61,91,64,-73,70,77,-26,111,-56,32,-18,122, + 28,-66,105,-49,33,112,-56,96,61,31,32,119,71,43,96,6,49,117,-63,-7,110,-98,39,70,37,-86,86,63,-93,-59, + 14,-126,-5,120,-43,0,74,-2,-103,11,127,0,-2,127,-1,46,105,-54,-8,127,0,8,64,109,-121,32,20,-54,50,104, + -121,-12,36,-124,4,29,27,13,-123,13,5,-126,63,92,-61,77,101,0,76,102,0,45,47,-109,-95,-12,83,-34,-101,-69, + -33,-123,0,-54,-115,0,-48,-92,-73,45,72,-94,2,23,-125,-29,2,-124,12,-11,-126,2,50,117,-24,-65,-106,-29,23,-121, + 79,-100,0,105,-72,0,77,-59,-83,47,-69,-96,8,120,-42,-10,126,-14,-26,75,-99,-34,71,-99,-61,111,0,-38,121,0, + -22,-47,-116,-7,-52,-115,-10,13,-126,6,14,-126,-21,78,-98,7,84,-95,-7,127,0,-31,123,0,3,82,-97,-7,-68,-107, + -3,17,-126,3,14,-126,-63,99,-48,0,127,10,14,-61,-110,114,56,0,47,31,-114,-85,-22,-91,-120,-40,3,54,-115,8, + -3,-127,0,-21,-45,-117,-30,-99,-73,43,31,-115,72,-104,5,35,76,-96,3,32,-123,-7,-127,-1,30,-123,0,5,-78,-100, + -1,-85,-94,48,-91,-74,31,-29,-120,8,-108,-66,6,-16,-126,11,28,-123,-113,-6,-59,40,-47,111,75,-102,2,40,-120,9, + -8,-127,4,-126,-15,0,-124,-26,-1,9,-36,-121,31,-71,-101,-110,9,-62,91,88,-10,-4,-110,-64,5,-127,-4,-1,-4,-127, + -88,-91,0,61,98,-53,-10,-32,-123,108,61,-29,41,-70,97,-1,36,-122,5,-110,-64,-8,-108,-67,0,-106,-70,-16,-98,-79, + 7,-126,-9,-4,-127,-9,-4,-126,-15,4,-126,-16,-31,-123,0,81,96,-21,54,114,-14,-44,-119,0,61,-112,0,21,25,123, + 11,12,126,67,86,65,64,-13,109,-22,87,-90,-35,-96,76,-52,-91,72,-50,27,114,-15,28,123,8,92,87,11,-11,126, + 0,49,-117,-23,-86,91,-28,17,123,47,-69,96,76,-59,83,-6,122,34,-26,75,99,-35,71,99,-27,-56,111,-42,-48,110, + -11,13,126,-21,78,98,6,14,126,7,84,95,-16,92,86,8,-85,94,21,17,124,11,9,126,-32,92,81,29,-72,101, + 47,31,114,-39,-3,121,-120,-40,-3,-42,-101,65,-25,-42,117,45,31,114,55,-114,1,-3,33,123,28,87,89,-1,-85,94, + 5,-78,100,24,-94,82,-1,-100,78,12,-40,120,5,-15,126,4,33,122,-27,112,53,81,-95,23,42,-45,-111,14,-45,118, + 31,-71,101,-110,9,62,44,119,6,3,-107,69,-1,-4,127,60,109,27,-12,-31,122,119,35,25,30,-70,-102,-10,38,121, + 21,-115,50,0,-120,43,-5,-118,47,87,-48,79,22,24,123,-2,35,122,-5,13,126,3,-98,-80,33,-62,-106,-14,-124,-22, + 66,-87,-65,55,-55,-100,88,-44,-80,-2,23,125,-5,103,75,-2,55,114,-113,57,-11,-40,77,-93,-56,111,-26,30,101,71, + 54,62,96,59,80,79,-27,107,-62,-3,122,-37,37,45,113,9,11,126,-7,3,127,43,48,-109,7,10,-126,62,32,-106, + -78,-53,85,-101,-60,49,-8,0,-127,-81,-75,-62,-99,-63,-48,0,-10,127,-91,-61,64,-39,111,47,-40,116,34,8,93,86, + 44,-33,115,77,-100,9,7,34,-122,13,37,-121,52,-46,-106,-51,110,37,13,8,126,7,6,127,16,122,32,-5,-13,126, + -18,74,-101,-39,110,-50,-55,109,-35,-29,-50,-113,-15,34,-121,9,-38,-121,78,27,-96,114,55,11,82,81,-54,52,80,-84, + -12,34,-122,92,68,55,88,72,56,23,120,-33,-11,-6,-126,82,73,-65,-8,21,-125,-21,95,-81,8,22,-125,-39,97,-72, + 22,-124,-18,38,-119,-22,-23,-9,-125,-9,-71,-105,-103,-14,73,-121,26,29,50,98,-64,65,99,-47,-31,87,-88,-82,-34,-91, + -126,-10,9,-2,106,71,-63,110,4,-47,115,27,104,69,24,19,68,-105,21,125,0,-116,23,46,49,100,-61,-68,88,-62, + 107,68,-1,1,119,-44,81,98,0,-11,-103,-74,-91,4,88,52,35,111,4,-2,-127,57,94,-64,4,61,-112,-61,30,-107, + -100,16,-77,-29,110,56,37,-22,-119,49,-71,-93,-63,35,-104,-44,119,0,-100,78,0,-60,94,-61,-12,41,-120,111,12,-60, + 22,43,-118,-63,-10,-110,-98,37,-71,-27,-109,-58,43,-119,2,-54,-110,34,24,-72,102,-20,-125,-1,105,72,1,-5,127,-2, + 69,107,0,58,92,65,3,127,0,5,64,-109,-122,27,-20,-56,48,-104,-124,-1,-29,-121,-17,-36,-116,-25,46,-115,-24,-49, + 33,0,-123,11,19,-125,22,30,-122,-7,-106,70,-17,-125,17,30,-62,107,52,-94,68,46,-42,111,48,-50,106,-14,23,-124, + -13,124,-23,-26,124,-7,-50,114,23,-62,107,28,-41,79,91,98,76,-28,1,64,-109,-8,-2,-127,9,10,-126,3,71,106, + 73,14,103,2,22,125,-20,-95,-82,-21,-115,-49,-4,-6,127,-23,-115,48,-32,-107,60,-1,-10,-127,-68,-94,-53,-58,106,38, + 119,-44,-8,55,-6,-114,-17,36,121,65,-2,109,9,60,112,9,12,-126,-96,77,-31,6,9,-127,-4,-15,-126,-33,117,-37, + -34,65,103,-32,93,80,-78,78,63,-32,-66,104,4,-42,120,2,36,122,46,25,116,61,49,100,76,57,84,87,13,91, + 20,32,121,12,95,-83,-33,114,44,4,87,92,-9,-13,126,-12,22,124,-44,94,73,-3,26,124,-81,75,63,22,-124,18, + 38,-119,22,-9,-71,105,-23,-9,125,-103,-14,-73,-121,26,-29,50,98,64,-31,87,88,65,99,47,-116,-51,-1,-71,-65,83, + 17,68,106,-63,94,-57,-84,95,-5,27,122,21,121,-38,10,1,119,45,-8,-104,72,-21,101,-74,50,33,-112,0,-2,127, + -7,62,111,20,113,53,-58,93,64,-73,71,76,-58,99,-55,38,-6,121,43,-58,105,-43,36,114,-60,94,61,32,32,119, + 70,42,97,-5,49,117,-63,-10,110,-100,35,70,30,-90,84,53,-100,-57,9,-127,-5,113,-58,0,71,-11,-105,22,125,0, + -33,123,-1,23,112,-56,-24,125,1,5,64,109,-122,27,20,-56,48,104,-121,-17,36,-124,-1,29,27,14,-123,13,6,-126, + 59,94,-62,73,104,0,71,105,0,43,47,-110,-95,-8,83,-19,-105,-69,-19,-126,0,-32,-123,1,-32,-98,-74,21,79,-97, + -1,24,-125,-26,1,-124,12,-11,-126,1,51,117,-21,-67,-106,-29,23,-121,86,-93,0,111,-62,0,82,-51,-82,52,-67,-95, + -9,117,-48,-23,123,-19,-13,78,-99,-31,72,-100,-51,116,0,-18,126,0,-15,-47,-117,-4,-52,-116,-9,13,-126,5,15,-126, + -27,77,-98,-1,84,-95,-20,125,0,-38,121,0,-8,81,-97,-4,-69,-106,-4,17,-126,2,14,-126,-60,101,-49,4,126,12, + 16,-62,-110,109,65,0,44,35,-114,-86,-19,-91,-119,-45,3,56,-114,10,-20,-125,0,-13,-46,-118,-20,-102,-74,41,33,-115, + 76,-102,5,17,80,-97,-3,32,-123,-29,-124,-1,12,-126,0,-8,-78,-100,-15,-83,-95,52,-92,-70,33,-30,-119,14,-109,-64, + 3,-16,-126,8,28,-124,-112,-1,-61,46,-43,110,87,-93,2,44,-119,12,1,-127,7,-125,-21,0,-123,-31,-1,11,-36,-121, + 25,-73,-101,-111,4,-62,91,88,-10,2,-110,-64,9,-127,-3,-1,-4,-127,-85,-95,0,34,108,-57,-6,-34,-122,110,58,-29, + 43,-70,96,-7,36,-122,9,-110,-62,-4,-109,-65,4,-107,-69,-30,-95,-79,10,-126,-8,1,-127,-8,0,-126,-16,8,-126,-17, + -26,-124,0,48,115,-26,27,123,-17,-28,-124,2,57,-114,0,19,25,123,10,14,126,64,88,66,66,-11,108,-25,83,-93, + -19,-99,77,-33,-97,76,-54,26,112,-24,29,121,-19,97,80,12,-11,126,-12,48,-117,-17,-91,87,-26,19,123,52,-67,95, + 82,-51,82,-21,121,33,-14,78,99,-32,72,100,-25,-57,110,-33,-53,110,-9,13,126,-27,77,98,5,15,126,-1,84,95, + -25,89,87,15,-89,90,22,18,124,12,9,126,-21,90,87,36,-72,98,43,35,114,-32,-12,122,-119,-45,-3,-29,-103,68, + -16,-45,118,43,33,115,82,-97,5,-11,32,122,8,92,87,-16,-83,95,-9,-78,100,24,-93,84,4,-98,81,12,-38,120, + 2,-15,126,2,32,123,-27,112,53,110,-60,21,55,-27,-111,16,-44,118,24,-73,101,-111,4,62,28,124,6,8,-106,69, + -2,-4,127,32,120,26,-9,-34,122,116,45,25,46,-61,-102,-15,36,121,27,-115,47,5,-121,39,-1,-120,43,79,-59,80, + 26,16,123,5,32,123,-2,15,126,7,-99,-80,36,-60,-106,-9,-125,-21,65,-91,-60,55,-58,-98,83,-53,-80,-4,23,125, + -31,101,70,-8,54,115,-116,8,-52,-51,69,-94,-67,95,-51,28,85,90,60,47,101,58,76,84,-15,120,-37,-20,125,-8, + 30,52,112,9,10,126,-8,2,127,34,56,-109,6,12,-126,65,24,-106,-66,-67,85,-86,-80,49,-8,-2,-127,-62,-91,-63, + -85,-82,-48,3,-9,127,-72,-82,65,-37,109,54,-48,112,37,1,84,95,47,-26,115,93,-86,9,4,34,-122,14,27,-123, + 58,-39,-106,-66,102,38,11,10,126,6,9,127,8,123,30,-5,-13,126,-21,73,-101,-47,106,-51,-58,108,-34,-27,-51,-113, + -15,34,-121,10,-37,-121,79,17,-98,116,50,13,91,66,-58,69,65,-85,-6,35,-122,90,71,55,86,75,56,17,122,-31, + -11,-7,-126,79,75,-64,-11,21,-125,-34,93,-80,4,22,-125,-54,89,-72,28,-123,-18,44,-117,-22,-23,-10,-125,-6,-71,-105, + -102,-19,73,-122,20,29,45,100,-64,60,102,-47,-35,85,-88,-70,-53,-91,-120,-42,8,-22,113,53,-69,101,-33,-68,92,-56, + 103,71,24,27,66,-105,35,122,0,-92,76,45,9,112,-60,-59,96,-59,124,29,-1,-4,119,-44,76,102,0,-16,-102,-74, + -81,44,88,52,32,111,5,-1,-127,33,105,-64,-12,60,-111,-50,45,-108,-81,51,-83,-49,103,55,42,-14,-119,59,-63,-93, + -62,33,-106,-50,117,0,-104,73,0,-64,91,-61,-2,44,-119,111,8,-61,19,44,-118,-62,-13,-110,-100,32,-71,-27,-109,-60, + 41,-120,2,-52,-111,35,26,-71,102,-15,-126,-1,102,75,1,-39,121,-2,46,118,0,33,103,66,-3,127,0,2,64,-109, + -124,21,-20,-58,45,-104,-123,-7,-29,-120,-23,-36,-114,-30,46,-113,-29,-49,35,8,-122,7,20,-125,27,39,-118,-3,-107,68, + -11,-125,17,33,-61,107,53,-96,65,47,-45,109,47,-52,106,-20,14,-125,-44,111,-45,-44,98,-68,-46,117,16,-58,108,34, + -37,76,95,98,81,-4,-2,62,-111,-8,-3,-127,8,12,-126,0,68,107,71,31,101,0,17,126,-17,-94,-83,-20,-115,-49, + -4,-7,127,-21,-115,49,-24,-109,61,1,-9,-127,-55,-100,-56,-88,83,39,123,32,-7,52,25,-113,-28,26,121,58,31,109, + -2,65,109,2,15,-126,-119,25,-38,2,13,-126,-2,-16,-126,-56,109,-33,-52,52,103,-33,89,84,-103,40,62,-27,-73,100, + 6,-43,119,4,37,122,45,27,116,59,50,101,73,60,85,87,17,91,20,33,121,6,99,-79,-52,109,38,-1,91,89, + -8,-14,126,-21,18,124,-77,71,72,-14,21,125,-105,31,65,28,-123,18,44,-117,22,-6,-71,105,-23,-10,125,-102,-19,-73, + -122,20,-29,45,100,64,-35,85,88,60,102,47,-109,-65,-1,-61,-71,86,25,66,106,-43,111,-44,-59,112,15,-1,123,30, + 126,-10,13,-4,119,45,-13,-104,72,-42,91,-78,42,38,-114,2,-1,127,-17,60,111,7,114,56,-41,98,69,-54,72,90, + -96,64,-53,38,14,121,63,-35,105,-39,38,115,-64,91,61,33,34,118,68,44,97,-21,45,117,-62,-13,110,-102,30,70, + 36,-90,82,55,-100,-55,23,-125,-7,113,-59,0,70,-10,-105,14,126,0,-52,116,0,9,112,-59,-26,124,1,2,64,109, + -124,21,20,-58,45,104,-120,-23,36,-124,-7,29,27,14,-123,13,6,-126,54,97,-62,68,107,0,66,108,0,54,31,-111, + -91,17,87,-2,-107,-69,-4,-127,1,-9,-127,2,-15,-102,-75,-7,81,-98,-7,24,-125,-24,5,-125,13,-10,-126,-13,49,117, + -18,-68,-105,-30,22,-122,80,-98,0,108,-67,0,80,-55,-82,48,-70,-94,-26,111,-55,-31,120,-26,-3,79,-99,-23,75,-100, + -36,122,0,-1,127,0,-9,-44,-119,-6,-49,-117,-7,13,-126,6,14,-126,-23,78,-97,3,84,-95,-15,126,0,-31,123,0, + -21,79,-97,-1,-70,-106,1,19,-126,4,12,-126,-57,102,-50,8,126,15,19,-63,-109,112,60,0,46,33,-114,-83,-10,-96, + -116,-51,3,54,-114,9,-29,-124,0,-4,-45,-119,-11,-102,-75,40,35,-115,91,-88,5,-7,83,-96,-10,30,-123,-43,-120,-1, + -5,-127,0,-18,-76,-100,-25,-81,-95,47,-96,-69,30,-35,-118,17,-108,-64,1,-16,-126,6,28,-124,-101,44,-63,55,-32,110, + 106,-70,2,44,-119,10,8,-127,7,-124,-27,0,-122,-37,-1,13,-36,-121,15,-76,-101,-111,-1,-62,78,100,-9,9,-110,-63, + 15,-126,-1,-2,-4,-127,-80,-99,0,0,114,-55,-2,-36,-122,113,50,-29,53,-64,96,-13,34,-122,12,-111,-61,0,-111,-63, + 8,-108,-67,-27,-95,-80,14,-126,-5,6,-127,-7,6,-126,-15,14,-125,-15,-20,-125,0,11,125,-18,-5,126,-16,-12,-126,4, + 45,-119,0,13,29,123,5,17,126,60,90,67,72,1,104,-70,15,-105,-4,-100,78,-13,-99,78,-44,21,117,-22,27,122, + -26,93,83,12,-10,126,-20,45,-117,-6,-98,81,-26,13,124,48,-70,95,79,-55,82,-28,119,33,-4,79,99,-24,75,100, + -24,-59,110,-20,-53,114,-8,13,126,-23,78,97,6,14,126,2,84,95,-26,86,90,25,-90,86,18,18,124,12,9,126, + -10,89,90,40,-65,101,45,33,114,-50,-46,107,-116,-51,-3,-18,-105,70,-6,-45,119,41,35,115,116,-50,9,-16,30,122, + -9,91,88,-25,-81,95,-18,-76,100,26,-97,78,8,-101,77,13,-41,119,0,-15,126,2,32,123,-50,100,61,126,4,18, + 62,3,-111,17,-36,120,15,-76,101,-111,-1,62,18,125,7,13,-104,72,-2,-4,127,2,123,33,-5,-34,122,107,63,29, + 66,-37,-102,-16,33,121,32,-113,49,14,-120,38,5,-121,39,78,-59,81,29,0,124,12,21,125,6,16,126,9,-99,-79, + 37,-59,-106,-5,-125,-20,66,-95,-53,59,-61,-94,72,-65,-82,-6,22,125,-67,98,44,-12,54,114,-94,-20,-84,-60,61,-94, + -67,79,-74,27,59,109,63,14,109,59,67,90,-12,126,-6,-27,121,29,34,50,111,12,7,126,-7,2,127,39,55,-108, + 9,13,-126,70,3,-106,-69,-63,86,-88,-77,49,-8,-1,-127,-58,-92,-66,-88,-78,-48,5,-9,127,-62,-88,68,-32,109,57, + -52,109,39,-1,76,102,51,-15,115,111,-61,8,2,33,-123,7,26,-124,64,-27,-106,-82,89,39,8,12,126,4,11,126, + -18,122,29,-3,-14,126,-23,73,-102,-53,102,-54,-59,108,-33,-27,-53,-112,-15,34,-121,11,-37,-121,79,-2,-100,119,40,15, + 108,30,-60,93,23,-83,5,34,-122,86,78,52,69,90,56,-9,123,-30,-10,-9,-126,62,90,-65,-17,19,-124,-59,81,-78, + -2,22,-125,-74,74,-72,32,-122,-18,48,-115,-22,-22,-11,-125,-3,-71,-105,-101,-23,73,-123,16,29,41,102,-64,56,104,-47, + -38,84,-88,-63,-59,-93,-114,-55,6,-42,120,0,-63,88,-67,-52,44,-107,100,75,23,33,62,-106,48,117,0,-56,101,54, + -19,115,-51,-55,104,-49,122,-37,4,-9,119,-45,72,104,0,-17,-102,-73,-42,84,85,59,19,111,7,0,-127,24,106,-66, + -20,58,-111,-42,58,-105,-55,81,-81,-78,84,54,45,0,-119,75,-45,-93,-61,32,-107,-54,115,0,-106,69,0,-68,88,-61, + 15,41,-119,110,1,-63,17,46,-117,-62,-15,-110,-101,28,-71,-7,-111,-61,56,-114,1,-29,-118,36,39,-64,102,11,-126,-1, + 85,94,1,-55,115,-1,52,116,0,24,105,68,-8,127,0,0,64,-109,-124,17,-20,-59,43,-104,-123,-12,-29,-119,-27,-36, + -113,-34,46,-112,-34,-49,38,12,-121,7,20,-125,46,55,-105,2,-108,68,-7,-126,17,35,-60,107,55,-95,64,47,-45,109, + 49,-50,106,-20,3,-125,-49,48,-107,-41,59,-105,-46,118,5,-49,112,35,-33,77,95,98,74,33,6,60,-112,-8,-2,-127, + 9,12,-126,10,67,108,71,36,99,2,15,126,-32,-90,-84,-39,-111,-48,-5,-5,127,-39,-110,50,-32,-105,63,3,-9,-127, + -56,-97,-59,-122,29,20,89,91,-6,35,48,-112,-32,15,122,39,56,107,-12,67,107,-5,15,-126,-115,-30,-46,-5,14,-126, + 2,-16,-126,-87,87,-29,-67,23,105,-26,89,86,-113,-12,57,-21,-78,98,7,-43,119,5,38,121,43,29,116,57,50,102, + 67,65,86,86,15,92,21,32,121,-16,101,-75,-82,91,33,-20,93,84,-5,-16,126,-28,8,123,-100,32,72,-21,12,125, + -106,-17,67,32,-122,18,48,-115,22,-3,-71,105,-22,-11,125,-101,-23,-73,-123,16,-29,41,102,64,-38,84,88,56,104,47, + -110,-64,0,-60,-68,89,31,62,106,-22,123,-20,-48,115,27,-19,118,42,126,1,16,-9,119,45,-15,-103,73,-54,78,-85, + 42,36,-114,3,0,127,-19,59,111,10,112,59,-29,101,71,-47,74,92,-115,13,-52,28,31,120,73,0,104,-37,41,114, + -68,88,61,34,35,117,65,48,98,-31,35,118,-62,-15,110,-103,27,70,56,-82,79,71,-91,-52,59,-112,-8,119,-45,-1, + 71,0,-106,-12,126,0,-55,114,-1,11,110,-63,-5,127,0,0,64,109,-124,17,20,-59,43,104,-119,-27,36,-123,-12,29, + 26,15,-123,13,7,-126,51,98,-62,64,110,0,62,111,0,62,2,-111,-66,62,89,10,-105,-70,5,-127,2,6,-127,1, + -1,-98,-80,-26,82,-94,-12,24,-124,-22,12,-124,13,-10,-126,-20,47,116,-17,-72,-103,-30,21,-122,67,-108,0,99,-79,0, + 73,-64,-82,38,-77,-94,-38,106,-59,-35,117,-33,13,78,-99,-7,78,-100,-5,127,0,22,125,0,-3,-38,-121,-10,-41,-120, + -4,14,-126,8,13,-126,-14,81,-97,12,83,-95,-1,127,0,-16,126,0,-31,77,-96,2,-73,-104,11,19,-125,7,9,-126, + -54,103,-51,12,125,16,22,-64,-108,118,46,0,49,27,-114,-75,15,-101,-114,-55,3,57,-113,8,-35,-122,0,4,-41,-120, + -4,-96,-83,39,36,-115,112,-59,5,-27,82,-93,-17,29,-123,-58,-113,-1,-33,-123,0,-30,-71,-101,-35,-77,-95,35,-96,-76, + 20,-39,-119,12,-102,-75,-1,-15,-126,3,28,-124,-59,95,-61,63,-13,110,123,-33,2,41,-120,7,10,-127,3,-123,-31,0, + -120,-41,-1,15,-36,-121,-3,-77,-101,-111,-5,-62,88,91,-8,15,-110,-62,19,-126,3,-3,-3,-127,-76,-102,0,-27,114,-50, + 1,-37,-122,122,18,-28,69,-47,96,-19,33,-121,17,-112,-58,4,-113,-58,11,-110,-63,-6,-98,-80,17,-126,2,11,-126,-5, + 10,-126,-12,18,-125,-10,-16,-126,0,-15,126,-4,-30,123,-15,0,-127,1,22,-125,0,6,32,123,-1,20,125,57,91,67, + 75,6,102,-78,-23,-97,5,-100,78,-1,-99,80,-43,22,118,-24,27,122,-34,89,84,12,-10,126,-21,44,-117,5,-103,74, + -20,-5,125,38,-77,94,73,-64,82,-33,118,33,12,78,99,-7,78,100,-25,-59,110,-12,-52,115,-4,14,126,-14,81,97, + 7,13,126,12,83,95,-24,87,89,32,-88,85,17,18,125,12,9,126,0,91,89,39,-56,107,49,27,114,-59,-56,98, + -114,-55,-3,-10,-104,72,0,-44,119,40,36,115,126,14,12,-18,29,122,-20,88,89,-35,-77,95,-30,-72,100,26,-97,77, + 9,-101,76,13,-42,119,-1,-14,126,1,33,123,-61,86,70,108,65,14,53,33,-111,15,-29,123,-3,-77,101,-111,-5,62, + 32,123,8,15,-101,75,-3,-3,127,-18,118,43,-3,-32,123,105,64,31,76,-2,-102,-15,32,122,34,-109,55,21,-118,41, + 11,-120,39,84,-48,82,27,-9,124,14,15,125,9,12,126,9,-100,-78,38,-58,-106,-4,-126,-18,62,-98,-51,59,-62,-94, + 63,-72,-84,-6,25,124,-74,95,-41,-12,64,109,-73,16,-103,-56,65,-94,-57,76,-84,28,49,114,57,-8,113,61,63,92, + -14,126,-9,-22,121,32,49,37,111,14,3,126,-6,5,127,55,40,-107,12,9,-126,68,-17,-106,-90,-34,83,-110,-42,48, + -8,2,-127,-70,-77,-72,-109,-41,-50,9,-9,126,-59,-86,73,-41,110,47,-53,110,34,-3,90,89,55,8,114,127,-7,6, + -1,35,-122,-1,56,-114,70,-2,-106,-102,63,41,2,16,126,-3,12,126,-78,96,29,4,-14,126,-24,72,-102,-53,100,-58, + -62,105,-36,-30,-58,-109,-15,33,-122,11,-37,-121,76,-17,-100,122,33,11,110,-3,-63,97,-13,-81,12,31,-123,86,81,46, + 13,112,58,-70,102,-29,-5,-13,-126,8,108,-66,-25,7,-124,-94,37,-77,-11,19,-125,-99,36,-71,34,-121,-18,50,-115,-22, + -22,-12,-125,-2,-71,-105,-101,-24,73,-123,14,29,40,102,-64,55,105,-47,-39,83,-88,-60,-57,-96,-114,-56,-1,-38,101,-66, + -52,84,-80,-42,34,-115,100,75,22,32,62,-106,47,118,0,-34,110,54,-23,115,-49,-44,110,-46,104,-72,7,-10,118,-45, + 71,105,0,-16,-103,-73,-21,91,86,61,1,112,10,-1,-127,29,104,-67,-23,60,-109,-35,67,-102,-36,93,-78,-112,26,54, + 39,22,-119,87,2,-93,-69,27,-103,-56,114,0,-107,68,0,-69,88,-61,26,35,-119,109,-4,-66,14,52,-115,-61,-16,-110, + -102,27,-71,49,-99,-62,102,-75,1,35,-116,37,67,-34,103,76,-102,-1,25,125,1,-61,112,0,85,94,0,27,103,70, + -10,127,0,-1,64,-110,-124,15,-20,-60,42,-104,-123,-14,-29,-118,-29,-36,-113,-36,46,-112,-35,-49,39,8,-120,8,17,-126, + 66,65,-87,5,-106,69,-5,-126,17,36,-59,107,51,-94,69,42,-41,112,47,-48,108,-19,0,-126,-41,24,-118,-32,47,-114, + -32,123,-2,-36,117,34,-23,81,95,95,67,52,28,52,-112,-8,1,-127,12,9,-126,33,58,108,75,17,101,7,14,126, + -64,-69,-85,-78,-88,-47,-7,-2,127,-79,-86,51,-55,-93,67,4,-9,-127,-65,-88,-65,-124,-19,-22,72,105,-6,26,53,-113, + -32,11,122,32,63,105,-8,69,107,-8,13,-126,-106,-51,-47,-8,11,-126,7,-15,-126,-106,63,-28,-69,1,106,-59,72,87, + -110,-34,54,-18,-76,100,8,-42,120,3,37,121,43,29,116,58,50,101,66,66,87,85,14,93,21,32,121,-36,100,-70, + -101,70,31,-38,92,79,-1,-16,126,-29,0,124,-104,3,73,-22,7,125,-99,-39,69,34,-121,18,50,-115,22,-2,-71,105, + -22,-12,125,-101,-24,-73,-123,14,-29,40,102,64,-39,83,88,55,105,47,-116,-52,1,-65,-58,93,31,62,106,-32,119,-30, + -60,111,13,-25,117,41,124,-16,19,-10,118,45,-15,-103,73,-56,71,-88,51,24,-114,6,0,127,-17,60,111,25,107,64, + -37,101,67,-59,75,84,-114,-19,-52,20,38,120,73,24,101,-44,31,115,-69,88,61,35,33,118,64,48,99,-29,31,120, + -61,-16,110,-103,25,70,75,-71,75,85,-81,-48,97,-82,-6,122,-34,-1,71,8,-105,-34,122,0,-50,117,-1,25,105,-68, + 43,120,0,-1,64,110,-124,15,20,-60,42,104,-118,-29,36,-123,-14,29,22,20,-124,12,9,-126,51,98,-62,62,111,0, + 61,112,0,60,-17,-111,-52,73,90,13,-104,-72,6,-127,2,10,-127,0,4,-95,-85,-28,84,-91,-13,26,-123,-21,17,-124, + 13,-9,-126,-23,46,116,-18,-78,-98,-32,18,-122,50,-117,0,86,-93,0,63,-74,-82,26,-82,-94,-37,104,-63,-30,118,-36, + 19,77,-99,5,78,-100,15,126,0,30,123,0,0,-36,-122,-12,-37,-121,-2,15,-126,9,12,-126,-2,82,-97,24,81,-95, + 18,126,0,3,127,0,-32,77,-96,4,-76,-102,15,14,-125,9,6,-127,-58,100,-51,12,126,15,23,-62,-108,124,28,0, + 52,20,-114,-70,34,-100,-114,-57,3,54,-115,8,-34,-122,0,7,-40,-120,-2,-91,-89,39,37,-115,127,10,5,-31,82,-92, + -18,29,-122,-58,-113,0,-44,-119,0,-33,-70,-101,-34,-77,-95,25,-95,-81,12,-42,-119,7,-96,-83,-1,-14,-126,2,29,-124, + -34,106,-61,61,22,109,122,34,1,37,-121,6,8,-127,0,-123,-33,0,-120,-43,-1,15,-35,-121,-14,-75,-101,-110,-7,-62, + 112,59,-8,17,-110,-61,19,-125,6,-4,-1,-127,-75,-103,0,-34,112,-48,2,-36,-122,122,-18,-29,83,-4,96,-20,33,-121, + 19,-112,-56,6,-115,-54,11,-112,-59,46,-87,-81,17,-126,8,12,-126,-3,12,-126,-6,20,-125,-3,-14,-126,0,-20,125,-3, + -35,121,-17,4,-127,-1,3,-127,0,7,32,123,0,18,126,56,93,67,72,-3,105,-85,7,-94,6,-99,79,3,-97,82, + -54,25,112,-30,27,120,-45,90,78,12,-10,126,-18,45,-117,10,-98,81,-22,-19,124,26,-82,94,62,-74,82,-28,120,31, + 18,77,99,5,78,100,-26,-59,109,-7,-55,114,-2,15,126,-2,82,97,9,12,126,24,81,95,-19,89,88,31,-80,94, + 16,15,125,12,9,126,-4,92,88,34,-48,113,52,20,114,-77,-26,98,-114,-57,-3,-7,-102,75,2,-44,119,39,37,115, + 115,52,11,-20,29,122,-23,88,88,-35,-77,95,-33,-70,101,22,-95,82,6,-98,80,10,-40,120,-1,-14,126,3,34,122, + -63,85,70,88,91,10,42,46,-111,13,-25,124,-14,-75,101,-110,-7,62,75,102,8,13,-99,78,-4,-1,127,-23,116,45, + -2,-30,123,118,34,31,75,22,-100,-13,32,122,31,-106,63,24,-114,50,15,-118,44,88,-40,82,25,-14,124,15,11,126, + 10,9,126,8,-101,-76,39,-57,-107,-5,-126,-17,58,-100,-53,58,-61,-95,56,-74,-87,-8,38,121,-57,89,-71,-15,80,97, + -42,65,-101,-43,72,-96,-44,80,-88,29,43,116,51,-20,115,61,63,92,-17,124,-22,-17,122,29,57,23,111,14,1,126, + -5,7,127,65,24,-107,13,5,-126,65,-28,-106,-100,0,78,-118,-4,47,-7,4,-127,-77,-61,-80,-116,-2,-51,12,-11,126, + -52,-88,75,-69,100,36,-54,113,24,-19,114,52,52,40,109,113,58,3,-2,42,-120,3,73,-104,67,31,-104,-114,30,48, + -8,16,126,-10,8,126,-120,31,29,12,-9,126,-29,70,-102,-44,105,-55,-78,95,-31,-36,-66,-102,-16,29,-123,11,-36,-121, + 72,-26,-101,123,32,6,106,-25,-65,93,-36,-79,16,29,-123,86,84,42,-56,97,59,-117,40,-29,4,-13,-126,-58,90,-67, + -25,-9,-124,-98,-28,-76,-20,11,-125,-105,-30,-65,34,-121,-18,50,-115,-22,-22,-12,-125,-2,-71,-105,-101,-25,73,-123,14,29, + 39,102,-64,54,105,-47,-40,83,-88,-57,-58,-98,-112,-60,-8,-33,81,-92,-39,86,-84,-38,37,-115,99,77,22,30,62,-106, + 44,119,0,-21,113,53,-23,115,-49,-35,114,-44,92,-87,8,-11,118,-45,70,106,0,-15,-103,-73,-14,90,88,58,-12,112, + 14,-3,-126,33,103,-66,-27,62,-107,-29,73,-100,-24,99,-75,-104,-51,52,20,41,-119,68,53,-93,-89,13,-89,-56,114,0, + -108,67,0,-69,87,-61,32,29,-120,106,-3,-69,9,55,-114,-61,-16,-110,-102,27,-71,97,-51,-64,127,-6,1,98,-71,38, + 73,14,103,123,-33,-1,-55,114,1,-66,108,1,107,68,0,29,103,69,-10,127,0,-2,64,-110,-124,15,-20,-60,42,-104, + -123,-14,-29,-118,-29,-36,-113,-36,46,-112,-35,-49,40,2,-121,11,15,-126,86,71,-60,7,-105,71,-5,-126,17,36,-59,107, + 47,-92,73,39,-38,115,46,-45,109,-19,0,-126,-43,21,-118,-27,45,-116,-14,126,-6,-21,121,34,-12,83,95,90,62,65, + 48,36,-112,-6,4,-127,14,4,-126,54,39,108,72,-8,104,11,9,126,-89,-33,-84,-110,-42,-48,-8,2,127,-109,-40,50, + -76,-73,71,7,-10,-126,-69,-79,-71,-115,-39,-37,64,110,-6,22,53,-113,-30,10,123,29,66,104,-1,70,106,-10,11,-126, + -101,-62,-46,-10,9,-126,10,-11,-126,-118,37,-28,-68,-10,107,-104,18,71,-106,-42,56,-16,-71,104,9,-41,120,0,36,122, + 42,30,116,58,51,101,64,67,87,84,13,94,21,31,121,-55,93,-66,-114,45,31,-56,87,74,3,-15,126,-26,-7,124, + -100,-20,75,-23,4,125,-92,-52,71,34,-121,18,50,-115,22,-2,-71,105,-22,-12,125,-101,-25,-73,-123,14,-29,39,102,64, + -40,83,88,54,105,47,-120,-43,0,-67,-50,96,29,63,107,-46,109,-47,-73,103,-7,-30,118,37,118,-40,23,-11,118,45, + -14,-103,73,-46,79,-88,58,5,-113,9,-2,127,-16,62,110,39,101,67,-47,101,62,-74,76,70,-108,-41,-53,14,42,119, + 68,44,98,-53,15,114,-69,87,61,35,30,118,62,49,99,-26,30,121,-61,-16,110,-103,25,70,91,-54,70,98,-67,-45, + 121,-38,-4,125,-20,-1,69,18,-105,-56,114,0,-47,118,0,37,99,-70,88,92,0,-2,64,110,-124,15,20,-60,42,104, + -118,-29,36,-123,-14,29,17,25,-123,9,11,-126,52,98,-62,62,111,0,60,112,0,58,-27,-110,-52,73,90,14,-103,-73, + 5,-127,3,11,-127,-1,6,-92,-87,-27,86,-89,-14,28,-123,-20,21,-124,13,-9,-126,-26,46,115,-19,-86,-91,-37,10,-121, + 38,-121,0,77,-101,0,55,-80,-82,18,-84,-94,-32,103,-67,-22,119,-37,22,77,-99,12,78,-100,28,124,0,34,122,0, + 2,-35,-122,-13,-35,-121,0,15,-126,11,11,-126,6,82,-97,32,78,-95,30,123,0,15,126,0,-28,79,-95,6,-76,-102, + 16,10,-126,10,4,-127,-74,93,-45,10,126,13,21,-58,-111,126,16,0,54,15,-114,-67,45,-98,-114,-57,3,50,-116,8, + -33,-123,0,8,-39,-121,0,-87,-92,39,37,-115,95,85,5,-31,83,-91,-18,30,-122,-56,-114,0,-51,-116,0,-34,-69,-101, + -33,-77,-95,19,-93,-84,7,-43,-119,4,-92,-87,-1,-14,-126,2,30,-123,-25,109,-61,37,54,109,83,96,0,34,-122,5, + 7,-127,-2,-123,-33,0,-119,-43,-1,15,-34,-122,-21,-73,-101,-110,-7,-62,124,28,-9,17,-110,-62,19,-125,8,-5,-1,-127, + -75,-103,0,-34,112,-49,3,-34,-122,116,-41,-30,69,47,96,-19,33,-121,19,-112,-57,7,-116,-51,11,-114,-56,89,-40,-81, + 16,-125,14,13,-126,-1,13,-126,-1,20,-125,4,-14,-126,0,-21,125,-6,-36,121,-18,6,-127,-2,-9,-127,0,10,31,123, + 2,17,126,55,94,66,65,-14,108,-75,46,-91,7,-99,80,6,-95,84,-70,30,102,-39,28,118,-55,91,69,12,-10,126, + -17,48,-116,12,-89,89,-26,-25,122,18,-84,94,55,-80,82,-21,122,30,22,77,99,12,78,100,-29,-60,108,-1,-59,113, + 0,15,126,6,82,97,10,11,126,32,78,95,-11,91,88,26,-70,103,16,12,125,12,9,126,-18,90,88,28,-42,117, + 54,15,114,-79,16,98,-114,-57,-3,-4,-100,78,4,-44,119,38,37,115,100,77,9,-21,29,122,-24,89,88,-33,-77,95, + -34,-69,101,19,-92,86,5,-96,84,8,-39,121,-1,-14,126,6,34,122,-53,94,67,71,105,5,33,53,-111,12,-22,125, + -21,-73,101,-110,-7,62,111,62,9,10,-97,81,-5,-1,127,-25,116,45,-2,-28,124,123,-7,31,69,40,-99,-10,32,122, + 25,-102,71,24,-109,60,19,-115,51,93,-30,81,23,-18,124,14,8,126,10,7,126,7,-103,-74,38,-57,-107,-7,-126,-14, + 53,-99,-59,58,-59,-96,49,-70,-94,-15,48,117,-51,106,-48,-32,81,92,-12,94,-85,-25,76,-99,-30,87,-87,27,35,119, + 45,-30,115,64,46,100,-18,125,-14,-18,120,37,61,-5,111,14,-4,126,-1,9,127,68,-7,-107,13,0,-126,56,-44,-105, + -90,48,76,-106,53,46,-5,7,-127,-94,-21,-82,-103,54,-52,8,-15,126,-80,-63,76,-106,62,33,-82,94,22,-64,109,15, + 37,62,104,82,97,2,-9,45,-119,11,70,-106,54,56,-101,-112,-2,59,-14,11,126,-13,3,126,-122,-18,28,15,-4,126, + -48,61,-101,-59,101,-49,-111,60,-12,-39,-76,-94,-15,25,-124,10,-36,-121,70,-36,-100,125,16,17,101,-51,-57,85,-55,-76, + 18,26,-123,88,81,43,-89,68,60,-124,-9,-28,9,-11,-126,-88,60,-68,-19,-19,-124,-77,-67,-75,-23,2,-125,-78,-80,-61, + 35,-121,-18,50,-115,-22,-22,-12,-125,-2,-71,-105,-101,-25,73,-123,14,29,39,102,-64,54,105,-47,-40,83,-88,-74,-33,-98, + -126,-13,-10,-44,76,-92,-29,95,-79,-40,47,-111,99,76,22,29,63,-106,43,120,0,-18,111,59,-27,116,-45,-35,117,-36, + 79,-99,9,-11,118,-45,70,106,0,-14,-103,-73,-11,88,90,53,-29,112,13,-9,-126,72,82,-65,-2,69,-107,-29,80,-94, + -20,105,-68,-71,-92,51,1,45,-119,39,77,-93,-110,-15,-62,-57,114,0,-108,67,0,-69,87,-61,34,23,-120,108,-8,-67, + 3,51,-116,-61,-16,-110,-102,27,-71,109,-11,-65,122,37,1,118,-27,39,62,42,103,126,18,-1,-95,84,1,-20,125,1, + 126,12,0,67,83,68,-10,127,0,-2,64,-110,-124,15,-20,-60,42,-104,-123,-14,-29,-118,-29,-36,-113,-36,46,-112,-35,-49, + 40,-2,-120,12,14,-126,102,71,-25,8,-103,74,-5,-126,17,36,-59,107,47,-92,73,39,-40,114,47,-45,109,-19,0,-126, + -51,21,-114,-26,48,-115,-5,127,-7,-9,122,36,-4,85,94,94,49,70,57,23,-111,-4,7,-127,15,1,-126,63,24,107, + 69,-21,105,12,6,126,-99,-1,-80,-118,-6,-48,-8,4,127,-117,-3,50,-82,-60,76,11,-11,-126,-60,-83,-75,-108,-58,-35, + 51,116,-7,16,55,-113,-30,7,123,24,70,103,3,71,105,-12,10,-126,-93,-74,-44,-11,6,-126,13,-7,-126,-124,3,-29, + -65,-21,107,-111,-27,55,-100,-52,58,-14,-67,107,9,-39,120,-2,35,122,44,26,116,62,44,102,69,64,86,85,5,94, + 21,30,122,-80,74,-65,-122,12,33,-79,68,73,7,-13,126,-22,-13,124,-91,-43,77,-23,1,125,-82,-65,72,35,-121,18, + 50,-115,22,-2,-71,105,-22,-12,125,-101,-25,-73,-123,14,-29,39,102,64,-40,83,88,54,105,47,-116,-51,-1,-62,-54,97, + 29,63,106,-37,116,-37,-65,109,0,-30,117,39,113,-53,22,-11,118,45,-12,-103,74,-39,84,-87,59,-7,-112,13,-3,126, + -23,63,108,39,101,67,-40,103,63,-64,80,76,-96,-64,-54,6,44,119,57,60,96,-58,5,113,-69,87,61,35,29,119, + 65,47,99,-25,27,122,-61,-16,110,-103,25,70,104,-24,69,113,-36,-44,127,7,-3,126,14,-1,62,36,-105,-86,94,0, + -57,114,0,36,100,-69,108,67,0,-2,64,110,-124,15,20,-60,42,104,-118,-29,36,-123,-14,29,14,26,-124,7,13,-126, + 54,97,-63,62,111,0,60,112,0,54,-36,-109,-43,82,87,14,-100,-77,5,-127,3,13,-126,-2,11,-89,-90,-31,89,-85, + -15,30,-122,-20,25,-123,13,-9,-126,-7,53,115,-16,-91,-87,-42,-3,-120,31,-123,0,71,-105,0,50,-83,-82,13,-85,-94, + -26,106,-66,-15,122,-31,23,76,-99,17,77,-100,34,122,0,36,122,0,7,-35,-122,-11,-31,-123,1,15,-126,11,11,-126, + 11,82,-97,36,76,-95,37,121,0,22,125,0,-23,82,-94,9,-72,-105,17,7,-126,10,2,-127,-93,80,-32,12,126,10, + 20,-51,-115,127,9,0,55,12,-114,-54,65,-95,-113,-57,3,49,-117,5,-30,-123,0,9,-37,-121,6,-81,-98,40,37,-115, + 50,117,5,-31,85,-90,-18,30,-122,-55,-115,0,-54,-115,0,-34,-68,-101,-32,-78,-95,12,-85,-94,2,-39,-121,2,-83,-96, + -1,-14,-126,3,30,-123,-20,111,-59,11,65,109,36,122,0,32,-123,-1,6,-127,-8,-123,-33,0,-119,-43,-1,14,-31,-122, + -25,-72,-101,-110,-7,-62,124,-27,-10,16,-107,-67,17,-125,9,-5,0,-127,-74,-103,0,-34,113,-47,2,-32,-123,100,-73,-30, + 43,71,96,-17,33,-122,20,-110,-60,10,-115,-52,12,-115,-52,98,-1,-81,16,-125,18,13,-126,3,13,-126,5,21,-125,10, + -14,-126,0,-24,125,-3,-35,121,-15,10,-126,-7,-16,-126,0,11,31,123,2,16,126,54,94,66,63,-19,109,-68,59,-89, + 7,-98,80,7,-94,85,-58,31,108,-34,29,119,-51,92,72,13,-10,126,-21,49,-115,15,-83,95,-27,-28,121,13,-85,94, + 50,-83,82,-14,122,32,23,76,99,17,77,100,-27,-55,111,3,-52,116,1,15,126,11,82,97,11,11,126,36,76,95, + -5,92,87,23,-63,108,15,11,126,11,8,126,-29,86,89,24,-38,119,55,12,114,-75,35,96,-113,-57,-3,-1,-98,81, + 5,-43,119,38,37,116,81,98,8,-20,29,122,-23,89,88,-32,-78,95,-34,-68,101,17,-91,87,3,-94,85,6,-40,120, + -1,-14,126,8,34,122,-47,97,67,50,117,2,22,59,-110,10,-19,125,-25,-72,101,-110,-7,62,123,31,10,7,-95,84, + -5,0,127,-26,115,47,-2,-26,124,119,-30,31,58,55,-98,-8,33,122,21,-99,77,25,-104,68,21,-112,56,98,-4,81, + 21,-23,123,14,4,126,10,5,126,2,-105,-71,35,-57,-108,-11,-126,-10,53,-98,-61,63,-59,-93,46,-73,-93,-29,36,118, + -44,113,38,-55,51,103,8,114,-56,-8,78,-100,-14,100,-77,29,31,120,40,-45,112,77,-6,101,-10,127,4,-15,115,51, + 50,-34,112,13,-9,126,3,8,127,55,-39,-107,11,-6,-126,42,-58,-105,-53,85,77,-65,99,46,0,9,-127,-93,31,-81, + -62,98,-52,-2,-16,126,-102,-7,75,-121,1,37,-120,16,37,-118,41,21,10,73,104,39,121,3,-22,37,-119,13,63,-109, + 28,74,-99,-95,-52,67,-18,3,126,-14,-4,126,-104,-68,28,15,2,126,-77,26,-97,-103,61,-42,-125,-14,14,-37,-82,-90, + -4,24,-125,7,-37,-121,68,-57,-91,116,-29,42,85,-84,-43,73,-72,-75,19,24,-123,99,68,42,-109,24,60,-109,-60,-27, + 13,-7,-126,-105,18,-69,-10,-26,-124,-40,-95,-75,-21,-9,-125,-28,-109,-59,34,-121,-18,49,-115,-22,-22,-11,-125,-3,-71,-105, + -101,-24,73,-123,15,29,40,102,-64,55,105,-47,-39,83,-88,-80,14,-98,-112,59,-8,-83,76,-58,-20,112,-57,-56,76,-85, + 104,70,19,29,63,-106,42,120,0,-14,107,66,-25,118,-38,-32,121,-24,64,-109,10,-10,118,-45,71,105,0,-12,-103,-74, + -5,88,91,41,-47,111,6,-14,-126,104,30,-66,36,58,-107,-27,88,-88,-15,112,-59,-20,-115,51,-20,41,-119,0,86,-93, + -108,-43,-51,-56,114,0,-107,68,0,-69,88,-61,36,18,-121,113,-29,-49,-6,41,-120,-61,-16,-110,-101,27,-71,103,34,-66, + 99,80,1,118,24,40,39,64,103,106,70,-1,-122,37,1,53,116,1,115,-54,0,101,33,69,-10,127,0,-1,64,-110, + -124,16,-20,-60,42,-104,-123,-13,-29,-118,-29,-36,-113,-35,46,-112,-35,-49,35,-11,-121,12,12,-126,84,41,-86,8,-102,75, + -6,-126,17,36,-59,107,53,-95,65,46,-56,104,55,-49,103,-20,-2,-125,-70,18,-105,-31,58,-109,-4,127,-2,-1,121,39, + 0,86,94,126,-13,-3,61,10,-111,-3,8,-127,15,-1,-126,68,8,107,66,-30,104,13,2,126,-98,28,-75,-115,26,-47, + -6,6,127,-113,29,51,-88,-43,81,11,-13,-126,-60,-82,-76,-97,-77,-27,34,122,-7,7,58,-113,-29,4,124,14,73,103, + 3,74,103,-14,7,-126,-81,-88,-42,-11,3,-126,14,-3,-126,-120,-31,-30,-62,-32,106,-103,-58,47,-91,-65,61,-14,-64,109, + 9,-38,121,-3,35,122,56,3,114,79,5,99,98,44,68,86,-25,90,21,27,122,-98,47,-66,-120,-23,34,-95,41,74, + 10,-10,126,-18,-18,124,-78,-63,78,-22,-3,125,-68,-79,73,34,-121,18,49,-115,22,-3,-71,105,-22,-11,125,-101,-24,-73, + -123,15,-29,40,102,64,-39,83,88,55,105,47,-118,-47,-2,-63,-53,97,29,63,106,-11,125,-17,-34,119,29,-22,116,46, + 102,-74,10,-10,118,45,-9,-103,74,-25,91,-85,57,-17,-112,16,-6,126,-20,65,107,48,97,66,-18,104,70,-33,81,92, + -79,-83,-54,-3,45,119,42,73,96,-59,2,112,-69,88,61,36,25,119,81,33,92,-25,24,122,-61,-16,110,-103,26,70, + 106,8,70,119,0,-45,119,44,-2,115,53,-1,48,54,-105,-110,64,0,-52,116,1,44,98,-67,120,40,0,-1,64,110, + -124,16,20,-60,42,104,-118,-29,36,-123,-13,29,11,26,-124,5,14,-126,57,94,-63,62,111,0,61,112,0,48,-45,-108, + -23,93,83,10,-95,-84,2,-127,3,12,-126,-3,12,-86,-93,-30,94,-80,-15,32,-122,-18,28,-123,13,-9,-126,24,48,115, + -5,-93,-86,-40,-21,-119,28,-124,0,68,-107,0,48,-84,-82,11,-85,-94,-18,112,-58,-9,126,-16,24,76,-99,18,76,-100, + 37,122,0,37,121,0,9,-34,-122,-9,-27,-124,1,15,-126,11,10,-126,13,81,-97,38,75,-95,40,120,0,26,124,0, + -15,87,-91,15,-64,-109,17,4,-126,10,-1,-127,-83,84,-47,28,124,4,20,-43,-118,127,5,0,55,10,-114,-31,82,-92, + -114,-56,3,50,-117,0,-28,-124,0,7,-36,-122,9,-75,-102,41,37,-115,-9,127,5,-25,89,-87,-15,32,-122,-54,-115,0, + -56,-114,0,-35,-68,-101,-32,-78,-95,7,-75,-102,-1,-34,-122,-1,-75,-103,0,-14,-126,5,31,-123,-10,114,-56,-20,63,108, + -25,125,0,29,-123,-9,3,-126,-15,-123,-32,0,-120,-42,-1,14,-28,-123,-27,-72,-101,-111,-6,-62,96,-83,-10,13,-100,-77, + 14,-126,9,-5,0,-127,-75,-102,0,-27,116,-43,0,-29,-124,72,-101,-29,6,83,96,-13,33,-122,21,-106,-66,16,-113,-55, + 13,-118,-45,89,41,-81,14,-125,18,11,-126,8,10,-126,13,19,-125,16,-15,-126,0,-20,125,6,-28,124,-6,11,-126,-10, + -19,-126,0,11,32,122,2,16,126,54,95,65,61,-27,108,-64,67,-87,5,-98,81,6,-93,87,-27,27,121,-18,30,122, + -31,92,82,13,-10,126,-20,52,-114,16,-79,98,-25,-31,120,11,-85,94,48,-84,82,-8,122,36,24,76,99,19,76,100, + -19,-41,119,3,-39,121,1,15,126,13,81,97,12,10,126,38,75,95,-1,92,87,21,-60,110,16,9,126,11,5,126, + -23,88,88,21,-35,120,55,10,114,-70,47,95,-114,-56,-3,0,-92,88,4,-39,121,37,36,116,57,113,7,-15,30,122, + -18,89,88,-32,-78,95,-34,-68,101,15,-92,86,0,-93,87,4,-43,119,-1,-14,126,10,36,122,-34,102,68,25,125,0, + 9,62,-110,9,-18,125,-27,-72,101,-111,-6,62,127,2,10,3,-94,86,-5,0,127,-21,115,50,-3,-25,125,113,-48,31, + 44,67,-99,-6,33,122,15,-96,82,24,-100,74,21,-110,60,94,26,81,19,-26,123,16,3,126,11,4,126,-4,-108,-68, + 31,-58,-109,-15,-126,-8,55,-101,-54,65,-63,-89,47,-84,-83,-28,13,123,-66,107,18,-89,38,82,-6,112,-59,-10,79,-99, + -13,97,-81,49,43,109,33,-52,111,84,-48,83,-10,121,-38,-7,125,23,36,-45,113,11,-11,126,5,7,127,38,-52,-109, + 9,-8,-126,33,-64,-104,-34,88,86,-39,111,48,1,8,-127,-60,91,-64,-40,110,-49,-10,0,127,-51,96,66,-125,9,22, + -109,-52,40,-123,-5,-30,3,68,107,27,124,4,-28,18,-122,5,65,-109,17,73,-103,-88,-80,46,-16,5,126,-11,-2,126, + -97,-76,31,14,4,126,-78,-5,-100,-111,-15,-60,-115,-53,-3,-32,-83,-90,13,34,-122,3,-38,-121,63,-73,-82,107,-47,50, + 68,-101,-36,65,-81,-73,20,22,-123,109,60,27,-111,8,62,-101,-70,-32,13,-4,-126,-106,2,-71,-9,-23,-125,-45,-90,-77, + -21,-7,-125,-54,-92,-69,33,-121,-18,48,-115,-22,-22,-11,-125,-3,-71,-105,-101,-23,73,-123,16,29,40,102,-65,55,104,-47, + -39,83,-88,-25,84,-92,3,127,7,-75,40,-94,-19,108,-64,-47,50,-107,109,64,13,31,62,-106,46,119,0,-2,116,51, + -17,117,-47,-19,121,-34,55,-114,13,-10,119,-45,72,105,0,-13,-103,-74,5,90,89,33,-55,109,-3,-5,-127,85,-68,-65, + 61,-9,-111,-17,87,-90,-4,110,-64,-37,-109,54,-15,42,-119,8,85,-94,-75,-20,-100,-55,114,0,-107,69,0,-68,88,-61, + 36,15,-121,114,-39,-41,-16,35,-121,-62,-15,-110,-101,29,-71,95,52,-66,78,100,2,112,45,40,27,69,103,92,87,-1, + -126,18,1,127,-4,-2,89,-91,0,84,-68,67,-9,127,0,-1,64,-110,-124,17,-20,-60,43,-104,-123,-12,-29,-119,-27,-36, + -113,-34,46,-112,-33,-49,28,-21,-122,13,7,-126,40,11,-120,6,-101,76,-9,-126,17,35,-59,107,47,-95,70,41,-59,105, + 55,-51,102,-20,-4,-125,-58,-1,-113,-27,44,-116,-3,127,-7,3,121,38,3,87,92,90,-57,-69,58,-13,-112,0,9,-127, + 14,-5,-126,65,-17,108,53,-49,104,13,-2,126,-81,55,-81,-99,65,-47,-3,7,127,-97,65,51,-102,2,75,0,-14,-126, + -100,-23,-74,-87,-89,-27,24,124,-7,3,57,-114,-29,1,124,10,75,102,3,76,102,-14,6,-126,-74,-95,-40,-11,1,-127, + 15,0,-126,-110,-56,-31,-59,-39,105,-93,-78,37,-84,-73,61,-15,-62,110,8,-37,121,-3,35,122,63,-21,108,85,-41,84, + 120,10,39,77,-53,86,22,24,123,-105,20,-69,-111,-50,36,-100,14,77,12,-7,126,-14,-20,125,-66,-74,79,-22,-6,125, + -59,-86,73,33,-121,18,48,-115,22,-3,-71,105,-22,-11,125,-101,-23,-73,-123,16,-29,40,102,65,-39,83,88,55,104,47, + -121,37,0,-82,-1,97,31,62,106,-12,114,-55,-34,122,10,-22,121,33,78,-100,4,-10,119,45,-13,-102,74,-9,91,-88, + 49,-34,-112,8,-10,126,24,61,109,94,52,67,-15,112,58,-31,90,84,-66,-93,-55,-8,44,119,31,77,96,-59,4,112, + -68,88,61,38,18,120,96,13,82,-25,23,122,-62,-15,110,-102,27,70,99,35,72,113,34,-47,107,68,-2,94,86,-1, + 29,65,-105,-123,33,0,32,123,1,92,54,-70,127,-10,0,-1,64,110,-124,17,20,-60,43,104,-119,-27,36,-123,-12,29, + 8,29,-123,2,15,-126,58,93,-65,63,110,0,61,111,0,45,-49,-108,-11,98,81,3,-92,-87,-3,-127,3,9,-127,0, + 9,-87,-92,-22,94,-83,-12,33,-122,-14,29,-123,12,-10,-126,50,-4,117,5,-104,-73,-33,-33,-118,17,-126,0,57,-114,0, + 39,-89,-82,3,-85,-94,-12,113,-57,-4,127,-9,25,76,-99,22,75,-100,40,121,0,39,121,0,7,-35,-122,-10,-27,-124, + 1,15,-126,12,9,-126,21,79,-97,46,71,-95,53,115,0,38,121,0,-11,90,-89,25,-67,-105,17,3,-126,10,-2,-127, + 17,95,-83,47,118,-3,24,-42,-117,127,-8,0,56,4,-114,-16,89,-89,-115,-55,3,47,-118,6,-31,-123,0,3,-35,-122, + 6,-78,-100,40,36,-115,6,127,5,-18,90,-87,-12,33,-122,-57,-113,0,-58,-113,0,-36,-67,-101,-34,-77,-95,7,-80,-98, + -3,-37,-121,-2,-79,-99,-1,-14,-126,5,34,-122,1,113,-58,-11,64,109,-11,127,1,27,-124,3,2,-127,-1,-123,-31,0, + -120,-41,-1,14,-27,-123,-29,-71,-101,-111,-5,-62,68,-107,-10,10,-97,-82,9,-126,10,-5,0,-127,-76,-102,0,-20,116,-49, + -2,-28,-124,54,-112,-27,13,81,97,-11,35,-122,23,-106,-65,23,-116,-46,9,-123,-32,80,55,-81,10,-125,19,7,-126,14, + 5,-125,20,15,-124,23,-18,-126,0,-16,126,-7,-23,125,-6,10,-127,-1,-26,-124,0,12,33,122,2,16,126,54,95,66, + 51,-42,108,-40,85,-86,4,-96,83,9,-88,91,-24,31,121,-18,33,121,-33,98,74,12,-10,126,15,51,-115,15,-77,100, + -25,-34,120,3,-85,94,39,-89,82,-5,122,34,26,76,99,22,75,100,-13,-32,122,9,-36,121,1,15,126,21,79,97, + 12,9,126,46,71,95,2,93,86,18,-59,111,17,5,126,11,1,126,-2,100,79,18,-34,121,56,4,114,-50,69,94, + -115,-55,-3,6,-80,98,6,-37,121,37,36,116,42,120,6,-15,32,122,-18,92,85,-34,-77,95,-36,-67,101,7,-81,97, + -1,-80,98,-1,-38,121,-1,-14,126,11,36,121,-15,110,62,10,127,-1,2,63,-110,8,-17,126,-29,-71,101,-111,-5,62, + 119,-44,10,-1,-93,87,-5,0,127,-19,118,43,-3,-23,125,93,-81,30,34,71,-100,-5,34,122,10,-94,85,21,-98,78, + 17,-109,63,81,54,82,18,-27,123,19,3,126,11,4,126,-3,-106,-70,30,-59,-108,-14,-126,-12,53,-104,-50,62,-66,-89, + 46,-92,-75,-24,2,125,-71,101,29,-96,9,82,-9,114,-55,-6,82,-97,-8,98,-81,91,74,49,30,-56,110,84,-83,47, + -8,113,-57,-1,127,3,34,-49,112,11,-11,126,5,6,127,36,-55,-109,9,-9,-126,31,-66,-104,-29,94,81,-32,113,49, + 2,8,-127,-23,113,-54,-33,113,-47,-4,10,127,16,114,53,-126,1,13,-80,-88,44,-117,-43,-23,2,65,109,24,125,6, + -29,8,-123,3,67,-108,12,73,-103,-81,-88,42,-15,6,126,-10,0,127,-98,-73,33,13,4,126,-72,-33,-99,-95,-61,-59, + -87,-92,-5,-24,-77,-98,12,38,-121,4,-39,-121,60,-81,-77,102,-57,51,59,-108,-32,61,-84,-73,21,22,-123,118,48,6, + -110,6,63,-101,-68,-35,13,-4,-126,-105,0,-72,-10,-21,-125,-56,-81,-80,-22,-5,-125,-62,-84,-72,33,-121,-17,47,-116,-22, + -22,-11,-125,-4,-71,-105,-101,-22,73,-123,17,29,40,101,-65,55,104,-47,-39,83,-88,33,91,-82,81,97,13,-83,27,-93, + -14,110,-61,-46,49,-108,114,56,4,32,62,-106,47,118,0,6,122,35,-11,116,-51,-12,120,-41,52,-115,14,-9,119,-45, + 72,105,0,-17,-102,-74,10,92,87,31,-57,109,0,2,-127,36,-109,-54,36,-49,-112,-12,86,-93,2,107,-68,-58,-98,56, + -8,44,-119,20,82,-95,-47,8,-118,-55,115,0,-106,70,0,-68,88,-61,37,14,-121,113,-41,-40,-20,30,-122,-62,-15,-110, + -101,29,-71,92,56,-66,73,104,3,110,49,41,24,70,103,89,90,-1,-126,16,1,84,-95,-5,84,-96,0,32,-111,53, + -9,127,0,-1,64,-110,-124,18,-20,-59,43,-104,-123,-11,-29,-119,-27,-36,-114,-34,46,-112,-33,-49,23,-24,-123,13,5,-126, + 25,5,-124,3,-101,77,-10,-126,16,35,-59,107,44,-98,68,41,-67,100,56,-57,99,-20,-4,-125,-56,-8,-114,-26,43,-117, + 4,127,-6,9,121,38,8,87,92,64,-63,-90,51,-29,-113,2,8,-127,12,-8,-126,56,-35,109,39,-59,105,12,-6,126, + -65,68,-85,-78,89,-47,-2,7,127,-78,86,50,-91,57,68,-9,-5,-127,-87,65,-66,-85,-90,-27,28,124,-7,4,56,-114, + -28,3,124,12,75,102,7,76,101,-14,6,-126,-77,-93,-38,-10,1,-127,14,2,-126,-98,-73,-34,-60,-41,104,-91,-84,29, + -86,-72,60,-18,-61,110,8,-37,121,-3,35,122,66,-45,99,82,-73,64,123,-32,-3,72,-65,82,20,23,123,-104,-7,-73, + -99,-69,40,-96,-12,82,14,-4,126,-12,-20,125,-61,-76,81,-22,-5,125,-60,-85,73,33,-121,17,47,-116,22,-4,-71,105, + -22,-11,125,-101,-22,-73,-123,17,-29,40,101,65,-39,83,88,55,104,47,-52,116,1,-57,64,93,32,62,106,-12,106,-69, + -31,123,5,-19,122,28,61,-111,-1,-9,119,45,-18,-102,74,-1,90,-89,40,-46,-111,0,-7,127,60,17,111,106,-26,65, + -14,115,53,-28,92,83,-64,-94,-56,-7,44,119,29,77,97,-62,0,111,-68,88,61,37,17,120,109,-14,64,-23,23,123, + -62,-15,110,-102,27,70,84,57,76,96,66,-50,96,84,-3,59,112,-1,7,71,-105,-127,0,0,116,51,0,104,-25,-68, + 117,-49,0,-1,64,110,-124,18,20,-59,43,104,-119,-27,36,-123,-11,29,8,32,-123,0,17,-126,57,93,-65,63,110,0, + 61,111,0,45,-50,-108,-8,98,80,-3,-89,-90,-7,-127,2,5,-127,1,5,-88,-91,-16,93,-85,-9,33,-122,-12,28,-123, + 12,-10,-126,31,-41,116,13,-101,-76,-27,-41,-117,18,-126,0,58,-113,0,40,-88,-82,5,-85,-94,-6,114,-56,1,127,-3, + 24,76,-99,20,76,-100,36,122,0,38,121,0,4,-35,-122,-11,-27,-124,1,15,-126,12,9,-126,20,79,-97,45,71,-95, + 52,116,0,37,121,0,-7,93,-87,29,-65,-105,18,3,-126,10,-2,-127,27,95,-79,49,117,-4,23,-41,-118,127,-6,0, + 56,5,-114,-13,90,-88,-115,-54,3,42,-119,7,-34,-122,0,0,-35,-122,2,-80,-98,39,35,-116,25,124,5,-12,91,-87, + -9,33,-122,-59,-113,0,-55,-114,0,-36,-68,-101,-35,-76,-95,6,-85,-94,-4,-41,-120,-2,-83,-96,-1,-14,-126,6,38,-121, + 6,111,-61,0,64,110,9,127,1,25,-124,8,2,-127,5,-123,-30,0,-120,-40,-1,13,-26,-124,-26,-72,-101,-111,-5,-62, + 62,-110,-10,4,-91,-88,4,-127,5,-5,0,-127,-77,-101,0,-14,115,-51,-4,-27,-124,50,-114,-27,24,77,98,-9,37,-121, + 18,-102,-74,23,-111,-56,9,-120,-42,78,58,-81,7,-127,8,4,-127,9,0,-126,13,11,-126,15,-20,-125,0,-12,126,-13, + -17,126,-4,7,-127,4,-22,-125,0,14,33,122,3,16,126,54,94,66,45,-47,109,-17,93,-84,2,-96,83,8,-86,93, + -19,30,122,-16,34,121,-29,99,73,12,-10,126,46,19,-117,12,-76,101,-26,-36,119,5,-85,94,40,-88,82,0,122,34, + 24,76,99,20,76,100,-10,-28,124,8,-35,122,1,15,126,20,79,97,12,9,126,45,71,95,5,93,86,16,-57,112, + 17,4,126,11,-1,127,2,102,76,16,-34,121,56,5,114,-30,81,93,-115,-54,-3,6,-77,101,5,-36,122,37,35,116, + 41,120,4,-14,32,122,-16,93,85,-35,-76,95,-36,-68,101,4,-76,102,-2,-76,102,-2,-35,122,-1,-14,126,12,36,121, + -3,113,58,13,126,-2,3,63,-110,7,-17,126,-26,-72,101,-111,-5,62,102,-76,10,-4,-92,87,-5,0,127,-16,119,41, + -3,-22,125,71,-102,29,31,71,-101,-4,34,122,7,-93,86,17,-98,79,13,-109,65,58,78,82,17,-27,123,21,3,125, + 12,4,126,-1,-102,-75,32,-60,-107,-10,-125,-17,41,-105,-58,58,-62,-95,39,-88,-83,-22,-3,125,-55,89,72,-76,-7,102, + 3,121,-40,4,85,-95,1,103,-75,101,77,-8,32,-59,108,88,-89,22,-1,115,-54,6,127,6,35,-48,112,10,-11,126, + 5,6,127,37,-54,-109,9,-9,-126,27,-66,-105,-32,92,81,-34,112,49,2,8,-127,-27,111,-54,-36,112,-47,-5,10,127, + 11,114,54,-117,50,2,-78,-91,41,-121,-39,0,1,63,110,23,125,7,-29,3,-124,9,69,-107,10,75,-102,-73,-95,42, + -15,6,126,-8,0,127,-73,-97,37,12,7,126,-67,-40,-100,-86,-73,-57,-98,-75,-30,-18,-66,-107,5,38,-121,6,-39,-121, + 58,-73,-86,112,-50,33,59,-104,-43,60,-82,-76,23,21,-123,117,50,-4,-97,-38,72,-74,-95,-40,14,1,-126,-89,-41,-80, + -7,-21,-125,-48,-83,-83,-22,-6,-125,-59,-86,-72,33,-121,-17,47,-116,-22,-22,-11,-125,-4,-71,-105,-101,-23,73,-123,17,29, + 40,101,-65,55,104,-48,-39,83,-88,29,92,-82,77,100,13,-117,23,-44,-6,119,-45,-62,75,-82,112,60,-1,27,64,-106, + 39,121,0,13,121,36,-6,117,-50,-6,120,-40,45,-118,12,-10,118,-45,71,105,0,-13,-103,-73,16,91,87,29,-57,110, + 0,1,-127,41,-107,-54,38,-47,-112,-8,86,-94,8,106,-69,-55,-99,57,-8,43,-119,16,80,-97,-46,17,-117,-55,114,0, + -106,70,0,-68,88,-61,38,13,-121,111,-26,-56,-19,28,-122,-62,-15,-110,-101,29,-71,60,84,-74,50,117,4,72,93,47, + -15,73,103,26,124,-1,-121,-38,1,89,-91,-5,86,-93,0,37,-109,54,-9,127,0,-1,64,-110,-124,18,-20,-60,43,-104, + -123,-11,-29,-119,-27,-36,-113,-34,46,-112,-33,-49,19,-25,-123,13,4,-126,23,4,-125,1,-100,78,-11,-126,16,35,-59,107, + 36,-100,69,37,-71,98,55,-58,99,-21,-3,-125,-66,-8,-108,-29,54,-111,11,127,-2,15,120,40,13,86,93,55,-62,-97, + 44,-39,-113,3,8,-127,10,-10,-126,48,-46,108,26,-65,106,10,-8,126,-53,80,-84,-57,103,-48,0,8,127,-59,101,49, + -61,92,62,-9,4,-127,-36,107,-57,-93,-82,-26,35,122,-7,7,56,-114,-29,3,124,16,74,102,12,76,101,-14,7,-126, + -82,-89,-37,-8,2,-127,13,7,-126,-71,-98,-38,-64,-35,104,-106,-59,39,-89,-67,61,-19,-58,111,7,-36,122,-4,35,122, + 64,-52,97,77,-81,59,121,-37,-12,69,-70,81,21,22,123,-85,-50,-80,-69,-96,46,-74,-52,89,15,2,126,-9,-19,125, + -56,-77,84,-22,-4,125,-66,-81,73,33,-121,17,47,-116,22,-4,-71,105,-22,-11,125,-101,-23,-73,-123,17,-29,40,101,65, + -39,83,88,55,104,48,25,125,0,-11,92,87,27,64,106,-4,107,-68,-22,125,11,-12,123,31,48,-118,-5,-10,118,45, + -14,-102,74,11,90,-89,31,-54,-111,-2,-1,127,56,-26,111,79,-81,58,-6,113,57,-21,91,86,-66,-92,-58,-4,44,119, + 27,75,99,-60,11,112,-68,88,61,37,15,120,109,-18,63,-23,24,123,-62,-15,110,-102,27,70,49,85,81,48,104,-55, + 69,107,-4,-15,126,0,-33,65,-104,-113,-59,0,122,-37,-1,76,-81,-61,104,-72,0,-1,64,110,-124,18,20,-60,43,104, + -119,-27,36,-123,-11,29,12,35,-121,2,18,-126,54,94,-65,63,111,0,61,111,0,41,-52,-108,-2,98,81,-8,-86,-93, + -9,-127,1,2,-127,1,2,-89,-90,-12,94,-85,-8,33,-122,-11,28,-123,12,-10,-126,33,-40,116,15,-87,-91,-28,-39,-118, + 25,-124,0,64,-110,0,45,-86,-82,10,-84,-94,0,115,-53,7,127,1,19,77,-99,15,77,-100,27,124,0,30,124,0, + 3,-35,-122,-14,-27,-123,0,15,-126,12,10,-126,16,80,-97,42,73,-95,46,118,0,30,123,0,-1,95,-84,24,-60,-109, + 17,2,-126,10,-2,-127,8,101,-77,40,121,-2,20,-40,-119,127,1,0,56,8,-114,-10,90,-89,-115,-54,3,30,-123,4, + -29,-124,0,-3,-34,-122,-2,-81,-97,38,35,-116,22,125,5,-6,93,-86,-7,33,-122,-51,-116,0,-47,-118,0,-31,-70,-101, + -30,-79,-95,0,-87,-92,-6,-41,-120,-8,-84,-95,0,-14,-126,9,40,-120,13,110,-62,0,63,110,10,127,2,14,-126,6, + -6,-127,5,-123,-31,0,-120,-40,-1,11,-25,-124,-22,-73,-101,-111,-5,-62,65,-109,-10,-1,-86,-94,1,-127,-1,-5,-1,-127, + -77,-101,0,-7,116,-51,-6,-26,-124,45,-115,-28,19,75,100,-7,38,-121,10,-95,-84,20,-101,-74,10,-111,-60,34,91,-82, + 5,-127,-7,2,-127,1,-3,-127,2,9,-127,2,-21,-125,0,-5,126,-11,-11,127,-1,2,-127,3,-14,-126,0,16,32,122, + 5,15,126,53,94,66,40,-50,110,3,96,-83,-1,-96,83,4,-87,92,-16,30,122,-12,34,122,-22,99,76,12,-10,126, + 46,-15,-117,9,-74,103,-30,-29,120,9,-84,94,44,-86,82,6,122,36,19,77,99,15,77,100,-12,-27,124,6,-35,122, + 0,15,126,16,80,97,12,10,126,42,73,95,9,93,86,14,-57,113,17,2,126,11,-1,127,-7,104,73,16,-34,121, + 55,8,114,-11,86,93,-115,-54,-3,2,-78,100,4,-36,122,36,35,117,42,120,0,-12,33,122,-11,93,85,-30,-79,95, + -31,-70,101,-1,-78,100,-7,-77,100,-4,-35,122,0,-14,126,14,35,121,10,113,57,24,125,-2,8,62,-111,7,-17,126, + -22,-73,101,-111,-5,62,84,-95,10,-5,-91,88,-5,-1,127,-10,120,42,-3,-22,125,48,-114,28,29,70,-102,-2,34,122, + 6,-92,87,14,-97,80,9,-108,66,4,98,81, +}; +static const unsigned short g_rcatI[] = { + 0,2,1,3,5,4,6,8,7,9,11,10,12,14,13,15,17,16, + 15,19,18,20,22,21,23,25,24,26,27,22,28,30,29,31,32,26, + 33,35,34,12,37,36,38,40,39,9,42,41,43,45,44,46,48,47, + 49,51,50,52,54,53,55,54,56,3,4,51,17,57,53,44,45,58, + 59,61,60,62,63,59,64,65,63,34,35,11,4,5,66,67,69,68, + 70,71,67,71,69,67,0,1,16,72,74,73,29,76,75,35,77,11, + 10,79,78,80,82,81,83,19,15,78,14,12,19,85,84,16,86,0, + 76,26,29,70,68,71,71,68,69,87,88,73,6,7,89,9,10,36, + 12,13,39,15,16,1,90,1,2,20,21,91,23,24,28,26,22,31, + 28,29,92,92,94,93,85,96,95,12,36,10,9,41,34,43,44,97, + 98,62,99,49,50,100,101,103,102,55,56,104,3,51,49,8,105,7, + 62,59,60,47,106,46,34,11,9,4,68,107,70,67,66,80,81,105, + 78,12,10,16,54,55,108,103,74,109,111,110,111,113,112,59,114,61, + 94,115,93,93,117,116,118,88,87,74,72,119,120,108,121,120,123,122, + 102,125,124,126,128,127,129,131,130,132,134,133,135,137,136,138,140,139, + 15,141,17,142,144,143,145,147,146,148,143,149,150,152,151,153,148,154, + 33,155,35,138,157,156,158,160,159,135,162,161,163,165,164,166,168,167, + 169,171,170,172,174,173,175,176,173,129,170,131,17,174,57,165,177,164, + 178,180,179,181,178,182,183,182,184,155,136,35,131,185,130,186,188,187, + 189,186,190,190,186,187,126,141,128,191,193,192,152,195,194,35,136,77, + 137,78,79,80,196,82,197,15,18,78,138,139,18,199,198,141,126,200, + 194,152,148,189,190,188,190,187,188,201,193,88,132,202,134,135,157,137, + 138,160,140,15,128,141,203,127,128,142,204,144,145,150,147,148,153,143, + 150,205,152,205,207,206,198,209,208,138,137,157,135,155,162,163,210,165, + 211,212,181,169,213,171,101,102,214,175,215,176,129,169,170,133,134,216, + 181,180,178,168,166,217,155,135,136,131,218,188,189,185,186,80,216,196, + 78,137,138,141,175,173,219,192,214,220,222,221,221,224,223,178,179,225, + 206,207,226,207,228,227,118,201,88,192,229,191,230,231,219,230,233,232, + 75,92,29,75,94,92,234,106,39,234,235,106,236,238,237,236,72,238, + 55,86,16,55,239,86,2,240,90,2,21,240,241,243,242,241,244,243, + 83,1,90,83,15,1,115,75,76,115,94,75,95,246,245,95,247,246, + 248,4,107,248,51,4,32,115,76,32,249,115,17,54,16,17,53,54, + 250,100,251,250,49,100,252,254,253,252,255,254,38,106,47,38,39,106, + 63,44,59,63,97,44,78,256,14,78,257,256,258,260,259,258,261,260, + 247,262,246,247,263,262,264,266,265,264,259,266,266,268,267,266,269,268, + 14,270,13,14,256,270,53,80,105,53,57,80,271,251,235,271,250,251, + 239,104,272,239,55,104,56,247,95,56,273,247,274,33,48,274,275,33, + 276,235,234,276,271,235,277,267,268,277,278,267,28,279,30,28,24,279, + 280,66,5,280,70,66,281,89,282,281,6,89,262,244,283,262,284,244, + 285,51,248,285,50,51,31,249,32,31,117,249,100,274,48,100,50,274, + 236,275,274,236,237,275,85,83,96,85,19,83,99,110,286,99,109,110, + 121,74,119,121,108,74,240,22,27,240,21,22,45,112,58,45,111,112, + 245,288,287,245,246,288,99,60,109,99,62,60,10,77,79,10,11,77, + 289,291,290,289,292,291,280,68,70,280,107,68,38,65,64,38,47,65, + 12,40,37,12,39,40,108,122,125,108,120,122,293,263,294,293,295,263, + 236,50,285,236,274,50,105,296,7,105,81,296,297,39,13,297,234,39, + 265,267,278,265,266,267,89,289,282,89,292,289,272,279,24,272,298,279, + 36,300,299,36,37,300,301,295,293,301,302,295,293,8,6,293,294,8, + 303,122,123,303,304,122,305,234,297,305,276,234,68,66,67,68,4,66, + 277,254,278,277,253,254,265,254,255,265,278,254,255,264,265,255,306,264, + 0,21,2,0,91,21,120,307,123,120,121,307,7,292,89,7,296,292, + 44,114,59,44,58,114,107,121,248,107,307,121,117,115,249,117,93,115, + 235,46,106,235,251,46,116,23,93,116,308,23,56,96,104,56,95,96, + 309,242,310,309,241,242,40,300,37,40,98,300,52,294,273,52,8,294, + 119,248,121,119,285,248,258,264,311,258,259,264,92,23,28,92,93,23, + 303,280,312,303,123,280,246,283,288,246,262,283,256,287,270,256,313,287, + 283,241,314,283,244,241,298,83,90,298,96,83,85,245,84,85,95,245, + 86,25,315,86,239,25,316,300,286,316,299,300,272,25,239,272,24,25, + 287,317,270,287,288,317,318,271,276,318,319,271,316,110,111,316,286,110, + 304,125,122,304,124,125,251,48,46,251,100,48,82,258,81,82,261,258, + 320,49,250,320,3,49,52,105,8,52,53,105,107,123,307,107,280,123, + 97,42,43,97,41,42,112,114,58,112,113,114,40,64,98,40,38,64, + 321,60,61,321,109,60,86,91,0,86,315,91,102,108,125,102,103,108, + 310,318,309,310,322,318,91,308,20,91,315,308,48,34,47,48,33,34, + 293,281,301,293,6,281,298,240,279,298,90,240,317,13,270,317,297,13, + 259,269,266,259,260,269,273,263,247,273,294,263,238,73,88,238,72,73, + 30,26,29,30,27,26,320,323,3,320,324,323,273,54,52,273,56,54, + 279,27,30,279,240,27,296,291,292,296,311,291,101,87,103,101,118,87, + 263,284,262,263,295,284,25,308,315,25,23,308,314,297,317,314,305,297, + 314,309,305,314,241,309,47,41,65,47,34,41,319,322,325,319,318,322, + 81,311,296,81,258,311,114,321,61,114,113,321,309,276,305,309,318,276, + 320,325,324,320,319,325,5,312,280,5,326,312,313,327,84,313,328,327, + 29,32,76,29,26,32,329,244,284,329,243,244,302,284,295,302,329,284, + 5,323,326,5,3,323,104,298,272,104,96,298,290,306,330,290,291,306, + 300,99,286,300,98,99,20,31,22,20,117,31,316,45,43,316,111,45, + 36,42,9,36,299,42,299,43,42,299,316,43,288,314,317,288,283,314, + 311,306,291,311,264,306,20,116,117,20,308,116,319,250,271,319,320,250, + 330,255,252,330,306,255,328,256,257,328,313,256,321,111,109,321,113,111, + 74,87,73,74,103,87,98,63,62,98,64,63,119,236,285,119,72,236, + 19,327,18,19,84,327,65,97,63,65,41,97,287,84,245,287,313,84, + 195,205,206,195,152,205,331,217,332,331,160,217,333,238,191,333,237,238, + 175,200,334,175,141,200,127,335,144,127,203,335,336,243,337,336,242,243, + 197,128,15,197,203,128,226,195,206,226,194,195,209,339,338,209,340,339, + 341,131,170,341,218,131,154,226,342,154,194,226,17,173,174,17,141,173, + 343,213,169,343,344,213,252,346,345,252,253,346,158,217,160,158,168,217, + 182,165,210,182,178,165,78,347,257,78,139,347,348,260,261,348,349,260, + 338,351,350,338,339,351,352,353,349,352,354,353,353,268,269,353,355,268, + 139,356,347,139,140,356,174,80,57,174,216,80,357,344,343,357,332,344, + 334,215,175,334,358,215,176,338,359,176,209,338,360,33,275,360,167,33, + 361,332,357,361,331,332,277,355,362,277,268,355,150,363,147,150,151,363, + 364,185,189,364,130,185,281,202,132,281,282,202,351,337,365,351,366,337, + 367,170,171,367,341,170,153,342,227,153,154,342,213,360,171,213,167,360, + 333,275,237,333,360,275,198,197,18,198,208,197,212,222,220,212,368,222, + 231,192,219,231,229,192,335,143,144,335,149,143,164,224,221,164,177,224, + 340,369,339,340,370,369,212,180,181,212,220,180,137,77,136,137,79,77, + 289,372,371,289,290,372,364,188,218,364,189,188,158,184,168,158,183,184, + 138,159,160,138,156,159,219,233,230,219,124,233,373,350,374,373,375,350, + 333,171,360,333,367,171,216,376,196,216,134,376,377,160,331,377,140,160, + 354,355,353,354,362,355,202,289,371,202,282,289,358,363,378,358,147,363, + 157,379,156,157,380,379,301,374,302,301,373,374,373,133,375,373,132,133, + 303,233,304,303,232,233,381,331,361,381,377,331,188,185,131,188,186,185, + 277,346,253,277,362,346,354,346,362,354,345,346,345,352,382,345,354,352, + 126,144,204,126,127,144,230,383,231,230,232,383,134,371,376,134,202,371, + 165,225,177,165,178,225,218,231,383,218,341,231,227,226,207,227,342,226, + 332,166,344,332,217,166,228,145,384,228,207,145,176,208,209,176,215,208, + 385,242,336,385,310,242,159,379,211,159,156,379,172,375,133,172,359,375, + 229,341,367,229,231,341,348,352,349,348,386,352,205,145,207,205,150,145, + 303,364,232,303,312,364,339,366,351,339,369,366,347,370,387,347,356,370, + 366,336,337,366,388,336,378,197,208,378,203,197,198,340,209,198,199,340, + 200,146,334,200,389,146,390,379,380,390,368,379,358,146,147,358,334,146, + 370,391,369,370,356,391,392,357,393,392,361,357,390,222,368,390,221,222, + 304,233,124,344,167,213,344,166,167,82,348,261,82,196,348,394,169,129, + 394,343,169,172,216,174,172,133,216,218,232,364,218,383,232,210,161,162, + 210,163,161,224,225,223,224,177,225,159,183,158,159,211,183,395,180,220, + 395,179,180,200,204,389,200,126,204,102,219,214,102,124,219,310,392,322, + 310,385,392,204,384,389,204,142,384,167,155,33,167,168,155,373,281,132, + 373,301,281,378,335,203,378,363,335,391,140,377,391,356,140,349,269,260, + 349,353,269,359,350,375,359,338,350,238,193,191,238,88,193,151,148,149, + 151,152,148,394,323,324,394,129,323,359,173,176,359,172,173,363,149,335, + 363,151,149,376,372,386,376,371,372,101,201,118,101,214,201,350,365,374, + 350,351,365,146,384,145,146,389,384,388,377,381,388,391,377,388,385,336, + 388,381,385,168,162,155,168,184,162,393,322,392,393,325,322,196,386,348, + 196,376,386,225,395,223,225,179,395,385,361,392,385,381,361,394,325,393, + 394,324,325,130,312,326,130,364,312,387,327,328,387,199,327,152,154,148, + 152,194,154,329,337,243,329,365,337,302,365,329,302,374,365,130,323,129, + 130,326,323,215,378,208,215,358,378,290,382,372,290,330,382,379,212,211, + 379,368,212,142,153,227,142,143,153,390,164,221,390,163,164,157,161,380, + 157,135,161,380,163,390,380,161,163,369,388,366,369,391,388,386,382,352, + 386,372,382,142,228,384,142,227,228,393,343,394,393,357,343,330,345,382, + 330,252,345,328,347,387,328,257,347,395,221,223,395,220,221,192,201,214, + 192,193,201,211,182,183,211,181,182,229,333,191,229,367,333,18,327,199, + 184,210,162,184,182,210,370,199,387,370,340,199, +}; + +#define OCAT_NV 716 +#define OCAT_NT 1459 +#define OCAT_QS (1.0f/16000.0f) +static const short g_ocatV[] = { + -18,-324,-12021,-160,-114,-10973,83,-542,-12851,191,-646,-13923,261,417,-11945,-880,-628,-12952, + -750,-254,-11995,-646,-80,-11151,279,502,-10897,-53,1405,-10987,-184,1169,-11948,115,1605,-9901, + 36,428,-9993,-909,555,-10896,-742,1322,-10966,-820,1505,-9882,-798,542,-9944,-967,512,-11921, + -609,1140,-11911,-1065,2524,9856,-1083,1483,9764,-81,1451,9458,1038,1766,9291,1071,2599,9633, + 2,4880,8931,1129,5645,8550,126,5470,8401,1101,412,7794,-54,412,7816,2312,6490,8807, + 2056,4601,8733,2317,3796,8891,2104,5563,6558,2960,-6703,6502,2395,-6790,6432,3360,-1665,6830, + 3785,-500,6415,3527,-431,6839,3712,-2368,6250,3865,-1650,6254,3572,1757,6990,3774,1626,6377, + 3807,2773,6526,3279,2452,7611,2799,-3238,2739,3114,-2597,2576,3488,-2598,3159,3113,-3542,3331, + 3807,-1638,3413,3512,-1827,1477,3096,-2561,1322,2053,-3654,1527,2605,-3214,937,3152,-2550,-1552, + 2879,-3249,-1973,2946,-3533,-2645,2302,-3928,-2682,-52,-3969,-2043,116,-3675,-2712,1281,-3931,-1767, + -141,-4204,-1685,2107,-6547,-4782,1485,-6384,-4987,2146,-6466,-5662,3435,507,-4921,3690,1446,-4427, + 3698,449,-4349,1044,4643,-5861,1080,3616,-6809,940,4152,-6529,1790,3228,-6542,1995,508,-6560, + 1027,551,-7089,1784,-500,-6516,1035,-546,-6841,36,-431,-6950,146,-1395,-6509,-738,-416,-6817, + 1045,6043,6537,1149,5723,5468,2023,3665,3332,1036,3819,3276,1087,4303,3799,2075,4536,4577, + 1,-2644,-4823,286,-3294,-3767,-354,-2933,-4008,-1,4464,-6756,169,740,-8979,-800,731,-9099, + -887,725,-7516,89,655,-7539,683,899,-7406,24,238,-7201,-724,282,-7189,1189,1496,-7213, + 826,1469,-7652,459,1448,-8899,6,567,-12727,49,2222,-8732,-47,2854,-7925,-67,3531,-7456, + 533,3508,-7237,1132,2650,-7143,1838,2271,-6785,1978,1398,-6696,2538,1450,-6211,2309,3008,-6217, + 2176,3730,-5828,1824,4268,-5690,1113,4911,-4872,2045,4391,-4785,-24,5040,-5897,97,5266,-4774, + 603,5195,-4441,125,5296,-3654,-12,5259,-3049,-127,5108,-2365,69,4819,-1660,-10,4431,-562, + 106,4215,110,48,4075,656,33,3875,1424,1029,3605,1474,1055,3576,2494,2262,3929,3959, + 2109,3267,2544,2554,2823,2154,3057,2560,2507,2921,2311,1330,2238,2871,1466,3401,1759,1374, + 3685,1192,667,3859,717,1506,3331,1759,239,3817,531,451,3721,307,-613,3527,978,-822, + 3198,1697,-726,3685,-398,-650,3500,-1303,-700,3948,-482,-1693,3570,-1371,-1315,3846,-1427,-1778, + 2103,-3593,-1683,996,-4003,-2632,1408,-4125,-2621,1908,-4113,-2904,1357,-4385,-3777,2123,-4449,-3863, + 2620,-4421,-4229,3190,-3499,-3778,2173,-4925,-4725,2765,-4370,-4842,3136,-3551,-4784,1999,-5753,-4978, + 2280,-5705,-5741,2672,-4480,-5596,2503,-4680,-5945,2623,-6498,-4977,1305,-4736,-4807,778,-3861,-3612, + 829,-3678,-4775,132,-2238,-5653,134,-1821,-6185,-723,-1620,-5997,732,-2636,-5935,919,-3563,-5842, + 1451,-5691,-5115,1091,-4563,-5749,1300,-2575,-6421,946,-1633,-6472,2274,-462,-6186,2949,-393,-5525, + 2464,153,-6098,2897,598,-5714,2931,1718,-5785,2925,2546,-5668,2918,3135,-5382,2484,3870,-5189, + 2885,3630,-4649,1810,4513,-3878,1122,4895,-3615,1082,4742,-2827,700,4346,-867,1145,3704,564, + 1722,3230,1524,2275,2847,387,2817,2395,386,2837,2252,-805,3903,562,-1696,3416,1523,-1527, + 3705,-2154,-2081,3905,-1651,-2605,3487,-2879,-2611,3724,-2410,-2806,3555,-2817,-3261,3696,-2331,-3837, + 3864,-1405,-3676,3402,-2448,-4821,2805,-3573,-5645,2905,-2657,-5588,2450,-3477,-6151,1930,-3578,-6561, + 1959,-4469,-6473,1893,-5148,-6349,1332,-5699,-5588,1429,-6341,-5505,1237,-3551,-6548,2240,-2420,-6097, + 1670,-1517,-6400,2156,-1488,-6179,2836,-1493,-5634,3419,-474,-4877,3457,1490,-4978,3387,2624,-4792, + 3629,2202,-4408,2981,3587,-3776,2789,3395,-2829,2430,4088,-3702,1793,4256,-2815,2162,3908,-2675, + 1121,4376,-1855,1227,3903,-577,1824,3220,359,2464,2718,-766,1758,3436,-530,2469,2850,-1469, + 3013,2370,-1803,3782,1687,-2555,4101,-388,-2664,4073,644,-2750,3918,-420,-3667,3646,-1429,-4469, + 3675,-559,-4386,3452,-1577,-4827,1862,-2702,-6392,1418,-4492,-6438,1560,-5178,-6292,3919,427,-3616, + 3809,1535,-3800,3730,2154,-3702,3511,2768,-3736,3634,2399,-3183,3146,2799,-2572,2014,3512,-1674, + 2922,-2601,-727,3566,-1447,489,3841,-483,544,3354,2837,3317,2905,3391,3633,3524,3094,4148, + 3040,3683,4491,1143,4781,4242,2323,4940,5344,1890,5371,5499,2541,5073,6035,3033,4554,6436, + 2849,4668,7597,2196,5712,7681,2241,5760,8603,1174,6001,7625,5,5929,7562,2871,3657,8372, + 2703,4408,8259,3314,3581,7531,3536,3613,6637,3426,3731,5558,2849,4460,5459,3709,3313,6342, + 3757,3197,5513,3631,2738,7098,2803,2560,8363,21,4308,9416,963,3554,9664,1100,4457,9233, + 136,3427,9797,-935,3527,9562,42,2719,9851,1931,2572,9299,2371,2465,8914,1983,1559,8607, + 3062,1633,7648,2995,484,7404,3500,457,6777,3744,587,6253,3004,-486,7253,3988,462,5553, + 4023,-360,5534,4092,-1475,5503,4084,-628,4509,4043,-1612,4370,3779,-2336,3605,2173,-3679,2598, + 3783,-1137,1416,3155,-2325,391,2651,-3101,254,1905,-3845,363,1944,-3706,-691,1166,-4171,-655, + -141,-4481,-703,3947,-315,1492,3726,2588,3649,4006,1725,3649,3755,1566,2670,3985,2573,4482, + 4043,2471,5549,4077,1650,5511,4169,1365,4338,4086,343,4643,3501,-1746,2495,3958,-577,3576, + 291,-4624,480,1073,-4349,544,1474,-4047,1923,2353,-3928,3167,2232,-4255,3673,2196,-4652,4410, + 2093,-4589,5551,2358,-5610,5565,2447,-5761,6188,2999,-5761,6269,3039,-4484,6296,2971,-6620,5456, + 2436,-6613,5612,3111,-4743,4280,2957,-5553,4700,3265,-5793,5437,2525,-5241,4974,2938,-4390,3791, + 3493,-3898,3987,4004,-2608,4547,3843,-3378,4466,3975,-2553,5646,1685,1943,9183,1061,899,8372, + 1227,1287,8809,1715,949,8164,2060,517,7763,3306,-2616,6439,3132,-3559,6264,3727,-3420,6029, + 3894,-3556,5495,3579,-4809,5613,3706,-4483,5360,2969,-6254,4944,2429,-4690,6177,2131,-3619,6249, + -53,867,8517,41,1116,8984,-778,914,8451,2216,-2625,6735,2158,-1461,7141,2502,-1988,7008, + 1131,-4401,5548,910,-3609,6347,863,-2591,6792,1263,-2017,6966,1132,-1453,7152,1103,-403,7424, + 22,-565,7446,55,-4542,5471,895,-4597,4508,118,-4619,4324,1044,-4275,3412,-10,-4462,3408, + 160,-4497,2571,239,-4615,1423,950,-4373,1467,1026,-4247,2521,2608,6007,8536,2609,6260,8747, + 496,1893,-8326,763,2506,-7592,3396,1934,2177,3684,-1231,2498,3877,-579,2495,2161,-492,7392, + 2906,-1349,7144,1985,3605,9272,1231,4898,8921,1719,4279,9141,3970,592,3317,3932,570,2366, + 2656,1845,8170,2378,1098,8011,-1202,2041,10099,-1194,1203,8726,81,-1310,7243,-713,4308,9188, + -1018,4685,8675,3703,-4256,4837,21,-2517,6849,-46,-3524,6441,181,-4149,6072,-682,-4545,2453, + -729,-4691,1392,-732,-4739,416,-1694,2459,9397,-1743,1613,8697,-2404,2468,7662,-822,5290,8176, + -1113,5706,7481,-1015,507,7617,-772,-380,7277,-1955,6726,6532,-2173,4769,6694,-1995,5624,6526, + 79,5990,5657,-2750,-6690,6609,-2076,-6657,6592,-2797,-1573,5434,-2223,-2554,5769,-2845,-2494,5321, + -3408,1617,4480,-3001,2687,4562,-2979,2524,3397,-3398,1527,3423,-2989,-3561,3438,-3372,-2574,3380, + -3924,-1456,1441,-3642,-2353,1581,-2924,-3575,1444,-3976,-2502,-1659,-3726,-3343,-2660,-3060,-3882,-2623, + -3153,-3707,-1775,-3619,-3243,-1762,-1100,-4235,-1678,-2068,-4157,-1768,-1206,-3836,-2618,-3054,-6681,-5648, + -2358,-6430,-5861,-4260,1429,-4734,-4381,1628,-3944,-2907,-6384,-6356,-1122,5091,-5971,-960,4367,-6958, + -2037,4533,-6437,-1877,3888,-7026,-2918,1468,-6806,-3005,496,-6714,-3472,1124,-6262,-1195,-1238,-6544, + -1971,-423,-6994,-1072,-2751,-4810,-1140,-3457,-3675,-1030,1433,-8830,-1208,1320,-7922,-1709,838,-7408, + -2032,429,-7204,-1778,1594,-7543,-758,706,-12753,-832,2154,-8767,-1035,2806,-7829,-1025,3517,-7550, + -1635,3383,-7493,-2165,2684,-7197,-1698,2556,-7478,-2344,1546,-7225,-3142,2641,-6669,-3001,3687,-6474, + -2875,4481,-5842,-2141,5028,-5695,-1808,5365,-4755,-1265,5335,-5298,-785,5382,-4727,-968,4929,-1821, + 64,3721,2473,111,3949,3208,-922,3622,2459,-948,3814,1381,-1889,3419,1307,-1746,3277,2321, + -2016,3717,328,-3235,2638,513,-2854,2584,1351,-3357,1688,1468,-3853,1518,414,-3615,1630,957, + -3763,542,1429,-4116,518,320,-4011,1490,-804,-4197,611,-656,-4159,-436,414,-4213,-309,-798, + -4044,-1351,-585,-4419,-470,-1660,-4281,-1480,-1691,-3790,-2307,-791,-2176,-4177,-2666,-1597,-4045,-2844, + -2078,-4365,-3729,-3015,-4319,-3691,-3852,-3531,-3708,-3023,-4790,-4831,-3874,-3524,-4764,-3626,-4115,-4709, + -3419,-4691,-5774,-3047,-5787,-5689,-2942,-5640,-6482,-3554,-6693,-5637,-2750,-5215,-5189,-2265,-5707,-5783, + -2124,-4705,-4854,-1630,-3960,-4080,-1378,-3391,-4342,-1672,-3704,-4887,-1101,-2201,-5580,-2039,-1539,-6514, + -1713,-2558,-5999,-1729,-3554,-5822,-1966,-4665,-5848,-2206,-3504,-6639,-2263,-2540,-6474,-3078,-612,-6539, + -3785,-472,-5773,-3870,567,-5661,-3813,1455,-5773,-3895,2636,-5785,-3737,3638,-5643,-2961,4764,-4777, + -2069,5311,-3703,-873,5436,-3705,-1027,5382,-3008,-1038,4391,-545,-895,4198,88,-2846,3248,155, + -2921,3552,-735,-3493,2770,-414,-3768,2091,-66,-3743,2344,-751,-4382,522,-1600,-4191,-2553,-2727, + -4441,-1593,-2741,-4285,-2495,-3819,-4428,-1584,-3855,-4081,-2540,-4835,-3674,-3426,-5634,-3733,-2471,-5557, + -3409,-3644,-6192,-3210,-2472,-6103,-2927,-3462,-6628,-2840,-2601,-6469,-2947,-4490,-6613,-3086,-6611,-5207, + -2295,-4629,-6550,-2678,-1581,-6437,-3203,-1760,-6128,-3731,-1353,-5705,-4236,-489,-4817,-4265,475,-4762, + -4156,2551,-4825,-3872,3627,-4682,-3886,3479,-3753,-3166,4572,-3866,-3526,3991,-3254,-2887,4570,-2736, + -2367,5004,-2696,-1840,5295,-2857,-1940,4641,-1620,-986,4045,581,-2299,3943,-544,-3221,3635,-1715, + -3477,3065,-1203,-3796,2633,-1686,-4178,1570,-1724,-4556,-392,-2838,-4491,-515,-3729,-4217,-1473,-4766, + -2364,-5546,-6381,-4461,446,-3765,-4255,2500,-3738,-4369,1584,-2707,-4044,2771,-2739,-3698,3404,-2588, + -3378,3948,-2641,-2595,4335,-1803,-1747,4229,-553,-4511,576,-2779,-3593,-4182,-5550,-3539,-2997,-814, + -3998,-1502,379,-2782,2437,2350,-2272,2913,2349,-2328,3026,1630,-884,3876,3273,62,4460,3682, + 889,5347,4691,5,5463,4665,-1027,5806,5635,-888,5277,4705,-1691,5638,5814,-1274,6384,6633, + -777,6068,6521,-44,6147,6660,-1316,6270,7158,-2163,4671,5628,-1859,4595,4629,-992,4830,4184, + -995,4391,3728,-1562,4292,3874,-107,5080,4193,-2013,3533,3453,-2625,3408,4464,-2274,3921,4353, + -2432,4034,5223,-1826,4303,8373,-2001,4798,7355,-1670,3333,9184,-2041,3533,8590,-2079,2707,8653, + -2594,2857,7334,-2640,2674,6590,-2664,3602,6549,-2640,3543,5469,-2850,2674,5430,-3475,466,4582, + -3194,593,5523,-3551,574,3995,-3421,-543,4467,-2985,-556,5500,-3542,-107,4232,-3586,-401,3558, + -3505,-913,3798,-3277,-1496,4511,-3449,-1614,3481,-3363,-2537,4395,-3348,-3664,4411,-2912,-3500,2550, + -3352,-2548,2552,-3754,-2278,487,-3035,-3593,391,-3092,-3611,-600,-2052,-4428,-623,-1200,-4566,-663, + -4027,-563,1242,-3307,1502,2463,-2343,2944,3013,-3572,322,2774,-3599,462,3390,-3524,962,3672, + -3506,-1725,2673,-3650,-1523,2357,-3555,-1033,3165,-2032,-4465,545,-2330,-3969,2517,-2282,-3981,3156, + -2124,-4287,3629,-1892,-4809,4317,-1128,-4578,5593,-1434,-5349,5539,-1985,-5356,4744,-1789,-5850,5354, + -1985,-5809,6353,-1889,-4565,6233,-2662,-6423,5755,-2054,-6524,5631,-2373,-6645,7057,-2595,-6661,7043, + -2906,-4505,4354,-2556,-4179,3767,-2730,-5192,4793,-2668,1560,6426,-2378,1533,6867,-2465,2203,6895, + -2052,1598,7515,-2676,729,6333,-1867,-3602,6070,-2216,-3254,5889,-1409,-4553,6112,-1135,-3642,6165, + -2804,-4618,5543,-2811,-3491,5463,-2689,-5641,5475,-2606,-5854,6139,-1700,928,7222,-1098,-2470,6701, + -1784,-2423,6323,-2020,-1464,6342,-950,-1546,6908,-723,-1039,7078,-1006,-4596,4516,-1064,-4405,3474, + -1928,-4363,1504,-1711,-4228,2637,-322,-1985,7012,-1643,1676,9496,-1662,5277,7328,-3578,-2359,2047, + -3676,-503,2364,-3154,1608,5364,-2602,-478,6112,-2040,-408,6532,-2530,-1087,6024,-2365,-1880,6002, + -2407,3734,7786,-2622,3416,7374,-2529,4183,6725,-3506,635,2369,-1251,-547,6926,-2078,466,6719, + -696,-1547,-15910,-609,-863,-15602,-884,-731,-14972,-354,-1450,-15822,-392,-842,-15538,-132,-727,-15014, + -144,-1278,-14995,-211,-1195,-14078,-829,-1365,-15003,-796,-1158,-14149,-980,-720,-13933,-291,-1061,-13391, + -726,241,-13921,-178,244,-13914, +}; +static const signed char g_ocatN[] = { + -41,109,-51,-35,114,-44,-61,102,-44,-126,7,15,-127,2,7,74,89,-53,84,89,-33,60,102,-45,-65,104,-35,-5,-125,22, + -20,-117,46,-61,-109,24,-41,114,-38,86,85,-39,61,-109,22,68,-102,32,59,109,-26,124,-23,10,57,-106,41,29,-5,-123, + 40,83,-88,-25,53,-113,-38,62,-104,-32,28,-120,20,-79,-97,8,-70,-106,28,-101,-71,-8,75,-102,16,79,-99,-80,28,-94, + -76,-45,-91,-90,-17,-88,-73,-103,15,-72,66,-81,116,27,-43,-71,31,-100,-116,-3,-51,-93,4,-87,-99,41,-69,-112,9,-59, + -112,17,-57,-116,14,-49,-122,-9,-34,-111,5,-61,-82,61,76,-102,74,19,-107,46,51,-88,57,71,-122,18,30,-115,54,4, + -104,73,4,-84,95,0,-95,84,-1,-100,65,-44,-84,78,-55,-80,93,-31,-36,111,-49,-12,114,54,11,119,42,-22,122,26, + -13,118,44,-49,101,-60,75,18,-101,-102,65,38,-109,6,65,-120,-1,40,-120,0,43,-53,-99,60,-61,-64,91,-57,-78,82, + -70,-45,96,-75,15,102,-47,31,114,-59,19,111,-21,35,120,-2,44,119,17,60,111,1,55,114,-26,-124,9,-39,-113,42, + -38,-106,59,-21,-109,62,-31,-91,83,-59,-89,69,40,104,61,78,94,36,-10,112,59,-30,-90,85,-54,114,-17,70,106,0, + 31,116,41,-46,109,47,-65,46,99,-5,63,110,8,64,109,-75,10,102,-112,33,49,-123,-31,6,-105,-64,33,-44,-99,67, + -18,-87,91,-32,-70,101,-63,-51,98,-76,-27,98,-74,-14,102,-79,2,100,-88,3,92,-89,-26,87,-82,-66,71,-68,-90,58, + -54,-113,20,-77,-100,17,-26,-117,43,-24,-124,17,-45,-119,1,-31,-123,-10,-31,-119,-31,-7,-121,-38,-36,-116,-38,-9,-119,-43, + -20,-120,-37,-20,-122,-30,-10,-125,-22,-44,-118,-14,-32,-122,17,-58,-95,62,-56,-106,41,-74,-99,29,-87,-81,45,-88,-90,15, + -77,-100,10,-108,-65,14,-117,-47,10,-123,-30,10,-109,-64,14,-124,-22,14,-126,-11,1,-118,-46,-2,-108,-66,4,-126,17,-6, + -119,43,-6,-123,14,-27,-113,41,-41,-114,33,-46,-64,107,-22,24,123,-23,0,117,-49,-13,120,-39,35,117,-37,-50,108,-45, + -80,87,-46,-107,68,-7,-69,91,-56,-111,62,6,-118,34,31,-60,17,-110,-93,57,65,-112,59,10,-111,28,55,0,0,0, + 103,65,-37,90,90,8,105,65,29,70,86,61,37,76,95,-9,82,97,86,54,77,120,26,31,109,21,-61,123,26,14, + 39,-5,121,5,37,121,-81,10,97,-97,6,81,-88,11,91,-98,10,80,-102,0,76,-97,-22,79,-100,-55,56,-86,-81,45, + -95,-83,15,-70,-105,-10,-57,-113,-12,-56,-110,-30,-49,-112,-34,-60,-110,-24,-61,-111,3,-84,-95,-5,-92,-87,5,-105,-71,-11, + -122,-23,-27,-109,-57,-32,-111,49,-38,-124,25,-6,-104,66,-30,-123,32,1,-115,53,4,-121,31,24,-124,16,22,-116,22,46, + -112,13,59,-100,7,77,-89,-7,91,-47,-4,118,-49,22,115,-29,45,115,95,52,65,72,73,75,76,10,101,-65,-4,109, + -37,25,119,-70,9,106,-93,2,86,-107,2,69,-110,-4,63,-113,-45,37,-121,-25,28,-100,-78,-11,-92,-78,-40,-78,-99,-16, + -72,-98,-36,-80,-90,-40,-69,-101,-34,-79,-96,-26,-77,-101,-9,-93,-85,-12,-85,-92,-19,-93,-77,-39,-100,-67,-39,-115,-46,-29, + -127,6,1,-126,-18,0,-123,3,30,-120,8,42,-119,1,45,-113,7,58,-51,-31,112,84,31,90,42,81,89,-123,-3,30, + -125,-19,15,-123,-30,7,-111,-61,-6,-117,-46,-20,-96,-70,-45,-86,-86,-37,-102,75,-1,-119,43,13,-125,16,15,-81,-81,55, + -65,-97,51,-89,-84,34,-83,-86,43,-34,-83,90,-71,-79,69,-60,-98,54,-87,-88,28,-107,-68,-4,-106,-64,-28,-104,-72,-8, + -66,-60,-90,-10,-125,-18,13,-120,-38,-106,-17,-68,-91,-51,-72,-117,-27,-42,-116,-45,-27,-105,-68,21,-90,-77,46,-116,-51,-4, + -110,-62,13,-108,-27,-61,-100,18,-76,6,-63,-110,-30,-25,-121,-24,-60,-109,7,-34,-122,51,-55,-102,-15,19,-125,-57,23,-111, + -92,21,-85,-65,67,-86,-92,31,-82,-70,21,-104,-105,5,-71,-119,1,-46,-58,18,-112,-124,0,-29,-126,-5,-18,-126,2,-13, + -127,2,9,-125,7,19,-119,20,38,-67,104,30,-122,36,3,-112,59,9,-98,80,11,-78,100,5,-85,95,3,-47,117,17, + -18,122,28,-127,8,3,-106,-52,47,-122,-20,30,-117,-41,29,-119,-43,14,-125,-25,-3,-126,1,-14,-127,-4,6,-127,5,0, + -117,45,18,-126,10,15,-26,124,11,-53,116,4,-63,110,1,-36,107,59,-11,104,72,37,116,37,84,58,-76,110,48,41, + 78,-5,-100,-100,16,-77,-39,2,-121,-104,65,31,93,51,70,-80,69,70,14,85,94,-110,50,38,76,83,58,-25,77,98, + -102,47,59,-123,23,23,-117,29,39,-118,16,-45,-56,62,-96,-25,96,-80,-37,89,-82,-30,76,-97,-31,53,-111,-70,59,-88, + -13,23,-124,-90,14,-89,-122,25,-27,-119,36,-24,-123,30,4,-127,-3,-1,79,-28,-96,-8,78,-100,2,106,-70,-4,111,-61, + 54,100,-56,-14,51,-116,-1,35,-122,-22,49,-115,-1,114,-55,-9,66,-108,-4,46,-118,-3,40,-120,-4,36,-122,-5,39,-121, + 10,43,-119,0,115,-54,2,127,7,-5,127,8,-21,121,31,-7,127,3,-24,124,-9,-23,125,-9,-56,114,-8,-49,117,-3, + -2,-34,122,-45,36,-113,-109,-35,54,-84,-50,81,-104,-70,18,-123,30,8,-126,14,3,-8,35,-122,-25,29,-121,-67,-18,-106, + -16,-67,-107,-44,-50,-108,-126,0,15,-127,-8,3,-81,45,-87,-77,60,-81,116,-7,52,47,112,-36,8,33,-122,41,-73,-95, + 64,-74,-80,-105,55,45,3,43,-119,13,61,-110,-3,88,-91,15,125,-14,12,126,-12,7,127,8,92,-10,-87,103,67,-31, + 117,29,-40,42,-89,-80,64,-93,-58,66,62,-89,45,48,-109,119,-21,39,117,-48,-6,104,-55,-49,-3,-120,42,88,48,-78, + -103,60,-43,103,19,-71,75,17,-101,100,-4,-79,123,-28,-13,117,-47,-11,107,-65,22,123,-32,5,97,71,43,125,23,7, + 123,21,-22,115,52,-16,100,77,-10,110,57,-28,95,80,-28,58,109,-30,84,95,-12,-26,-29,121,-3,119,45,18,123,24, + -20,120,37,82,55,-80,-92,70,52,120,-4,40,126,-11,11,2,79,99,3,-111,61,9,-86,93,42,-87,82,52,-58,100, + 82,5,97,78,11,100,97,1,82,-28,61,108,17,38,120,-75,91,47,-58,104,45,126,-11,-12,105,44,57,40,49,110, + 33,32,118,66,0,108,100,-74,25,50,-94,69,53,-62,97,10,-66,108,59,-32,108,64,-21,107,88,-13,91,65,4,109, + 83,-8,95,74,-46,92,73,-92,49,42,-110,49,37,-121,14,9,-122,33,-10,-125,19,5,-118,-47,-1,-127,9,-3,-108,66, + 36,-122,6,30,-120,-29,66,-104,-32,66,-108,7,58,-104,-44,92,-76,-43,93,-81,-30,110,-55,-30,118,-36,-28,110,-47,-44, + 120,-26,-33,123,-19,-26,122,-31,-19,125,-16,-15,126,4,-14,126,7,-17,124,25,-15,125,9,-22,121,29,-27,114,54,-14, + 10,125,-21,-70,106,4,-42,118,-23,53,109,-38,104,70,-20,65,101,-40,119,44,12,105,71,-2,120,36,17,98,42,-69, + 74,30,99,0,0,0,34,81,-92,-90,53,-72,-97,76,-29,-105,71,10,-103,67,32,-111,59,19,-69,75,76,-14,44,118, + -79,29,95,-115,38,38,-122,26,25,-75,5,102,-15,-9,126,64,24,107,106,5,70,109,2,65,109,2,65,108,-12,65, + 106,-58,41,81,-97,15,50,-116,-6,-3,-127,-1,-3,-124,-25,14,-118,-46,22,-118,-40,74,-92,-48,81,-89,-40,102,-70,-30, + 115,-48,-26,117,-45,-19,125,-13,-20,119,41,-16,125,18,-8,123,31,9,126,17,10,119,15,41,115,8,53,102,5,76, + 104,1,73,83,-12,96,46,-15,117,40,-47,111,61,12,111,0,0,0,-65,18,107,39,16,120,79,17,98,105,12,71, + 120,3,42,121,0,38,123,-20,22,111,-62,5,115,-53,-8,91,-88,-9,103,-72,-21,82,-91,-33,66,-102,-37,24,-118,-41, + 47,-107,-51,23,-120,-34,64,-100,-45,96,-74,-38,105,-67,-25,113,-51,-29,121,-32,-23,127,2,-5,126,4,18,120,9,39, + -95,36,77,126,-6,14,123,-33,-3,125,-21,-10,117,-42,-23,108,-59,-31,98,-73,-36,75,-92,-45,39,-111,-49,126,-15,-8, + 116,51,-7,-88,-35,84,124,27,-6,102,-76,1,82,-97,-9,78,-98,-19,23,-106,66,-5,-86,94,-35,-98,72,8,-108,66, + 33,-104,65,42,-98,68,85,-45,83,-54,-115,-2,34,-117,-36,-2,-127,-4,82,93,27,106,-62,33,80,-85,51,43,-90,79, + 25,-83,93,64,-88,66,-2,-90,90,69,-91,54,108,-65,19,93,-74,44,109,-60,26,87,-66,-65,95,-68,-50,77,-41,-92, + 91,-44,-77,118,12,-45,123,21,-23,124,-3,-27,125,-23,4,121,-38,3,121,-36,-17,124,0,-28,115,5,-53,126,-12,-13, + 120,15,-39,112,23,-55,126,4,-17,126,6,-10,124,16,-21,120,9,-39,125,15,-15,121,-3,-38,124,26,-10,103,74,3, + 118,45,-12,116,51,-7,97,81,-8,98,81,-6,56,114,6,5,124,29,123,-10,-31,120,-42,0,84,-91,30,126,-16,2, + 126,-13,2,123,-27,-13,121,32,-19,122,17,-30,126,15,-11,52,115,-7,71,105,-10,70,104,17,26,114,50,-44,98,68, + -65,104,-33,-114,55,10,-10,89,90,-91,74,48,-30,-7,-123,54,-15,-114,106,49,51,-14,99,78,6,-127,6,-6,127,-7, + 92,71,51,52,83,81,47,87,79,112,1,-60,101,34,-69,123,23,-23,91,77,-43,95,19,-82,33,-15,-122,70,-9,-106, + -74,21,-101,19,53,-114,108,3,-67,96,-6,-83,101,56,53,105,-31,-65,84,54,-79,39,37,-115,73,39,-96,69,20,-105, + 49,20,-116,42,31,-116,-6,126,15,15,126,11,58,112,-16,40,121,-3,24,32,-121,108,39,-54,78,-87,-49,120,29,-30, + 124,-5,-25,120,-21,-37,89,26,-86,67,37,-102,96,31,-77,94,26,-82,115,-26,-47,122,-22,-30,115,-49,-22,123,-22,-21, + 59,24,-110,77,29,-96,80,66,72,30,-92,82,101,-59,48,-95,53,65,-55,-60,97,-94,-57,63,-72,104,-9,-69,105,-18, + 46,117,-17,-3,119,-44,125,-20,6,72,46,-94,72,-87,59,-69,-93,52, +}; +static const unsigned short g_ocatI[] = { + 0,4,2,8,12,11,8,9,11,4,8,9,6,13,7,14,9,15, + 14,15,13,12,13,16,13,15,16,9,11,15,1,13,12,24,26,25, + 35,37,36,35,39,38,40,42,41,42,40,43,44,46,45,46,44,47, + 51,50,52,53,55,54,57,59,58,64,66,65,67,69,68,73,72,74, + 72,73,71,77,76,75,32,78,79,80,82,81,82,80,83,84,86,85, + 8,13,16,8,16,12,12,16,88,89,88,16,90,88,89,88,90,91, + 94,77,93,72,93,75,72,95,92,92,96,91,88,91,96,68,101,102, + 101,68,87,68,102,103,95,104,103,104,95,105,105,95,72,71,105,72, + 107,104,105,104,107,70,108,70,107,70,108,68,109,68,108,68,109,67, + 109,110,67,110,109,111,112,87,67,67,87,69,110,112,67,112,110,113, + 114,113,110,115,113,114,117,116,118,122,121,123,125,81,80,124,80,81, + 126,127,128,131,132,133,132,131,134,135,132,134,134,137,135,137,134,138, + 137,136,135,141,140,139,140,141,142,142,53,140,53,142,143,59,144,145, + 145,58,59,56,145,144,145,56,146,147,146,56,55,144,54,144,55,56, + 148,145,146,146,147,148,149,147,56,149,56,55,150,149,151,152,149,150, + 150,153,152,152,156,155,152,158,156,158,152,157,148,152,160,152,148,149, + 161,148,160,148,161,145,85,161,162,161,85,58,58,145,161,164,165,163, + 84,163,165,76,164,163,162,163,84,163,162,166,162,84,85,167,166,162, + 155,160,152,155,168,160,61,155,63,169,160,168,169,167,160,163,166,164, + 76,171,74,74,75,76,173,174,172,174,173,175,71,172,174,172,71,73, + 71,174,175,175,106,71,175,176,106,105,71,106,176,177,106,108,177,178, + 108,107,177,105,177,107,177,105,106,178,179,108,179,178,180,180,111,179, + 179,111,108,108,111,109,110,111,181,182,114,110,114,182,115,183,115,182, + 115,183,116,183,118,116,184,119,118,120,184,185,184,120,119,185,121,120, + 185,123,121,188,130,187,130,188,129,134,129,188,129,134,131,188,138,134, + 138,188,189,190,137,191,137,190,136,139,190,141,190,139,136,141,143,142, + 143,193,192,194,193,195,193,194,192,194,195,196,195,198,197,198,195,193, + 197,196,195,197,151,196,151,197,199,201,200,154,202,203,158,156,207,63, + 207,156,206,207,62,61,155,61,62,170,171,166,209,171,170,171,209,210, + 211,73,210,73,211,172,74,210,73,210,74,171,212,172,211,172,212,173, + 64,173,213,64,175,173,214,175,64,176,175,214,177,214,215,214,177,176, + 215,214,216,215,180,178,178,177,215,215,217,180,217,219,180,181,180,219, + 180,181,111,219,220,181,220,219,221,220,183,181,181,183,182,181,182,110, + 118,183,222,184,222,223,222,184,118,223,185,184,123,185,186,224,186,185, + 186,224,130,187,130,224,225,187,226,187,225,188,189,188,225,189,191,138, + 191,189,228,138,191,137,190,191,229,190,230,141,230,190,231,141,193,143, + 198,230,232,230,198,193,233,198,234,235,198,233,198,235,197,199,197,235, + 235,212,199,199,212,201,201,212,209,170,203,236,203,170,208,204,208,237, + 208,204,203,238,237,206,169,206,237,62,207,168,211,209,212,210,209,211, + 234,235,233,235,234,213,213,212,235,212,213,173,232,234,198,234,239,66, + 239,234,232,66,213,234,213,66,64,66,239,240,65,66,240,242,240,241, + 240,242,216,216,65,240,65,216,214,215,216,242,242,241,243,242,217,215, + 243,244,242,218,242,244,242,218,217,218,221,217,218,245,221,217,221,219, + 221,245,220,222,220,245,220,222,183,245,223,222,223,245,226,185,226,224, + 226,185,223,224,226,187,226,245,225,227,225,245,228,225,227,225,228,189, + 245,244,227,244,245,218,228,227,244,229,228,244,228,229,191,229,231,190, + 240,229,243,229,244,243,65,214,64,61,63,207,156,158,204,154,200,153, + 194,151,55,196,151,194,246,140,53,247,139,140,139,247,248,139,248,136, + 135,136,248,135,133,132,250,249,251,128,250,126,80,126,250,252,80,250, + 80,252,125,125,252,83,83,80,125,83,253,82,83,254,255,79,255,32, + 32,255,256,256,257,32,32,257,258,258,259,32,261,26,262,260,25,29, + 264,263,31,263,264,258,258,265,263,265,258,257,266,265,257,257,268,267, + 256,254,268,254,256,255,254,257,268,268,257,256,252,267,268,268,83,252, + 269,267,270,270,42,269,43,265,266,265,43,272,272,263,265,273,275,274, + 273,276,277,282,284,283,284,282,41,284,286,283,286,284,37,290,289,288, + 289,290,291,291,48,292,45,293,44,247,50,49,50,247,295,52,295,296, + 295,52,50,246,297,296,297,246,298,298,299,297,144,299,298,59,300,299, + 300,59,60,60,59,57,299,144,59,53,144,246,298,246,144,246,296,295, + 295,140,246,140,295,247,247,294,248,294,247,49,301,248,294,248,301,135, + 302,304,303,304,302,128,249,128,302,302,249,305,252,251,305,251,252,250, + 270,252,305,252,270,267,42,270,306,270,305,306,306,305,307,303,308,305, + 307,309,287,309,307,308,41,307,285,306,41,307,41,282,40,41,285,284, + 307,287,285,290,288,309,48,291,290,312,299,300,299,312,313,313,297,299, + 296,297,52,51,52,297,51,314,293,33,323,324,33,324,34,319,317,318, + 293,47,44,315,47,293,325,316,317,326,325,317,326,317,328,324,319,34, + 47,329,325,331,47,332,47,331,46,291,331,289,292,331,291,333,289,331, + 289,39,36,36,288,289,281,335,336,337,281,338,341,322,340,342,322,341, + 322,342,343,343,342,344,344,327,343,325,327,343,327,326,323,323,326,345, + 330,325,332,332,47,330,342,331,332,331,342,333,289,333,39,39,333,38, + 342,341,333,343,327,321,354,347,318,355,351,347,351,355,356,351,356,357, + 359,360,27,363,354,362,354,363,361,347,354,355,363,364,365,363,362,364, + 367,313,312,313,367,368,368,297,313,297,368,51,51,368,314,369,314,368, + 369,293,314,365,364,366,369,366,364,368,366,369,366,368,367,340,318,347, + 320,322,321,38,339,35,292,48,46,19,21,20,21,19,278,30,260,259, + 260,370,29,259,260,29,261,259,260,260,25,261,370,260,371,29,371,260, + 371,29,370,261,262,78,88,97,11,11,12,88,372,97,96,99,97,372, + 96,373,372,372,373,99,100,99,373,97,99,11,4,9,10,70,103,104, + 103,70,68,77,165,76,69,87,68,58,85,86,78,32,261,126,130,127, + 129,127,130,127,129,128,123,124,122,130,126,186,186,124,123,124,186,126, + 374,131,304,128,374,304,128,131,374,131,128,129,45,51,293,45,50,51, + 49,45,310,45,49,50,294,310,375,310,294,49,301,375,376,375,301,294, + 339,347,340,364,293,369,315,364,316,293,364,315,364,362,317,354,317,362, + 317,354,318,316,364,317,338,283,286,377,286,378,339,347,351,378,353,352, + 353,378,35,378,352,377,274,279,23,31,379,30,30,381,275,379,274,381, + 263,272,31,279,379,280,280,31,272,349,336,335,335,348,349,37,35,286, + 311,290,309,309,382,311,376,375,311,301,376,383,382,383,376,308,382,309, + 304,133,383,133,304,131,303,382,308,304,383,382,359,377,352,338,27,335, + 335,337,338,281,282,385,385,338,281,338,385,282,348,387,349,387,348,350, + 336,349,21,30,25,260,25,30,380,19,277,276,24,25,380,275,380,30, + 379,381,30,274,275,381,13,0,6,0,13,8,0,8,4,98,4,10, + 387,20,349,388,360,359,19,276,278,25,26,261,259,258,30,264,30,258, + 31,30,264,24,273,389,277,389,273,278,276,274,23,278,274,23,22,21, + 379,279,274,23,279,22,279,280,281,273,24,380,389,390,24,20,19,386, + 278,23,21,380,275,273,276,273,274,22,336,21,272,281,280,31,280,379, + 282,281,384,272,384,281,267,266,257,266,269,42,266,271,43,271,266,42, + 272,282,384,282,272,43,282,283,338,83,268,254,267,269,266,42,43,271, + 282,43,40,306,41,42,261,32,259,311,375,48,310,48,375,322,346,318, + 318,340,322,36,284,285,284,36,37,35,36,39,48,310,46,340,38,339, + 38,333,339,304,382,303,382,376,311,301,383,133,133,135,301,160,167,162, + 209,202,200,236,202,209,202,236,203,167,170,166,170,167,208,153,157,152, + 158,203,204,167,169,208,237,208,169,158,200,202,200,158,157,200,157,153, + 90,93,91,93,90,94,96,92,95,373,102,101,102,373,103,96,103,373, + 103,96,95,373,101,100,168,206,169,156,204,205,205,237,238,237,205,204, + 151,149,55,149,148,147,160,162,161,154,199,201,209,200,201,170,236,209, + 154,153,151,199,154,151,151,153,150,53,54,144,143,192,53,54,53,194, + 194,55,54,230,193,141,240,231,229,231,240,239,239,230,231,230,239,232, + 240,243,241,45,46,310,292,46,331,333,340,339,340,333,341,321,33,320, + 34,320,33,320,34,319,324,327,319,324,326,327,326,324,323,321,323,33, + 323,321,327,326,319,327,344,391,343,325,343,391,343,321,322,317,319,328, + 319,326,328,327,325,326,325,329,316,325,330,47,391,332,325,344,332,391, + 332,344,342,318,320,319,320,318,346,322,320,346,47,315,316,47,316,329, + 393,356,355,356,393,392,394,355,354,355,394,393,354,361,394,365,366,395, + 367,395,366,395,367,396,312,396,367,396,312,397,300,397,312,63,155,156, + 206,168,207,168,155,62,238,156,205,206,156,238,281,22,334,279,334,22, + 279,281,334,27,348,335,335,281,337,28,27,360,336,22,281,287,36,285, + 36,287,288,290,311,48,80,124,126,249,250,128,251,302,305,302,251,249, + 303,305,302,305,308,307,309,288,287,353,351,352,357,352,351,352,357,358, + 351,35,339,35,351,353,392,358,356,358,392,388,352,358,359,388,359,358, + 35,378,286,359,27,338,356,358,357,27,28,348,349,20,21,286,377,338, + 338,377,359,72,75,74,91,72,92,96,97,88,171,76,164,91,93,72, + 164,166,171,77,75,93,76,165,164,194,53,192,414,416,415,416,414,417, + 424,426,425,426,424,423,426,423,427,429,428,430,428,57,430,57,428,60, + 58,430,57,437,436,438,438,439,437,441,440,442,77,444,443,390,401,26, + 445,86,84,86,445,446,90,89,448,447,448,89,18,9,14,9,18,10, + 14,11,15,11,14,9,15,99,453,99,15,11,453,100,454,100,453,99, + 455,100,101,100,455,454,437,101,87,101,437,455,439,455,437,455,439,456, + 457,456,439,456,457,458,460,459,457,459,460,440,459,451,457,450,459,440, + 440,441,450,461,457,439,457,461,460,462,461,438,439,438,461,463,462,438, + 463,438,436,463,465,464,465,463,436,465,436,466,466,112,113,112,466,436, + 87,436,437,436,87,112,116,117,467,118,467,117,124,81,468,81,469,468, + 472,470,473,470,472,471,472,475,474,475,472,476,475,476,477,478,475,479, + 478,479,480,478,483,482,483,478,481,484,483,481,483,484,485,487,486,488, + 486,487,485,489,488,486,488,489,423,426,429,490,490,430,491,430,490,429, + 490,425,426,490,491,492,490,493,425,493,490,492,493,495,494,494,497,496, + 497,494,495,500,431,435,431,500,499,492,505,504,506,505,446,505,506,507, + 446,492,491,492,446,505,491,430,446,84,508,445,508,84,165,509,165,443, + 508,165,510,511,508,510,508,511,445,445,506,446,507,445,511,445,507,506, + 504,507,511,504,511,512,503,504,512,511,510,513,514,510,509,165,509,510, + 516,441,517,441,516,515,442,517,441,517,442,518,460,518,440,518,460,519, + 442,440,518,461,519,460,519,461,520,462,520,461,520,462,521,464,462,463, + 462,464,521,522,521,464,466,464,465,523,113,115,113,523,466,524,115,116, + 115,524,523,467,524,116,119,467,118,467,119,525,120,525,119,525,120,526, + 121,526,120,474,471,472,527,474,475,475,529,528,530,475,478,475,530,529, + 531,529,530,482,530,478,530,482,531,532,485,487,485,532,483,488,423,533, + 494,535,533,535,536,533,496,537,535,537,496,538,538,539,537,540,541,538, + 541,540,542,542,543,541,432,435,431,546,512,513,547,541,514,541,547,548, + 515,548,547,547,509,515,549,515,516,515,549,548,551,516,517,516,551,550, + 433,517,518,517,433,551,433,518,552,518,519,552,520,552,519,552,520,553, + 520,521,553,553,555,554,553,521,555,554,555,556,521,522,555,555,522,557, + 522,559,558,524,522,523,522,524,559,523,464,466,464,523,522,559,467,560, + 467,559,524,525,560,467,526,121,561,122,561,121,561,122,471,471,474,561, + 561,474,526,528,474,527,474,528,562,528,527,475,528,564,563,564,528,529, + 529,531,564,531,566,565,566,531,482,566,482,532,483,532,482,534,536,568, + 536,534,533,569,536,535,537,569,535,549,537,539,537,549,569,542,514,543, + 514,542,513,544,513,542,513,544,546,500,546,544,546,500,570,503,570,432, + 549,539,548,550,549,516,549,550,569,568,569,550,569,568,536,571,550,551, + 550,571,568,434,551,433,551,434,571,434,572,573,554,572,552,434,552,572, + 552,553,554,572,554,574,575,574,554,554,556,575,576,575,556,557,556,555, + 556,557,576,576,557,563,557,577,563,560,557,558,557,560,577,558,557,522, + 560,558,559,560,578,562,560,525,578,474,562,578,526,578,525,578,526,474, + 562,528,560,577,560,528,528,563,577,565,564,531,564,565,563,563,575,576, + 563,565,575,574,575,565,565,566,574,532,567,579,567,532,487,567,571,579, + 571,567,568,573,574,566,568,567,534,434,433,552,500,498,499,498,500,544, + 538,580,498,540,498,544,533,424,494,533,423,424,426,423,489,486,485,484, + 484,582,486,480,484,481,480,481,478,477,480,479,477,479,475,476,584,583, + 584,476,585,473,585,472,585,473,584,586,468,469,468,586,470,586,469,587, + 81,587,469,587,81,82,588,83,255,83,588,253,255,79,588,588,408,589, + 408,588,79,589,408,590,590,591,589,590,594,593,262,26,402,402,26,401, + 596,593,405,593,596,402,592,597,590,598,590,597,590,598,591,591,598,599, + 600,599,601,602,591,599,591,602,589,587,599,600,599,587,602,587,600,586, + 602,253,588,253,602,587,587,82,253,588,589,602,416,603,604,599,605,601, + 605,599,598,605,598,606,597,606,598,610,390,277,615,613,616,613,615,614, + 617,621,620,623,620,624,626,625,419,628,419,627,419,628,418,629,419,418, + 419,629,630,420,421,631,422,631,421,631,422,632,631,582,420,634,633,632, + 429,635,428,635,429,634,634,426,633,426,634,429,489,633,426,633,489,581, + 632,489,631,489,632,633,631,486,582,486,631,489,420,484,636,484,420,582, + 583,477,476,477,583,637,638,473,603,473,638,584,603,470,586,470,603,473, + 416,638,603,583,638,416,638,583,584,637,416,417,416,637,583,639,637,640, + 417,640,637,640,641,619,641,640,417,417,617,641,417,414,617,617,619,641, + 640,619,617,617,620,622,623,624,626,630,643,642,626,644,623,623,639,640, + 397,634,645,634,397,635,632,645,634,629,646,422,647,418,648,649,651,650, + 651,649,652,653,654,651,409,654,410,656,409,657,410,409,659,651,652,653, + 661,660,648,660,661,418,653,657,410,658,409,410,409,658,659,652,660,662, + 621,625,620,625,621,411,621,617,618,663,613,664,400,665,613,663,664,667, + 668,669,412,668,671,670,657,653,674,674,653,652,628,672,660,673,627,413, + 627,673,628,673,669,668,409,656,675,412,671,668,671,678,677,679,677,678, + 677,679,680,671,361,650,361,671,393,650,361,682,363,683,682,363,682,361, + 396,645,684,645,396,397,684,632,422,632,684,645,684,422,646,646,685,684, + 685,646,647,683,685,648,685,683,395,647,648,685,650,682,649,684,395,396, + 395,684,685,677,393,671,393,677,392,392,677,686,654,655,670,413,412,669, + 658,410,659,592,405,407,592,590,405,593,405,590,402,593,594,594,262,402, + 262,594,595,407,405,594,407,688,406,262,595,78,458,448,447,448,458,451, + 447,16,15,447,89,16,13,17,14,453,447,15,447,454,458,454,447,453, + 17,18,14,77,443,165,58,446,430,446,58,86,79,78,408,408,78,595, + 399,687,20,20,398,19,398,20,687,585,476,472,122,124,468,471,468,470, + 468,471,122,630,629,422,630,421,689,421,630,422,689,421,643,643,630,689, + 420,643,421,643,420,690,646,629,647,682,648,649,683,648,682,606,615,605, + 415,691,414,691,415,616,618,617,414,692,618,667,618,692,621,693,621,692, + 671,412,678,679,678,695,412,411,695,679,411,694,604,605,615,607,610,696, + 614,698,697,608,697,698,697,608,696,698,406,608,401,390,402,608,607,696, + 406,698,614,615,606,597,615,406,614,387,666,399,666,350,403,348,28,350, + 690,623,644,630,642,626,690,644,626,643,626,642,636,480,690,636,690,420, + 637,699,480,480,477,637,699,690,480,693,700,679,403,701,676,701,664,676, + 701,692,667,692,701,693,616,613,691,414,691,618,613,663,691,618,663,667, + 663,618,691,402,405,593,405,402,594,407,594,688,406,688,608,402,688,594, + 387,399,20,611,610,398,398,609,19,610,611,696,17,13,6,403,350,28, + 398,610,609,597,592,407,407,406,597,595,594,408,594,590,408,389,277,390, + 400,612,697,400,399,666,697,696,400,611,400,696,614,697,613,611,399,400, + 390,610,607,277,19,609,26,24,390,687,611,398,611,687,399,612,613,697, + 402,390,608,390,607,608,402,608,688,610,277,609,597,406,615,600,601,603, + 605,603,601,605,604,603,605,415,604,604,616,415,616,604,615,603,586,600, + 415,416,604,630,626,419,670,655,668,671,650,670,622,620,623,622,640,617, + 622,623,640,413,411,412,411,413,625,625,413,627,627,419,625,669,673,413, + 639,623,690,690,699,639,690,626,643,480,636,484,538,541,539,514,513,510, + 498,495,502,495,498,580,504,502,495,502,504,503,544,542,540,511,513,512, + 540,538,498,495,492,504,492,495,493,495,496,497,496,495,580,496,580,538, + 457,451,458,458,454,456,454,455,456,502,499,498,503,499,502,570,512,546, + 512,570,503,424,493,494,493,424,425,507,504,505,543,514,541,514,509,547, + 548,539,541,535,494,496,533,534,488,534,487,488,487,534,567,579,566,532, + 566,579,573,573,572,574,573,571,434,571,573,579,647,629,418,418,661,648, + 410,654,653,675,656,674,654,409,675,410,657,409,674,656,657,652,662,674, + 672,654,675,654,672,655,674,672,675,670,651,654,660,652,649,660,674,662, + 660,672,674,628,660,418,673,655,672,655,673,668,672,628,673,670,650,651, + 660,649,648,363,365,683,17,452,10,18,17,10,428,300,60,300,428,635, + 393,394,361,683,365,395,635,397,300,431,499,503,503,432,431,500,432,570, + 432,500,435,350,666,387,403,676,666,613,665,664,400,613,612,664,666,676, + 28,404,403,404,28,360,400,664,665,664,400,666,692,679,694,679,692,693, + 681,700,404,700,681,680,680,679,700,621,692,694,411,621,694,620,625,624, + 624,625,626,699,637,639,695,678,412,695,411,679,392,686,388,701,403,693, + 664,701,667,404,693,403,693,404,700,404,360,681,681,388,680,388,681,360, + 680,686,677,686,680,388,459,450,449,449,451,459,448,451,449,444,515,509, + 515,444,441,450,441,444,94,450,77,449,94,90,94,449,450,444,77,450, + 509,443,444,90,448,449,705,703,702,703,705,706,706,707,703,706,705,707, + 709,3,708,3,707,708,2,4,98,6,5,17,702,703,704,702,704,710, + 702,710,708,709,708,710,712,710,704,711,710,712,712,711,5,0,2,5, + 2,6,0,6,2,5,709,2,3,12,8,1,8,4,1,4,0,1, + 708,707,705,705,706,702,703,702,706,714,707,715,707,714,704,452,715,98, + 715,452,714,10,452,98,10,9,14,703,707,704,715,707,3,98,3,2, + 98,715,3,710,708,705,705,702,710,2,709,711,711,5,2,6,1,0, + 1,6,7,7,12,1,12,7,16,13,16,7,18,452,10,10,14,18, + 714,712,704,5,712,452,6,5,452,452,17,6,714,452,712,17,452,18, + 711,708,710,708,711,709,12,97,11,97,12,88,11,447,15,12,447,88, + 88,447,97,97,447,11,16,447,12,2,713,709,2,713,5,707,708,709, + 3,707,709, +}; +static const unsigned char g_ocatC[] = { + 93,87,85,97,91,90,101,99,97,97,93,93,109,104,103,104,103,101,106,103,101,101,96,94,106,101,100,106,103,103, + 96,90,88,103,96,91,78,68,67,77,70,65,97,90,88,106,99,96,101,90,84,97,85,81,113,101,96,103,90,83, + 133,125,120,94,88,85,104,97,93,96,87,84,97,90,85,91,81,80,139,126,119,91,84,81,97,88,84,94,87,83, + 101,96,94,96,90,88,97,91,90,99,93,91,93,87,84,94,88,85,94,87,81,93,85,83,97,88,85,100,91,90, + 99,93,90,94,88,85,93,87,84,97,91,88,100,93,88,100,91,85,97,88,84,97,90,85,96,88,83,97,88,83, + 97,87,83,100,91,87,106,97,93,110,101,97,119,110,104,120,110,104,100,93,88,101,94,90,112,103,99,107,100,96, + 113,103,99,103,93,87,117,107,100,100,90,83,88,81,80,91,83,81,94,85,83,84,77,75,87,81,80,87,81,80, + 85,80,78,85,81,80,90,84,80,78,71,68,70,64,61,71,65,62,68,62,59,113,101,97,119,109,104,112,100,94, + 106,94,88,103,91,85,113,99,91,113,100,91,109,99,94,106,96,91,112,100,93,116,103,93,138,122,110,135,122,113, + 142,128,117,139,129,122,130,120,114,122,112,104,130,122,117,119,109,103,135,125,120,119,110,106,119,109,103,123,114,109, + 125,114,110,91,80,77,93,83,80,91,80,77,103,91,88,103,93,90,107,97,93,106,97,91,141,130,125,152,143,136, + 141,129,120,159,149,145,142,133,128,93,83,80,93,83,80,91,83,80,96,90,85,97,90,87,99,91,87,97,91,87, + 100,91,87,101,94,88,106,97,91,103,94,88,107,99,93,101,93,87,97,88,83,97,88,83,103,94,88,106,96,90, + 107,99,91,112,103,97,112,101,96,112,103,97,116,106,100,117,107,100,107,97,90,112,100,93,107,97,90,117,106,99, + 116,104,96,106,94,88,107,97,88,99,88,83,97,87,81,84,75,72,83,75,72,83,75,75,84,78,75,83,77,77, + 80,75,74,85,78,77,91,85,81,84,77,74,91,84,81,65,61,58,67,62,58,84,74,65,84,75,68,97,84,72, + 77,67,61,74,65,59,87,74,65,104,88,77,103,87,77,119,104,94,110,100,91,142,130,125,155,145,138,138,126,119, + 141,129,120,96,85,81,100,88,84,101,90,85,99,90,84,96,90,84,97,90,85,96,87,83,94,87,81,94,87,83, + 94,88,85,94,88,84,99,91,87,106,97,91,107,99,93,107,99,93,107,99,93,106,97,91,106,97,91,101,93,87, + 88,80,75,94,85,81,110,100,94,96,85,81,94,84,80,112,101,94,116,104,97,116,106,99,122,110,103,116,104,96, + 122,110,101,107,96,88,93,83,77,96,84,78,93,81,75,91,81,75,88,78,75,83,74,72,81,74,72,80,75,74, + 80,74,72,83,78,77,83,77,75,80,74,71,83,77,75,68,62,59,77,71,68,72,67,62,80,72,70,78,70,65, + 78,70,64,87,74,67,91,80,74,101,90,81,104,94,87,100,90,84,99,88,83,128,119,113,112,101,94,167,158,152, + 162,154,148,142,132,125,100,91,84,101,93,85,81,74,70,87,81,78,90,83,78,94,88,84,81,75,72,88,84,81, + 84,80,77,91,87,84,93,88,85,91,87,83,97,90,87,100,93,87,101,94,88,100,93,88,103,96,91,107,99,93, + 100,91,85,101,93,87,97,90,84,96,87,81,93,84,80,91,83,78,96,85,81,87,78,75,103,91,85,109,97,91, + 117,106,97,107,97,90,99,88,83,96,85,80,97,87,81,93,83,78,93,83,78,94,83,78,88,78,75,83,74,72, + 97,87,81,93,84,78,91,83,78,85,78,75,80,74,72,85,80,77,90,84,80,90,83,78,99,93,88,138,126,119, + 136,126,119,129,113,103,119,101,88,114,99,84,78,72,70,75,70,67,74,68,65,84,78,74,83,77,72,84,77,75, + 113,104,97,96,87,84,90,81,78,97,88,85,94,87,84,101,93,87,96,87,83,103,96,90,132,119,110,143,129,120, + 151,136,128,143,132,128,143,132,129,151,136,129,120,112,107,128,114,107,132,116,107,145,130,123,149,135,128,143,133,129, + 130,123,120,126,119,116,138,126,119,148,133,125,129,120,116,136,128,123,117,109,104,112,103,97,114,109,107,109,103,103, + 128,119,113,136,122,113,146,129,119,104,94,88,114,104,99,93,84,81,94,85,83,90,80,77,88,78,75,85,75,71, + 85,75,71,80,72,65,106,94,88,87,80,75,84,78,74,97,88,83,97,88,83,100,93,87,104,96,91,120,109,103, + 119,107,101,135,123,116,149,139,133,149,139,135,119,107,101,93,85,81,103,94,90,88,83,78,78,72,70,74,68,65, + 71,67,64,80,72,70,74,68,65,74,70,67,104,97,93,104,96,93,110,101,97,110,103,99,113,103,99,110,101,96, + 107,100,97,113,106,103,106,100,100,106,100,99,103,96,93,101,94,90,94,85,80,96,87,83,91,83,80,100,91,88, + 97,88,84,85,78,75,90,81,77,90,80,75,84,75,70,146,135,126,141,128,120,130,119,112,109,99,93,117,106,100, + 122,110,104,133,120,110,130,119,109,142,126,113,101,90,85,113,99,94,139,122,109,132,116,104,139,123,110,122,107,96, + 94,81,77,90,78,74,91,80,75,84,74,70,84,74,68,88,77,71,78,68,64,81,71,67,186,162,142,159,141,123, + 112,99,91,110,99,91,119,104,97,112,99,91,126,112,101,125,109,100,130,114,103,133,116,103,94,83,78,90,78,75, + 91,81,75,93,81,77,84,74,68,84,75,70,97,87,81,138,122,110,135,120,112,114,101,96,128,113,104,123,110,101, + 167,152,139,159,142,129,168,151,139,139,123,113,149,132,120,161,141,128,142,130,123,135,125,117,129,119,110,125,113,107, + 128,117,110,126,114,107,120,107,100,142,126,114,141,125,113,128,114,107,130,120,113,113,101,94,139,123,112,74,68,64, + 84,75,68,187,162,142,152,133,117,132,117,112,139,122,114,141,125,117,148,132,126,135,122,116,143,126,120,139,123,116, + 142,125,117,132,122,117,103,99,96,99,94,93,104,99,96,103,100,96,104,99,96,103,100,96,100,97,94,103,99,97, + 103,100,100,101,94,88,100,93,88,91,81,78,96,88,85,107,100,96,146,133,126,88,80,75,88,80,75,94,87,83, + 99,88,83,87,77,72,94,84,78,90,81,77,90,84,81,97,90,87,94,87,84,90,83,80,113,101,96,112,100,94, + 96,85,81,99,90,84,87,80,74,87,80,75,84,77,72,81,74,70,93,84,78,133,117,106,155,135,122,139,122,112, + 159,141,126,158,142,128,149,133,120,159,139,123,104,93,87,97,85,81,90,81,77,101,90,83,85,77,72,106,93,87, + 122,106,96,114,101,93,101,90,85,109,97,90,135,117,107,146,128,113,145,126,112,203,180,159,216,193,172,83,72,71, + 87,80,72,88,81,74,85,78,72,81,75,71,88,81,77,94,85,80,84,78,75,85,78,75,99,90,85,90,84,80, + 117,104,96,178,162,146,170,151,135,128,112,100,136,120,107,114,103,94,229,206,183,228,206,183,203,178,155,119,106,101, + 109,99,94,133,119,107,104,96,91,104,94,90,109,96,90,101,90,85,103,97,96,97,93,90,91,87,84,99,96,94, + 220,197,175,148,133,122,116,104,96,125,114,109,138,125,119,130,117,113,117,104,97,107,97,91,126,114,104,99,91,87, + 109,97,88,165,145,125,123,109,99,146,126,112,162,139,122,101,93,88,112,101,96,167,143,123,138,122,109,106,94,90, + 100,93,88,188,165,142,164,141,123,138,122,109,136,120,109,149,130,116,123,117,116,112,106,104,117,110,106,112,106,104, + 128,113,104,122,109,101,107,97,91,122,110,104,117,112,109,113,106,103,106,96,91,101,94,93,151,135,128,81,75,68, + 85,78,72,135,120,110,120,107,99,80,72,70,83,74,71,75,67,64,88,80,74,83,77,71,81,74,68,97,91,87, + 87,80,74,78,72,68,78,72,70,126,117,112,113,100,91,109,97,90,104,94,87,99,88,84,100,91,85,129,116,109, + 117,107,100,123,114,109,138,129,125,116,104,96,123,110,101,130,114,106,93,88,84,91,85,80,100,91,88,97,91,88, + 100,94,91,101,96,93,100,93,88,99,94,91,164,155,149,152,142,135,165,157,152,149,141,135,122,107,96,114,104,99, + 135,125,120,113,97,87,113,100,91,104,93,87,132,114,103,119,101,88,145,128,114,101,90,85,71,67,62,100,87,80, + 112,97,87,72,67,64,87,81,78,90,84,81,81,77,74,80,75,72,97,90,84,97,87,81,87,77,71,87,78,72, + 99,88,81,152,138,126,141,128,116,130,116,103,132,116,104,130,114,103,122,107,96,143,130,119,133,117,106,141,122,110, + 110,97,90,109,96,88,135,119,109,142,125,112,136,119,107,128,112,101,116,101,91,96,83,78,96,84,78,97,87,81, + 99,88,83,139,123,110,139,125,112,139,123,112,129,113,104,112,97,88,158,139,126,167,148,135,164,145,130,170,149,132, + 177,159,145,138,125,117,139,128,123,143,132,128,142,130,123,141,129,122,146,135,128,145,133,126,155,146,139,145,135,126, + 167,158,152,170,159,152,168,143,123,158,135,117,155,130,113,212,191,172,183,161,141,199,183,167,180,157,135,81,72,68, + 84,74,70,83,75,68,90,81,78,106,97,93,114,106,101,117,109,103,107,100,97,100,91,88,88,80,74,99,88,81, + 113,100,93,132,117,106,77,71,67,83,75,71,157,139,126,157,139,125,133,119,107,143,128,114,87,77,75,157,143,130, + 142,126,114,222,201,183,206,183,161,106,94,88,120,109,100,93,84,83,97,88,87,101,94,91,90,80,75,96,88,85, + 93,83,78,93,85,83,90,78,75,88,80,72,90,83,80,85,77,74,104,93,88,104,93,88,94,84,80,110,100,96, + 132,125,119,159,152,146,128,119,114,87,80,77,84,80,77,80,75,71,88,81,78,112,103,97,90,83,78,88,83,77, + 91,87,84,97,91,90,106,103,101,107,104,103,104,101,100,106,103,103,96,93,91,103,100,99,90,87,84,94,91,88, + 91,87,84,90,85,83,88,83,81,87,83,80,83,78,77,83,77,75,77,72,71,75,71,68,74,70,67,77,72,68, + 81,78,74,85,80,75,88,81,77,77,72,70,72,68,65,90,83,78,84,78,74,94,87,81,90,83,80,93,85,81, + 91,84,81,88,81,78,96,88,84,88,83,80,87,80,78,90,83,81,109,100,93,114,104,99,97,90,85,96,88,84, + 99,88,84,97,88,83,81,75,71,85,78,74,80,74,68,74,68,64,77,70,67,85,78,74,81,74,71,83,75,71, + 93,84,78,110,97,93,107,94,88,103,90,87,103,91,87,91,80,77,93,83,78,93,81,77,94,84,80,96,84,80, + 109,97,91,109,96,91,141,128,119,161,149,138,151,141,129,183,170,161,181,170,161,104,94,90,93,84,78,94,84,78, + 96,85,80,93,84,80,93,84,78,94,84,80,94,83,80,88,80,77,93,81,78,91,80,77,93,83,78,96,85,80, + 91,81,77,96,85,80,103,91,87,107,97,93,130,119,113,91,80,77,90,81,77,88,78,75,88,83,80,100,91,87, + 81,77,72,72,67,65,70,65,62,70,65,62,71,67,64,75,71,67,83,77,72,88,83,77,93,85,81,94,87,83, + 94,87,83,91,84,80,90,83,80,91,83,78,90,81,78,99,88,83,88,80,75,100,91,85,107,97,91,91,83,77, + 91,83,77,85,77,71,94,85,78,90,81,77,77,71,67,74,70,65,75,71,67,72,70,65,74,71,67,80,74,71, + 78,74,71,109,97,91,101,91,85,113,101,96,133,122,113,117,107,101,139,128,119,148,135,128,133,122,114,142,128,120, + 130,117,112,116,104,100,175,164,155,107,96,91,104,94,90,110,99,94,107,97,93,99,90,84,107,97,94,113,101,97, + 85,80,77,91,85,81,70,65,64,75,71,68,68,64,61,72,67,64,80,74,71,84,78,74,90,84,80,85,80,75, + 93,85,81,83,75,70,93,84,80,93,84,80,99,90,87,87,80,75,96,88,84,85,77,74,85,78,74,97,88,84, + 100,91,87,87,80,75,87,78,72,96,85,78,94,84,78,85,77,72,80,72,68,78,72,68,80,72,70,77,71,67, + 71,67,62,71,67,62,70,67,62,70,65,61,78,72,68,88,81,77,96,87,83,107,97,94,116,106,101,110,100,94, + 117,106,100,120,110,103,113,103,99,109,99,94,104,93,90,122,110,104,120,109,103,148,136,128,143,132,125,151,139,132, + 117,106,100,103,94,90,104,96,91,99,91,87,100,91,88,88,83,78,93,87,83,74,70,67,80,75,71,71,67,64, + 75,70,65,74,70,65,80,74,71,71,67,62,70,64,61,74,68,64,75,70,65,88,81,75,85,78,72,83,77,72, + 84,78,74,96,88,84,90,83,78,96,88,84,99,90,87,83,75,72,81,72,70,80,72,70,78,70,67,80,72,68, + 83,75,72,84,78,74,80,72,70,67,62,59,65,61,58,74,68,65,67,61,58,64,59,56,65,61,58,97,88,84, + 110,99,94,88,83,78,96,88,84,68,64,61,109,99,94,71,67,64,183,172,161,168,158,146,171,159,149,161,148,138, + 132,120,113,116,104,99,96,84,81,94,84,80,90,80,75,78,71,68,78,71,67,77,70,65,74,68,62,87,78,75, + 91,84,81,96,87,83,96,87,83,99,90,85,101,93,87,97,88,84,94,87,84,96,88,84,125,113,106,109,100,94, + 133,120,113,114,106,100,120,110,104,125,114,109,132,119,113,136,126,120,109,103,99,112,103,97,155,143,135,141,130,120, + 146,133,128,141,129,126,136,125,120,132,120,113,114,104,97,128,116,107,125,113,106,112,101,94,116,106,99,100,91,85, + 113,104,99,109,100,96,101,94,90,116,107,101,100,93,90,117,109,103,125,116,112,130,123,119,142,132,128,123,113,104, + 139,123,113,145,128,116,97,87,84,81,71,70,78,71,67,96,85,81,99,90,84,93,84,78,90,80,74,90,78,75, + 90,80,75,91,80,75,90,78,75,94,83,78,125,114,109,109,97,93,93,83,78,93,83,78,93,81,78,93,81,77, + 93,81,77,93,81,77,93,81,77,96,83,80,85,77,72,87,77,72,83,75,70,83,74,70,96,88,85,93,85,83, + 99,90,85,100,91,87,94,85,84,88,80,77,87,80,77,88,80,75,87,78,75,84,75,71,85,77,71,83,74,72, + 81,72,70,84,75,72,85,77,74,85,77,75,88,78,75,91,80,78,74,65,64,97,87,81,78,70,67,81,71,68, + 113,101,96,122,112,104,100,88,84,103,91,87,117,106,100,158,142,130,154,136,125,149,132,122,165,152,143,178,165,155, + 201,190,181,157,138,126,125,113,106,119,109,101,155,141,132,197,186,175,226,213,203,146,132,123,96,85,81,101,93,85, + 101,91,85,165,142,125,165,141,122,152,133,119,138,126,117,142,129,120,158,141,130,154,136,125,129,117,110,112,101,96, + 110,99,94,135,123,116,171,157,145,132,119,110,142,126,114,145,129,117,151,132,119,161,145,135,167,149,136,162,148,138, + 141,126,117,159,143,133,122,110,104,128,116,110,99,88,84,106,94,88,109,99,93,125,114,109,129,117,113,130,117,112, + 142,129,125,129,117,112,148,133,123,136,125,120,136,125,119,155,136,122,168,149,133,162,145,132,152,136,128,133,122,113, + 207,194,186,157,142,135,152,139,132,148,136,130,129,120,113,117,109,104,110,106,103,151,138,132,162,145,135,114,109,106, + 90,84,84,87,83,80,101,97,97,99,93,93,107,104,101,97,93,93,85,81,80,90,85,85,106,103,100,91,83,80, + 106,97,91,101,94,90,129,119,113,119,112,107,197,174,152,170,146,128,183,159,141,93,85,81,103,94,87,103,93,85, + 103,93,85,93,83,78,93,83,77,91,81,77,94,84,78,96,85,80,93,83,78,94,83,78,114,103,99,133,120,110, + 129,116,110,135,125,119,93,84,81,106,96,91,91,83,78,113,101,94,112,100,93,117,104,96,135,122,112,139,123,113, + 126,113,104,126,113,103,123,113,109,125,113,104,135,117,107,145,126,114,146,128,116,157,139,126,117,106,100,146,130,119, + 148,130,117,146,135,128,149,132,122,216,191,167,222,200,177,226,204,183,85,75,71,88,80,74,83,74,70,90,81,75, + 87,77,74,90,78,75,87,78,74,83,75,70,88,78,74,122,107,97,194,177,161,181,162,145,122,110,101,123,110,100, + 125,110,100,94,84,80,135,117,106,123,110,100,117,103,96,142,130,123,138,128,122,149,135,129,165,148,135,138,126,119, + 210,186,164,128,113,101,158,136,119,126,113,104,109,104,103,225,204,184,138,123,110,152,136,130,158,142,133,114,109,104, + 128,119,114,130,119,109,148,126,112,193,168,143,146,126,112,148,129,114,141,122,109,164,142,125,119,109,103,159,139,123, + 106,97,93,161,141,123,175,154,135,143,123,110,136,123,114,141,126,117,151,135,125,139,126,114,152,138,129,104,96,91, + 107,100,97,107,101,99,107,101,99,110,101,97,125,114,107,100,91,85,100,93,90,81,74,68,152,139,130,152,138,129, + 88,77,74,88,77,74,84,74,70,120,110,101,106,97,90,99,90,84,85,77,72,132,120,112,84,74,71,87,78,74, + 87,78,74,83,75,71,130,119,113,94,83,80,148,138,128,165,155,143,130,120,112,136,126,117,141,129,120,97,87,83, + 162,149,139,122,110,103,125,112,104,157,145,135,162,151,141,167,155,145,78,74,71,80,75,74,84,80,77,167,157,145, + 159,148,139,120,110,104,133,123,116,135,122,114,119,106,100,101,93,87,109,97,93,94,85,80,117,106,101,148,135,126, + 119,107,103,117,104,100,114,103,99,85,78,74,72,68,64,68,64,61,75,71,68,77,72,68,109,99,93,114,103,96, + 145,129,120,154,138,126,159,145,135,175,164,154,162,146,136,155,139,128,135,119,109,143,128,117,145,129,117,154,138,129, + 142,128,117,151,136,128,145,130,120,117,107,101,142,128,117,141,126,117,125,113,106,165,149,139,133,120,112,138,123,114, + 109,104,103,107,104,101,161,152,145,148,138,132,184,164,148,138,125,119,142,132,125,174,162,154,162,151,143,158,148,139, + 177,165,157,226,203,177,225,203,183,172,148,129,155,133,117,213,188,167,200,183,167,177,162,148,177,151,130,193,167,145, + 129,114,104,128,113,103,132,116,104,136,119,106,135,117,106,119,107,99,116,104,96,83,74,72,75,68,67,87,78,74, + 136,122,113,129,114,104,161,143,132,158,142,130,145,128,116,162,148,135,123,109,99,149,135,122,151,132,117,151,135,122, + 157,139,126,157,138,126,93,85,81,91,85,81,96,88,85,93,84,81,96,87,84,90,83,80,93,85,81,91,85,80, + 96,87,83,90,83,80,85,78,75,93,87,84,117,107,107,119,107,109,114,107,106,117,109,109,103,97,94,106,101,99, + 94,90,88,112,106,104,112,103,103,112,104,104,113,104,104,109,100,100,112,103,103,117,107,107,117,109,109,101,96,93, + 99,93,90,107,101,99,100,93,91,90,84,83,90,84,83,90,84,83,114,106,106,119,109,109,116,106,106,112,107,106, + 109,104,101,106,103,100,110,106,104,104,100,99,106,104,103,110,104,103,109,104,103,97,93,90,104,100,97,114,106,106, + 117,109,109,107,99,97,112,104,103,96,88,87,101,96,94,96,90,88,101,96,94,107,101,100,107,103,101,106,103,100, + 109,101,100,112,106,104,110,106,104,109,104,101,109,103,101,107,103,100,114,106,106,112,103,101,101,96,94,99,93,90, + 103,99,99,97,91,90,101,96,93,104,99,97,99,93,91,99,93,90,103,97,96,107,101,99,106,100,97, +}; diff --git a/fx/bloom.hlsl b/fx/bloom.hlsl new file mode 100644 index 0000000..81b3089 --- /dev/null +++ b/fx/bloom.hlsl @@ -0,0 +1,57 @@ +// Threshold/downsample -> bloom[0], vertical -> bloom[1], horizontal -> bloom[0]. +// Blur offsets are half-resolution texels and are ONLY used on half-res inputs. +cbuffer Bloom : register(b0) { float4 gStep; float4 gParam; }; +Texture2D gSrc : register(t0); +SamplerState gLin : register(s1); +struct VSOut { float4 pos : SV_Position; float2 uv : TEXCOORD0; }; +static const float VOFF[2] = { 1.3730788, 3.2295113 }; +static const float VW[2] = { 0.3171515, 0.0646567 }; +static const float VC = 0.2363835; +static const float HOFF[4] = { 1.4415567, 3.3767077, 5.3550101, 7.3868203 }; +static const float HW[4] = { 0.2503180, 0.1212263, 0.0394885, 0.0133097 }; +static const float HC = 0.1513151; + +float3 knee(float3 c) +{ + c = min(max(c, 0.0), 32.0); + float l = max(c.r, max(c.g, c.b)); + float t = gParam.x, k = max(gParam.y, 1e-4); + float s = clamp(l - t + k, 0.0, 2.0 * k); + s = s * s / (4.0 * k + 1e-6); + return c * (max(s, l - t) / max(l, 1e-4)); +} +float3 brightPass(float2 uv) +{ + // Threshold four full-res texel centres BEFORE averaging: retain thin light. + float2 d = gStep.xy * 0.5; + return (knee(gSrc.Sample(gLin, uv + float2(-d.x, -d.y)).rgb) + + knee(gSrc.Sample(gLin, uv + float2( d.x, -d.y)).rgb) + + knee(gSrc.Sample(gLin, uv + float2(-d.x, d.y)).rgb) + + knee(gSrc.Sample(gLin, uv + float2( d.x, d.y)).rgb)) * 0.25; +} +float3 blurV(float2 uv) +{ + float3 c = gSrc.Sample(gLin, uv).rgb * VC; + [unroll] for (int j = 0; j < 2; j++) { + float2 d = float2(0.0, VOFF[j] * gStep.y); + c += (gSrc.Sample(gLin, uv + d).rgb + gSrc.Sample(gLin, uv - d).rgb) * VW[j]; + } + return c; +} +float3 blurH(float2 uv) +{ + float3 c = gSrc.Sample(gLin, uv).rgb * HC; + [unroll] for (int j = 0; j < 4; j++) { + float2 d = float2(HOFF[j] * gStep.x, 0.0); + c += (gSrc.Sample(gLin, uv + d).rgb + gSrc.Sample(gLin, uv - d).rgb) * HW[j]; + } + return c; +} +float4 main(VSOut i) : SV_Target +{ + float3 c; + if (gParam.w < 0.5) c = brightPass(i.uv); + else if (gParam.w < 1.5) c = blurV(i.uv); + else c = blurH(i.uv); + return float4(c, 1.0); +} diff --git a/fx/burst.hlsl b/fx/burst.hlsl new file mode 100644 index 0000000..20b1a31 --- /dev/null +++ b/fx/burst.hlsl @@ -0,0 +1,668 @@ +// THE BURST. +// +// The fastest passage in the demo, under the second solo. Matter tearing +// past the camera at a speed nothing else in the demo goes. +// +// What the viewer sees in the first half second: a black frame with the +// direction of travel somewhere in it, and everything else in the shot flying +// out of that point. Near it the particles are end-on and read as dots; the +// further from it they get the longer they stretch, until at the corners they +// are streaks a third of the screen long leaving frame. There is no lighting +// model here at all. Depth is carried entirely by brightness, because that is +// the only channel the ten step quantiser in the post pass still has left. +// +// gTune.x speed, world units per second. 40 is a drift, 130 is the solo. +// gTune.y density, 0..1 - how full the field is. 1 is as busy as this scene +// is allowed to get, not as busy as the lattice could be; see below. +// gTune.z colour temperature, 0 cold blue .. 0.5 amber .. 1 phosphor green +// gTune.w EXTRA flare at the vanishing point, 0..1.5, on top of the shot's +// own. Every burst row in the edit passes 0 here. +// +// =========================================================================== +// F I V E B U R S T S , N O T O N E B U R S T F I V E T I M E S +// +// The edit cuts here five times: bars 31, 45, 67, 73 and 78. Those rows +// differ only in speed (45, 90, 100, 110, 118), density (0.30 .. 0.42) and +// temperature - and post.hlsl regrades every frame through a per section +// palette, so temperature buys nothing at all. Speed alone is not a shot. +// Measured pairwise as luminance thumbnails, the five appearances were the +// most repetitive multi appearance scene in the demo: five near identical +// fields of streaks at five brightnesses. +// +// So the three knobs the table does vary are hashed into a SIGNATURE, and +// the signature decides what SHAPE the field is - not what colour it is. +// Five topologies, one per appearance as the table stands: +// +// OPEN the full isotropic field. The scene as it was. +// TUNNEL the throat is cleared. A dark aperture with a thin rim of +// light around it and the debris ripping past outside. +// SHEET the field collapses into one band through the axis: a bar of +// streaks across black, turning as the field rolls. +// ARMS three or six vanes, twisted along the depth axis, so the +// matter arrives in wedges with clean gaps between them. +// SHOAL occupancy clumped into blocks a dozen cells across and ten +// planes deep, which sweep past as gusts with holes in. +// +// Every appearance also gets its own roll rate and direction, its own drift +// of the vanishing point off frame centre, its own surge in the flow so a +// held shot is not a constant blur, its own lattice scale, streak length, +// streak width, hero/dust balance, and a shock ring on its own period that +// crosses the frame outward through the debris. Behind all of it, a fixed +// far field of pinpoints that does NOT flow, which is the only thing in a +// scene of streaks that can give it parallax. +// +// THE HASH IS NOT CHAINED. s2 is not hash(s1): four separate calls on four +// well conditioned inputs. Chaining costs everything - the last frac() in +// the hash works on products around 5000, which in fp32 leaves about 5e-4 of +// output precision, and feeding that back in amplifies it until the answer +// depends on the driver. Unchained, the fp32 and fp64 selectors agree to +// 4e-3 while the nearest shot sits 0.2 of a bucket from an edge, so which +// shape a row gets is a property of the row, not of the machine. +// +// Change a row in shots.h and that appearance is reshuffled to another +// shape. It is still a shape, and the five are still five pictures. +// =========================================================================== + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect (w/h) + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look direction, w roll + float4 gTune; // per shot knobs +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// ---- the signature, decoded once per pixel into these -------------------- +// Per SHOT, not per pixel: every pixel derives the same values from the same +// gTune, so every branch on gMode below is uniform across the whole draw and +// costs a branch rather than a mask. +static int gMode; // 0 open, 1 tunnel, 2 sheet, 3 arms, 4 shoal +static float gCoreR; // TUNNEL: cleared radius, tan units +static float gSheetHW; // SHEET: half width of the band, tan units +static float2 gSheetN; // SHEET: band normal in the lattice frame +static float gTwist; // ARMS: radians of twist per world unit of depth +static float gArmPh; // ARMS: phase, spins over the shot +static float gArm6; // ARMS: 0 -> three vanes, 1 -> six +static float gBlockS; // SHOAL: 1/clump size across, in cells +static float gBlockZ; // SHOAL: 1/clump size along, in planes +static float gBlockT; // SHOAL: how much of the field a clump keeps + +float sq(float x) { return x * x; } + +// Hoskins' three-in three-out hash. No texture, and stable: a cell keeps the +// same particle for as long as it is on screen, which matters more than it +// sounds. A particle that re-rolls its seed does not move, it flickers, and +// against the ordered dither flicker turns into crawling grain. +float3 hash33(float3 p) +{ + p = frac(p * float3(0.1031, 0.1030, 0.0973)); + p += dot(p, p.yxz + 33.33); + return frac((p.xxy + p.yxx) * p.zyx); +} + +// The palette knob. Three anchors, because these are the three the demo +// actually lives in: cold blue, warm amber, phosphor green. +float3 tempColour(float t) +{ + float3 cold = float3(0.34, 0.62, 1.00); + float3 amber = float3(1.00, 0.66, 0.26); + float3 green = float3(0.36, 1.00, 0.48); + return t < 0.5 ? lerp(cold, amber, t * 2.0) + : lerp(amber, green, t * 2.0 - 1.0); +} + +// Complex multiply, used to advance an angle without a transcendental. +float2 cmul(float2 a, float2 b) +{ + return float2(a.x * b.x - a.y * b.y, a.y * b.x + a.x * b.y); +} + +// --------------------------------------------------------------------------- +// W H A T S H A P E T H E F I E L D I S +// +// Asked once per cell per depth plane, BEFORE the cell is hashed, and from +// the cell's own centre rather than from the particle inside it. That +// ordering is the whole reason this is affordable: a cleared cell costs a +// dot product and returns, and never pays for the hash or the segment solve. +// SHEET throws away three quarters of the lattice, so at the shot level the +// shaped modes are CHEAPER than the open one, not dearer. +// +// The answer is a weight, not a yes. It multiplies the streak's amplitude, +// so a particle near the edge of the shape fades as the shape turns. +// Thresholding the occupancy instead would be cheaper and would pop: the +// cell hash is fixed, so a particle crossing the boundary would blink rather +// than dim, and forty of them blinking is the lattice announcing itself. +// +// `rot` is (cos, sin) of the ARMS phase at this depth, advanced by the +// caller with a complex multiply so the loop never touches a sine. +// --------------------------------------------------------------------------- +float fieldMask(float2 ci, float pn, float cell, float zc, float2 rot) +{ + if (gMode == 0) return 1.0; // OPEN: the whole lattice + + float2 cc = (ci + 0.5) * cell; // cell centre, camera space + float iz = 1.0 / zc; // ...read as an angle off axis + + if (gMode == 1) // TUNNEL + return smoothstep(gCoreR * 0.42, gCoreR, length(cc) * iz); + + if (gMode == 2) // SHEET + return 1.0 - smoothstep(gSheetHW * 0.52, gSheetHW, + abs(dot(cc, gSheetN)) * iz); + + if (gMode == 3) // ARMS + { + // cos/sin of N.theta straight out of the cell direction. Chebyshev + // for three, one doubling for six. atan2 in here would be fifty + // transcendentals a pixel; this is four multiplies. + float2 d = cc * rsqrt(dot(cc, cc) + 1.0e-6); + float c3 = d.x * (4.0 * d.x * d.x - 3.0); + float s3 = d.y * (3.0 - 4.0 * d.y * d.y); + float ca = lerp(c3, 2.0 * c3 * c3 - 1.0, gArm6); + float sa = lerp(s3, 2.0 * s3 * c3, gArm6); + return smoothstep(-0.35, 0.40, ca * rot.x - sa * rot.y); + } + + // SHOAL. A second hash on a COARSE index - twenty cells across, a dozen + // planes deep. Big and deep on purpose: a ray crosses thirty six cells, + // so clumps smaller than this average out along it and the shot looks + // exactly like the open field. The index is absolute - the plane NUMBER, + // not its depth - so the clumps are nailed to the field and sweep past + // the camera instead of sitting still in front of it. + float3 b = hash33(float3(floor(ci.x * gBlockS), floor(ci.y * gBlockS), + floor(pn * gBlockZ)) + 3.1); + return smoothstep(gBlockT, gBlockT + 0.35, b.x); +} + +// --------------------------------------------------------------------------- +// ONE LATTICE OF STREAKS +// +// This is the whole trick, and it is the non-obvious part, so at length. +// +// There is no particle system. There is an infinite lattice of cells, each +// holding at most one particle at a hashed position inside itself, and the +// pixel shader asks: which of those particles does MY ray pass close to? +// +// Everything below is in the camera's own frame - x right, y up, z forward - +// so the ray starts at the origin and the camera's motion is straight down +// +z. That choice is what nails the expansion centre to the direction of +// travel, which is the whole composition. Main tilts that frame off the +// optical axis before handing it over, which is how the vanishing point gets +// to sit somewhere other than the middle of the picture. +// +// Cells are addressed as (lateral cell, depth plane). Depth planes sit every +// `cell` units along z and slide towards the camera as `flow` grows, so the +// particles are what is standing still and the camera is what is moving - +// which is also why the lattice never has to be updated or stored. +// +// The ray crosses each depth plane at exactly one point, so there is a +// single lateral cell per plane to test, and the loop is one particle per +// step with no traversal and no misses. That is the reason for slicing the +// grid into planes rather than marching it: a fixed step through a 3D grid +// skips cells and visits others twice. +// +// A STREAK, not a point. During the time the shutter is open the camera +// travels forward, so relative to the camera the particle sweeps a segment - +// and since the camera only moves along +z, that segment runs from the +// particle straight out along +z, away from us, for `len` units. Projected, +// that is a line pointing back at the vanishing point, which is exactly what +// a radial motion streak looks like. So the shading question is the distance +// from the view ray to a segment, in closed form: +// +// ray P(s) = e * s e unit, s >= 0 +// streak Q(u) = (px, py, z0 + u) u in [0, len] +// +// Setting the connecting vector perpendicular to both and using the fact +// that the segment is axis aligned along z, nearly all of the usual algebra +// cancels and the closest point on the streak is just +// +// u = e.z * (e.x*px + e.y*py) / (e.x^2 + e.y^2) - z0 +// +// clamped to [0, len]. The closest point on the ray is then the projection +// of Q(u) onto e, and the distance between the two falls straight out. +// --------------------------------------------------------------------------- + +// One cell, tested. Split out of the loop because the wide layer walks four +// cells per plane and this is the part that gets done four times. +// Returns (energy, the part of that energy that came from close in). +float2 streakAt(float3 e, float2 ci, float pn, float cell, float len, + float invLen, float wide, float dens, float3 sofs, + float zc, float zf, float g, float mk) +{ + float3 h = hash33(float3(ci.x, ci.y, pn) + sofs); + // Two more decorrelated numbers out of the same hash, which is a lot + // cheaper than hashing twice and plenty for a field this busy. + float r1 = frac(h.x * 57.31 + h.z * 13.77); // occupancy roll + float r2 = frac(h.y * 91.73 + h.x * 29.31); // size and brightness + + if (r1 > dens) return float2(0.0, 0.0); // this cell is empty + + // The particle inside its cell. Kept clear of the cell wall: a particle + // sitting on a wall would pop in and out as the ray crossed it. 0.62 of a + // cell of travel leaves a margin wider than the streak everywhere the far + // fade has not already taken the streak to nothing. + float2 pxy = (ci + 0.5 + (h.xy - 0.5) * 0.62) * cell; + + // Depth is jittered across a whole cell, not part of one. At less than + // that the planes still show: the first render of this scene had visible + // horizontal rows of dashes across the middle of frame, which is the + // lattice announcing itself. The floor of a third of a unit is not + // cosmetic - at zero depth the closest point on the ray is the camera + // itself, every pixel gets the same distance, and one particle flashes + // the entire frame white. + float z0 = max(zc + (h.z - 0.5) * cell, 0.33); + + // ---- nearest approach of the ray to this streak --------------------- + float m = e.x * pxy.x + e.y * pxy.y; + float u = clamp(e.z * m / g - z0, 0.0, len); + float s = max(m + e.z * (z0 + u), 0.02); + float3 d = float3(e.x * s - pxy.x, e.y * s - pxy.y, e.z * s - z0 - u); + float d2 = dot(d, d); + + // Width is an ANGLE, not a world size: the radius grows with distance so + // every streak is the same handful of pixels wide wherever it is. At 640 + // x360 a streak thinner than a pixel does not get thinner, it gets noisy, + // and the dither turns that noise into crawling grain. `wide` is a half + // width in radians; at fov 0.62 the two layers come out 2.8 and 2.3 + // pixels wide at half maximum, which is the floor for a 360 line frame + // that then goes through a CRT resample. + float rw = wide * max(s, 0.4); + float w = rw * rw / (d2 + rw * rw); + w *= w; // solid core, fast edge + + // Nothing here is lit, so brightness is the only thing separating near + // from far, and it has to survive ten quantiser steps - hence a steep + // falloff and a short near fade rather than anything physical. + float t01 = saturate(z0 / zf); + float fade = (1.0 - t01 * t01) * smoothstep(0.30, 1.60, z0); + + // A streak has a head and a tail. u = 0 is where the particle is now, + // u = len is where it was when the shutter opened, so the energy has to + // fall off along the trail - otherwise a corner streak is a uniform bar + // as bright at its far end as at its head, the value structure goes flat + // exactly where the composition wants it steepest, and the ends terminate + // on a hard edge instead of dissolving. + float tail = 1.0 - 0.62 * u * invLen; + + // r2 SQUARED, so most streaks are dim and a few are hero bright. A flat + // spread of brightnesses quantises into one grey band and the whole frame + // turns into television snow; this keeps a handful of streaks up at the + // top of the ramp with darkness between them. + // mk SQUARED for the same reason it exists: the shape's edge wants to be + // narrow in the picture even though it has to be wide in the maths to + // stay pop free, and squaring buys that for one multiply. + float amp = (0.14 + 0.86 * r2 * r2) * fade * tail * mk * mk; + + return float2(w * amp, w * amp * (1.0 - t01)); +} + +float2 streakLayer(float3 e, float cell, float flow, int slabs, float len, + float wide, float dens, float seed, bool pair) +{ + // Streak length is capped against the CELL. A streak is only ever found + // by rays whose cell is the streak's own cell at one of the depths tested + // below; let it run much longer than the span those cover and the middle + // of every long streak falls in cells nobody looks at, and the field + // turns into rows of dashes with holes in. + len = min(len, cell * (pair ? 1.35 : 0.95)); + + // e.x^2 + e.y^2, since e is unit. It is zero only for the ray that goes + // exactly down the axis, and the guard keeps that one pixel finite - the + // clamp on u does the rest. + float g = max(1.0 - e.z * e.z, 1.0e-4); + float ez = max(e.z, 1.0e-3); + float zf = float(slabs) * cell; // far plane of this lattice + float rc = 1.0 / cell; + float invL = 1.0 / max(len, 1.0e-3); + float3 sofs = float3(seed, seed * 1.7, seed * 0.3); + + // Where the lattice sits right now. n0 is the ABSOLUTE index of the first + // plane in front of the camera, and carrying it into the hash is what + // stops the entire field re-rolling every time a plane passes. Without it + // the shot strobes at the plane crossing rate, which at these speeds is + // about fifteen times a second. + float fz = flow / cell; + float n0 = floor(fz); + float ph = fz - n0; + + // ARMS advances its phase one step per depth plane and one third of a + // streak per walk step. Angle addition by complex multiply, seeded by two + // sincos outside the loop, so a twisted field costs six multiplies a + // plane instead of two sines a plane. + float2 rot = float2(1.0, 0.0); + float2 drot = float2(1.0, 0.0); // one plane + float2 qrot = float2(1.0, 0.0); // one third of a streak + if (gMode == 3) + { + sincos(gTwist * ((1.0 - ph) * cell) + gArmPh, rot.y, rot.x); + sincos(gTwist * cell, drot.y, drot.x); + sincos(gTwist * len * (1.0 / 3.0), qrot.y, qrot.x); + } + + float2 acc = float2(0.0, 0.0); + + // [loop], never [unroll]: `slabs` is a literal at both call sites, so an + // unrolled body would be 36 copies of the body in the blob. + [loop] for (int k = 0; k < slabs; k++) + { + // Depth of this plane in camera space. (1 - ph) slides the whole set + // towards us as flow grows; k pushes it out towards the far plane. + float zc = (float(k) + 1.0 - ph) * cell; + float pn = n0 + float(k); + + // The lateral cell the ray is inside when it crosses that plane. + float2 ci = floor(e.xy * (zc / ez) * rc); + + float mk = fieldMask(ci, pn, cell, zc, rot); + if (mk > 0.06) + acc += streakAt(e, ci, pn, cell, len, invL, wide, dens, sofs, + zc, zf, g, mk); + + // ONE CELL PER PLANE IS NOT ENOUGH FOR THE LONG ONES. + // + // A streak is only ever found by the rays whose cell at `zc` is the + // streak's own cell, so it is visible only inside the screen space + // footprint of that cell - roughly cell/zc radians across, the same + // angular size everywhere. Its projected LENGTH, on the other hand, + // grows with distance from the vanishing point: about tan(theta) + // times len/cell cells. Dead centre that is nothing. At the corner of + // a 0.8 lens it is close to two cells, and with the vanishing point + // pushed off centre it is three - so the middle of every corner + // streak was landing in cells nobody tested. That is what the blocky + // shearing in the corners of the old renders was: not aliasing, whole + // streaks chopped into dashes, and chopped at the same radius for + // every cell, which puts a ring of streak-ends through the frame. + // + // So walk the streak instead of sampling its ends: quarter steps from + // head to tail, each one skipped when it lands in a cell already + // done, which over most of the frame is all of them. The branch pays + // for itself everywhere the streaks are short, and the corners - the + // part of the frame this whole scene exists for - stop shearing. + if (pair) + { + float2 ca = ci, cb = ci; + float2 rq = rot; + [unroll] for (int q = 1; q <= 3; q++) + { + float zq = zc + len * (float(q) * (1.0 / 3.0)); + float2 cq = floor(e.xy * (zq / ez) * rc); + rq = cmul(rq, qrot); + if ((cq.x != ci.x || cq.y != ci.y) && + (cq.x != ca.x || cq.y != ca.y) && + (cq.x != cb.x || cq.y != cb.y)) + { + float mq = fieldMask(cq, pn, cell, zq, rq); + if (mq > 0.06) + acc += streakAt(e, cq, pn, cell, len, invL, wide, + dens, sofs, zc, zf, g, mq); + } + cb = ca; ca = cq; + } + } + + if (gMode == 3) rot = cmul(rot, drot); + } + return acc; +} + +float4 main(VSOut i) : SV_Target +{ + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y is FLIPPED here on purpose. The fullscreen triangle emits + // uv.y = 0 at the top of the screen, so i.uv*2-1 gives -1 up + // there and the top of the frame ended up looking DOWNWARD - + // the cathedral vault springs at y=+12 and was rendering in the + // lower half, the monolith hung from the ceiling. + uv.x *= gTime.w; + + // The standard setup, so this scene frames identically to every other + // one. The world-up fallback is not decoration: CAM_CRANE rows aim very + // close to straight up, and cross((0,1,0), fw) is the zero vector there, + // which normalize() turns into NaN and NaN painted over the whole frame. + float3 fw = normalize(gDir.xyz); + float3 wup = abs(fw.y) > 0.999 ? float3(0.0, 0.0, 1.0) : float3(0.0, 1.0, 0.0); + float3 rt = normalize(cross(wup, fw)); + float3 up = cross(fw, rt); + + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 rx = rt * cr + up * sr; + float3 ry = up * cr - rt * sr; + float3 rd = normalize(fw + (uv.x * rx + uv.y * ry) * gCam.w); + + // Straight back into the camera frame, because that is where the lattice + // lives. (rx, ry, fw) is orthonormal and rd is unit, so e is unit too. + // gCam.xyz never appears: the field is defined relative to the camera on + // purpose, so that a shot can orbit or roll through the burst without the + // expansion centre sliding off the direction it is travelling in. + float3 e = float3(dot(rd, rx), dot(rd, ry), dot(rd, fw)); + + float speed = max(gTune.x, 0.0); + float flare = max(gTune.w, 0.0); + float t = gTime.x; + + // ---- T H E S H O T S I G N A T U R E ---------------------------- + // Twelve decorrelated numbers out of the three knobs that actually differ + // between the five appearances. Four separate hashes, never fed into each + // other - see the note at the top of the file. All of it is per shot, so + // every branch below is uniform over the draw. + float3 kk = float3(gTune.x * 0.089, gTune.y * 11.90, gTune.z * 2.30); + float3 s1 = hash33(kk + 1.7); + float3 s2 = hash33(kk.yzx * 2.11 + 5.9); + float3 s3 = hash33(kk.zxy * 1.37 + 11.7); + float3 s4 = hash33(kk * 0.83 + 41.3); + + gMode = (int)min(4.0, floor(s1.x * 5.0)); + + // How much of the lattice each shape throws away, put back so a shaped + // shot is not simply an emptier one. Not a full area compensation - the + // black a shape makes IS the composition and must not be filled in. This + // restores the density INSIDE the shape to about what the open field has, + // measured off the mean of the mask over a frame. + float densK = 1.00; + if (gMode == 1) densK = 1.10; + if (gMode == 2) densK = 2.15; + if (gMode == 3) densK = 1.25; + if (gMode == 4) densK = 1.90; + + gCoreR = (0.24 + 0.26 * s2.y) * gCam.w; + gSheetHW = (0.34 + 0.34 * s2.y) * gCam.w; + float sang = s4.z * 3.14159; + gSheetN = float2(cos(sang), sin(sang)); + float tw = (s4.z - 0.5) * 0.40; + gTwist = tw < 0.0 ? min(tw, -0.022) : max(tw, 0.022); + gArmPh = t * (s3.y - 0.5) * 2.20; + gArm6 = s4.y > 0.5 ? 1.0 : 0.0; + gBlockS = 0.05 + 0.05 * s4.x; + gBlockZ = 0.06 + 0.06 * s4.y; + gBlockT = 0.20 + 0.20 * s4.y; + + // A directional shape has to TURN or it is wallpaper, so SHEET and ARMS + // get a floor under the roll rate; the isotropic shapes do not need one + // and a rolling isotropic field looks identical to a still one anyway. + float roll = (s4.z - 0.5) * 1.60; + if (gMode == 2 || gMode == 3) roll += roll < 0.0 ? -0.30 : 0.30; + + float tiltAmp = 0.05 + 0.11 * s2.y; // rad off the optical axis + float tiltRate = 0.30 + 0.70 * s1.z; + float lenMul = 0.55 + 1.10 * s3.x; + float cellMul = 0.80 + 0.55 * s3.y; + float wideMul = 0.80 + 0.55 * s2.z; + float heroW = 0.62 + 0.42 * s2.x; + float dustW = 0.50 + 0.62 * s4.x; + float surgeA = 0.18 + 0.50 * s3.z; + float surgeW = 2.00 + 4.20 * s4.y; + float ringAmp = 0.22 + 0.40 * s1.y; + float ringRate = 0.45 + 0.95 * s4.z; + float starW = 0.30 + 0.70 * s1.z; + float glow = 0.05 + 0.10 * s3.z; + + // ---- W H E R E T H E V A N I S H I N G P O I N T S I T S ---- + // The lattice frame is tilted off the optical axis and rolled about it. + // Both are exact rotations, so e stays unit and every line of the solve + // is untouched - but the expansion centre moves off the middle of the + // picture and wanders, and the whole field turns. Three sincos for the + // frame, and it is the single biggest difference between two appearances. + // + // The tilt is capped at 0.16 rad and that ceiling is load bearing. Push + // the vanishing point further and the far corner passes 70 degrees off + // axis, where a streak projects longer than the four cells the walk in + // streakLayer covers and the corner shears into dashes again. Measured: + // at 0.32 rad the pairwise spread between the five appearances is a fifth + // better and two of them have a broken corner, which is not a trade. + float ax = cos(t * tiltRate + s1.y * 6.2832) * tiltAmp; + float ay = sin(t * tiltRate * 0.73 + 1.7) * tiltAmp; + float cy, sy, cp, sp, cz, sz; + sincos(ax, sy, cy); + sincos(ay, sp, cp); + sincos(t * roll, sz, cz); + float3 e1 = float3(cy * e.x - sy * e.z, e.y, sy * e.x + cy * e.z); + float3 e2 = float3(e1.x, cp * e1.y - sp * e1.z, sp * e1.y + cp * e1.z); + e = float3(cz * e2.x - sz * e2.y, sz * e2.x + cz * e2.y, e2.z); + + // ---- H O W F A S T , A N D H O W S T E A D I L Y ------------ + // How far the camera has flown since the shot began. The scene owns its + // own speed instead of reading it off the shot's camera, because the + // burst wants a hundred units a second while the camera itself barely + // moves. If they were the same number the shot would have to fly a + // kilometre and every `at` in the row would be nonsense. + // gTime.x must be SHOT time, not song time: n0 is floor(flow/cell), and + // at song time with speed 130 that index is in the thousands, where the + // hash starts to lose its low bits. + // + // Not linear any more. This is the integral of speed*(1 + A sin(Wt)), so + // the field surges and eases instead of running at one rate for the whole + // shot - two bars of a constant blur is a still frame with noise on it. + // A is well under one, so the derivative never goes negative and nothing + // ever runs backwards. The solo envelope shoves a unit or so on top, far + // too small against a hundred a second to reverse anything. + float flow = speed * (t + surgeA * (1.0 - cos(surgeW * t)) / surgeW) + + gVoice.y * 1.2; + + // Streak length is the shutter, exaggerated. The layers clamp it against + // their own cell size; this is the shot's taste in it - dashes at one end + // of the knob, threads at the other - and the solo stretches it. + float baseLen = clamp(speed * 0.032, 0.40, 4.40); + float len = baseLen * lenMul * (1.0 + 0.18 * gVoice.y); + + // Distance from the direction of travel, 0 at the vanishing point and 1 + // at the top and bottom of frame. Measured against the TILTED axis, so + // the flare and the shock ring stay concentric with the burst wherever + // the burst has drifted to. + float rn = length(e.xy / max(e.z, 1.0e-3)) / max(gCam.w, 1.0e-3); + float rr = rn * rn; + + // Two lattices, not one. A single grid at a single scale reads as a grid. + // A coarse one carrying long bright streaks and a fine one carrying dust + // gives the frame a bold shape with a texture under it, and bold shape + // plus texture is the only thing that survives being dithered into ten + // levels at 360 lines. + // The occupancy the knob asks for is scaled well down, because the useful + // range turned out to be far narrower than 0..1: a quarter of the cells + // carrying a particle already fills the frame, and a field with no black + // in it has no streaks in it either, only texture. + // cellMul moves BOTH lattices together, so one appearance is a coarse + // field of big slow slabs of matter and another is fine grit. + float hc = 3.40 * cellMul; + float dc = 1.75 * cellMul; + + // HOW MUCH INK IS ON THE FRAME, held roughly where the shot table asked. + // Coverage per plane goes as wide * len / cell, so the three knobs above + // do not just change the SCALE of the streaks, they change how full the + // frame is - and left alone they compound: the appearance that happened + // to draw a fine lattice with long fat streaks came out twice as bright + // as the row asked for, milky instead of streaks on black. The square + // root is deliberate and is not a rounding: correct it fully and every + // appearance sits at the same exposure, which throws away variation the + // edit wants; correct it not at all and one of the five washes out. Half + // leaves gTune.y in charge of fullness and lets the lattice be felt. + float inkK = wideMul * (min(len, hc * 1.35) / baseLen) / cellMul; + float dens = saturate(gTune.y * densK / sqrt(max(inkK, 0.35))); + + float2 hero = streakLayer(e, hc, flow, 20, len, + 0.0076 * wideMul, dens * 0.28, 0.0, true); + float2 dust = streakLayer(e, dc, flow, 16, len * 0.35, + 0.0062 * wideMul, dens * 0.22, 37.0, false); + + float3 tint = tempColour(saturate(gTune.z)); + float3 hot = float3(1.00, 0.94, 0.86); + + // Near black, slightly blue, so the darkest thing on screen is still the + // demo's black and not a hole. + float3 col = float3(0.010, 0.012, 0.024); + + // ---- T H E S H O C K R I N G ------------------------------------ + // A ring of extra energy born at the vanishing point and swept out past + // the corners on its own period, brightening the debris it crosses. It is + // multiplicative on a base of one, and it is a ring rather than a frame, + // so the floor never moves and no more than a fifth of the picture is + // lifted at once - a wave through the field, not a flash. Born and killed + // at zero by the sine, because a ring that appeared at full strength on + // the flare would be exactly the pop this is avoiding. + float rp = frac(t * ringRate); + float ring = exp(-sq(rn - rp * 2.40) * 22.0) * sin(rp * 3.14159); + float lift = 1.0 + ringAmp * ring; + + // Streaks that came from close in go white hot while distant ones keep + // the tint - acc.y is the share of the energy that arrived from near, so + // the ratio is the mix, per pixel, for free. The bias on that ratio is + // there because the near streaks dominate almost every pixel: mapped + // straight, the whole frame went white and the temperature knob did + // nothing at all. + // The two layers carry their own weights now, so one appearance is a + // handful of big threads over black and another is a sandstorm. Both + // audio terms are base + k*s and never scale the frame: the floor holds. + float heroA = heroW * lift * (1.0 + 0.22 * gSync.x); + float dustA = dustW * lift * (1.0 + 0.45 * gSync.z); + col += hero.x * lerp(tint, hot, saturate(hero.y / max(hero.x, 1.0e-4) * 1.5 - 0.45)) * 1.75 * heroA; + col += dust.x * lerp(tint, hot, 0.20) * 0.26 * dustA; + + // ---- T H E F A R F I E L D -------------------------------------- + // Pinpoints locked to the lattice frame rather than flowing through it. + // One hash, no loop, and they buy the one thing a field of streaks cannot + // give itself: parallax. Everything else in the shot is ripping past; + // these barely move, and that is what says the streaks are near rather + // than merely fast. base + k*high, so the hats shimmer them without ever + // taking them out. + float2 sptr = e.xy / max(e.z, 0.25) * 7.0; + float2 scel = floor(sptr); + float3 hs = hash33(float3(scel, 11.0)); + float2 sd = sptr - scel - 0.15 - hs.xy * 0.70; + float star = exp(-dot(sd, sd) * 260.0) * step(hs.z, 0.30); + col += lerp(tint, hot, 0.45) * star * starW * 0.36 * (0.55 + 0.45 * gSync.z); + + // ---- T H E L I G H T ---------------------------------------------- + // Everything in frame is coming out of one point, so that point is the + // only thing in the shot allowed to blow out. It is a hard core and + // nothing else: the wide bloom that used to sit under it is driven by + // gTune.w alone, which every burst row in the edit sets to zero, and + // adding one back lifted the black off the middle third of the frame and + // turned the scene from streaks on black into a smudge. So the shot's own + // flare is small and tight - an anchor for the eye, about twenty pixels + // across, not a light source. + // In TUNNEL it is tighter still and the throat gets a thin rim, so the + // cleared centre reads as a shaft with something at the end of it rather + // than as a hole somebody forgot to fill. + float tight = gMode == 1 ? 210.0 : 130.0; + col += tint * exp(-rr * 10.0) * 0.40 * flare; + col += hot * exp(-rr * tight) * 0.85 * (flare + glow * (1.0 + 0.25 * gSync.w)); + if (gMode == 1) + col += tint * 0.08 * exp(-sq(rn - gCoreR / max(gCam.w, 1.0e-3)) * 190.0) + * (0.70 + 0.50 * gSync.y); + + // Shot fade only. No dither, no scanlines, no ramp, no exposure, no + // tonemap: post.hlsl owns every one of those and doing any of it twice + // is how the quantiser ends up with five usable steps instead of ten. + return float4(col * gTime.z, 1.0); +} diff --git a/fx/c64.hlsl b/fx/c64.hlsl new file mode 100644 index 0000000..8959ec0 --- /dev/null +++ b/fx/c64.hlsl @@ -0,0 +1,324 @@ +// The boot screen, in the middle of the demo. +// +// It was at the START once, and the author cut it: fourteen seconds of text +// before anything happens is a bad opening. But the breakdown at bar 53 is +// the middle of the piece both by the clock and by the form, and a half-time +// bar is exactly where a demo can afford to stop and be a computer for a +// moment. So it lives there now, and it is over in four bars. +// +// The text is theirs, baked out of crtkafa.c by tools/bake_font.py rather +// than retyped, so it cannot drift from what they wrote. NOTHING in here +// touches a word or an order of it. Everything below is about the MACHINE +// the words are printed on: how far the boot has got, what the drive is +// doing, whether the tube has warmed up, and who owns the raster. +// +// The scene is cut TWICE, at bar 53 and bar 55, and the shot table hands the +// second one a later boot than the first. So the two appearances are not two +// looks at the same screen - they are one boot, continued. The first takes +// the machine from a cold tube to the middle of a load. The second picks up +// exactly where that left off, watches READY. and RUN arrive, and then hands +// the raster over to the program, which is the shot the dissolve into the +// corridor is cut against. +// +// gTune.x the progress the boot has reached by the END of this shot, 0..1. +// The shot animates INTO it from x - BOOT_STEP, so consecutive +// entries in the table chain rather than repeat. Line arrival is on +// the schedule below, which is the author's own g_bootAt. +// gTune.y screen brightness +// gTune.z barrel of the tube's own curvature, on top of the post pass +// gTune.w tube age: extra warm-up droop and roll that never quite settles. +// 0 is a healthy set, and both current shots are healthy. + +cbuffer Scene : register(b0) +{ + float4 gTime; + float4 gCam; + float4 gDir; + float4 gTune; + float4 gSync; + float4 gVoice; +}; + +// b1, uploaded once and never touched again - b0 is rewritten every frame for +// every shot and this has no business being in it. +cbuffer Font : register(b1) +{ + uint4 gFont[24]; // 47 glyphs x 2 uints, padded to float4 rows + uint4 gText[40]; // 40x16 cells, four to a uint +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +#define COLS 40 +#define ROWS 16 + +// How much of the whole boot one shot of this scene gets through. It is the +// gap between the two gTune.x values in shots.h (0.55 and 1.00) written down +// once, so a shot starts where the shot before it stopped. +#define BOOT_STEP 0.45 + +// How long a shot takes to travel that distance, in seconds. Shorter than +// either shot on purpose: the boot finishes and the picture then SITS in the +// state it reached, which is what gives the second appearance its handover. +#define BOOT_SPAN 2.2 + +// When each line arrives, as a fraction of the whole boot. This is the same +// schedule as g_bootAt[] in crtkafa.c - the author's pacing, not mine - kept +// in step by hand because a shader cannot read that array. Rows past the end +// of the text never arrive. +static const float kBootAt[16] = { + 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, // the banner, up the instant it is on + 0.14, 0.14, // LOAD, typed + 0.22, // SEARCHING + 0.32, // LOADING - and then the long wait + 0.74, // READY. + 0.86, // RUN + 9.0, 9.0, 9.0, 9.0 // blank glass +}; + +uint cellAt(uint col, uint row) +{ + uint i = row * COLS + col; // which cell + uint w = i >> 2; // which uint + uint b = (i & 3u) * 8u; // which byte in it + uint v = gText[w >> 2][w & 3u]; + return (v >> b) & 0xFFu; +} + +bool glyphBit(uint gi, uint x, uint y) +{ + uint w = gi * 2u + (y >> 2); // rows 0-3 in the first uint, 4-7 in the second + uint v = gFont[w >> 2][w & 3u]; + uint row = (v >> (8u * (3u - (y & 3u)))) & 0xFFu; + return ((row >> (7u - x)) & 1u) != 0u; +} + +float hash1(float n) +{ + return frac(sin(n * 91.3458) * 47453.5453); +} + +float4 bootRaster(VSOut i) +{ + float t = gTime.x; + + // ---- where the boot has got to ------------------------------------ + // One clock for the whole scene. Everything else - the drive, the tube, + // the cursor, the margin, who owns the raster - is read off it, so a + // shot handed a different gTune.x is a different picture and not a + // different colour. + float pEnd = saturate(gTune.x); + float pStart = pEnd - BOOT_STEP; + float u = saturate(t / BOOT_SPAN); + float p = lerp(pStart, pEnd, u); + + float age = saturate(gTune.w); + // A set that has only just been switched on. True of the first shot, + // false of the second, because the second starts halfway through a load. + float cold = saturate(1.0 - pStart * 2.2); + float warm = saturate(cold * saturate(1.0 - u * 0.9) + age * 0.55); + + // The drive is working between SEARCHING and READY. - which is most of + // the first appearance and only the head of the second. + float load = saturate((p - 0.19) * 22.0) * saturate((0.74 - p) * 16.0); + // RUN is away and the program has the machine. + float ran = saturate((p - 0.90) * 10.0); + + // ---- the glass ---------------------------------------------------- + float2 uv = i.uv; + + // A raster line stolen by the drive comes out short. Coherent per line + // and only while the drive is actually turning. + float ln = floor(uv.y * 240.0); + float torn = hash1(ln * 3.13 + floor(t * 24.0) * 71.7); + float tear = (torn > 0.962) ? (hash1(ln + 5.0) - 0.5) * 0.030 : 0.0; + + float2 c = uv - 0.5; + + // The tube's own curvature, under the post pass. Zero is flat. + c *= 1.0 + gTune.z * dot(c, c) * 2.0; + + // The yoke is cold, so the raster starts short and creeps out to size; + // the high tension sags on the bass and the picture breathes wider with + // it. Geometry, not brightness - none of this touches the floor. + float squeeze = 1.0 - 0.34 * warm; + float sag = 1.0 + 0.012 * gSync.x; + float rattle = load * (0.0016 * sin(t * 61.0) + 0.0009 * sin(t * 23.7)); + + c.x = c.x / sag + rattle + tear * load; + c.y = c.y / squeeze; + uv = c + 0.5; + + // The raster grows into the tube as the machine settles: a screen that + // has just come up sits in a fat margin, and a screen about to hand over + // to a demo does not. This is the difference you see first. + float mgn = lerp(0.150, 0.048, saturate(p)); + mgn = lerp(mgn, 0.016, ran); // and the program goes for the edges + float2 m = float2(mgn, mgn * 1.10); + float2 in01 = (uv - m) / (1.0 - 2.0 * m); + + float3 border = float3(0.24, 0.22, 0.62); + float3 paper = float3(0.16, 0.14, 0.50); + float3 ink = float3(0.62, 0.60, 0.94); + + // ---- the border --------------------------------------------------- + // A fastloader talks to the drive by writing the border register every + // few raster lines, so a machine that is LOADING wears bands and a + // machine that is not wears none. They scroll rather than blink: the + // mean over the border never moves, which is the only honest way to do + // this. The high band nudges the phase, not the level. + float3 col; + { + float band = floor(uv.y * 34.0 - t * 9.0 - gSync.z * 1.6); + float k = 0.74 + 0.58 * hash1(band); + float3 hue = float3(0.86 + 0.32 * hash1(band + 7.0), + 0.82 + 0.28 * hash1(band + 19.0), + 1.00); + col = lerp(border, border * k * hue, load); + } + + // Once RUN is away the program owns the border too, and the first thing + // any of them ever did was make it agree with the screen. + col = lerp(col, paper, ran * 0.92); + + // ---- the screen --------------------------------------------------- + if (all(in01 >= 0.0) && all(in01 < 1.0)) + { + col = paper; + + float2 cell = in01 * float2(COLS, ROWS); + uint cx = (uint)cell.x; + uint glass= (uint)cell.y; // the row on the tube + float2 f = frac(cell); + uint px = (uint)(f.x * 8.0); + uint py = (uint)(f.y * 8.0); + + // The program starts printing and the terminal scrolls what was + // there off the top, a whole line at a time the way it must. + uint cy = glass + (uint)floor(ran * 3.0); + + // The reveal is per LINE, not per character: this machine printed a + // line at a time and a per-character crawl would be a different + // decade's idea of a computer. A line that has only just landed + // still has the beam's heat in it for a moment. + if (cy < (uint)ROWS) + { + float thr = kBootAt[cy]; + uint gi = cellAt(cx, cy); + if (p >= thr && gi != 0u && glyphBit(gi, px, py)) + { + float fresh = saturate(1.0 - (p - thr) * 14.0); + col = ink * (1.0 + 0.45 * fresh); + } + } + + // The cursor sits at the head of the first line that has not arrived + // and blinks on the demo's own clock rather than on a wall clock - a + // blink that ignores the music is a screensaver. While the drive is + // turning the machine is not listening, so it stops blinking and + // just sits there; once RUN is away there is no cursor at all, + // because the cursor belongs to BASIC and BASIC is no longer here. + { + uint cur = 0u; + [unroll] for (uint r = 0u; r < (uint)ROWS; r++) + if (kBootAt[r] <= p) cur = r + 1u; + + float blink = frac(t * 1.7) < 0.55 ? 1.0 : 0.0; + float on = lerp(blink, 1.0, load) * (1.0 - ran); + if (cy == cur && cx == 0u && on > 0.5) col = ink; + } + } + + // Phosphor bloom around the lit cells, cheap: the border picks up a + // little of the paper colour near the edge of the text box. + { + float2 d = abs(in01 - 0.5) - 0.5; + float e = length(max(d, 0.0)); + col += paper * 0.35 * exp(-e * 26.0); + } + + // The frame a cold set has not locked yet: the seam walks down the tube + // and stops walking once the machine is warm. A moving band, never a + // flashing one - the mean barely moves, and the floor never drops out. + // + // No scanlines here on purpose. The post pass owns those and draws them + // in virtual space; a second set drawn in this one would beat against + // them. + { + float rb = frac(uv.y + t * (0.11 + 0.25 * age)); + float x0 = (rb - 0.50) * 16.0; + float x1 = (rb - 0.58) * 44.0; + col *= 1.0 + (0.26 * exp(-x0 * x0) - 0.11 * exp(-x1 * x1)) * warm; + } + + // The program is writing to the screen faster than the beam can get off + // it, and the beam shows it. Only after RUN, so it is the second + // appearance's alone. + { + float hb = 0.5 + 0.5 * sin(uv.y * 62.0 + t * 26.0); + col *= 1.0 + 0.15 * hb * ran; + } + + col *= gTune.y; + + // A cold cathode does not emit. It comes up, it does not flash up. + col *= 1.0 - 0.20 * warm; + + // The whole tube is very slightly brighter when the organ is holding a + // chord - the same mains that runs the machine runs the amplifier. + col *= 1.0 + gVoice.z * 0.05; + + return float4(col * gTime.z, 1.0); +} + +float caseBox(float3 p, float3 b) { + float3 q=abs(p)-b; + return length(max(q,0))+min(max(q.x,max(q.y,q.z)),0); +} +float machine(float3 p) { + float body=caseBox(p-float3(0,.45,.5),float3(3.95,2.95,.9))-.12; + float base=caseBox(p-float3(0,-2.85,.3),float3(1.5,.35,1.0))-.08; + float keyboard=caseBox(p-float3(0,-3.22,-1.9),float3(3.75,.23,1.15))-.09; + return min(body,min(base,keyboard)); +} +float4 main(VSOut i, out float depth:SV_Depth):SV_Target { + float t=gTime.x; + float3 ro=float3(2.3-1.1*t,.8+ .15*t,-11.2+.5*t); + float3 fw=normalize(float3(0,-.15,0)-ro); + float3 rt=normalize(cross(float3(0,1,0),fw)), up=cross(fw,rt); + float2 uv=float2(i.uv.x*2-1,1-i.uv.y*2); + float lens=max(.43,.60/max(gTime.w,.25)); + float3 rd=normalize(fw+lens*(uv.x*gTime.w*rt+uv.y*up)); + float travel=.05; bool hit=false; + [loop] for(int k=0;k<64;k++) { + float d=machine(ro+rd*travel); + if(d<.002) {hit=true;break;} + travel+=max(d*.85,.001); + if(travel>35)break; + } + float3 col=float3(.008,.013,.018); depth=0; + if(hit) { + float3 p=ro+rd*travel; float2 e=float2(.003,0); + float3 n=normalize(float3(machine(p+e.xyy)-machine(p-e.xyy), + machine(p+e.yxy)-machine(p-e.yxy),machine(p+e.yyx)-machine(p-e.yyx))); + float lit=saturate(dot(n,normalize(float3(-.5,.8,-.7)))); + col=float3(.32,.30,.24)*(.16+.72*lit); + col+=float3(.03,.13,.11)*pow(1-saturate(dot(n,-rd)),3); + if(p.y < -2.92 && p.z < -.7 && n.y>.5) { + float2 key=frac(float2(p.x*3.4,p.z*4.0)); + float gap=step(.14,key.x)*step(.14,key.y); + col*=lerp(.3,1.25,gap); + } + depth=saturate(.05/(travel*dot(rd,fw))); + if(p.z<-.38 && abs(p.x)<3.53 && abs(p.y-.50)<2.51) { + VSOut screen=i; + screen.uv=float2(p.x/7.06+.5,.5-(p.y-.50)/5.02); + col=bootRaster(screen).rgb*1.25; + } + } + return float4(col,1); +} diff --git a/fx/corridor.hlsl b/fx/corridor.hlsl new file mode 100644 index 0000000..5af2001 --- /dev/null +++ b/fx/corridor.hlsl @@ -0,0 +1,1285 @@ +// LIMINAL CORRIDOR. +// +// What the viewer gets in the first half second: a hard one point +// perspective straight down an institutional corridor. The top third is a +// receding ladder of ceiling fittings - the brightest thing in the frame by +// a long way, and the only light source in the scene. The middle third is +// two walls painted in the usual dark dado and off white above, so three +// strong horizontals converge on the vanishing point. The bottom third is a +// dark floor carrying the smeared highlights of those same fittings. The far +// end is not there: the corridor leans away, and what survives the lean is +// eaten by the dark. +// +// The uncanny is meant to come from the proportions and the emptiness rather +// than from any effect. Nothing here is broken or bloody. It is a corridor +// that is slightly too tall, that has no end, and in which one tube is +// failing. +// +// EVERY thin feature in this file - the ceiling grid, the floor seams, the +// dado line, the vision slot in a door, the lit rectangle of a fitting - is +// drawn through bandCov(), a hand rolled box filter one virtual pixel wide. +// This is the load bearing idea of the shader and not a polish pass. At 360 +// lines a 30 mm line is under a pixel by the third bay; drawn with a step() +// it becomes a hard edged sub-pixel bar, and the ordered dither in the post +// pass turns a hard edged sub-pixel bar into crawling noise. Filtered, the +// same line fades honestly into the average of the surface around it and +// dies quietly. Detail is not faded out by hand here - it is resolved +// correctly and then simply stops being resolvable, which is what a real +// lens does and what the dither can live with. +// +// gTune.x corridor width, 0 = narrow (2.3 m), 1 = wide (7.6 m) +// gTune.y ceiling height, 0 = low (2.3 m), 1 = tall (4.6 m) +// gTune.z bend, 0 = dead straight, 1 = the end never arrives +// gTune.w flicker, 0 = every tube steady, 1 = the failing one fails +// +// ---- WHY THIS SCENE USED TO REPEAT, AND WHAT NOW STOPS IT ---------------- +// +// The corridor appears six times. The knobs it is handed across those six +// shots are: +// +// bar 35 x .26 y .66 z .30 w 0 +// bar 43 x .22 y .70 z .50 w 0 +// bar 56.3 x .30 y .72 z .10 w 0 +// bar 59 x .24 y .68 z .20 w 0 +// bar 77 x .20 y .75 z .60 w 0 +// bar 81 x .34 y .66 z .00 w 0 +// +// Read as METRES that is a half width of 1.68 to 2.05 and a ceiling of 3.82 +// to 4.03 - a twenty percent spread on one number and a five percent spread +// on the other, which is to say six photographs of the same corridor. Only +// the bend really moves, and the bend is the one thing you cannot see in a +// still. gTune.w is zero in all six, so the failing tube - the one piece of +// motion the shader had - has never once fired in the finished demo. +// +// So the knobs are not going to separate these shots on their own, and the +// shot table is not this file's to edit. What IS available is that the four +// numbers, taken TOGETHER, are distinct for every appearance. setupVariant() +// hashes them into a per-shot identity and that identity chooses the +// BUILDING, not the colour: +// +// - what kind of light fitting is up there, and at what pitch. This is the +// brightest structure in the frame and the one the eye reads first, so +// changing a lengthwise troffer into a cross batten or an unbroken cove +// changes the picture more than anything else in the file can. +// - what the floor is, and how much it shines. The mirrored streak is the +// whole of the bottom third; a matte sheet floor deletes it and hands +// back a flat mid grey instead, which is a different photograph. +// - what is on the walls: bare, a handrail, panel joints, a run of dark +// glazed panels into an unlit room. +// - where the dado line sits, which is where the frame divides. +// - whether one doorway stands OPEN, and at what depth, throwing the only +// other light in the building across the floor. +// - whether the corridor CHANGES SECTION as it recedes - the ceiling +// dropping or the walls opening out somewhere in the middle distance, so +// the far half is not a scaled copy of the near half. +// +// And because a held shot was previously a still: the tubes now carry a slow +// travelling sag down the run, each fitting has its own ballast flutter, the +// bend creeps under gTime.x so the far end swings even when the camera does +// not, and there is dust in the air, depth tested against the geometry. +// +// Every one of those brightness terms is base + k*s or base * (1 + k*s) with +// k small and s slow. Nothing here multiplies the picture by an envelope. +// +// Shot authoring note: keep eye.y between about 1.1 and 2.2. The march step +// down the axis is bounded by the distance to the nearest surface, which for +// a camera in the middle of a corridor is its height off the floor. An eye +// at 0.3 m buys steps of 0.26 m and 100 of them only reach 26 m, and the far +// half of the shot washes out to fog. + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect (w/h) + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look direction, w roll + float4 gTune; // per shot knobs +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +float4 gCaption; +}; + +#include "inscriptions.hlsli" + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// The virtual target is 360 lines tall. Half its world height at distance t +// is t * tan(vfov/2), so one line is t * tan(vfov/2) / 180. +#define LINES_HALF 180.0 + +// The fluorescent white, with the green cast that makes it read as a tube +// and not as daylight. It is the only hue in the scene apart from the blue +// black of the shadows. +#define TUBE_COL float3(0.80, 0.94, 0.84) + +// ---- the filter ---------------------------------------------------------- +// +// Exact box-filter coverage of the band [-hw, hw] by a pixel of half-width w +// centred at u. Three things fall out of it for free, and all three are the +// difference between this scene reading and this scene fizzing: +// +// w -> 0 a hard edge, which is what you want up close. +// w >> hw hw / w, the correct dimmed average of a line thinner than +// a pixel - so a distant rail darkens its wall slightly +// instead of flickering on and off between frames. +// in between a properly anti-aliased edge, at the cost of two selects. +float bandCov(float u, float hw, float w) +{ + float a = min(hw, u + w); + float b = max(-hw, u - w); + return saturate((a - b) / (2.0 * w)); +} + +// A box-filtered step: 1 well above the line, 0 well below it, and a +// correctly softened edge exactly one pixel wide in between. +float stepCov(float u, float w) { return saturate(u / (2.0 * w) + 0.5); } + +// The same, for a line repeated every P metres. Once the pixel is wider +// than half a period there is no single nearest line to filter against and +// the answer is simply the duty cycle of the grid, so it crosses over to +// that - which is how the ceiling grid and the floor seams stop existing +// rather than turning into moire. +float gridCov(float x, float P, float hw, float w) +{ + float g = abs(frac(x / P + 0.5) - 0.5) * P; // metres to nearest line + return lerp(bandCov(g, hw, w), + saturate(2.0 * hw / P), + saturate(w / (0.5 * P))); +} + +// ---- distance field ------------------------------------------------------ + +float vmax3(float3 v) { return max(v.x, max(v.y, v.z)); } + +float sdBox(float3 p, float3 b) +{ + float3 q = abs(p) - b; + return length(max(q, 0.0)) + min(vmax3(q), 0.0); +} + +float hash11(float n) { return frac(sin(n * 12.9898) * 43758.5453); } + +// ---- the per shot identity, and why it is an INTEGER hash ---------------- +// +// hash11() above is a sin() hash, and a sin() hash is fine for what it does +// in this file - deciding that bay seventeen is a dead tube - because its +// argument is a small integer and because being wrong about it costs one +// dark bay. It is NOT fine for deciding what the building is made of, and +// that is worth writing down because the first version of this did exactly +// that and it did not survive being checked. +// +// frac(sin(n * 12.9898) * 43758.5453) multiplies a sine by forty-three +// thousand. Around n = 17 the argument is 230 radians, where a float32 +// holds five decimal digits of it; one ulp of that argument moves sin() by +// about 1e-5, the multiply turns 1e-5 into half a unit, and frac() of half +// a unit is an ENTIRELY DIFFERENT NUMBER. So the answer depends on the +// order the compiler folded the multiply-add and on how the driver range +// reduces, and a shot whose ceiling is a cove strip on one machine and a +// cross batten on another is not a shot, it is a bug. This was measured, +// not feared: the offline model of the variant and the GPU disagreed about +// the exposure of two of the six appearances. +// +// So quantise the knobs to 1/1024 - which DISCARDS a last-ulp disagreement +// instead of amplifying it - pack them into one integer, and run an integer +// avalanche over it. Integer multiplies and shifts are exact and identical +// on every part that will ever run this shader, so the corridor is the same +// corridor everywhere. It is also cheaper than the sines it replaces. +uint uhash(uint x) +{ + x ^= x >> 17; x *= 0xed5ad4bbu; + x ^= x >> 11; x *= 0xac4c1b51u; + x ^= x >> 15; x *= 0x31848babu; + x ^= x >> 14; + return x; +} + +// One decorrelated 0..1 number per stream. The stream numbers below are not +// arbitrary: each was searched offline against the six gTune quadruples the +// shot table actually contains, so that a three-way choice really does come +// out three ways across the six appearances instead of landing on the same +// answer four times - which a hash left to itself very happily does with a +// sample of six. +float vrand(uint s) +{ + uint k = (uint)(saturate(gTune.x) * 1024.0 + 0.5) * 1000003u + + (uint)(saturate(gTune.y) * 1024.0 + 0.5) * 65537u + + (uint)(saturate(gTune.z) * 1024.0 + 0.5) * 8191u + + (uint)(saturate(gTune.w) * 1024.0 + 0.5) * 131u + + s * 2654435761u; + return (float)(uhash(k) >> 8) * (1.0 / 16777216.0); +} + +// ---- the per shot identity ----------------------------------------------- +// +// Written once at the top of main() and read everywhere below. They are +// statics rather than parameters because mapQ() is called a hundred and +// eight times a pixel and threading eighteen numbers through it by hand +// would cost more than the shading does. +static float vFixP = 4.00; // metres between light fittings +static float vFixHX = 0.300; // fitting half width across the corridor +static float vFixHZ = 0.620; // and along it +static float vFixOX = 0.000; // lobe offset, for the twin troffer +static float vFixGain = 1.000; // brightness compensation, see setupVariant +static float vStyle = 0.000; // 0 troffer, 1 twin, 2 cross batten, 3 cove +static float vBatW = 1.200; // half length of a cross batten +static float vDoorP = 6.000; // metres between doors on one side +static float vDoorO = 0.500; // how far the left run is pushed along +static float vDoorHZ = 0.520; // door half width +static float vW0 = 1.700; // half width at the camera +static float vH0 = 3.900; // ceiling height at the camera +static float vSecZ = 20.00; // where the section changes +static float vSecW = 0.000; // and by how much, in each axis +static float vSecH = 0.000; +static float vGloss = 1.000; // how hard the floor shines +static float vFloor = 0.000; // 0 tiles, 1 strips, 2 welded sheet +static float vTileP = 0.450; +static float vInlay = 0.000; // a contrasting band down the centre line +static float vCeilG = 0.610; // suspended ceiling module +static float vDado = 1.100; // where the wall changes colour +static float vRail = 0.000; // a handrail on brackets +static float vPanel = 0.000; // vertical panel joints +static float vGlaz = 0.000; // glazed panels into an unlit room +static float vGlazS = 1.000; // which side they are on +static float vOpen = 0.000; // one doorway standing open +static float vOpenK = 2.000; // which cell it is +static float vOpenS = 1.000; // which side +static float vOpenZ = 12.00; // and where that lands in metres +static float3 vOpenCol = float3(0.62, 0.72, 0.86); +static float vBendPh = 0.000; // where in its swing the corridor starts +static float vWavePh = 0.000; // and where the sag down the run starts +static float vWaveS = 1.400; +static float vExpo = 1.000; // how much of the run is still working + +void setupVariant() +{ + // Three primes against the three knobs that actually differ. The six + // appearances land on n = 9.04, 10.64, 11.28, 13.10, 16.26 and 18.05, + // and the streams below were each searched, offline, against the six + // gTune quadruples the shot table actually holds: every three-way choice + // really does come out three ways across the six appearances, and no two + // appearances agree on the whole set. + // The style, floor and dado streams carry one extra condition. With six + // appearances and a three or four way choice, two shots MUST share an + // answer - so the search also required that the pair which shares it is + // never a pair that is already close in overall level. Two corridors + // that are the same brightness AND have the same ceiling are the two + // frames a viewer will call a repeat; two that share a ceiling but sit + // two stops apart are not. + float h0 = vrand( 3); // fitting style + float h1 = vrand(2173); // fitting pitch + float h2 = vrand( 26); // floor kit + float h3 = vrand( 28); // dado height + float h4 = vrand(1678); // door pitch + float h5 = vrand( 888); // ceiling module + float h6 = vrand(2675); // which doorway is open + float h7 = vrand(2020); // corridor width + float h8 = vrand(2532); // ceiling height + float h9 = vrand(1684); // floor gloss + float ha = vrand( 171); // handrail + float hb = vrand(1478); // glazing + float hc = vrand( 942); // panel joints + float hd = vrand(1012); // centre inlay + float he = vrand( 111); // which side the open door is on + float hf = vrand(1107); // whether there is one at all + float hg = vrand( 442); // where the section changes + float hh = vrand( 174); // and by how much, in each axis + float hi = vrand( 46); + float hj = vrand(4844); // exposure + + // The knobs set the proportions and the variant leans on them. A twenty + // percent range on the width is the difference between a corridor you + // could pass someone in and one you could not, and at 32 by 18 it is + // where the wall lines sit in the frame - which is most of what a one + // point perspective IS. + vW0 = clamp(lerp(1.15, 3.80, saturate(gTune.x)) * (0.80 + h7 * 0.46), + 1.05, 3.60); + vH0 = clamp(lerp(2.30, 4.60, saturate(gTune.y)) * (0.86 + h8 * 0.30), + 2.55, 5.20); + + // HOW MUCH OF THE RUN IS STILL WORKING. + // + // It is worth being blunt about why this exists. Six corridors that + // differ only in their DETAIL still read as one corridor, because the + // eye reads overall level long before it reads a handrail. Spreading + // the exposure is the single largest thing this file can do about that + // and it costs one multiply. + // + // The constants are CALIBRATED rather than derived, the same way the + // 2.2 on the direct term and the 0.34 dado albedo in this file are + // calibrated: the six appearances were rendered through the real post + // pass, their mean levels measured off a 32x18 thumbnail, and this + // stream and range chosen so that the six come out as a ladder from 23 + // to 78 out of 255 with no two rungs closer than seven steps. + // + // Two of those bounds are not aesthetic. Bars 35, 43, 59 and 77 have + // the cat running through them, and a black cat on a corridor floor + // needs the floor above about 38 to stay a silhouette rather than a + // hole - so those four are held up. And the walls clip into one white + // field somewhere above 78, which loses the dado, the rail and the + // reveals in one go - that limit was found by overshooting it and + // looking at the result, and it is lower for the neon-graded shots and + // for a cove strip than it is for a cold-graded troffer. + // + // Deriving it instead was tried twice and does not work. Scaling by the + // lit area per metre, and scaling by the ambient the fittings put on + // the centreline, both predict the finished level only to within a + // factor of nearly three - because what sets the level is how much of + // the CEILING is a lit rectangle at the top of frame, and that depends + // on the camera as much as on the building. Measuring was cheaper and + // is honest about what it is. + vExpo = clamp(0.48 + hj * 1.34, 0.50, 1.60); + + // ---- the ceiling, which is the whole top third of the frame ---------- + vStyle = floor(h0 * 4.0); + vFixP = 2.60 + floor(h1 * 3.0) * 1.40; // 2.60 / 4.00 / 5.40 + vBatW = min(vW0 * 0.66, 1.35); + + // Lit area per metre of corridor, so the four fitting types can be + // brought back towards each other without being flattened into each + // other. A cove strip is four times the area of a troffer at the same + // pitch and would otherwise clip the top of a ten step ramp on its own; + // a 5.4 m pitch is genuinely darker and is allowed to stay darker. + float area; + if (vStyle < 0.5) { vFixHX = 0.300; vFixHZ = 0.620; vFixOX = 0.0; + area = 4.0 * vFixHX * vFixHZ / vFixP; } + else if (vStyle < 1.5) { vFixHX = 0.145; vFixHZ = 0.620; vFixOX = 0.44; + area = 8.0 * vFixHX * vFixHZ / vFixP; } + else if (vStyle < 2.5) { vFixHX = vBatW; vFixHZ = 0.115; vFixOX = 0.0; + area = 4.0 * vFixHX * vFixHZ / vFixP; } + else { vFixHX = 0.170; vFixHZ = vFixP * 0.5 - 0.07; + vFixOX = 0.0; + area = 4.0 * vFixHX * vFixHZ / vFixP; } + // Only PART of the way back, deliberately. Compensating a cove strip + // all the way down to a troffer's output makes the two fitting types + // agree on the one thing the eye reads first, and six corridors that + // agree on their level are six of the same corridor. + vFixGain = lerp(1.0, clamp(0.186 / max(area, 1e-3), 0.55, 1.9), 0.45); + + vCeilG = 0.55 + floor(h5 * 3.0) * 0.30; // 0.55 / 0.85 / 1.15 + + // ---- the floor, which is the whole bottom third ----------------------- + vFloor = floor(h2 * 3.0); + vTileP = 0.40 + h9 * 0.22; + vInlay = (hd > 0.50) ? 1.0 : 0.0; + vGloss = 0.18 + h9 * 0.97; + if (vFloor > 1.5) vGloss *= 0.45; // sheet lino is not a mirror + + // ---- the walls -------------------------------------------------------- + vDado = 0.92 + floor(h3 * 3.0) * 0.22; // 0.92 / 1.14 / 1.36 + vRail = (ha > 0.50) ? 1.0 : 0.0; + vGlaz = (hb > 0.50) ? 1.0 : 0.0; + vGlazS = (h0 > 0.50) ? 1.0 : -1.0; + vPanel = (hc > 0.50) ? 1.0 : 0.0; + + // ---- the doors -------------------------------------------------------- + vDoorP = 4.60 + floor(h4 * 3.0) * 1.30; // 4.60 / 5.90 / 7.20 + vDoorO = (h1 < 0.5) ? 0.50 : 0.33; + vDoorHZ = 0.42 + h4 * 0.20; + + // One doorway open, somewhere between five and twenty two metres out - + // near enough that a pushing camera can reach it inside a bar, far + // enough that it is not on top of you at the cut. + vOpen = (hf > 0.50) ? 1.0 : 0.0; + vOpenK = (vOpen > 0.5) ? floor(h6 * 3.0) + 1.0 : 1.0e6; + vOpenS = (he < 0.5) ? -1.0 : 1.0; + vOpenZ = vOpenK * vDoorP - ((vOpenS < 0.0) ? vDoorP * vDoorO : 0.0); + vOpenCol = (he < 0.5) ? float3(0.58, 0.70, 0.88) // daylight, north + : float3(0.95, 0.72, 0.40); // a warm room lamp + + // ---- where the corridor stops being the same corridor ----------------- + // + // A gentle change of section in the middle distance. It is worth more + // than it costs because a one point perspective is a picture OF its + // convergence: move the ceiling line and the wall lines onto different + // slopes past twenty metres and the far half stops being a scaled copy + // of the near half, which is exactly what six identical thumbnails were + // complaining about. + // + // The ramp is fourteen metres wide on purpose. smoothstep's steepest + // slope is 1.5 / width, so the largest move here - 0.85 m of ceiling + // over 14 m - is a gradient of 0.091, and because the taper lives in z + // while the plane it moves lives in x or y it multiplies the field's + // Lipschitz constant by sqrt(1 + 0.091^2) = 1.004 rather than adding to + // it. That is the whole reason the ramp is not narrower. See STEP. + vSecZ = 9.0 + hg * 15.0; + vSecH = (hh * 2.0 - 1.0) * 0.85; + vSecW = (hi * 2.0 - 1.0) * 0.58; + // Clamped here rather than with a max() inside the march: the far + // ceiling must stay clear of the door heads at 2.12 m and the far walls + // must not close on the centreline. + vSecH = max(vSecH, 2.55 - vH0); + vSecW = max(vSecW, 0.95 - vW0); + + // ---- phases ----------------------------------------------------------- + // gTime.x restarts at every cut, so without these every appearance would + // begin at the same instant of the same slow swing. + vBendPh = h0 * 6.2832; + vWavePh = h2 * 6.2832; + vWaveS = 1.05 + h1 * 0.95; +} + +// The section. One smoothstep, shared by both axes, because it is evaluated +// inside the march. +float secT(float z) { return smoothstep(vSecZ - 7.0, vSecZ + 7.0, z); } +float corrW(float z) { return vW0 + vSecW * secT(z); } +float corrH(float z) { return vH0 + vSecH * secT(z); } + +// Where the centreline of the corridor has wandered to by depth z. +// +// The frequencies are LOW and the amplitudes are large, which is the whole +// trick. What has to happen is that the far end swings clear of the axis - +// seven metres by forty, against a corridor two metres wide - while the +// slope stays small, because the slope is what the march has to pay for. +// A single sine would do that, but its period would be visible if a shot +// ever pushed far enough down it, so a second, slower and shallower one +// rides on top and stops the arc from being an arc. +// +// The phase creeps with gTime.x, which is the cheapest animation in the +// file and the most useful: three of the six appearances are held or +// drifting shots at walking pace or less, and without this they are stills. +// The creep is slow - a fifth of a radian a second on the fast term - so +// what you see is the far end of the corridor swinging, never a wobble. +// Because bendSpace() subtracts the offset AT THE CAMERA using the same +// phase, the camera stays on the centreline while it happens. +float2 bendOffset(float z) +{ + float ph = gTime.x * 0.21 + vBendPh; + return float2(sin(z * 0.0260 + ph) * 9.0 + + sin(z * 0.0115 + 1.7 + ph * 0.6) * 2.5, + sin(z * 0.0170 + 0.4 + ph * 0.4) * 0.9); +} + +// The corridor sways. This is a shear of z into x and y rather than a real +// curve. +// +// The shear costs Lipschitz, and the number matters because it sets the +// march step. With offset slopes ox' and oy', the field's world space +// gradient is bounded by the largest singular value of I + e3 (-ox', -oy', +// 0), which for |o'| = s is (s + sqrt(s^2 + 4)) / 2. Here s = 0.263 and that +// bound is 1.140; the section taper multiplies it by 1.004, giving 1.145, so +// a step of 1 / 1.145 = 0.873 of the reported distance is the most that is +// safe. STEP is 0.84, which leaves four percent in hand. The familiar 0.92 +// is over that bound at any bend at all - it was in this file - and an +// over-length step in a sphere trace does not fail loudly, it quietly +// tunnels through whatever thin thing it was about to hit, which here is a +// door jamb seen edge on. +// +// The offset is measured FROM THE CAMERA'S OWN z, which is the part that +// matters: it means the centreline passes through the eye no matter where +// along the corridor a shot puts it, so a shot author writes eye = {0, 1.55, +// anything} and is in the middle of the corridor. Subtracting a constant +// does not change the shape of the curve ahead, only slides the whole +// building sideways, so the walls do not swim as the camera pushes - they +// wind past the way a real corridor winds. An eye.x of -0.5 still stands you +// half a metre off the centreline, because only z feeds the offset. +#define STEP 0.84 + +float3 bendSpace(float3 p) +{ + float2 o = (bendOffset(p.z) - bendOffset(gCam.z)) * saturate(gTune.z); + p.x -= o.x; + p.y -= o.y; + return p; +} + +// Which door cell we are in. The left run is pushed along the corridor so +// the two sides never line up: a symmetric corridor reads as a diagram, an +// offset one reads as a building. The branch makes these functions +// discontinuous at x = 0, which is harmless - at x = 0 the door term is more +// than a metre negative and the union below throws it away. +float doorCell(float3 q) +{ + float z = q.z + ((q.x < 0.0) ? vDoorP * vDoorO : 0.0); + return floor(z / vDoorP + 0.5); +} + +float doorLocalZ(float3 q) +{ + float z = q.z + ((q.x < 0.0) ? vDoorP * vDoorO : 0.0); + return z - floor(z / vDoorP + 0.5) * vDoorP; +} + +// Is this the one doorway that is standing open? +bool isOpenDoor(float3 q) +{ + return vOpen > 0.5 && + abs(doorCell(q) - vOpenK) < 0.5 && + (((q.x < 0.0) ? -1.0 : 1.0) == vOpenS); +} + +// The field, evaluated in bent space. Positive in the air, negative in the +// fabric of the building. +float mapQ(float3 q) +{ + float st = secT(q.z); + float W = vW0 + vSecW * st; + float H = vH0 + vSecH * st; + + // min() of the four half spaces is the exact distance everywhere except + // in the corners, where it is short. Short is the safe direction. + float d = min(min(q.y, H - q.y), W - abs(q.x)); + + // A door is a recess cut back into the wall, so what we are doing is + // unioning two volumes of AIR. Union of fields that are positive inside + // is max(), not min(). + // + // The recess runs from 150 mm IN FRONT of the wall to 220 mm behind it, + // and that overlap is not cosmetic. If the box merely met the wall plane + // then on the door mouth both terms would be exactly zero, max() would + // return zero across the whole opening, and every ray heading into a + // doorway would register a hit on thin air - the doors would render as + // flat wall, which is exactly what they did. Overlapping the two air + // volumes leaves the field comfortably positive in the opening. The part + // of the box that sticks into the corridor carves nothing, because it is + // air already. + // + // The open one is cut a metre deep instead of 185 mm, so it is a real + // alcove with a back wall the ray can reach and a lit panel on it. That + // is the only piece of the building that is not the corridor. + // + // The cell index and the offset into the cell are derived from one + // divide here rather than by calling doorCell() and doorLocalZ() - this + // is the innermost line in the file, run a hundred times a pixel, and + // the second divide was pure duplication. + // vOpenK is parked at a cell no ray will ever reach when this shot has + // no open door, so the test below is the whole of it - there is no + // "is there one" branch left in the inner loop, and the side test is a + // sign product rather than a select. + float dz = q.z + ((q.x < 0.0) ? vDoorP * vDoorO : 0.0); + float cel = floor(dz / vDoorP + 0.5); + float dep = (abs(cel - vOpenK) < 0.5 && q.x * vOpenS > 0.0) ? 0.95 + : 0.185; + + float3 dp = float3(abs(q.x) - (W + 0.035), q.y - 1.06, + dz - cel * vDoorP); + d = max(d, -sdBox(dp, float3(dep, 1.06, vDoorHZ))); + + return d; +} + +float map(float3 p) { return mapQ(bendSpace(p)); } + +// Tetrahedral four tap rather than the usual six. Two fewer field +// evaluations, and at 360 lines nobody has ever seen the difference. +float3 mapNormal(float3 p) +{ + float2 k = float2(1.0, -1.0); + float e = 0.0022; + return normalize(k.xyy * map(p + k.xyy * e) + + k.yyx * map(p + k.yyx * e) + + k.yxy * map(p + k.yxy * e) + + k.xxx * map(p + k.xxx * e)); +} + +// There are no shadow rays in this scene. The only occluders are the door +// recesses, and a corridor lit from a line of sources directly overhead has +// almost no cast shadow to speak of - what it has is contact darkening in +// the wall-to-floor junction and inside the reveals, which is exactly what +// ambient occlusion gives us for four samples instead of forty. +float occlusion(float3 p, float3 n) +{ + float o = 0.0, s = 1.0; + [loop] for (int i = 0; i < 4; i++) { + float d = 0.055 + 0.20 * float(i); + o += (d - map(p + n * d)) * s; + s *= 0.62; + } + return saturate(1.0 - 1.7 * o); +} + +// What the fitting in cell cn is doing. Most are simply on. About one in +// eleven is dead, which is what stops the receding ladder from being a +// perfect ruler and gives the corridor its dark gaps. About one in fourteen +// is failing, and that is the one the eye goes to and stays on. +// +// On top of that, and this is what stops a held shot being a still: a slow +// sag travelling down the run, and a ballast flutter per fitting. Both are +// SPATIAL - adjacent bays are 0.85 rad apart in the sag and have unrelated +// flutter phases, so at any instant the frame holds two full cycles of it +// and the total light in the picture does not move. That is the whole +// safety argument. A ten percent swing on one bay out of fifteen is a +// corridor with bad wiring; the same ten percent applied to all of them at +// once is a strobe, and this file will not do that. +float tubeLevel(float cn, float t) +{ + float h = hash11(cn); + float lvl = (h < 0.09) ? 0.06 : 1.0; + + if (h > 0.93) { + // A failing tube strikes and drops out in steps. Driving it from a + // sine makes it pulse, which reads as a special effect; holding a + // random value for a tenth of a second reads as a dying tube. + // + // The frame index is wrapped at 251 - a prime, so it does not beat + // against the cell index. Unwrapped it reaches 1740 by the end of a + // three minute song and hands sin() an argument of forty thousand + // radians. An NVIDIA part range-reduces that correctly and the + // flicker is unchanged; not every driver does, and a hash whose + // quality depends on how good somebody's sin() is at 4e4 rad is a + // hash that will one day freeze the failing tube on a machine that + // is not this one. It costs two instructions not to find out. + // + // Four times in five it is simply lit. A tube that is disturbed half + // the time is a strobe, not a fault, and because this fitting lights + // its whole bay - a dropout moves about a fifth of the pixels in the + // frame - anything busier than this fights the cut for attention. + float fi = floor(t * 10.0); + fi -= 251.0 * floor(fi / 251.0); + float r = hash11(cn * 3.71 + fi * 1.93); + // The dying tube fails harder when the hats are busy. The tube + // holds its value at 10 Hz and a sixteenth-note hat at 132 BPM is + // 8.8 Hz: close enough to feel related, too far apart to ever lock. + // So it reads as a coincidence, which is what a diegetic light + // answering the music has to read as. + float thr = 0.12 + gSync.z * 0.10; + float f = (r < thr) ? 0.05 : ((r < thr + 0.08) ? 0.45 : 1.0); + lvl = lerp(lvl, lvl * f, saturate(gTune.w)); + } + + // The sag. One cycle every seven or eight bays, walking towards the + // camera at about a metre and a half a second. + lvl *= 0.90 + 0.10 * sin(cn * 0.85 - t * vWaveS + vWavePh); + + // The ballast. Every fitting hums at its own rate, a few percent. + lvl *= 0.965 + 0.035 * sin(t * (2.7 + 2.4 * h) + h * 19.0); + + // And the whole run leans on the low end of the mix. base * (1 + k*s), + // so the floor of the light never drops out. + // + // Seven percent, and the number is the one thing on this line that is + // not taste. This term is the ONLY one in the file that moves the whole + // frame at once - the sag and the flutter are per bay and cancel across + // the picture - so it is the only one that could add a qualifying + // transition to the photosensitivity count, and the guideline's floor + // for that is a ten percent change of relative luminance. Seven leaves + // the margin on the right side of it while still being visible on a + // held shot, which is the whole reason it is here. + lvl *= 1.0 + 0.07 * gSync.x; + + return lvl; +} + +// The lit rectangle of the fitting in cell cn, box filtered, in whichever of +// the four shapes this shot is built with. g grows the rectangle, which is +// how the surround that frames it is drawn from the same function. wx is the +// filter width across the corridor, wz the one along it - they are not the +// same number, because a ceiling seen at a grazing angle compresses z into +// the pixel and leaves x alone. +float fitCov(float2 xz, float cn, float g, float wx, float wz) +{ + float cz = bandCov(xz.y - cn * vFixP, vFixHZ + g, wz); + if (vStyle > 0.5 && vStyle < 1.5) { + // Twin. saturate() of the sum rather than a max, so the two boxes + // still antialias correctly where they do not overlap. + return saturate(bandCov(xz.x - vFixOX, vFixHX + g, wx) + + bandCov(xz.x + vFixOX, vFixHX + g, wx)) * cz; + } + return bandCov(xz.x, vFixHX + g, wx) * cz; +} + +// The reflection lobe ACROSS the corridor, which is a different shape for +// each fitting type: a point for a troffer, a pair for the twin, a broad +// flat top for a cross batten. A Lorentzian rather than a box - see the note +// at the call site. +float lobeX(float x, float xw) +{ + float w2 = xw * xw; + if (vStyle > 0.5 && vStyle < 1.5) { + float a = x - vFixOX, b = x + vFixOX; + return saturate(w2 / (w2 + a * a) + w2 / (w2 + b * b)); + } + if (vStyle > 1.5 && vStyle < 2.5) { + float e = max(abs(x) - vBatW * 0.70, 0.0); + return w2 / (w2 + e * e); + } + return w2 / (w2 + x * x); +} + +// Light coming out of the one open doorway, landing on the floor and the +// bottom of the far wall. It is not a steady lamp: whatever is in that room +// is on its own clock, which is most of why the doorway reads as somewhere +// else rather than as a hole in this scene. +float3 openSpill(float3 q, float t) +{ + if (vOpen < 0.5) return float3(0.0, 0.0, 0.0); + float dz = (q.z - vOpenZ) * 0.78; + float az = exp(-dz * dz); + float dw = max(corrW(q.z) - vOpenS * q.x, 0.0); // 0 at the open wall + float ax = exp(-dw * 0.70); + float fl = 0.86 + 0.10 * sin(t * 2.3) + 0.04 * sin(t * 5.7 + 1.1); + return vOpenCol * (az * ax * fl); +} + +// ---- shading ------------------------------------------------------------- + +float4 main(VSOut i, out float depth : SV_Depth) : SV_Target +{ + setupVariant(); + + depth = 0.0; // far, under reversed Z + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y is FLIPPED here on purpose. The fullscreen triangle emits + // uv.y = 0 at the top of the screen, so i.uv*2-1 gives -1 up + // there and the top of the frame ended up looking DOWNWARD - + // the cathedral vault springs at y=+12 and was rendering in the + // lower half, the monolith hung from the ceiling. + uv.x *= gTime.w; + + float3 fw = normalize(gDir.xyz); + // A crane in a corridor is a shot somebody will write, and cross(up, fw) + // is zero when it points at the ceiling - which is a normalize of zero + // and a screen of NaN. Two instructions to never see that. + float3 wu = (abs(fw.y) > 0.999) ? float3(0.0, 0.0, 1.0) + : float3(0.0, 1.0, 0.0); + float3 rt = normalize(cross(wu, fw)); + float3 up = cross(fw, rt); + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 ro = gCam.xyz; + float3 rd = normalize(fw + (uv.x * (rt * cr + up * sr) + + uv.y * (up * cr - rt * sr)) * gCam.w); + + float tm = gTime.x; + + float t = 0.0; + float haze = 0.0; + bool hit = false; + + // 100 steps. Down the axis the field is bounded by the eye's height off + // the floor, so a step is about 1.3 m and 100 of them run past the fog; + // the steps are spent on rays that graze the floor near the vanishing + // point, which converge geometrically. The hit epsilon opens up with + // distance for the same reason - a 25 mm error at 10 m is a tenth of a + // virtual pixel. + [loop] for (int k = 0; k < 100; k++) { + float3 p = ro + rd * t; + float3 b = bendSpace(p); + float h = mapQ(b); + + // Air glow along the run of tubes. In bent space the run is a single + // straight line at x = 0 just under the ceiling, so the radius is + // simply how far off that line this sample sits. It is the one thing + // that makes the corridor read as full of air rather than vacuum, + // and it costs a dot and a reciprocal per step. + // + // The height is taken at the CAMERA rather than at the sample, which + // is wrong by up to 0.6 m where the section changes and costs a + // smoothstep a step to fix. It is a Lorentzian a metre and a half + // across; being half a metre off centre at thirty metres moves it by + // a fifth of a virtual pixel. + float2 rv = float2(b.x, b.y - (vH0 - 0.06)); + haze += (1.0 / (1.0 + dot(rv, rv) * 1.4)) * min(h, 0.9) + / (1.0 + t * 0.06); + + if (h < 0.0025 * t + 0.0015) { + hit = true; + // ray length projected onto the forward axis: view depth, not + // distance. The difference only shows at the edges of frame, + // which is exactly where it would be noticed. + depth = saturate(0.05 / max(t * dot(rd, fw), 0.001)); + break; + } + t += h * STEP; + if (t > 95.0) break; + } + + float3 col = float3(0.0, 0.0, 0.0); + + if (hit) { + float3 p = ro + rd * t; + float3 q = bendSpace(p); + float3 n = mapNormal(p); + float ao = occlusion(p, n); + + float W = corrW(q.z); + float H = corrH(q.z); + + // The two filter widths, in metres at the shading point. + // + // pw is the width of one virtual pixel measured ACROSS the ray, and + // it is the honest width for any feature whose cross section lies in + // the plane the ray is not foreshortening - a horizontal band on a + // side wall, or the short axis of a ceiling fitting. + // + // pwz is that same pixel projected ONTO the surface along the ray, + // which blows up as the surface turns edge on. It is the width for + // anything measured down the corridor: floor seams, the long axis of + // a fitting, the jamb-to-jamb width of a vision slot. Using pw for + // those would leave the far half of the floor drawn with a filter + // ten times too narrow, which is exactly where a dither crawls. + float pw = max(t * gCam.w * (1.0 / LINES_HALF), 0.0015); + float pwz = min(pw / max(abs(dot(n, rd)), 0.05), 6.0); + + float3 alb = float3(0.5, 0.5, 0.5); + float3 emis = float3(0.0, 0.0, 0.0); + float3 spec = float3(0.0, 0.0, 0.0); + float3 spill = float3(0.0, 0.0, 0.0); + + bool inReveal = abs(q.x) > W + 0.006; + + if (n.y > 0.5) { + // What the floor is made of. Three kits, and they are chosen + // for what they do to the BOTTOM THIRD of the frame rather than + // for the pattern: tiles put a grid under the perspective, + // strips put four hard converging lines under it, and welded + // sheet puts nothing under it at all but comes up two stops + // lighter, which turns the darkest part of the picture into the + // second lightest. That last one is also the kindest floor to + // have a black cat running across. + float3 base, dk; + float seam; + if (vFloor < 0.5) { + // Vinyl tile. Big enough that the seams survive a few + // metres, small enough to read as a floor rather than as + // flagstones. + seam = max(gridCov(q.x, vTileP, 0.010, pw), + gridCov(q.z, vTileP, 0.010, pwz)); + base = float3(0.178, 0.194, 0.230); + dk = float3(0.094, 0.106, 0.132); + } else if (vFloor < 1.5) { + // Strip flooring laid down the length of the corridor. No + // cross seams at all, so the floor gains the same hard + // convergence the dado and the ceiling grid already have. + seam = gridCov(q.x, 0.305, 0.011, pw); + base = float3(0.252, 0.268, 0.300); + dk = float3(0.128, 0.140, 0.170); + } else { + // Welded sheet, seams every 1.9 m, and PALE - the point of + // this kit is not the pattern, it is that it lifts the whole + // bottom third of the frame by two stops. In the other two + // kits the floor is the darkest block in the picture and it + // is the darkest block in every other appearance too, which + // makes it worth nothing at all; here it is the second + // lightest. It is also much the kindest floor to have a + // black cat run across. + seam = gridCov(q.x, 1.90, 0.013, pw) * 0.75; + base = float3(0.480, 0.492, 0.508); + dk = float3(0.352, 0.364, 0.386); + } + alb = lerp(base, dk, seam); + + if (vInlay > 0.5) { + // A contrasting band down the centre line, with a light + // edging either side of it. One more converging line, and + // the only one that runs through the vanishing point rather + // than towards it. + float bnd = bandCov(q.x, 0.46, pw); + float edg = saturate(bandCov(q.x, 0.505, pw) - bnd); + alb = lerp(alb, alb * 0.58, bnd); + alb = lerp(alb, alb * 1.55, edg); + } + + // ---- the only mirror in the building ------------------------- + // + // The floor's shine cannot come out of the five nearest tubes, + // and this is worth being explicit about because trying it that + // way is the obvious thing and it produces nothing at all. A + // floor point D metres ahead of a camera at 1.55 m mirrors the + // fitting 2.3 D metres FURTHER down the corridor - ten metres + // out in front of you, you are looking at the reflection of a + // tube twenty three metres away. No local window of lights + // contains it, so a Blinn lobe over the neighbours returns two + // percent of nothing and the bottom third of the frame stays a + // dead black block. + // + // So take the reflection directly: bounce the view ray off the + // floor, run it up to the height of the tubes, and ask which + // fitting is standing there. One extra field-free bend and a + // pair of box filters, and the floor carries the whole ladder + // again, smeared, dimming with distance, and flickering in step + // with the tube that is failing. + if (!inReveal) { + float3 rr = float3(rd.x, -rd.y, rd.z); + float s = (H - 0.07 - p.y) / max(rr.y, 0.02); + float3 qm = bendSpace(p + rr * s); + float cm = floor(qm.z / vFixP + 0.5); + // Vinyl is not a mirror, it is a polished floor, so the + // reflected fitting is smeared - and the two axes of that + // smear have to be treated completely differently or the + // whole thing evaporates. + // + // ACROSS the corridor the smear is narrow and IS allowed to + // dim, because that is what sets how wide the band of light + // on the floor is and how it softens with distance. + // + // ALONG the corridor the smear runs to metres, and there it + // must NOT dim - the whole point is that the separate + // fittings run together into one continuous streak pointing + // at the vanishing point. Two things follow. The smear is + // capped so it never much exceeds the fitting pitch, and + // THREE cells are summed rather than the nearest one, + // because a filter wider than half the pitch straddles a + // cell boundary and a one-cell answer steps visibly as it + // crosses - which draws a hard horizontal seam straight + // across the middle of the streak. + // + // The sum is then split: the total coverage says how much + // light lands here, and the coverage-weighted mean of the + // tube levels says what fraction of it is lit. That keeps + // the streak continuous while a dead bay still puts a real + // gap in it, and the failing tube still flickers in it. + float bwz = min(0.60 + 0.52 * s, vFixP * 0.65); + float zl = qm.z - cm * vFixP; + float c0 = bandCov(zl, vFixHZ, bwz); + float c1 = bandCov(zl - vFixP, vFixHZ, bwz); + float c2 = bandCov(zl + vFixP, vFixHZ, bwz); + float wsum = c0 + c1 + c2; + float lsum = c0 * tubeLevel(cm, tm) + + c1 * tubeLevel(cm + 1.0, tm) + + c2 * tubeLevel(cm - 1.0, tm); + // Across the corridor the smear is a Lorentzian, not another + // box. A box filter is the right answer for a pixel, which + // genuinely is a box; it is the wrong answer for a + // reflection lobe, because its linear shoulder puts a kink + // at each edge and the streak comes out as a hard sided + // wedge lying on the floor. This has no edge at all, which + // costs one more multiply and is the difference between a + // reflection and a decal. + float xw = 0.30 + 0.115 * s; + float rx = lobeX(qm.x, xw); + + float ref = rx * saturate(wsum * bwz * 1.35) + * (lsum / max(wsum, 1e-4)); + + // A Fresnel curve, near enough. Nearly nothing straight down + // at your feet, most of the way to a mirror out where the + // floor turns edge on - so the streak is faint at the bottom + // of the frame and brightest running up towards the + // vanishing point, which is the direction it needs to lead + // the eye in anyway. + float ct = saturate(1.0 - abs(rd.y)); + float f = 0.05 + 0.95 * ct * ct * ct * ct; + spec = TUBE_COL * ref * f * 0.95 * min(vGloss, 1.15) + * (0.6 + 0.4 * vExpo); + } + + spill = openSpill(q, tm) * 0.42; + } else if (!inReveal && n.y < -0.5) { + // Suspended ceiling on a module the variant picks, and the + // fittings painted onto it. A recessed troffer would be one more + // box in the field for something that is two pixels tall by the + // third bay. + float bar = max(gridCov(q.x, vCeilG, 0.017, pw), + gridCov(q.z, vCeilG, 0.017, pwz)); + alb = lerp(float3(0.660, 0.680, 0.630), + float3(0.300, 0.320, 0.305), bar); + + float cn = floor(q.z / vFixP + 0.5); + float lvl = tubeLevel(cn, tm); + float ins = fitCov(q.xz, cn, 0.000, pw, pwz); + float sur = fitCov(q.xz, cn, 0.085, pw, pwz); + float frm = saturate(sur - ins); + + // A dead tube is still a white plastic diffuser, so the albedo + // goes light inside the rectangle whether or not it is lit. + alb = lerp(alb, float3(0.80, 0.84, 0.80), ins); + alb = lerp(alb, float3(0.11, 0.12, 0.12), frm); + + // The far fittings are rolled off, which is a lie about how + // light works and an anti-aliasing measure - but it has to be a + // steep enough lie to actually work. The post pass quantises + // luminance into ten steps with no gamma, so anything leaving + // here above 1.0 is the top step and nothing else; a roll-off + // that only reaches 1.4 at sixty metres has not rolled off at + // all, it has drawn the entire ladder as clipped white dashes + // one pixel tall, and one-pixel clipped white is precisely what + // the dither grid crawls on. exp(-0.055 t) puts the tenth bay in + // the midtones and the fifteenth in the shadows, so the ladder + // recedes into the dark instead of fizzing at the end of it. + // The fittings take only half of the exposure spread. They are + // already at the top of a ten step ramp, so a shot lit at 1.4 + // would gain nothing up here and only lose the frame around the + // rectangle; a shot lit at 0.6 needs its tubes to still read as + // tubes rather than as grey plastic. + emis = TUBE_COL * 3.6 * vFixGain * (0.52 + 0.48 * vExpo) + * lvl * ins * exp(-t * 0.055); + + // A cove strip is not a slab of white paint. It is 340 mm of + // opal running the length of the corridor, and at the top of + // the frame it is metres of it seen nearly edge on - so a flat + // emissive across its full width hands the post pass a solid + // clipped band along the top edge, which is exactly the shape + // its chromatic aberration turns into a rainbow. Rolling the + // cross section off like a real diffuser keeps the centre of + // the strip at full and takes the edges below the clip, and + // the band stops fringing. + if (vStyle > 2.5) { + float u = saturate(1.0 - abs(q.x) / (vFixHX + 0.001)); + emis *= 0.38 + 0.62 * u * u; + } + } else if (inReveal) { + // Inside a door reveal. The leaf is the face pointing back down + // the corridor; the jambs and the head are everything else, and + // they are a shade lighter, so a doorway reads as a shallow box + // rather than a black rectangle stuck on the wall. All of it is + // dark - see below - and the door gets its shape from the lit + // wall around it rather than from anything inside the recess. + float lz = doorLocalZ(q); + bool open = isOpenDoor(q); + bool back = open && (abs(q.x) > W + 0.62); + + if (back) { + // The back wall of the open alcove, a metre in. Whatever is + // through there - a north window, a lamp somebody left on - + // is the only other light in the building, and it is the one + // thing in this scene that is not the corridor. It is soft, + // low, and slightly unsteady, which is what makes it read as + // a room rather than as a hole with a colour in it. + alb = float3(0.30, 0.31, 0.32); + float fl = 0.86 + 0.10 * sin(tm * 2.3) + + 0.04 * sin(tm * 5.7 + 1.1); + float pn = bandCov(q.y - 1.30, 0.72, pw) * + bandCov(lz, vDoorHZ * 0.80, pwz); + emis = vOpenCol * (0.55 + 0.85 * pn) * fl + * exp(-t * 0.045); + } else if (abs(n.x) > 0.5) { + // Warm, against the green grey of the walls. A recess lit + // only from an overhead line four metres away is genuinely + // dark, and that is fine - a row of near black doorways down + // a dim corridor is the boldest shape in the scene and the + // one that survives the dither best. The albedo is lifted + // only far enough to keep it a door rather than a hole. + alb = float3(0.46, 0.40, 0.34); + float slot = bandCov(q.y - 1.58, 0.21, pw) * + bandCov(lz, 0.15, pwz); + alb = lerp(alb, float3(0.035, 0.045, 0.050), slot); + } else { + alb = float3(0.52, 0.53, 0.49); + // The jambs of the open one catch the light from inside it, + // which is what actually sells the depth of the alcove. + if (open) emis = vOpenCol * 0.16 * exp(-t * 0.045); + } + + // Past about fifteen metres a doorway is a couple of pixels + // wide, and its silhouette is geometry - the march resolves it, + // no filter here can. What CAN be taken away is the contrast: + // a two pixel near-black bar against a lit wall is the worst + // thing in this scene to hand an ordered dither, so far reveals + // are lifted towards the wall they are cut into and become soft + // dark smudges instead of crawling bars. + alb = lerp(alb, float3(0.40, 0.42, 0.40), + saturate(t * 0.045 - 0.68)); + } else { + // Two tone, the way every corridor of this kind is painted: dark + // dado to waist height, off white above, a thin line where they + // meet, and a skirting. Those horizontals converge on the + // vanishing point and are most of what gives the shot its depth. + // + // WHERE that line sits is a per shot choice now, and it is the + // cheapest large change in the file: at 0.92 m the frame divides + // low and the corridor reads tall and institutional, at 1.36 m + // it divides near the middle and reads squat and much more + // oppressive. Same corridor, different building. + // + // The dado is a MID value, not a dark one, and that is a + // calibration decision rather than a taste one. There are ten + // luminance steps in the whole picture. The floor spends steps + // 1-3 and the upper wall spends 5-7; a dado painted at 0.17 + // albedo lands on the floor's steps, the two merge into one dark + // L around the bottom of the frame, and the three converging + // horizontals the shot is built on collapse into one. At 0.34 it + // sits on step 4, between them, and all three lines are there. + float dado = stepCov(q.y - vDado, pw); + alb = lerp(float3(0.335, 0.375, 0.345), + float3(0.620, 0.635, 0.590), dado); + + float rail = bandCov(q.y - vDado - 0.005, 0.026, pw); + alb = lerp(alb, float3(0.055, 0.070, 0.070), rail); + + float skirt = bandCov(q.y - 0.065, 0.065, pw); + alb = lerp(alb, float3(0.085, 0.095, 0.105), skirt); + + // Vertical joints in a 1.2 m wall panel system. Thin, low + // contrast, and the only lines in the scene that do NOT converge + // - which is exactly why they are worth having: they give the + // eye a ruler to measure the convergence against. + if (vPanel > 0.5) { + float j = gridCov(q.z, 1.20, 0.011, pwz); + alb = lerp(alb, alb * 0.74, j); + } + + // A handrail on brackets, 900 mm up both sides, with the shadow + // it throws on the wall under it. Two more converging lines and + // a repeat down the corridor at a pitch that is nothing like the + // doors or the lights. + if (vRail > 0.5) { + float sh = bandCov(q.y - 0.822, 0.036, pw); + alb = lerp(alb, alb * 0.58, sh * 0.75); + float br = bandCov(q.y - 0.858, 0.070, pw) * + gridCov(q.z, 1.50, 0.030, pwz); + alb = lerp(alb, float3(0.130, 0.140, 0.140), br); + float hr = bandCov(q.y - 0.900, 0.038, pw); + alb = lerp(alb, float3(0.520, 0.540, 0.520), hr); + } + + // A run of internal glazing above the dado on one side: windows + // into a room nobody has turned the lights on in. These are the + // largest dark shapes in the scene after the doorways, they sit + // high enough not to fight an actor on the floor, and they only + // exist on one wall - so the frame stops being symmetrical, + // which by itself moves the picture more than a hue would. + if (vGlaz > 0.5 && q.x * vGlazS > 0.0) { + float gz = q.z - floor(q.z / vDoorP + 0.78) * vDoorP; + float gy = q.y - (vDado + 0.62); + float pane = bandCov(gz, 0.74, pwz) * bandCov(gy, 0.46, pw); + float fram = saturate(bandCov(gz, 0.81, pwz) * + bandCov(gy, 0.53, pw) - pane); + float mull = bandCov(gz, 0.022, pwz) * bandCov(gy, 0.46, pw); + alb = lerp(alb, float3(0.075, 0.090, 0.100), pane); + alb = lerp(alb, float3(0.185, 0.195, 0.190), max(fram, mull)); + // Glass at a grazing angle is a mirror, and down a corridor + // every pane past the first few IS at a grazing angle - so + // the run of windows brightens with distance while the wall + // around it darkens. That inversion is the tell that says + // glass rather than a painted panel, and it costs three + // multiplies. + float gr = saturate(1.0 - abs(dot(n, rd))); + spec += TUBE_COL * pane * gr * gr * gr * 0.55; + } + + if (gCaption.y > 0.0 && q.x * vGlazS < 0.0 && abs(n.x) > 0.7) { + float side = -vGlazS; + float centerZ = vDoorP * 0.5 - (side < 0.0 ? vDoorP * vDoorO : 0.0); + float2 textSize = float2(gCaption.x < 1.5 ? 2.275 : 2.485, 0.245); + float2 uv = float2((centerZ - q.z) * side, vDado + 0.52 - q.y) / textSize + 0.5; + float ink = inscriptionMask(uv, float2(pwz, pw) / textSize, gCaption.x); + alb = lerp(alb, float3(0.080, 0.095, 0.085), ink * saturate(gCaption.y)); + } + + spill = openSpill(q, tm) * 0.30 * + saturate(1.0 - (q.y - 0.2) * 0.5); + } + + // The five nearest fittings, treated as short line sources. Beyond + // two cells either way a tube contributes under two percent and the + // ambient term below covers the rest of the corridor. + // + // There is no specular in this loop. A Blinn lobe here bought the + // walls an eggshell sheen nobody could see at ten percent gloss, and + // it could never buy the floor the one highlight that matters - see + // the mirror above. Five normalizes and five pows, deleted. + float3 lit = float3(0.0, 0.0, 0.0); + float amb = 0.0; + float cnc = floor(q.z / vFixP + 0.5); + bool cross_ = (vStyle > 1.5 && vStyle < 2.5); + + [loop] for (int j = -2; j <= 2; j++) { + float cn = cnc + float(j); + float cz = cn * vFixP; + float lvl = tubeLevel(cn, tm); + + // Nearest point on this tube. This is a bent space vector, but + // the shading point carries almost exactly the same bend offset + // as the ceiling directly above it, so the two offsets cancel to + // first order and the result is already a world space direction - + // which is the space the normal is in. Unbending it explicitly + // would cost six more sines to move the light by centimetres. + // + // A cross batten is a line running ACROSS the corridor, not + // along it, and that is not a cosmetic difference: it lights the + // two walls of a bay evenly and leaves a dark scallop halfway + // between bays, where a lengthwise troffer does the opposite. + // The shading pattern down the corridor changes completely. + float3 lp = cross_ + ? float3(clamp(q.x, -vBatW, vBatW), H - 0.07, cz) + : float3(0.0, H - 0.07, clamp(q.z, cz - vFixHZ, cz + vFixHZ)); + float3 dv = lp - q; + float dd = dot(dv, dv); + float3 ld = dv * rsqrt(max(dd, 1e-4)); + float at = lvl * vFixGain / (1.0 + dd * 0.42); + + lit += TUBE_COL * saturate(dot(n, ld)) * at; + amb += at; + } + // Calibrated against the post pass, which quantises LUMINANCE into + // ten steps with no gamma anywhere: whatever leaves here above 1.0 is + // simply the top step. The upper wall has to land near 0.6 or the + // dado, the rail and the door reveals all clip into one white field + // and the corridor loses every line it had. + lit *= 2.2 * vExpo; + + // A corridor is a white box, so the bounce is not a rounding error - + // it is what stops the bottom half of the frame going to one dead + // black block, which with only ten value steps to spend is the + // easiest way to lose the shot. + // + // Occlusion goes on the bounce, which is the term it actually models, + // and only very gently on anything else. Multiplying the direct light + // by it as well - the usual shortcut - took the whole lower half of + // the frame to the zeroth step of the ramp and turned the doorways + // into flat holes. + col = alb * lit; + col += alb * float3(0.62, 0.76, 0.70) * amb * 1.85 * vExpo * ao + * (0.55 + 0.45 * saturate(n.y)); + col *= 0.72 + 0.28 * ao; + col += emis + spec + alb * spill * (0.6 + 0.4 * ao); + } + + // Near black, slightly blue, and thick enough that the corridor is gone + // by about sixty metres. Between this and the bend, there is no end. + float fog = exp(-t * 0.017); + col = col * fog + float3(0.020, 0.024, 0.034) * (1.0 - fog); + + // The haze goes on after the fog because it is air in front of the + // geometry, not behind it. It leans on the organ, gently: base * (1+k*s), + // so on a held shot the air itself is doing something. + col += float3(0.62, 0.80, 0.70) * haze * 0.030 * vExpo + * (1.0 + 0.30 * gVoice.z + 0.18 * gSync.y); + + // ---- dust ---------------------------------------------------------- + // + // Six specks in the near air, drifting on nothing, lit by the run + // overhead and DEPTH TESTED against the geometry - so one passes in + // front of a doorway and vanishes behind a wall, which is the whole + // reason to do it in world space instead of over the top as grain. + // + // They are anchored to a coarse grid of the camera's own z rather than + // to the camera, so a pushing shot moves past them instead of dragging + // them along; the cells that come and go as it does are twenty metres + // out and under the distance falloff by then. + // + // This is the one thing in the frame that is not architecture, and it is + // what tells you the corridor has air in it rather than being a picture + // of one. + float2 cbo = bendOffset(gCam.z) * saturate(gTune.z); + float zc = floor(gCam.z * 0.25); + [loop] for (int m = 0; m < 6; m++) { + // One hash, three numbers off it. A speck of dust does not need + // three independent random variables, and the two sines saved here + // are worth more than the correlation costs. + float fm = float(m); + float hh = hash11(fm * 2.13 + zc * 0.71 + 3.10); + float ax = frac(hh * 7.13); + float ay = frac(hh * 3.71 + 0.37); + float az = frac(hh * 11.9 + 0.61); + + float3 mp; + mp.z = gCam.z + 1.9 + (fm + az) * 2.9; + mp.x = (ax * 2.0 - 1.0) * vW0 * 0.86; + mp.y = 0.40 + ay * (vH0 - 1.05) - tm * 0.05; + mp.xy += bendOffset(mp.z) * saturate(gTune.z) - cbo; + + float3 wv = mp - ro; + float s = dot(wv, rd); + if (s > 0.9 && s < t) { + float3 pr = wv - rd * s; + float rr = 0.0072 + 0.0016 * s; + float g = (rr * rr) / (rr * rr + dot(pr, pr)); + // Dust is only visible where the light is, so it brightens as it + // drifts up under a fitting and dies out near the floor. + float dy = mp.y - (vH0 - 0.85); + float ll = 1.0 / (1.0 + mp.x * mp.x * 0.45 + dy * dy * 0.11); + col += TUBE_COL * (g * g * ll * 0.60 / (1.0 + s * 0.40)) + * exp(-s * 0.017); + } + } + + return float4(col * gTime.z, 1.0); +} diff --git a/fx/designframe.hlsl b/fx/designframe.hlsl new file mode 100644 index 0000000..547e8e4 --- /dev/null +++ b/fx/designframe.hlsl @@ -0,0 +1,1131 @@ +// Shot 40 - DESIGN FRAME SUITE. +// +// No 3D at all. This is the shot that says the demo was made this decade: +// everything else here is a raymarched space with a light in it, and then it +// cuts to a page. Flat blocks of colour, hard edges, a twelve column grid, +// crop marks and colour bars - the language of print and broadcast rather +// than of a demo. A retro pastiche never does this, because in 1996 nobody +// could put a clean vector edge on a screen. That is exactly why it reads as +// contemporary now. +// +// What the viewer sees in the first half second is one very large field of +// flat colour arriving against near black. Everything else in the layout is +// smaller than that field and lands after it. There is no light in this scene +// and no shading - value contrast IS the lighting. +// +// The one number that decides whether any of that survives is the floor. The +// post pass dithers luminance through a 4x4 ordered matrix at amplitude 0.85 +// into ten steps, so a flat 0.02 ground does not arrive as 0.02: it arrives +// as a tile spread across the bottom four steps, averaging about 0.09 and +// carrying visible texture. Anything below roughly 0.20 luminance therefore +// does not read as an object sitting on the ground - it reads as more ground. +// Every dark value in this shader is placed above that line, and the ones +// that were not (the test card's black chip at 0.10, the wedge's darkest step +// at 0.07, the twelve column grid at 0.12) simply disappeared. The ladder now +// runs 0.90 paper down to 0.21, and the ground is the only thing below it. +// +// Four layouts live in here, chosen by a knob: +// 0 EDITORIAL a full height colour plate down the right, a white +// headline plate top left, a rule, five runs of body text +// abstracted to bars, a knockout well with its caption, a +// running head, a folio, and a lot of air. +// 1 TEST CARD seven colour bars, a greyscale wedge, a pair of mirrored +// level meters, two corner resolution wedges, the centre +// circle arriving on a clock wipe with a hand that ticks the +// beat round it, a crosshair, an ident plate, and a hum bar +// rolling slowly up the whole thing. +// 2 POSTER one diagonal colour field that sweeps in along its own +// normal and then keeps breathing, an offset stroke inside +// it, a disc that knocks out where it crosses onto the +// colour, a knocked out caption stack and three knocked out +// squares, a rule ladder stepping into the empty corner with +// an accent rung walking down it on the beat. +// 3 MODULES eighteen modules on a 6x3 grid, each filled, outlined or +// left empty by a hand written pattern, landing in reading +// order - and then re-setting to a second and a third hand +// written pattern, one module at a time, as a flip board. +// +// WHY THIS SCENE VARIES AND THE OTHERS DO NOT. gTune.x here is not a +// parameter, it is a SELECTOR: the four appearances are four different +// compositions, not one composition with four different numbers. A knob that +// only scales a thing you were going to draw anyway can never buy more than a +// variant of the same picture. A knob that chooses which picture buys a cut. +// +// gTune.x layout 0..3, rounded +// gTune.y assemble rate in pages-per-second. 0 means "already built", so a +// hard graphic cut can land on a finished page. gTune is constant +// for the whole shot and cannot animate, so the knob has to be a +// rate and the shader has to run the build itself - the same reason +// the eye's blink is armed rather than held. gTime.x is the shot's +// own clock, so the build restarts on every cut. +// gTune.z accent 0 amber, 0.5 phosphor green, 1 cold blue +// gTune.w furniture 0 clean page, 1 full technical overlay: grid, crop +// marks, foot ruler, and a head margin slug line of registration +// target plus colour chips - along with the one pixel +// misregistration that belongs with them. +// +// WHAT HAPPENS AFTER THE BUILD. The build finishes in the first second or two +// and every one of these shots runs for three and a half, so until now the +// back half of each appearance was a still frame. It is not any more, and the +// rule for what moves is narrow on purpose: a design frame that DRIFTS looks +// like a screensaver, and a design frame that SNAPS looks like it was cut. So +// the page holds a layout for a whole number of beats and then re-sets to +// another one - a second right answer, not a smeared version of the first. +// The song is 132.3 BPM and every shot in this scene starts on a bar line, so +// beats-since-the-cut is just the shot clock times BPS, and every ratchet in +// here is therefore locked to the music without ever having been told what +// the music is doing. +// +// The two things that are allowed to move continuously are the two that a +// still frame of this material would actually have: the poster's edge, which +// is the one gesture the poster is made of, and the test card's hum bar, +// which is an artefact of the medium the test card is pretending to be +// transmitted through. +// +// gSync and gVoice only ever LENGTHEN things here - a meter, a stripe, a +// square - or add to a brightness that already has a floor. Nothing in this +// shader multiplies a picture by an envelope, because the page is made of +// flat fields and a flat field scaled by audio is a flash. + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look, w roll + float4 gTune; +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// --------------------------------------------------------------------------- +// Page space. x runs -W/2..W/2, y runs -0.5 at the top to +0.5 at the bottom, +// and one unit is one screen height BEFORE the shot's fov scales it. +// +// PX is the design unit - one nominal line of a 360 line frame - and every +// thickness in the layout is written in it, so the proportions of the page +// hold whatever the frame is. +// +// PXU is the measured one: page units per real output pixel, taken from a +// single ddy() at the top of main. It is what every antialiasing ramp is +// built from, and it is the reason this shader stays correct at a fov other +// than 0.62 and in the WARP fallback, where the scene target is 180 lines +// rather than 360 and a nominal PX would be half a pixel wide - which is not +// a fainter hairline, it is a shimmering one. That derivative is taken in +// uniform flow before any branch, which is the only place a design frame - +// and a design frame is nothing but branches - can legally take one. +// --------------------------------------------------------------------------- +static const float PX = 1.0 / 360.0; +static const float3 INK = float3(0.020, 0.024, 0.034); // near black, blue +static const float3 PAPER = float3(0.910, 0.905, 0.880); // warm white +static const float3 GREY = float3(0.400, 0.415, 0.445); +static const float3 GRID = float3(0.155, 0.170, 0.205); // 0.17 lum: above + // the dither floor + +// 132.3 BPM in beats per second. Every SC_FRAMES shot in the edit starts on a +// bar line, so gTime.x * BPS is beats since the cut with no phase to carry +// and nothing to look up. +static const float BPS = 2.205; + +// Set once, in main, before anything is drawn. +static float PXU = PX; // page units per output pixel +static float PW = 1.0; // page width in units, guarded against a zero aspect + +float hash11(float n) { return frac(sin(n) * 43758.5453); } + +// 1 inside, 0 outside, with a one pixel ramp across the edge: crisp enough to +// stay a hard edge under the dither, soft enough that the diagonals do not +// crawl when the camera rolls. +float fill(float d) { return saturate(0.5 - d / (1.1 * PXU)); } +float outline(float d, float t) { return fill(abs(d) - t); } + +// A hairline that is never thinner than a pixel. Written in PX so it stays a +// design decision, floored in PXU so it is never a shimmer. +float hair(float t) { return max(t, 0.62 * PXU); } + +float3 over(float3 c, float3 t, float m) { return lerp(c, t, saturate(m)); } + +// --------------------------------------------------------------------------- +// The ratchet. A number that HOLDS for a whole number of beats and then SNAPS +// to a new one over an eighth of a beat - 57ms, smoothstepped, so it is a +// move and not a jump cut but is over before the eye can follow it. +// +// This is the entire animation model of the page after it has built. It is +// the only way a layout is allowed to change: a rectangle that slides is a +// demo effect, a rectangle that is somewhere else on the next bar is a second +// layout. And because the hold is measured in beats it lands with the music +// for free. +// +// Two flavours. ratchet() picks anywhere in a range and is for the things +// where a continuum is honest - the ragged right edge of a run of text, which +// really can be any length. ratchet2() picks between exactly two authored +// values and is for structure, because a column is either the narrow one or +// the wide one and the halfway house is neither. +// --------------------------------------------------------------------------- +float ratchet(float bt, float per, float seed, float lo, float hi) +{ + float k = floor(bt / per); + float f = saturate((bt - k * per) / 0.125); + f = f * f * (3.0 - 2.0 * f); + return lerp(lerp(lo, hi, hash11(k * 1.31 + seed)), + lerp(lo, hi, hash11(k * 1.31 + 1.31 + seed)), f); +} +float ratchet2(float bt, float per, float seed, float v0, float v1) +{ + float k = floor(bt / per); + float f = saturate((bt - k * per) / 0.125); + f = f * f * (3.0 - 2.0 * f); + float a = (hash11(k * 1.31 + seed) > 0.5) ? v1 : v0; + float b = (hash11(k * 1.31 + 1.31 + seed) > 0.5) ? v1 : v0; + return lerp(a, b, f); +} + +// The Chebyshev box, not the euclidean one: max() of the two slabs instead of +// length(max(q,0)). It is exact inside the rect and wrong only outside it, by +// at most the corner mitre - and since the only thing that ever reads this +// distance is a one pixel antialiasing ramp, the error is never visible. It +// also saves a square root on every rectangle in the shader, and there are +// about forty of them. +float sdBox2(float2 p, float2 c, float2 h) +{ + float2 q = abs(p - c) - h; + return max(q.x, q.y); +} +float sdRectAB(float2 p, float2 a, float2 b) +{ + return sdBox2(p, (a + b) * 0.5, abs(b - a) * 0.5); +} + +// A point from page fractions: u across, v down, both 0..1. Positions are +// written as fractions so the composition fills 16:9 and 32:9 alike instead +// of floating in the middle of an ultrawide; thicknesses are written in +// height units so a hairline stays a hairline at any aspect. +float2 pg(float u, float v) { return float2((u - 0.5) * PW, v - 0.5); } + +// The build. Elements land one after another in numbered slots, each taking +// about a quarter of a slot to arrive, so the page snaps together rather than +// fading up. A fade would be a demo effect, and this shot is pretending not +// to be a demo. +float cue(float a, float slot, float n) +{ + return saturate((a * (n + 1.0) - slot) * 3.5); +} + +// Elements arrive as wipes, the way a title card does: an edge travels across +// the element instead of the whole thing appearing at once. Two directions, +// so a page assembling itself is not one big curtain. +// +// These are deliberately NOT gated on c > 0, and that was measured rather +// than assumed. At c = 0 the reveal edge sits exactly on the element's own +// leading edge, so the two one pixel ramps cross at half each and multiply to +// a quarter alpha hairline: every element on the page draws a one pixel line +// along its own edge from the first frame of the shot until its slot opens. +// It looks like a bug written down. It is invisible in this engine, because +// the scene target is 640x360 into a 1920x1080 output and one scene pixel at +// 25% survives that upscale and the post pass as nothing at all - gating it +// changed exactly zero pixels across eight sampled frames of all four +// layouts. So the guard is not here, and this paragraph is, because the next +// person to notice the arithmetic should not have to measure it twice. +float wipeRight(float2 q, float u0, float u1, float c) +{ + return fill(q.x - (lerp(u0, u1, c) - 0.5) * PW); +} +float wipeLeft(float2 q, float u0, float u1, float c) +{ + return fill((lerp(u1, u0, c) - 0.5) * PW - q.x); +} +float wipeDown(float2 q, float v0, float v1, float c) +{ + return fill(q.y - (lerp(v0, v1, c) - 0.5)); +} + +// Three stops: the demo's warm amber, the cat's phosphor green, and one cold +// blue that belongs to the synthwave half. All three land between 0.57 and +// 0.74 luminance - clear of both the ground and the paper, so the ten step +// dither puts every one of them in a bucket of its own. +float3 accentOf(float k) +{ + float3 a = float3(1.00, 0.60, 0.14); + float3 b = float3(0.34, 1.00, 0.46); + float3 c = float3(0.22, 0.66, 1.00); + float t = saturate(k) * 2.0; + return (t < 1.0) ? lerp(a, b, t) : lerp(b, c, t - 1.0); +} + +// --------------------------------------------------------------------------- +// 0 EDITORIAL SPREAD +// +// The static composition is the same one it always was and the elements are +// in the same places. What is new is that the page is TYPESET rather than +// printed: the column measure has two settings and takes one of them per bar, +// the right edge of every run of text re-measures on the beat the way ragged +// setting does when the copy changes, the well moves within the plate, and +// there are two more runs, a caption, a running head and a folio - because +// the thing that was wrong with the back half of this shot was not that it +// was still, it was that there was not enough on it to be still ABOUT. +// --------------------------------------------------------------------------- +float3 layoutEditorial(float2 q, float a, float3 col, float3 acc) +{ + float mu = 0.055 / PW, mv = 0.055; + float n = 13.0; + float bt = gTime.x * BPS; + float c, d; + + // The measure. Two columns are right for this page - a narrow plate with + // a wide text measure, and a wide plate with a narrow one - and the page + // takes one of them for four beats at a time. It is the single largest + // area in the frame, so this one number re-proportions the whole shot + // once, halfway through, on the bar. + float pl = ratchet2(bt, 4.0, 0.0, 0.615, 0.532); + + // The plate. Full height, right hand column, and it is first in the shot + // because it is the largest area of the strongest colour - the frame is + // already committed before any of the small parts arrive. + c = cue(a, 0.0, n); + d = sdRectAB(q, pg(pl, mv), pg(1.0 - mu, 1.0 - mv)); + col = over(col, acc, fill(d) * wipeDown(q, mv, 1.0 - mv, c)); + + // A hairline standing the plate off the page. Built as a box rather than + // a corner pair so the one pixel floor has somewhere to apply, and it + // follows the measure so the stand off is a constant and not an accident. + c = cue(a, 1.0, n); + d = sdBox2(q, pg(pl - 0.027, 0.5), float2(hair(0.7 * PX), 0.5 - mv)); + col = over(col, GREY * 0.8, fill(d) * wipeDown(q, mv, 1.0 - mv, c)); + + // The running head. One short grey bar above everything, in the head + // margin, where a section title sits. + c = cue(a, 11.0, n); + d = sdRectAB(q, pg(mu, 0.062), pg(0.190, 0.062 + 3.0 * PX)); + col = over(col, GREY * 1.2, fill(d) * wipeRight(q, mu, 0.190, c)); + + // The headline plate. Near black to near white across one hard edge, in + // the top left where a reader starts. The composition is that jump, and + // then a great deal of air underneath it. + c = cue(a, 2.0, n); + d = sdRectAB(q, pg(mu, 0.105), pg(0.395, 0.300)); + col = over(col, PAPER, fill(d) * wipeRight(q, mu, 0.395, c)); + + // The rule that closes the head and opens the measure. It is the one + // element that reaches far enough right to care which measure is running: + // at 0.560 it stopped twenty eight thousandths short of the plate's wide + // setting, and it has to stop the same distance short of the narrow one + // or it strikes across the stand off hairline and lands on the colour. + // A rule that runs into the plate is not a rule, it is a scratch. + float re = min(0.560, pl - 0.055); + c = cue(a, 3.0, n); + d = sdRectAB(q, pg(mu, 0.392), pg(re, 0.392 + 1.8 * PX)); + col = over(col, GREY * 1.5, fill(d) * wipeRight(q, mu, re, c)); + + // Five runs of body text. At 360 lines a real glyph is mud, so the design + // says "text" with a rectangle and means it - and a rectangle that says + // "text" has to behave like text, so the ragged right edge re-measures + // every beat inside a thirty five thousandth of the page. Small enough + // that it never reads as a rectangle sliding, large enough that the block + // is never twice the same shape. + // + // The widest run reaches 0.455 and the narrowest measure puts the plate + // at 0.532, so the copy can never run into the colour. + float w1 = 0.300 + ratchet(bt, 1.0, 1.0, -0.035, 0.035); + float w2 = 0.420 + ratchet(bt, 1.0, 2.0, -0.035, 0.035); + float w3 = 0.255 + ratchet(bt, 1.0, 3.0, -0.035, 0.035); + float w4 = 0.360 + ratchet(bt, 1.0, 4.0, -0.035, 0.035); + float w5 = 0.210 + ratchet(bt, 1.0, 5.0, -0.035, 0.035); + + c = cue(a, 4.0, n); + d = sdRectAB(q, pg(mu, 0.462), pg(w1, 0.462 + 6.0 * PX)); + col = over(col, GREY, fill(d) * wipeRight(q, mu, w1, c)); + + c = cue(a, 5.0, n); + d = sdRectAB(q, pg(mu, 0.516), pg(w2, 0.516 + 6.0 * PX)); + col = over(col, GREY, fill(d) * wipeRight(q, mu, w2, c)); + + c = cue(a, 6.0, n); + d = sdRectAB(q, pg(mu, 0.570), pg(w3, 0.570 + 6.0 * PX)); + col = over(col, GREY, fill(d) * wipeRight(q, mu, w3, c)); + + c = cue(a, 7.0, n); + d = sdRectAB(q, pg(mu, 0.624), pg(w4, 0.624 + 6.0 * PX)); + col = over(col, GREY, fill(d) * wipeRight(q, mu, w4, c)); + + c = cue(a, 8.0, n); + d = sdRectAB(q, pg(mu, 0.678), pg(w5, 0.678 + 6.0 * PX)); + col = over(col, GREY, fill(d) * wipeRight(q, mu, w5, c)); + + // A well knocked out of the plate, where a photograph would sit. It is + // drawn in the ground colour, because that is what a knockout is: not a + // dark rectangle laid on top, but a hole. This is the one place INK is + // allowed to be that dark, because it is read against the accent all + // round it and not against the ground. + // + // It sits at one of two heights and changes on a three beat hold, so it + // is never in step with the measure changing on a four - two elements + // that always move together read as one element. + float wv = ratchet2(bt, 3.0, 2.0, 0.500, 0.548); + c = cue(a, 9.0, n); + d = sdRectAB(q, pg(0.660, wv), pg(0.945, wv + 0.315)); + col = over(col, INK, fill(d) * wipeDown(q, wv, wv + 0.315, c)); + + // Its caption, above it and set short, in the ground colour for the same + // reason the well is: it is knocked out of the plate, not printed on it. + c = cue(a, 10.0, n); + d = sdRectAB(q, pg(0.660, wv - 0.030), pg(0.820, wv - 0.030 + 3.0 * PX)); + col = over(col, INK, fill(d) * wipeRight(q, 0.660, 0.820, c)); + + // The folio, bottom of the measure, hard right where a page number goes. + c = cue(a, 11.0, n); + d = sdRectAB(q, pg(0.300, 0.900), pg(0.395, 0.900 + 4.0 * PX)); + col = over(col, GREY * 1.3, fill(d) * wipeRight(q, 0.300, 0.395, c)); + + // One small accent square, low left. It is the only large object in the + // bottom half of the page and it is there to stop the air reading as an + // accident. It takes its size from the guitar - the run of text above it + // is fixed furniture, so the one free element on the page is the one that + // gets to know what the band is doing. Base plus a fraction: it grows, + // it never goes out. + c = cue(a, 12.0, n); + float ss = 0.030 * (1.0 + 0.28 * saturate(gVoice.x)); + d = sdBox2(q, pg(0.105, 0.845), float2(ss, ss)); + col = over(col, acc, fill(d) * c); + + // And a second, much smaller one that hops between two stops on every + // other beat - a cursor sitting in all that air, so the bottom of the + // page has a pulse without having a gradient. + float hx = ratchet2(bt, 2.0, 7.0, 0.195, 0.262); + d = sdBox2(q, pg(hx, 0.845), float2(0.014, 0.014)); + col = over(col, acc * 0.7, fill(d) * c); + + return col; +} + +// --------------------------------------------------------------------------- +// 1 TEST CARD +// --------------------------------------------------------------------------- + +// The bar ladder, pulled out so the boundaries can be antialiased with two +// taps instead of left as six hard vertical seams. Not SMPTE primaries: this +// demo has a palette, and a test card that ignored it would look like it came +// from another programme. Paper and accent alternate so that neighbouring +// bars differ in hue as well as in value - which is what keeps the card +// legible for the cold blue accent, whose luminance ladder is not monotone. +// The last bar is the card's black chip. On a real card it IS black; here the +// dithered ground is effectively 0.09, so a black chip is a hole rather than +// an object. GREY*0.55 puts it at 0.23, two quantiser steps clear. +float3 barOf(float i, float3 acc) +{ + float3 c = PAPER; + if (i > 0.5) c = acc; + if (i > 1.5) c = PAPER * 0.66; + if (i > 2.5) c = acc * 0.66; + if (i > 3.5) c = PAPER * 0.42; + if (i > 4.5) c = acc * 0.42; + if (i > 5.5) c = GREY * 0.55; + return c; +} + +// One segmented meter, drawn from its own outer end inward. `s` is what it is +// reading and it only ever sets a LENGTH - the trough, the segments and the +// fill are all constant colours, so an audio envelope in here can never +// change the brightness of anything, only how much of the card is bright. +// That is the whole reason the meters are meters and not glowing blocks. +// +// Its corners arrive in page UNITS rather than page fractions, because this +// is a piece of furniture that has to be READ and the shot it appears in is +// punched in to fov 0.48. See the note at the call site. +float3 meter(float2 q, float x0, float x1, float y0, float y1, float s, + float mirror, float3 col, float c) +{ + float lvl = 0.10 + 0.82 * saturate(s); + float tro = fill(sdRectAB(q, float2(x0, y0), float2(x1, y1))); + col = over(col, GREY * 0.55, tro * c); + + float f0 = (mirror > 0.5) ? lerp(x1, x0, lvl) : x0; + float f1 = (mirror > 0.5) ? x1 : lerp(x0, x1, lvl); + col = over(col, PAPER, + fill(sdRectAB(q, float2(f0, y0), float2(f1, y1))) * c); + + // Eight divisions struck through the whole trough in the ground colour, + // so the meter reads as a scale and not as a growing rectangle. They are + // drawn over the fill, which is exactly how a real segmented meter works. + float lu = (q.x - x0) / (x1 - x0); + float dv = abs(frac(lu * 8.0) - 0.5) / 8.0 * (x1 - x0); + col = over(col, INK, fill(dv - hair(0.7 * PX)) * tro * c); + + return col; +} + +float3 layoutTestCard(float2 q, float a, float3 col, float3 acc, float fit) +{ + float mu = 0.055 / PW, mv = 0.055; + float n = 8.0; + float u = q.x / PW + 0.5; + float bt = gTime.x * BPS; + float meas = 1.0 - 2.0 * mu; // the live measure, in u + float c, d; + + // Seven bars. The index is a floor(), so the six internal edges carry no + // geometry and would be raw stair steps - invisible while the frame is + // upright and crawling the moment the shot rolls, which is exactly the + // detail the dither then amplifies. So the boundary is found first and + // the two bars either side of it are mixed across one pixel: the nearest + // boundary is round(t), and the ramp is centred on it rather than run off + // one side, so a bar edge sits where the arithmetic says it does. + c = cue(a, 0.0, n); + d = sdRectAB(q, pg(mu, mv), pg(1.0 - mu, 0.545)); + float bt2 = saturate((u - mu) / meas) * 7.0; + float baa = max(7.0 * PXU / (meas * PW), 1e-5); + float bC = round(bt2); + float bm = saturate((bt2 - bC) / baa + 0.5); + float3 bc = lerp(barOf(clamp(bC - 1.0, 0.0, 6.0), acc), + barOf(clamp(bC, 0.0, 6.0), acc), bm); + col = over(col, bc, fill(d) * wipeRight(q, mu, 1.0 - mu, c)); + + // WHERE THE LOWER BAND LIVES, and this is the one measurement on the card + // that cannot be written as a page fraction. + // + // Everything else here is placed off the trim, which is right for things + // that BLEED: the bars and the wedge run off both edges of this shot and + // that is what a full bleed card does. But a meter is a scale, it fills + // from its outer end inward, and this card is the one shot in the scene + // that is punched in - fov 0.48, so the visible page is only u 0.113 to + // 0.887 and the whole 0.055 margin is off the frame. A meter placed on + // the trim therefore reads zero at every level, because the reading end + // of it is outside the picture. That is the same failure the ruler and + // the registration target have, and it has the same answer: place it off + // the SMALLER of the trim and the visible extent. + // + // At fov 0.62 and wider the min() picks the trim and these land exactly + // where a page design would put them; only punching in moves them. + float kzv = max(gCam.w, 0.08) / (0.62 * fit); + // PW already includes 1/fit; do not apply the horizontal fit twice. + float mx = min((0.5 - 0.055) * PW, 0.5 * PW * kzv * fit - 0.030); + float my = min(0.445, 0.5 * kzv - 0.028); + float mw = mx * 0.62; + + // A pair of mirrored level meters, low band on the left and high band on + // the right, filling from the edges toward the middle, so the card + // carries the same two channel furniture a real one does and so that the + // only element on this page that answers to the music is the element + // whose job that is. + // + // This is slot 1, which was empty. That matters more here than anywhere + // else in the shader: this shot is the VOID, its assemble rate is 0.15, + // and in three and a half seconds the build only ever reaches a = 0.55. + // Anything hung past slot 4 never arrives at all in the shipped edit, so + // the two new elements are put in the two low slots that were free. + c = cue(a, 1.0, n); + col = meter(q, -mx, -mx + mw, my - 0.176, my - 0.130, + gSync.x, 0.0, col, c); + col = meter(q, mx - mw, mx, my - 0.176, my - 0.130, + gSync.z, 1.0, col, c); + + // The greyscale wedge under it, which is the thing a monitor is actually + // lined up against. It wipes the other way so the card does not read as + // one curtain coming across. The ladder is linear here, so the same + // boundary trick costs one lerp on the index rather than two on colour. + // It bottoms out at GREY*0.5 and not at black for the same reason the + // black chip does: eight steps of which the first three are the ground is + // a five step wedge. + c = cue(a, 2.0, n); + d = sdRectAB(q, pg(mu, 0.545), pg(1.0 - mu, 0.660)); + float st = saturate((u - mu) / meas) * 8.0; + float saa = max(8.0 * PXU / (meas * PW), 1e-5); + float sC = round(st); + float si = clamp(sC - 1.0 + saturate((st - sC) / saa + 0.5), 0.0, 7.0); + col = over(col, lerp(GREY * 0.50, PAPER, si / 7.0), + fill(d) * wipeLeft(q, mu, 1.0 - mu, c)); + + // Two converging line bursts in the bottom corners - the resolution + // wedge every card has, and the one piece of the layout that is there to + // be looked AT rather than measured against. abs() on x buys both for the + // price of one, and mirroring is what a corner pair wants anyway. + // + // Radial lines at a constant angle converge on the apex, so the pitch + // falls as you go in; the burst is cut off at r = 0.075 because inside + // that the pitch is under four pixels and a resolution wedge that has + // itself aliased is a joke told at its own expense. + // Pinned to the same mx and my as the meters, and for the same reason: + // a burst whose apex is off the frame is a handful of stray diagonals. + c = cue(a, 3.0, n); + float2 W = float2(abs(q.x) - mx, q.y - (my - 0.061)); + float wr = length(W); + float wg = atan2(W.y, -W.x); + float wl = abs(frac(wg * 5.0 + 0.5) - 0.5) / 5.0 * wr; + float wm = fill(wl - hair(0.6 * PX)); + wm *= fill(max(abs(W.y) - 0.055, abs(W.x + 0.165) - 0.085)); + wm *= fill(0.075 - wr); + col = over(col, GREY * 1.2, wm * c); + + // The circle. Every test card has one, and this one is also a match cut: + // a bright ring dead centre is the same shape as the blown pupil in the + // cold open, so a hard cut either way lands on a shape and not a change. + // + // The radius is 0.270 and not 0.335 because at 0.335 the bottom of the + // ring landed at y = +0.335 and the top of the ident plate at +0.290, so + // the ring cut straight through the plate. Two elements crossing turns a + // composition into a tangle - the rule this shader applies to the poster's + // vertical rule, and it applies here too. + // + // It arrives on a clock wipe, which is the only correct way for a circle + // to arrive - antialiased, because a raw step() here is a hard radial + // edge sweeping the whole frame, and it is moving, which is the one thing + // that makes an aliased edge impossible to miss. The threshold is pushed + // a hair past 1 so the leading edge closes cleanly on the twelve o'clock + // seam instead of leaving a half lit pixel there for the rest of the shot. + c = cue(a, 4.0, n); + float rr = length(q); + float ang = frac(atan2(q.x, -q.y) * 0.15915494); // 0 at twelve o'clock + float caa = max(PXU / (6.2831853 * 0.270), 1e-5); // one pixel, in turns + float clk = saturate((c * (1.0 + 3.0 * caa) - ang) / caa + 0.5); + col = over(col, PAPER, outline(rr - 0.270, hair(1.3 * PX)) * clk); + + // And then the card becomes a clock, which is what the ring on a card is + // FOR: eight marks inside it and a hand that steps one mark per beat, so + // the two bars of near silence this shot occupies are not a still frame, + // they are a shot of something keeping time. The hand takes exactly one + // turn over the two bars and we cut away as it closes - the only piece of + // animation in the demo that is finished at the same instant its shot is. + // + // It ticks rather than sweeps, over a tenth of a beat, because a second + // hand that glides is a quartz movement and a card is not a wristwatch. + float tk = abs(frac(ang * 8.0 + 0.5) - 0.5) / 8.0 * 6.2831853; + col = over(col, GREY * 1.4, + fill(tk * rr - hair(0.9 * PX)) + * fill(abs(rr - 0.243) - 0.017) * c); + + float kt = floor(bt); + float tf = saturate((bt - kt) / 0.10); + tf = tf * tf * (3.0 - 2.0 * tf); + float th = (kt + tf) * 0.78539816; // an eighth turn a beat + float2 hd = float2(sin(th), -cos(th)); + float al = dot(q, hd); + float ax = dot(q, float2(hd.y, -hd.x)); + float hm = fill(max(abs(ax) - hair(1.2 * PX), + abs(al - 0.100) - 0.140)); + hm = max(hm, fill(rr - 0.020)); + col = over(col, PAPER, hm * c); + + // Crosshair, full measure both ways. + c = cue(a, 5.0, n); + float hx = hair(0.6 * PX); + float cw = fill(max(abs(q.y) - hx, abs(q.x) - (0.5 - mu) * PW)); + cw = max(cw, fill(max(abs(q.x) - hx, abs(q.y) - (0.5 - mv)))); + col = over(col, GREY * 1.2, cw * c); + + // Five small crosses on a line across the lower band. frac() gives all + // five from one test, the same way abs() gives four crop marks from one - + // but frac() also repeats forever, and at a fov wider than 0.62 the page + // is smaller than the frame, so the sixth and seventh crosses would march + // out into the margin. The whole row is clipped back to the measure. + c = cue(a, 6.0, n); + float2 X = float2((frac(u * 5.0) - 0.5) / 5.0 * PW, q.y - 0.200); + float hc = hair(0.7 * PX); + float xm = fill(max(abs(X.x) - 0.020, abs(X.y) - hc)); + xm = max(xm, fill(max(abs(X.x) - hc, abs(X.y) - 0.020))); + xm *= fill(abs(q.x) - (0.5 - mu) * PW); + col = over(col, GREY * 1.3, xm * c); + + // The ident plate at the foot: a white block with the accent struck + // through it, where a station logo would go. It sits below the ring with + // eleven pixels of air and above the trim with seven. The strike grows + // upward off the organ, which is the voice that owns the section this + // card is cut into - again a height and not a brightness. + c = cue(a, 7.0, n); + d = sdRectAB(q, pg(0.415, 0.800), pg(0.585, 0.925)); + col = over(col, PAPER, fill(d) * wipeDown(q, 0.800, 0.925, c)); + float sh = 0.040 + 0.028 * saturate(gVoice.z); + d = sdRectAB(q, pg(0.415, 0.925 - sh), pg(0.585, 0.925)); + col = over(col, acc, fill(d) * wipeDown(q, 0.800, 0.925, c)); + + // The hum bar. A band of mains frequency beat rolling slowly up the + // picture, which is what an analogue card transmitted down a cable with a + // ground loop on it actually does, and the one thing in this shader + // allowed to move continuously rather than on the beat - because it does + // not belong to the design, it belongs to the medium. + // + // It is a five per cent swell on a band a third of the frame tall taking + // six seconds to cross, so the fastest change any pixel sees is under one + // per cent of full scale per frame. It multiplies, but it multiplies a + // shape, not a clock: there is no term in here that an audio envelope can + // reach, and a hum bar that flashed would not be a hum bar. + float hy = frac((q.y + 0.5) + gTime.x * 0.17); + col *= 1.0 + 0.052 * smoothstep(0.0, 0.18, hy) + * smoothstep(0.36, 0.18, hy); + + return col; +} + +// --------------------------------------------------------------------------- +// 2 DIAGONAL POSTER +// --------------------------------------------------------------------------- +float3 layoutDiagonal(float2 q, float a, float3 col, float3 acc) +{ + float n = 8.0; + float v = q.y + 0.5; + float bt = gTime.x * BPS; + float c, d; + + // One field, one edge. It sweeps in along its own normal, so the diagonal + // IS the transition: nothing else has to move for the shot to open, and + // for the first third of a second the frame is a single travelling edge. + // + // And then it does not stop. The edge keeps breathing along its own + // normal on a twelve beat period - a tenth of the frame over the whole + // shot, far too slow to read as an object moving and far too large to + // read as a still. This is the one place a continuous motion is right + // rather than lazy: the poster is not a page with a diagonal on it, the + // poster IS the diagonal, so the poster's animation has to be that. The + // low band adds to it and cannot subtract from it, so the field never + // retreats on a quiet bar. + c = cue(a, 0.0, n); + float2 nrm = normalize(float2(0.58, 0.81)); + float brk = 0.055 * sin(bt * 0.5236) + 0.018 * saturate(gSync.x); + float ed = lerp(-1.7, 0.06, c) + brk * c; + float sd = dot(q, nrm) - ed; + float fld = fill(sd); + col = over(col, acc, fld); + + // An offset stroke inside the colour, thirty thousandths in and parallel + // to the edge. It is knocked out, not printed, so it is the same hole the + // disc is - and because it is pinned to sd it travels with the breath, + // which is what makes the breath legible at all. A field with no interior + // mark can move a long way before anyone notices. + c = cue(a, 1.0, n); + col = over(col, INK, + fill(abs(sd + 0.030) - hair(1.1 * PX)) * fill(sd) * c); + + // A caption stack knocked out of the colour, top left, three short rules + // set to a hanging indent. Three rectangles that say "there is writing + // here", in the one corner of the poster that had nothing in it. + c = cue(a, 6.0, n); + float cm = fill(sdRectAB(q, pg(0.075, 0.085), pg(0.230, 0.085 + 3.0*PX))); + cm = max(cm, fill(sdRectAB(q, pg(0.075, 0.117), + pg(0.196, 0.117 + 3.0 * PX)))); + cm = max(cm, fill(sdRectAB(q, pg(0.098, 0.149), + pg(0.215, 0.149 + 3.0 * PX)))); + col = over(col, INK, cm * fill(sd) * c); + + // The disc straddles the edge, and where it crosses onto the colour it + // knocks out to the ground instead of overprinting. That single trick is + // what makes a two colour poster look like it was paid for - and now that + // the edge breathes, the split across the disc walks across it for the + // whole shot, which is the composition changing rather than moving. + c = cue(a, 2.0, n); + col = over(col, lerp(PAPER, INK, fld), + fill(length(q - float2(-0.130, -0.020)) - 0.300 * c)); + + // Three small knockout squares stepping diagonally down the colour, + // clear of the disc and clear of the edge at both ends of its travel - + // checked at 4:3, 16:9 and 32:9, because the squares are placed in page + // fractions and the edge is placed in page units, so the two of them + // close on each other as the frame narrows and the narrow frame is the + // one that decides where they can go. + // They answer the disc: one big round hole and three small square ones. + c = cue(a, 7.0, n); + float qm = fill(sdBox2(q, pg(0.075, 0.660), float2(0.019, 0.019))); + qm = max(qm, fill(sdBox2(q, pg(0.120, 0.712), float2(0.019, 0.019)))); + qm = max(qm, fill(sdBox2(q, pg(0.165, 0.764), float2(0.019, 0.019)))); + col = over(col, INK, qm * fill(sd) * c); + + // The only vertical: a thick paper rule out in the dark wedge. Its + // position is a fraction of the half width so it stays proportionally + // right on a wide frame, but its thickness is fixed in pixels. + c = cue(a, 3.0, n); + float xr = 0.68 * PW * 0.5; + d = sdBox2(q, float2(xr, 0.0), float2(0.026, 0.435)); + col = over(col, PAPER, fill(d) * wipeDown(q, 0.055, 0.945, c)); + + // A ladder of rules stepping down into the empty corner, each shorter + // than the last and each a beat later. Derived from v with floor() rather + // than run as a loop - there is no loop anywhere in this shader and there + // does not need to be. The floor() boundary is put in the middle of a gap + // so the antialiasing of one rule can never be clipped by the next rule's + // cell. + // + // The ladder is boxed in on three sides and every one of them was a bug + // the first time. It starts at u = 0.620 because the disc reaches u = + // 0.596 and the first four rungs used to run straight across it. It ends + // no further than u = 0.800 because the vertical rule begins at 0.825, + // and it steps 0.026 and not 0.050 because at 0.050 the sixth rung's end + // landed exactly on its own start - and a zero width rect is not a short + // rule, it is a dot with a one pixel ramp on either side of it. + // + // Once it has landed, one rung a beat is struck in the accent and walks + // down the ladder in reading order. Six rungs and six beats to a bar and + // a half, so the walk is a bar and a half long and lands back at the top + // as the shot ends. It is the smallest element on the poster and it is + // the only thing keeping the beat, which is the correct division of + // labour: the big shape breathes, the small one counts. + float j = floor((v - 0.640) / 0.045); + if (j > -0.5 && j < 5.5) + { + float yy = 0.655 + j * 0.045; + float xe = 0.800 - j * 0.026 + ratchet(bt, 2.0, j, -0.014, 0.014); + c = cue(a, 4.0 + j * 0.15, n); + d = sdRectAB(q, pg(0.620, yy - 0.004), pg(xe, yy + 0.004)); + + float lit = (abs(fmod(floor(bt), 6.0) - j) < 0.5) ? 1.0 : 0.0; + float3 rc = lerp(PAPER * 0.72, acc, lit); + col = over(col, rc, fill(d) * wipeRight(q, 0.620, xe, c)); + } + + // One small paper square in the top right, off the field, so the dark + // wedge is composed rather than merely empty. The hat sets its size and + // only its size - it grows off a floor and never leaves. + c = cue(a, 5.0, n); + float sq = 0.024 * (1.0 + 0.30 * saturate(gVoice.w)); + d = sdBox2(q, pg(0.915, 0.130), float2(sq, sq)); + col = over(col, PAPER, fill(d) * c); + + return col; +} + +// --------------------------------------------------------------------------- +// 3 MODULAR GRID +// +// Eighteen modules, two bits of state each, packed into one constant so that +// the composition is something a person chose rather than something a hash +// produced: 0 empty, 1 outline, 2 accent, 3 paper. There are three of them +// now and the grid flips between them, because the answer to "the same page +// twice" is a second page and not a randomiser - a hash would give an endless +// supply of grids and every one of them would be the average of all the +// others. +// +// Read as three rows of six they come out +// +// A paper - outline outline - accent +// - accent accent - outline - +// outline - - paper - outline +// +// B accent - - outline paper - +// - paper outline - - accent +// outline - accent - outline - +// +// C paper outline - accent - paper +// - - outline - accent - +// - outline - - - accent +// +// A is the one the grid builds into: a white module top left, the pair of +// accent modules across the middle, a second white one on the opposite +// diagonal to answer it, and eight empties so the grid breathes. B moves the +// weight to the corners and C loads the top row and empties the bottom, so +// the three of them do not average out to a grey rectangle. +// --------------------------------------------------------------------------- +uint moduleState(uint idx, uint g) +{ + uint p0 = 0xc1128853u, p1 = 0x00000004u; + if (g == 1u) { p0 = 0x2181c342u; p1 = 0x00000001u; } + if (g == 2u) { p0 = 0x04210c87u; p1 = 0x00000008u; } + uint pat = (idx < 16u) ? p0 : p1; + return (pat >> ((idx & 15u) * 2u)) & 3u; +} + +float3 layoutModules(float2 q, float a, float3 col, float3 acc) +{ + float mu = 0.055 / PW, mv = 0.100; + float n = 20.0; + float u = q.x / PW + 0.5, v = q.y + 0.5; + float bt = gTime.x * BPS; + float c, d; + + // Header and footer, so the modules sit on a page instead of in a void. + c = cue(a, 0.0, n); + d = sdRectAB(q, pg(mu, mv - 0.038), pg(1.0 - mu, mv - 0.038 + 1.8 * PX)); + col = over(col, GREY * 1.5, fill(d) * wipeRight(q, mu, 1.0 - mu, c)); + + // The footer slug re-measures on the beat, so the one element that is not + // on the grid is also the one element that never sits still. + float fe = ratchet(bt, 1.0, 9.0, 0.170, 0.300); + c = cue(a, 19.0, n); + d = sdRectAB(q, pg(mu, 1.0 - mv + 0.030), + pg(fe, 1.0 - mv + 0.030 + 5.0 * PX)); + col = over(col, acc, fill(d) * wipeRight(q, mu, fe, c)); + + float cw = (1.0 - 2.0 * mu) / 6.0; + float ch = (1.0 - 2.0 * mv) / 3.0; + float ci = floor((u - mu) / cw); + float cj = floor((v - mv) / ch); + + // Register crosses at every module corner, under the modules, so the grid + // is visibly a grid even where three cells in a row are empty. Kept at + // GREY*0.55 - 0.22 luminance, one step clear of the dithered ground, + // which is the whole reason the twelve column furniture grid at 0.12 was + // never visible and this is. + // + // Distance to the NEAREST cell boundary, not the offset into the cell: + // frac() alone puts the whole cross on one side of its own corner, which + // is not a cross, it is a corner bracket pointing the same way eighteen + // times. And frac() tiles the plane, so the row is clipped back to the + // module area plus one arm - far enough out to draw the boundary crosses + // whole, nowhere near far enough to reach the next line of them. + float2 G = float2(abs(frac((u - mu) / cw + 0.5) - 0.5) * cw * PW, + abs(frac((v - mv) / ch + 0.5) - 0.5) * ch); + float ht = hair(0.6 * PX); + float xm = max(fill(max(G.x - 0.016, G.y - ht)), + fill(max(G.x - ht, G.y - 0.016))); + xm *= fill(abs(q.x) - ((0.5 - mu) * PW + 0.016)) + * fill(abs(q.y) - ((0.5 - mv) + 0.016)); + col = over(col, GREY * 0.55, xm * cue(a, 1.0, n)); + + if (ci > -0.5 && ci < 5.5 && cj > -0.5 && cj < 2.5) + { + uint idx = (uint)(cj * 6.0 + ci); + + // They land in reading order, which is the only order a grid can + // assemble in without looking shuffled. + c = cue(a, 2.0 + float(idx) * 0.85, n); + + // And they re-set in reading order too. Each module holds its state + // for two beats and then flips, and the flip time is staggered by + // idx so the change crosses the grid as a wave rather than arriving + // as one cut - the same reason the build is staggered, applied to the + // thing the build produced. Eighteen modules at 0.045 of a beat each + // makes the wave four fifths of a beat wide, so it is a gesture and + // not a sweep. + // + // The flip happens in the last eight per cent of the hold, so the + // page is stationary for the whole of the beat it lands on and the + // move belongs to the beat AFTER it. The module collapses to nothing + // and reopens on the new state, which is a split flap board and not a + // crossfade: a crossfade between two flat fields is a third flat + // field, and there is no such colour in this palette. + // + // k can be -1 for the last modules in the first fraction of a second, + // so the composition index is taken with a multiple of three added to + // it before the fmod - which shifts nothing and makes the cast safe. + float mbt = bt - float(idx) * 0.045; + float kk = floor(mbt / 2.0); + float ff = saturate(((mbt - kk * 2.0) / 2.0 - 0.92) / 0.08); + uint g = (uint)fmod(kk + ((ff < 0.5) ? 30.0 : 31.0), 3.0); + uint st = moduleState(idx, g); + float sq = abs(cos(ff * 3.14159265)); + + // The gutter is a fixed number of pixels rather than a fraction of a + // cell, so every module keeps the same rhythm when the frame widens. + float2 ca = pg(mu + ci * cw, mv + cj * ch) + float2(0.013, 0.013); + float2 cb = pg(mu + (ci + 1.0) * cw, mv + (cj + 1.0) * ch) + - float2(0.013, 0.013); + d = sdBox2(q, (ca + cb) * 0.5, + float2((cb.x - ca.x) * 0.5, + (cb.y - ca.y) * 0.5 * sq)); + + float wp = wipeDown(q, mv + cj * ch, mv + (cj + 1.0) * ch, c); + float m = 0.0; + float3 cc = INK; + if (st == 1u) { m = outline(d, hair(1.3 * PX)) * wp; cc = GREY; } + if (st == 2u) { m = fill(d) * wp; + cc = acc * (1.0 + 0.20 * saturate(gSync.y)); } + if (st == 3u) { m = fill(d) * wp; cc = PAPER; } + col = over(col, cc, m); + } + + return col; +} + +// --------------------------------------------------------------------------- +// The technical furniture: a grid under the layout, marks over it. +// +// Two different things live in here and they are pinned to two different +// rectangles. Crop marks belong to the PAGE - they mark its trim, and if a +// shot punches in past the trim then real crop marks leave the frame too. The +// ruler, the registration target and the slug line belong to the FRAME, the +// way a safe area overlay does, so they are placed off the visible half +// extent `he` and cannot be zoomed off the edge by a shot whose fov is +// anything other than 0.62. At fov 0.55 a hard 0.470 is outside the frame and +// all three of them vanish with no warning at all. +// +// NOTE FOR THE EDIT, not for this file: all four SC_FRAMES shots currently +// pass gTune.w = 0, so everything below this line is written, correct, and +// never once seen. It costs nothing at runtime - both functions return on the +// first line - but it is a whole register of variation sitting unspent, and +// spending it is a one character change in shots.h and not a change here. +// --------------------------------------------------------------------------- +float3 gridUnder(float2 f, float amt, float3 col) +{ + if (amt <= 0.001) return col; + + float u = f.x / PW + 0.5, v = f.y + 0.5; + + // Twelve columns and eight rows, one pixel wide. It is not there to be + // looked at; it is there so the eye believes the blocks were placed on + // something rather than dropped where they landed. Under the ordered + // dither it survives as a sparse dotted line, which is exactly right - + // but only from about 0.16 luminance up. Below that the ground's own + // dither tile is brighter than the line and it disappears completely. + float t = hair(0.55 * PX); + float dx = abs(frac(u * 12.0 + 0.5) - 0.5) / 12.0 * PW; + float dy = abs(frac(v * 8.0 + 0.5) - 0.5) / 8.0; + float g = max(fill(dx - t), fill(dy - t)); + + // frac() tiles the whole plane, so the grid has to be cut back to the + // page. Without this a wide fov shows ruled lines running out past the + // crop marks, which does not read as a grid - it reads as a bug. + g *= fill(abs(f.x) - 0.5 * PW) * fill(abs(f.y) - 0.5); + + return over(col, GRID, g * amt); +} + +float3 marksOver(float2 f, float2 he, float amt, float3 col, float3 acc) +{ + if (amt <= 0.001) return col; + + // The frame furniture sits just inside the visible edge, top and bottom. + float fy = he.y - 0.030; + + // The head margin furniture gets its own knocked out ground first. On the + // editorial and modular pages the margin is already the ground and this + // does nothing; on the diagonal poster the colour field is full bleed and + // runs straight through the margin, and without this the registration + // target and the first two colour chips sit on the accent, where a grey + // target is invisible and an accent chip on an accent field reads as a + // printing fault rather than as furniture. A control strip on a real + // proof has its own white ground for exactly this reason. + float slug = fill(sdBox2(f, float2(0.0, -fy), float2(0.036, 0.036))); + slug = max(slug, fill(sdBox2(f, float2(he.x - 0.375 + 0.150, -fy), + float2(0.156, 0.016)))); + col = over(col, INK, slug * amt); + + float m = 0.0; + + // Crop marks at the trim corners. abs() buys all four for the price of + // one, and they stand off the trim the way real ones do. + float2 A = abs(f); + float2 C = float2(0.5 * PW - 0.055, 0.445); + float ct = hair(0.0022); + m = max(m, fill(sdRectAB(A, float2(C.x + 0.012, C.y - ct), + float2(C.x + 0.042, C.y + ct)))); + m = max(m, fill(sdRectAB(A, float2(C.x - ct, C.y + 0.012), + float2(C.x + ct, C.y + 0.042)))); + + // The ruler along the foot: a tick every forty eighth of the frame, tall + // every sixth, because a scale with no accented divisions is unreadable. + float ur = f.x / (2.0 * he.x) + 0.5; + float t = ur * 48.0; + float dt = abs(frac(t) - 0.5) / 48.0 * (2.0 * he.x); + float tall = (fmod(floor(t), 6.0) < 0.5) ? 0.020 : 0.009; + m = max(m, fill(dt - hair(0.6 * PX)) * + fill(abs(f.y - (fy - tall * 0.5)) - tall * 0.5) * + fill(abs(f.x) - he.x)); + + // A registration target top centre - the one piece of furniture that is a + // mark rather than a measure. + float2 R = f - float2(0.0, -fy); + float rt = hair(0.6 * PX); + m = max(m, outline(length(R) - 0.016, hair(0.7 * PX))); + m = max(m, fill(max(abs(R.x) - 0.026, abs(R.y) - rt))); + m = max(m, fill(max(abs(R.x) - rt, abs(R.y) - 0.026))); + + col = over(col, GREY * 1.5, m * amt); + + // A five chip strip out at the right hand end of the head margin, on the + // same line as the registration target - a slug line. It has to live in + // the margin and not on the page: the first version put it inside the + // trim, where it sat on top of the editorial layout's colour plate and + // read as a mistake rather than as furniture. Each chip is its own + // antialiased box with a gap either side, so the colour changes happen in + // the gap and none of the four seams needs filtering. The dark chip is + // GREY*0.5 rather than INK*3 for the same reason as the card's black bar. + float sx = (f.x - (he.x - 0.375)) / 0.060; + float sf = floor(sx); + if (sf > -0.5 && sf < 4.5) + { + float cm = fill(max(abs(frac(sx) - 0.5) * 0.060 - 0.026, + abs(f.y + fy) - 0.011)); + float3 chip = PAPER; + if (sf > 0.5) chip = acc; + if (sf > 1.5) chip = acc * 0.45; + if (sf > 2.5) chip = GREY * 0.7; + if (sf > 3.5) chip = GREY * 0.5; + col = over(col, chip, cm * amt); + } + + return col; +} + +float4 main(VSOut i) : SV_Target +{ + // No ray is built - this scene is a page, not a space. It still answers + // to the shot table in the two ways a page can: roll turns the plate, and + // the shot's fov scales it, with the demo's usual 0.62 meaning one to one. + float kz = max(gCam.w, 0.08) / 0.62; + + PW = max(gTime.w, 0.5); + + // The one derivative in the shader, taken here, in uniform flow, before a + // single branch has been entered. uv.y runs 0..1 over the scene target, + // so ddy(uv.y) is exactly one over its height - 1/360 normally, 1/180 in + // the WARP fallback - and multiplying by the fov scale converts it into + // page units per output pixel. Everything downstream antialiases against + // that constant, which is why there is no gradient instruction inside any + // of the four layouts. + PXU = max(abs(ddy(i.uv.y)) * kz, 1e-6); + + float2 P = float2((i.uv.x - 0.5) * PW, i.uv.y - 0.5); + float cr = cos(gDir.w), sr = sin(gDir.w); + P = float2(P.x * cr - P.y * sr, P.x * sr + P.y * cr); + P *= kz; + + // The visible half extent, in page units, for the frame pinned furniture. + float2 he = float2(0.5 * PW, 0.5) * kz; + + float amt = saturate(gTune.w); + + // The build is driven by time, not by the knob, because gTune is constant + // for the whole shot. So the knob is a rate: 1.5 builds the page in two + // thirds of a second, 0 means it was already built when we cut to it. + float asmv = (gTune.y <= 0.0) ? 1.0 : saturate(gTime.x * gTune.y); + + // Once in a while the plate is one pixel out of register against the + // grid. It is a fault and not an animation, so it fires on an irregular + // schedule and is gone again inside an eighth of a second, and it turns + // off with the rest of the furniture. One real pixel, not one nominal + // one: half a pixel of misregistration is a soft edge, which is a + // different and much worse looking fault. + float h = hash11(floor(gTime.x * 8.0) * 1.37 + 11.0); + float2 J = (h > 0.94) ? float2(PXU, -PXU) * amt : float2(0.0, 0.0); + + float3 acc = accentOf(gTune.z); + float3 col = INK; + + col = gridUnder(P, amt, col); + + // A uniform branch on a constant buffer value: all four layouts are in + // the blob, only one of them runs, and none of them contains a gradient + // instruction or a loop. + int L = (int)(gTune.x + 0.5); + if (L <= 0) col = layoutEditorial(P + J, asmv, col, acc); + else if (L == 1) { + // SceneCB has no matte field. Reserve the test card's BAR(57) + // matte (0.170 in shots.h) throughout its closing animation. + // Fit the authored +/-0.445 trim with two scene lines of clearance. + // Uniform scaling keeps the clock round; PW preserves the full width. + float fit = min(1.0, max(kz * (0.5 - 0.170) - 2.0 * PXU, PXU) / 0.445); + float pageWidth = PW, pixelSize = PXU; + PW /= fit; + PXU /= fit; + col = layoutTestCard((P + J) / fit, asmv, col, acc, fit); + PW = pageWidth; + PXU = pixelSize; + } + else if (L == 2) col = layoutDiagonal (P + J, asmv, col, acc); + else col = layoutModules (P + J, asmv, col, acc); + + col = marksOver(P, he, amt, col, acc); + + // Deliberately no vignette and no falloff, and no dither, no scanlines + // and no grade: the post pass owns all four and doing any of them twice + // is what turns a clean page into mud. Every other scene has a light in + // it and therefore a gradient; the whole point of this one is that its + // fields are perfectly flat, which is also what gives the ordered dither + // in the post pass a clean uniform tile to lay down. + return float4(col * gTime.z, 1.0); +} diff --git a/fx/drive.hlsl b/fx/drive.hlsl new file mode 100644 index 0000000..81b4dad --- /dev/null +++ b/fx/drive.hlsl @@ -0,0 +1,1375 @@ +// THE DRIVE. +// +// Looking forward through the windscreen of a car, at night, in the rain. +// This is the only shot in the demo that is an ordinary place, and that is +// its whole job: after a cathedral that cannot exist and a white void with +// nothing in it, a wet motorway at 2am is what gives the rest of the demo +// somewhere to be. It is also the shot everyone already knows, so it has to +// be the one everyone knows - two rows of sodium lamps running away to a +// point, wet tarmac holding them, a car ahead, and water on the glass. +// +// What the first half second has to say: black frame, a road under you, and +// two lines of amber lights converging dead centre. Nothing else. The +// composition is a one point perspective with the vanishing point on the +// horizon and the horizon just above the middle of the frame, so the road +// owns the bottom half and the lamps own the middle band. Getting the +// horizon above centre is a SHOT job, not a shader job: aim `at` below `eye` +// (see the rows in shots.h). The light is all practical - sodium overhead, +// your own headlights on the tarmac in front of you, and one pair of tail +// lights twenty metres ahead. +// +// There is no raymarch here and no SDF. Every solid in the frame is ONE +// ray/plane intersect - the road against y = 0, the guard rail and the +// treeline against x = k, the sign gantry and the vehicle ahead against +// z = k - and every light is a closed form angular distance from the ray to +// a segment. That makes the perspective exact right down to the vanishing +// point, where a marched version would be a mess of step artefacts. Nothing +// in it scales with resolution the way a march does. +// +// ========================================================================== +// T H R E E D I F F E R E N T R O A D S +// -------------------------------------------------------------------------- +// The shot appears three times (shots.h: bar 12, beat 80.3, bar 83) and it +// used to be the same picture three times, because the only things gTune +// changed were how fast the ground scrolled and how far the streaks were +// pulled. The road itself - its width, its lane count, the spacing and the +// height of the lamp columns, what else was on it - was carved into static +// consts and could not move. +// +// It is all derived per shot now, and gTune.y is what derives it. The three +// rows hand this file rain = 0.20 / 0.45 / 0.70, which is the cleanest +// separator in the table, so rain doubles as HOW FAR OUT OF TOWN WE ARE: +// +// bar 12 3 bars, fast, drifting: a lit four lane motorway. Lamps every +// 26 m on short columns, woodland right up against the fence, +// oncoming headlights every forty metres, two vehicles ahead of +// you, gantry signs. It is crowded, and it is meant to be - it is +// the establishing shot and it is the busiest frame in the demo. +// +// beat 80.3 ONE SECOND, mid abstraction: three lanes, taller columns 35 m +// apart, the woodland thinning to scrub, heavier rain and two +// wipers going. Half the traffic, twice the streak. +// +// bar 83 4 bars, held, up from black, abstraction 0.9: two lanes on an +// open moor. A lamp every 43 m on ten metre columns, nothing at +// the roadside at all, one pair of oncoming lights a hundred +// metres out, and the tarmac essentially gone. What is left is +// long light, which is what the held note wants. +// +// So the three appearances differ in LAYOUT before they differ in anything +// else, and the post pass's per-section palette cannot flatten a layout. +// ========================================================================== +// +// THE VALUE LADDER. This shot is graded downstream by a ten step quantiser +// (post.hlsl: `steps = 10`), so it has ten values to work with and no more. +// Two things follow and they govern every constant below: +// +// - anything over 1.0 is white, and it is white by WHOLE pixels, so a +// coverage-antialiased edge whose fill is 5.0 antialiases to nothing. +// The first version of this shader lit the lane paint to 4.9, which meant +// the careful `stripe()` coverage below was thrown away on every line in +// the frame and the road markings crawled like a 1994 demo; +// - the whole picture has to LAND on those ten steps, in big deliberate +// jumps, because half a step of separation is not a value, it is noise. +// +// Measured off a rendered frame, not estimated. This is the ladder: +// +// sky, away from the lamps 0.02 (black, and it must be black) +// treeline silhouette 0.03 +// road at 3 m, off the beam 0.16 +// road at 30 m, between lamps 0.22 +// road under a lamp 0.30 +// sign panel, lit 0.35 .. 0.75 +// road in the headlight lobe 0.45 +// lane paint, mid distance 0.45 .. 0.62 +// lane paint at the bumper 1.1 (blown, and it should be) +// lamp halo 0.15 .. 0.9 +// lamp core 1.6 .. 2.4 (the only clipped thing) +// +// BLOCKING, AND IT IS NOT IN THIS FILE: post.hlsl multiplies the Bayer +// threshold, which spans +-0.469, by gGrade.x = 0.85 (gfx.c) and adds it to a +// luminance that is then quantised in steps of 1/10. That makes the dither +// FOUR quantiser steps wide. An ordered dither has to span exactly one step +// to preserve the mean; four steps is additive noise that saturate() then +// rectifies upward at the black end, so a scene value of 0.0 comes out of the +// tube as a 7-of-16 speckle reaching step 0.4, and the whole range 0.0 -> 0.35 +// is compressed into an output range of 3.5:1. Every scene in the demo is +// rendering to a flat grey field. The fix is one line in post.hlsl - +// `float thr = ((kBayer[bi] + 0.5) / 16.0 - 0.5) / steps;` - or equivalently +// gfx.c's `pc.grade[0] = 0.85f` becoming `0.11f`. Until that lands, nothing +// in this file can make the shot read, and once it lands this ladder is what +// it lands on. +// +// gTune.x speed world units per second the road scrolls past +// gTune.y rain 0 dry glass .. 1 downpour. ALSO the road itself: +// lane count, lamp pitch, column height, what is at +// the roadside, how much traffic there is +// gTune.z abstraction 0 a road .. 1 pure streaks of light +// gTune.w wet how much the tarmac mirrors, 0 dry .. 1 flooded + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect (w/h) + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look direction, w roll + float4 gTune; // per shot knobs +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// ---- the road, in metres ------------------------------------------------- +// These are the REFERENCE numbers, and they are real on purpose: a lane is +// 3.5 m, a lantern hangs about 7 m up, and the eye sits 1.35 m off the deck. +// Guessing at them is what makes most synthwave roads read as a toy, because +// the spacing of the lamps against the width of the tarmac is the only cue +// the viewer has for how fast they are going. +// +// Everything derived from them is now a local in main() and moves per shot; +// these stay as the mid point that the shot knob is measured against. +static const float ROAD_W = 5.2; // half width of the tarmac +static const float RAIL_X = 7.4; // guard rail, just off the hard shoulder +static const float LAMP_X = 7.9; // lamp columns, behind the rail +static const float LAMP_Y = 7.0; // and the height of the lantern +static const float LAMP_S = 24.0; // one lamp every 24 m, both sides + +// ---- noise --------------------------------------------------------------- + +float hash21(float2 p) +{ + float3 q = frac(float3(p.x, p.y, p.x) * 0.1031); + q += dot(q, q.yzx + 33.33); + return frac((q.x + q.y) * q.z); +} + +float2 hash22(float2 p) +{ + float3 q = frac(float3(p.x, p.y, p.x) * float3(0.1031, 0.1030, 0.0973)); + q += dot(q, q.yzx + 33.33); + return frac((q.xx + q.yz) * q.zy); +} + +float vnoise(float2 p) +{ + float2 i = floor(p), f = frac(p); + f = f * f * (3.0 - 2.0 * f); + return lerp(lerp(hash21(i), hash21(i + float2(1.0, 0.0)), f.x), + lerp(hash21(i + float2(0.0, 1.0)), hash21(i + float2(1.0, 1.0)), f.x), + f.y); +} + +// How much of a pixel a painted stripe covers: `d` is how far the surface +// point is from the centre of the stripe, `halfw` is half its width, and `w` +// is how much ground one pixel spans there. Both terms are needed. +// +// Using a plain smoothstep with a fwidth() width instead - the usual reflex - +// is what put a fan of hard wedges across the road in the first version of +// this shader. On a ground plane, one pixel at the bottom of the frame spans +// tens of metres, so the smoothstep's edges open up to tens of metres too and +// the lane line paints half the carriageway; and because a ground plane maps +// x onto a screen radial, the bands come out as wedges from the vanishing +// point. The second term is the fix: once the stripe is thinner than a pixel +// it FADES rather than widening, which is what a real line does. +// +// It is also used for the guard rail's vertical extent, which is the same +// problem stood on its end: a converging ribbon four pixels tall at 20 m and +// a quarter of a pixel tall at 300 m. +float stripe(float d, float halfw, float w) +{ + return saturate((halfw - d) / w + 0.5) * saturate(2.0 * halfw / w); +} + +// A periodic or noisy detail is only worth evaluating while a pixel is +// smaller than its period. Past that it must go to its MEAN, not carry on +// sampling, or it turns into slow crawling blobs that the dither then +// amplifies. `foot` is the pixel footprint and `per` the detail's period, +// both in the same units. +float bandlimit(float foot, float per) +{ + return saturate(foot / per * 1.7 - 0.35); +} + +// ---- lights -------------------------------------------------------------- + +// One light source, drawn as a glow around the closest approach of the ray +// to the SEGMENT A..B. That segment is the whole trick in this shader: +// +// with B == A it is a point, and this draws a lamp; +// stretch B away down the road and the same code draws the long exposure +// streak that lamp would leave on film. +// +// So the abstraction knob does not cross-fade between two different shaders. +// It opens up a segment, and the road turns into light because the lights +// stop being lights. Nothing else has to change for it to be believable. +// +// Everything in here is branchless on purpose. It is the body of a loop that +// runs up to thirty eight times and every compare in it is a compare the +// whole warp pays for, including the lanes it does not apply to. +float lightSeg(float3 ro, float3 rd, float3 A, float3 B, + float rad, float pixAng, float fogK) +{ + // Closest approach between the ray (unit rd) and the segment. Standard + // line/line solve with the segment parameter clamped to 0..1. + float3 v = B - A; + float3 w0 = ro - A; + float b = dot(rd, v), c = dot(v, v), d = dot(rd, w0), e = dot(v, w0); + + // `den` is zero when the ray runs PARALLEL to the segment - which, for a + // 34 m light trail lying down the road, is exactly what happens at the + // vanishing point, the one place the streaks matter most. The original + // guarded this with `(den > 1e-5) ? solve : 0.0`, and the result was that + // every streak pinched back to a point in the centre of the frame. The + // saturate already clamps the blow-up, so the guard only has to keep the + // divide finite - and doing it this way also drops a cmp and a movc out + // of the hot loop. + float tc = saturate((e - b * d) / max(c - b * b, 1e-6)); + + float3 cp = A + v * tc; + float s = max(dot(cp - ro, rd), 0.0); // ray parameter at that point + float sq = max(s, 0.30); // and a safe version to divide by + + // Work in METRES at distance sq rather than in angles: one reciprocal + // instead of three divides, and the clamp below reads as what it is. + float perp = length(cp - (ro + rd * s)); + + // Clamp the light's apparent radius to about a virtual pixel. A lamp two + // hundred metres out is smaller than one pixel of a 360 line frame, and + // if it is allowed to be it crawls and twinkles as the perspective moves + // it - the one artefact that reads as "cheap shader" rather than as + // "CRT". Giving it a floor and paying for it in brightness (k) is what a + // real lens does, and it is the single most important line in the file + // for how the far end of the lamp row behaves. + float rw = max(rad, pixAng * sq); + float inv = 1.0 / rw; + float k = rad * inv; // energy lost to the clamp + float x = perp * inv; + + // A small hot core inside a soft halo, weighted towards the halo on + // purpose: a tight bright point blows straight through the top of the + // quantiser and comes back very nearly white, and the halo is what stays + // amber and what survives the fog at three hundred metres. + // + // The halo's tail is QUARTIC, and that x^4 term is not a refinement - it + // is the difference between this shot working and not working. A plain + // 0.75/(1+0.85x^2) lobe still returns 5.5e-4 at forty radii out, which is + // nothing until you remember this function runs thirty times per pixel + // and every one of those lobes covers the whole frame. Summed, they put a + // floor of 0.07 luminance over the entire picture - and 0.07 is a third + // of a step on the post pass's ten step quantiser, so the sky stopped + // being black and became a field of half-lit dither. The frame was amber + // lamps on beige. With the quartic term the same tail is 1e-5, the sky + // goes back to 0.01, and the dither has something to quantise. + float x2 = x * x; + float glow = exp(-x2 * 1.35) * 0.85 + 0.55 / (1.0 + x2 * (0.60 + x2 * 0.02)); + + // saturate(s * 3.0) replaces an early `return 0` for lights behind the + // windscreen. As a hard cut it threw away a passing lamp's ENTIRE streak + // the instant its nearest point crossed the eye plane, which at 30 m/s is + // a lamp popping out of frame every eight tenths of a second. + return glow * k * exp(-s * fogK) * saturate(s * 3.0) / (1.0 + s * 0.0025); +} + +// EVERY light in the shot goes through ONE loop and one inlined copy of +// lightSeg: both rows of lamps, the tail lights of the vehicles ahead, and +// the headlights of the traffic coming the other way. +// +// This matters more than it looks. Written the obvious way, as a function per +// kind of light called once for the direct pass and again for the reflection, +// fxc inlines that closest-approach solve eight times. Folding the side and +// the kind of light into index arithmetic costs a handful of selects per +// iteration and gets most of that back. Measured with fxc 10.0.26100: +// rolled, this shader is a 21 KB blob; with [unroll] forced on these two +// loops it is 61 KB, in an executable that wants to be 85 KB in total. The +// [loop] attribute is not decoration, and adding two more KINDS of light to +// the existing loop is nearly free where adding a second loop would not be. +// +// The index runs lamps first (two per rung, one each side), then the tail +// light entries, then two headlights per oncoming vehicle. Every group has +// an even length so `k & 1` is the left/right bit throughout. +// +// `lamps` is how many lamps ahead to walk; they are re-anchored to the camera +// every lampS, so the row is endless without the loop ever growing. +// +// Returns three channels, because the frame has exactly three colours of +// light in it and keeping them apart costs one select: +// .x sodium amber .y tail light red .z oncoming headlight white +float3 lightPass(float3 ro, float3 rd, float zs, float camZ, + float4 lampP, // x pitch, y column x, z lantern height, w radius + float4 carP, // x world z of the lead car, y gap to the next, + // z trail scale, w brake light lift + float4 oncP, // x lane centre, y spacing, z anchor z, w trail + float trail, float pixAng, float fogK, + int lamps, int nCar, int nOnc) +{ + float3 sum = float3(0.0, 0.0, 0.0); + float lampS = lampP.x; + float base = floor((ro.z + zs) / lampS) * lampS - zs; + int nl = lamps * 2; + int n = nl + nCar + nOnc * 2; + + // The far end of the row. Because `base` re-anchors every lampS, the + // last lamp in the loop sawtooths between (lamps-1)*lampS and one span + // further out, which means WITHOUT this it appears from nothing at full + // strength - a lamp switching itself on at the vanishing point every + // eight tenths of a second. Fading the last span in costs three ops and + // is what lets the fog stay as thin as it is. + float zEnd = ro.z + float(lamps - 1) * lampS; + + // Where the oncoming stream is in its cycle. The nearest slot is the one + // level with you or just past, so this also says how much of the FARTHEST + // slot has arrived - same problem as zEnd, same three op fix. + float oPh = frac((camZ - oncP.z) / oncP.y); + + [loop] for (int k = 0; k < n; k++) { + int kc = k - nl; // >= 0: a tail light + int ko = kc - nCar; // >= 0: an oncoming headlight + bool car = (kc >= 0); + bool onc = (ko >= 0); + float side = ((k & 1) == 0) ? -1.0 : 1.0; + + // This rung of the lamp ladder, and its identity. `idx` is an integer + // in WORLD space, not a loop counter, so a given lamp keeps the same + // hash the whole way in: one lamp in twenty is dead, none of them is + // quite as bright as its neighbour, and no two streaks are the same + // length. That last one is what stops the abstract appearance from + // being twenty six identical strokes. + float zl = base + float(k >> 1) * lampS; + float idx = floor((zl + zs) / lampS + 0.5); + float2 h = hash22(float2(idx * 1.37, side * 5.70 + 0.31)); + float bulb = (0.72 + 0.56 * h.x) * step(0.052, h.y); + float jit = 0.55 + 0.90 * h.y; + + float ci = float(max(kc, 0) >> 1); // which vehicle ahead + float vi = float(max(ko, 0) >> 1); // which one coming at us + + // The streak trails behind where the light is now, which from inside + // the car means it reaches away from you, towards the vanishing + // point. That is the direction that makes the frame converge - and it + // is the same direction for the oncoming traffic, whose trail is the + // road it has already covered. + float3 A = onc ? float3(oncP.x + side * 0.86, 0.70, oncP.z + vi * oncP.y) + : (car ? float3(side * 0.98, 0.80, carP.x + ci * carP.y) + : float3(side * lampP.y, lampP.z, zl - trail * jit * 0.25)); + float r = onc ? 0.30 : (car ? 0.20 : lampP.w); + float tl = onc ? oncP.w : (car ? trail * carP.z : trail * jit); + float3 B = A + float3(0.0, 0.0, tl); + + float g = lightSeg(ro, rd, A, B, r, pixAng, fogK); + float ef = saturate((zEnd - zl) * (1.0 / lampS)); + + // A dipped beam is aimed away from you and drops off HARD as it comes + // level - which is both what a real headlight does and the reason a + // car every second and a half is not a flash. The photosensitivity + // budget is two qualifying transitions per second and this shot + // already spends most of it on the lamp row. + float dOn = oncP.z + vi * oncP.y - camZ; + float ow = saturate(float(nOnc - 1) - vi + oPh) + * (0.20 + 0.80 * saturate((dOn - 5.0) * 0.045)); + + sum += onc ? float3(0.0, 0.0, g * ow) + : (car ? float3(0.0, g * (1.0 + carP.w), 0.0) + : float3(g * ef * bulb, 0.0, 0.0)); + } + return sum; +} + +// ---- overhead signage ---------------------------------------------------- +// +// A sign gantry striding the carriageway every gsp metres. It is one +// ray/plane intersect against z = const, and every extent of every box is a +// stripe() - never a pair of half planes. That is not fussiness. A product +// of two opposing half planes does NOT go to zero when the box it bounds +// falls below a pixel: both terms tend to 0.5 and the box settles at a +// quarter coverage forever. A 0.72 m beam is sub-pixel from about 220 m out, +// so half planes would leave a permanent grey ladder hanging across the +// vanishing point - the same failure the guard rail was rewritten to avoid. +// stripe()'s second term is what retires each member on its own. +// +// It is the only thing in the frame with a HORIZONTAL edge in it, and that is +// why it is here. Everything else in the shot converges on the vanishing +// point; a beam crossing the top of the picture and sweeping up out of frame +// is the one event that says how fast the road is actually moving. +// +// Returns rgb in .xyz and coverage in .w, plus the marker lamps on the beam +// ends, which are additive and have to survive outside the coverage. +float4 gantryHit(float3 ro, float3 rd, float D, float pixAng, float gw, + float fogK, float abk, float seed, out float3 glow) +{ + float rz = max(rd.z, 0.05); + float tt = D / rz; + float x = ro.x + rd.x * tt; + float y = ro.y + rd.y * tt; + float w = max(tt * pixAng, 0.006); + + // Facing us, so the ray only reaches it going forward, and it is gone + // long before it can be behind us: at 6 m the beam is already past the + // top of a 31 degree frame. + float front = saturate(rd.z * 8.0 - 0.2) * saturate((D - 6.0) * 0.22); + + float beam = stripe(abs(y - 6.05), 0.36, w) * stripe(abs(x), gw, w); + // The legs start above the guard rail so the two never argue about which + // of them owns the bottom eight pixels of the verge. + float leg = stripe(abs(abs(x) - gw), 0.20, w) * stripe(abs(y - 3.65), 2.80, w); + + // The sign itself hangs on the left half of the beam, which keeps the + // vanishing point - the whole reason the shot exists - clear. + float pcx = -2.50, pcy = 4.45, phx = 2.20, phy = 1.15; + float py0 = pcy - phy; + float pan = stripe(abs(x - pcx), phx, w) * stripe(abs(y - pcy), phy, w); + float ins = stripe(abs(x - pcx), phx - 0.17, w) * stripe(abs(y - pcy), phy - 0.17, w); + + // Three lines of legend, each a different length. It is not text and it + // does not have to be: at the distance a gantry is legible from, a road + // sign IS three ragged white bars on green. + float row = floor((y - py0) * 3.4); + float rf = frac((y - py0) * 3.4); + float len = (phx * 2.0 - 0.90) * (0.34 + 0.62 * frac(seed * 5.17 + row * 0.413)); + float bx0 = pcx - phx + 0.45; + float bar = smoothstep(0.16, 0.30, rf) * smoothstep(0.64, 0.50, rf) + * stripe(abs(x - bx0 - len * 0.5), len * 0.5, w) + * ins * (1.0 - saturate(w * 3.2)); + + // Retroreflective sheeting: it is only bright because your own headlights + // are pointed at it, so it comes up out of the dark as it arrives. The + // gain is deliberately shallow. Sheeting really is that bright, but a + // panel that reaches 1.1 at thirty metres is a white slab arriving in the + // frame every four seconds, and the ten step quantiser has nowhere to put + // it: at 0.9 it lands on the top step legibly instead of clipping. + float head = exp(-D * 0.042) * 0.55 + 0.05; + + // NOT multiplied by the coverage - `cov` below already carries it, and + // doing both is what makes an antialiased edge come back a step dark. + float3 rgb = float3(0.26, 0.26, 0.29) * head * 1.7; + rgb = lerp(rgb, float3(0.075, 0.185, 0.115) * (0.30 + head * 2.1), pan); + rgb += float3(0.92, 0.94, 0.90) * (bar + (pan - ins) * 0.55) * (0.14 + head * 1.9); + + float cov = saturate(max(max(beam, leg), pan)) * front * (1.0 - abk * 0.88); + + float fg = 1.0 - exp(-tt * fogK * 1.4); + rgb = lerp(rgb, float3(0.016, 0.019, 0.032) * (1.0 - abk * 0.94), fg); + + // The two amber markers on the ends of the beam, and - once the shot has + // abstracted far enough that the steel is gone - the beam itself, which + // is by then just a bar of light lying across the top of the frame. + float2 m1 = float2(x - gw, y - 6.45); + float2 m2 = float2(x + gw, y - 6.45); + glow = float3(1.00, 0.66, 0.26) + * (exp(-dot(m1, m1) * 2.6) + exp(-dot(m2, m2) * 2.6)) * front + * exp(-tt * fogK * 0.9) + + float3(1.00, 0.72, 0.34) * beam * front * abk * 0.42; + + return float4(rgb, cov); +} + +// ---- water on the glass -------------------------------------------------- + +// Returns a refraction offset in camera space, and how much of the frame is +// covered by water. The drops are NOT painted over the picture: they bend +// the ray before anything is traced, which is what a lens of water on a +// windscreen actually does. It costs nothing extra and it means the lamps +// smear through every drop correctly, including the ones behind you. +float2 glassWater(float2 g, float t, float amt, out float film) +{ + float2 o = float2(0.0, 0.0); + film = 0.0; + if (amt < 0.01) return o; + + // Beads. One cell lookup, with the drop kept away from the cell edges so + // it never straddles a boundary and there is nothing to fix up. Five cells + // across the frame, not twenty: a drop has to be six or eight virtual + // pixels wide before the dither leaves anything of it, and anything finer + // comes back as speckle that reads as a dirty sensor. + float2 p = g * 5.2; + float2 c = floor(p); + float2 h = hash22(c + 3.71); + float live = step(1.0 - amt * 0.9, hash21(c * 1.37 + 9.1)); + float2 dc = c + float2(0.30, 0.30) + h * 0.40; + float2 dv = p - dc; + float rr = 0.13 + 0.15 * h.x; + float dd = length(dv) / rr; + float m = smoothstep(1.0, 0.70, dd) * live; + o += dv * (-0.045 / rr) * m; // a drop inverts what is behind it + film += m; + + // Runners: the drops that have joined up and are sliding down the glass, + // one per column, each with a tail above it. These are what sell rain at + // speed - a still field of beads reads as a dirty lens instead. + float colx = floor(g.x * 4.5); + float2 hh = hash22(float2(colx, 21.3)); + float live2 = step(1.0 - amt * 0.85, hh.x); + float gx0 = (colx + 0.5) / 4.5; + float dx = g.x - gx0; + float dy = g.y - (1.35 - frac(hh.y + t * (0.16 + hh.x * 0.22)) * 2.9); + float band = exp(-dx * dx * 620.0) * live2; + float head = band * exp(-dy * dy * 500.0); + float tail = band * exp(-max(dy, 0.0) * 4.5) * step(0.0, dy) * 0.55; + float run = head + tail; + o += float2(-dx * 20.0, 0.30) * run * 0.042; + film += run; + + return o * (0.45 + 0.55 * amt); +} + +// ---- one instrument ------------------------------------------------------ +// Returns .x the lit face, .y the bezel and its ticks, .z the needle. Two of +// these sit behind the wheel with only their top arcs above the dash line - +// which is all you ever see of your own instruments while you are driving, +// and it is what puts the camera inside the car rather than on a tripod +// strapped to the bumper. +float3 dial(float2 p, float r, float nd) +{ + // p.y grows UP the frame, so straight up is +y. Getting this sign wrong + // sweeps the needle through the bottom of the dial, which is the half + // that is behind the dash and never seen. + float d = length(p); + float a = atan2(p.x, p.y); // 0 straight up, positive clockwise + + float face = smoothstep(r, r * 0.87, d); + float ring = smoothstep(0.030, 0.012, abs(d - r * 0.94)); + + // Ten ticks to the turn, and only on the outer band of the face. + float tk = abs(frac(a * (5.0 / 3.14159265)) - 0.5) * 2.0; + float tick = smoothstep(0.74, 0.95, tk) + * smoothstep(r * 0.95, r * 0.83, d) * smoothstep(r * 0.68, r * 0.76, d); + + // The needle is the distance to a ray from the centre, clamped short of + // the bezel, so it has no tail sticking out the back of the pivot. + float2 nv = float2(sin(nd), cos(nd)); + float s = clamp(dot(p, nv), 0.0, r * 0.80); + float nl = length(p - nv * s); + float ndl = smoothstep(0.024, 0.007, nl) * smoothstep(r * 1.02, r * 0.92, d); + + return float3(face, max(ring, tick), ndl); +} + +// ---- the shot ------------------------------------------------------------ + +float4 main(VSOut i) : SV_Target +{ + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y is FLIPPED here on purpose. The fullscreen triangle emits + // uv.y = 0 at the top of the screen, so i.uv*2-1 gives -1 up + // there and the top of the frame ended up looking DOWNWARD - + // the cathedral vault springs at y=+12 and was rendering in the + // lower half, the monolith hung from the ceiling. + uv.x *= gTime.w; + + float3 fw = normalize(gDir.xyz); + float3 rt = normalize(cross(float3(0.0, 1.0, 0.0), fw)); + float3 up = cross(fw, rt); + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 ro = gCam.xyz; + + float3 gx = rt * cr + up * sr; // screen right, after roll + float3 gy = up * cr - rt * sr; // screen up, after roll + + // THE VERTICAL SENSE, AND IT WAS BACKWARDS. + // + // There used to be a `uv.y = -uv.y;` here, with a comment saying it was + // what kept the tarmac under the horizon and the bonnet at the bottom + // edge, "or the shot just reads as a mistake". It did the exact opposite: + // it was the line putting the road on the CEILING. + // + // Measured, not argued. Rendering + // col = (rd.y > 0.0) ? blue : red; + // and looking at the dumped frame put RED - the rays aimed DOWN, the ones + // that hit the tarmac - across the TOP half of the picture, and the sky + // across the bottom. The road, the lamp pools, the lane paint and the + // guard rail were all being drawn above the horizon, upside down, and the + // whole shot was a mirror of itself. + // + // It survived because this scene is nearly symmetrical about its own + // horizon - two rows of lights converging on a point in the middle of the + // frame look much the same either way up - and because the one element + // that would have given it away instantly, the bonnet, is nearly black + // and sat under a 2.39:1 matte in the only appearance that has one. + // Putting a lit instrument cluster on the dash is what finally made it + // obvious: the dials came out on the roof. + // + // fullscreen.hlsl emits uv.y = 0 at the TOP, and `uv` above is built as + // 1 - i.uv.y*2, so uv.y is ALREADY +1 at the top and -1 at the bottom - + // which is exactly what `gy` (world up, from cross(fw, rt)) wants, and + // exactly what every 2D constant in this file was authored against: the + // bonnet at uv.y < -0.66, the wiper pivot at -2.35 below the frame, the + // runners on the glass sliding from +1.35 down to -1.55. Nothing needed + // correcting. Deleting the flip makes the ray basis and the 2D layer + // agree, and both of them right. + float3 rd = normalize(fw + (uv.x * gx + uv.y * gy) * gCam.w); + + // The screen vertical the 2D layer works in: +1 at the TOP of the frame, + // -1 at the BOTTOM. With the flip gone that is just uv.y, and the alias + // is kept only so the wiper, the glass and the dash read as screen space + // rather than as something to do with the ray. + float sy = uv.y; + + float t = gTime.x; + float speed = gTune.x; + float rain = saturate(gTune.y); + float ab = saturate(gTune.z); + // The knob is eased for everything that DESTROYS the road - the tarmac + // going out, the paint turning neon, the rail going magenta - while the + // light trails grow on the raw value. Linear on both, the first quarter + // turn already had violet crash barriers, and the useful part of the + // range (a real road whose lamps have just started to smear) was gone. + float abk = ab * ab; + float wet = saturate(gTune.w); + float zs = speed * t; // metres travelled this shot + + // ---- WHICH ROAD THIS IS ---------------------------------------------- + // See the header. `var` is the shot's place on the town-to-moor axis and + // it is what makes the three appearances three different pictures rather + // than three grades of the same one. Every number below used to be a + // static const. + float var = saturate(rain * 1.45); // 0.29 / 0.65 / 1.00 + float roadW = lerp(7.40, 4.20, var); // 6.5 / 5.3 / 4.2 m half width + float railX = roadW + 2.00; + // The two lamp rows are the dominant SHAPE in the frame - a pair of + // diagonals running out of the vanishing point - and the angle of those + // diagonals is atan(lampY / lampX). Moving both, in opposite directions, + // is what makes the three appearances different pictures rather than the + // same picture at three exposures: 39 degrees on the motorway, 55 in the + // middle, 65 out on the moor where the columns are tall and tight in. + float lampX = roadW + lerp(2.20, 1.40, var); // 8.7 / 6.7 / 5.6 + float lampY = lerp(5.20, 12.00, var); // 7.2 / 9.6 / 12.0 + float lampS = lerp(19.0, 43.0, var); // 26 / 34.7 / 43 m pitch + float lanes = (var < 0.45) ? 4.0 : ((var < 0.85) ? 3.0 : 2.0); + float dashP = lerp(7.0, 13.0, var); // and the dash pitch with it + // Woodland right against the fence on the motorway, nothing at all on the + // moor. The empty appearance is the one whose picture is made of light. + float treeX = railX + lerp(3.4, 9.0, var); + // Real motorway planting is ten to fifteen metres, and it has to be: the + // trees stand 5 m BEHIND the lamp columns, so at a 1.25 m eye anything + // under about four metres never clears the horizon and the whole feature + // renders as a two pixel smudge. The first version of this was nominally + // 7.6 m and came out at 1.9 after its own modulation - 26 changed pixels + // in a 1920x1080 frame, measured. Height, and gentler modulation with it. + float treeH = lerp(12.0, 1.2, var); // 8.9 / 5.0 / 1.2 + float gsp = lerp(110.0, 260.0, var); // gantry spacing + + // How busy the road is. Straight off the speed, which is the other thing + // the three rows disagree about (32 / 28 / 18), and which is also the + // honest reason: nobody does thirty two on an empty moor. + float traffic = saturate((speed - 14.0) * (1.0 / 16.0)); // 1.0 / .875 / .25 + // Clamped, so the loop bound below is provably constant however the shot + // table is edited later: 26 lamp entries + 4 tail lights + 8 headlights. + int nOnc = 2 + (int)clamp((speed - 16.0) * 0.14, 0.0, 2.0); // 4 / 3 / 2 + int nCar = (traffic > 0.5) ? 4 : 2; // 2 / 2 / 1 ahead + float oncSp = lerp(150.0, 40.0, traffic); // 40 / 54 / 122 m apart + float oncSpd = speed + 26.0; // closing rate + float oncZ0 = ro.z - frac(oncSpd * t / oncSp) * oncSp; + + // gTime.y (beat) is hard-wired to 0.0f by drawScene() and gTime.z to 1.0f + // - the post pass owns the shot fade via gGrade.w. The multiply by + // gTime.z at the bottom is kept only because it is the documented + // contract and the offline renderer may yet drive it. + + // ---- the wipers ------------------------------------------------------ + // A blade sweeping out and back from a pivot below the frame, and behind + // it a band of glass that has just been scraped clear and is filling back + // in. The refill is an approximation - it is keyed to how far a pixel is + // from where the blade currently is rather than to a real per-pixel + // memory of when it was last crossed - but at 360 lines and three seconds + // a sweep, nobody can tell the difference, and a real one would need a + // history buffer this engine does not have. + // + // The PERIOD is the rain knob now, and so is the blade count: at 0.20 it + // is one blade on intermittent, four and a half seconds apart; at 0.70 it + // is the pair of them going flat out every one and a half. That alone + // makes the three appearances move at different rates, and a wiper is the + // one clock in the frame the viewer already knows how to read. + float wipeMul = 1.0, blade = 0.0; + if (rain > 0.05) { + float per = lerp(4.60, 1.55, rain); + float u = frac(t / per); + float sw = saturate(u / 0.45); + float ph = sin(sw * 3.14159265); + + float2 pv = float2(-0.10 * gTime.w, -2.35); + float a = atan2(uv.x - pv.x, sy - pv.y); + float wb = lerp(-0.82, 0.88, ph); + float da = abs(a - wb); + blade = smoothstep(0.055, 0.014, da) * smoothstep(0.47, 0.44, u) * rain; + wipeMul = lerp(saturate(0.10 + da * 1.6), 1.0, saturate((u - 0.45) / 0.45)); + + // The passenger blade, in tandem. It only comes on once the rain is + // heavy enough to warrant it, which is the second and third rows. + if (rain > 0.33) { + float2 pv2 = float2(1.05 * gTime.w, -2.28); + float a2 = atan2(uv.x - pv2.x, sy - pv2.y); + float wb2 = lerp(-0.94, 0.52, ph); + float da2 = abs(a2 - wb2); + blade = max(blade, smoothstep(0.052, 0.013, da2) + * smoothstep(0.47, 0.44, u) * rain); + wipeMul = min(wipeMul, + lerp(saturate(0.10 + da2 * 1.6), 1.0, + saturate((u - 0.45) / 0.45))); + } + } + + float wfilm; + // Screen space in, screen space out - and `gy` is the ray basis vector + // for screen up, which is the same sense sy has, so the offset applies + // with a plus. + float2 refr = glassWater(float2(uv.x, sy), t, rain * wipeMul, wfilm); + rd = normalize(rd + gx * refr.x + gy * refr.y); + + // One virtual pixel, in the same angular units lightSeg works in. The + // frame is always 360 lines (gfx.c: VH), whatever the window is. + float pixAng = 2.0 * gCam.w / 360.0; + // Thin fog. The first version of this used ten times as much, on the + // theory that a rainy night is murky, and it ate the lamp row four lamps + // in - which took away the one thing the shot is about. Real sodium + // carries for a kilometre in rain; the end of the row is now handled by + // the per-lamp fade in lightPass rather than by drowning it. + // Thicker out of town, where the rain is heavier. This sets how far down + // the road you can see, which is a large-area change: it decides whether + // the middle band of the frame is lifted or black. + float fogK = lerp(0.0042, 0.0105, var); + + // The vehicle ahead. Hoisted above BOTH light passes so the reflection + // and the direct image agree about where it is - they did not before, and + // a pair of tail lights whose reflection sits somewhere else is the kind + // of thing nobody can name and everybody notices. + // + // It sits closer on the busy motorway and a long way off on the moor, and + // it brakes: a slow, low duty pulse on the red, so that a held shot has + // one event in it that is not the road going past. + float carD = lerp(16.0, 46.0, var); + float carZ = ro.z + carD + sin(t * 0.31) * 6.0; + float brake = smoothstep(0.72, 0.97, sin(t * 0.83 + 1.7) * 0.5 + 0.5) * 0.9; + // The solo is one held note through the third appearance, so hanging the + // streak length on it means the picture breathes with the thing the edit + // cut this shot for. base + k*s, never k*s. + float trail = lerp(0.45, 34.0, abk) * (1.0 + 0.30 * gVoice.y); + // A motorway lantern really is about three quarters of a metre across, + // and using the real size is what finally made the row read: at a third + // of that every lamp past the fourth one was a single dithered pixel. + float lrad = lerp(0.75, 1.05, ab) * (1.0 + rain * 0.22); + + float4 lampP = float4(lampS, lampX, lampY, lrad); + float4 carP = float4(carZ, 30.0, 0.5, brake); + float4 oncP = float4(-roadW * 0.52, oncSp, oncZ0, trail * 1.9 + 6.0); + + // ---- sky ------------------------------------------------------------- + // Almost nothing: a cold near-black, plus two warm bands - a tight one on + // the skyline and a broad one up into the cloud. Sodium bouncing off low + // cloud is most of what a night sky looks like from a motorway, it is the + // only thing separating the horizon from the road before the lamps + // arrive, and without it the top half of the frame is a flat field that + // the dither turns into noise. + float hy = saturate(rd.y); + float3 col = lerp(float3(0.022, 0.026, 0.046), + float3(0.005, 0.007, 0.019), saturate(rd.y * 2.8)); + // How much town there is beyond the treeline, and it is the single + // largest-area difference between the three appearances: the sky is half + // the frame. A lit motorway on the edge of somewhere sits under a real + // sodium dome; the moor at bar 83 has nothing on the horizon at all, and + // that emptiness is most of why the last appearance reads as arrival. + float urb = lerp(1.90, 0.30, var); + col += float3(0.19, 0.085, 0.024) * exp(-hy * 9.0) * 1.30 * urb; // the skyline + col += float3(0.13, 0.062, 0.024) * exp(-hy * 2.2) * 0.34 * urb; // and the cloud + col *= 1.0 - abk * 0.94; + + // ---- the roadside ---------------------------------------------------- + // A treeline beyond the fence, as a third ray/plane intersect - the same + // solve as the guard rail, at a different x, and read as a SILHOUETTE + // rather than as a surface. It costs two noise lookups and it is what + // stops the upper half of the frame being an empty gradient: the lamps + // now stand against something, and the horizon is a real horizon because + // there is a mass on either side of it running to the same point. + // + // It also carries the biggest single difference between the three + // appearances. Woodland at bar 12, scrub at bar 80, bare moor at bar 83. + float sxr = (rd.x >= 0.0) ? 1.0 : -1.0; + float rdx = max(abs(rd.x), 2e-4) * sxr; + + [branch] if (treeH > 0.4 && abk < 0.92) { + float tT = min((sxr * treeX - ro.x) / rdx, 4000.0); + float yT = ro.y + rd.y * tT; + float zT = ro.z + rd.z * tT + zs; + float dTt = tT * tT * pixAng / treeX; + float wty = max(tT * pixAng + abs(rd.y) * dTt, 1e-4); + float wtz = max(abs(rd.z) * dTt, 1e-4); + + // Two octaves for the crowns and one very slow one for the gaps in + // the planting, all band-limited: a treeline is a horizon line, and a + // horizon line that aliases is a horizon line that boils. + float n1 = lerp(vnoise(float2(zT * 0.055, 3.70)), 0.5, bandlimit(wtz, 18.0)); + float n2 = lerp(vnoise(float2(zT * 0.230 + 17.0, 9.10)), 0.5, bandlimit(wtz, 4.4)); + // Modulation ABOUT the height, not a fraction of it, or the mean + // collapses and the treeline never clears the horizon. The clearings + // are deliberately rare - a gap in the planting is an event, and when + // one goes past you see the sky through it. + float gap = smoothstep(0.20, 0.34, vnoise(float2(zT * 0.011 + 41.0, 2.30))); + float hT = treeH * (0.65 + 0.45 * n1) * (0.85 + 0.30 * n2) + * (0.25 + 0.75 * gap); + + float tcov = saturate((hT - yT) / wty + 0.5) + * step(0.5, tT) * saturate((1400.0 - tT) * 0.004); + + // Near black, with a little of your own headlights on the nearest + // trunks. The fog takes it to the same value the sky already is, so + // the treeline dies out rather than ending. + // A silhouette and nothing else is worth two noise lookups and zero + // pixels: near-black trees on a near-black sky is not a shape, and + // for three appearances that was all this was. The lamp row stands in + // FRONT of the planting and lights it, so the verge carries the same + // rhythm the pools on the tarmac do - the same period, running to the + // same point - and the frame gets a second pair of converging lines + // for almost nothing. It is also the biggest thing separating the lit + // motorway from the moor, where `urb` takes it back out again. + float tpulse = 0.5 + 0.5 * cos(zT * (6.2831853 / lampS)); + tpulse = tpulse * tpulse; + tpulse = lerp(tpulse, 0.375, bandlimit(wtz, lampS)); + + float3 tc = float3(0.013, 0.015, 0.021) + + float3(0.34, 0.26, 0.16) * exp(-tT * 0.085) * 0.11 + + float3(0.34, 0.22, 0.11) * (0.06 + 0.60 * tpulse) + * exp(-tT * 0.030) * urb * 0.85; + float fgt = 1.0 - exp(-tT * fogK * 1.7); + tc = lerp(tc, float3(0.016, 0.019, 0.032) * (1.0 - abk * 0.94), fgt); + + col = lerp(col, tc, tcov * (1.0 - abk * 0.95)); + } + + // ---- the road -------------------------------------------------------- + // One intersect against y = 0. Exact perspective, no march, and the + // vanishing point is a real vanishing point rather than the place the + // step size gave up. + float tG = (rd.y < -1e-4) ? (-ro.y / rd.y) : 1e9; + float tg = clamp(tG, 0.0, 900.0); // bounded, so the footprints + float3 P = ro + rd * tg; // are finite even on sky pixels + float xw = P.x; + float zw = P.z + zs; + + // The horizon, as COVERAGE rather than as a bool. The old `bool hitG` + // put a hard binary edge between fully fogged road (0.02) and the warm + // skyline band (0.25) - a quarter of the ramp in one pixel, with nothing + // in between, on the one line in the frame the eye is guaranteed to be + // looking at. Any roll at all, or a drifting camera, and it stairsteps. + // One vertical pixel is pixAng of rd.y, so the coverage is closed form. + float gcov = saturate(-rd.y / pixAng + 0.5); + + // ---- screen footprints, ANALYTIC ------------------------------------- + // Not fwidth(). This shader draws the road inside a branch, and fxc sinks + // both the road coordinates and the deriv_* instructions that measure + // them into that branch - which was verified in the /Fc listing, not + // guessed. A derivative under divergent control flow reads whatever the + // inactive lanes of the quad last left in those registers, so along the + // horizon - the exact line this coverage exists to clean up - half the + // quads get a garbage footprint, and with it garbage lane-paint widths + // and garbage band-limiting. The original shader has the same defect. + // + // For a ground plane there is no reason to ask the hardware anyway. The + // whole chain is closed form: + // + // tg = -ro.y / rd.y so d(tg)/d(pixel) = tg^2/ro.y * pixAng + // xw = ro.x + rd.x * tg + // zw = ro.z + rd.z * tg + zs + // + // This is exact where it matters, bounded where tg is clamped, needs no + // quad at all, and costs six ops against ten deriv instructions. + float dTg = tg * tg * pixAng / max(ro.y, 0.25); // metres of road per pixel + float wx = max(tg * pixAng + abs(rd.x) * dTg, 0.0025); + float wzg = max(abs(rd.z) * dTg + tg * pixAng * 0.4, 0.0025); + // The speed term is a cheap motion blur along z: at 30 m/s a dash moves + // half a metre between frames, and without smearing the mark by that much + // the lane line strobes horribly against chunky pixels. It belongs to the + // PAINT, not to the surface texture - folding it into the one footprint + // used everywhere is what wiped the road aggregate out at three metres. + float wz = wzg + speed * 0.017; + + // gTune.w is 0.0 in all three rows, so in practice the rain is what wets + // the road - and it is worth a wide range, because a mirrored carriageway + // and a matt one are different pictures over the whole bottom half. + float wetAmt = saturate(wet + rain * 0.75); // 0.15 / 0.34 / 0.53 + + [branch] if (gcov > 0.002) { + // Aggregate, streaked along the direction of travel - which is both + // what worn asphalt actually looks like and four times cheaper to + // keep in band, because a ground plane at a grazing angle compresses + // metres of road into one virtual pixel along z while barely moving + // along x. Isotropic noise here aliases into slow crawling blobs from + // about fifteen metres out. + float grain = vnoise(float2(xw * 2.2, zw * 0.55)); + grain = lerp(grain, 0.5, + max(bandlimit(wx, 0.45), bandlimit(wzg, 1.8))); + + // Where the tyres run, polished a little darker than the rest. It is + // per LANE now rather than at two fixed offsets, so a four lane + // motorway gets eight worn strips and a country road gets four - and + // because they are keyed to the lane pitch they agree with the paint + // instead of drifting across it. + float lanePitch = 2.0 * roadW / lanes; + float lc = (frac((xw + roadW) / lanePitch) - 0.5) * lanePitch; + float wl = (abs(lc) - 0.85) * 1.5; + float wheel = 1.0 - 0.14 * exp(-wl * wl); + + // The tarmac edge is metres wide, so it can use the raw footprint - + // but not an unbounded one, or the far half of the road dissolves. + float wxr = min(wx, 1.2); + float onRoad = smoothstep(roadW + wxr, roadW - wxr, abs(xw)); + // Asphalt is about a tenth reflective and the temptation is to use + // the real figure. Do not: at a tenth, lit by lamps that are already + // fifty metres up the road, the tarmac never leaves the bottom step + // of the quantiser and the road has no width at all. These are + // asphalt values with a thumb on them. + float3 surf = lerp(float3(0.058, 0.062, 0.058) * (0.6 + 0.8 * grain), + lerp(float3(0.185, 0.190, 0.208), + float3(0.275, 0.282, 0.305), grain) * wheel, + onRoad); + + // Your own headlights: a wedge that starts a couple of metres off the + // bumper and widens. This is the brightest thing at the bottom of the + // frame and it is what anchors the camera inside a vehicle. + // + // The shape along z is a LOBE, not a decaying exponential, and that is + // the difference between this reading as a car and reading as a white + // slab. A ramp-in plus exp(-z k) peaks wherever the ramp finishes - + // six metres, here - so the brightest tarmac in the frame was the + // tarmac immediately under the bumper, and since a 1.35 m eye puts + // everything below the middle of the frame within eight metres, that + // was the entire bottom half of the picture at 0.7-0.94 luminance. + // Measured off a vertical profile, not guessed. + // + // Real dipped beams are aimed down and cut off: dark under the car, + // hot at fifteen to twenty metres, gone by fifty. z^2 e^-2z peaks at + // exactly zn = 1, so the peak is placed by one constant - 16 m - and + // the near field goes properly dark, which is also what lets the lamp + // pools be seen at all. + // Where that peak sits is per shot, and it is the biggest lever in + // the bottom half of the frame: a short punchy beam on the lit + // motorway, where you can see by the lamps anyway, against a long + // wide one on the unlit moor, where your own lights are all there is. + // The peak VALUE does not move - z^2 e^-2z is e^-2 at zn = 1 whatever + // the scale - so this slides the bright wedge up and down the frame + // without touching the value ladder. + float xl = P.x - ro.x; + float zl = max(P.z - ro.z, 0.0); + float bw = lerp(2.60, 3.80, var) + zl * 0.22; + float zn = zl / lerp(15.0, 21.0, var); + float beam = exp(-(xl * xl) / (bw * bw) * 0.9) * zn * zn * exp(-zn * 2.0); + float3 lit = float3(1.00, 0.92, 0.76) * beam * 11.0; + + // The pools under the lamps. The lamps are on a fixed grid, so the + // pool pattern on the tarmac is periodic in z and needs no loop at + // all - nearest lamp each side, proper inverse square with the + // cosine, and the next one along is far enough away to ignore. + float dz = (frac(zw / lampS + 0.5) - 0.5) * lampS; + // Past about a hundred metres one pixel spans a whole span, and this + // pattern - which does NOT fall off with camera distance, only with + // fog - becomes a field of crawling lozenges running to the vanishing + // point. A quarter of the pitch is where the mean of the falloff over + // one span sits, so this fades to the correct average rather than to + // an arbitrary constant. + dz = lerp(abs(dz), lampS * 0.242, saturate(wzg * (1.5 / lampS))); + float dxL = xw + lampX, dxR = xw - lampX; + float d2L = dxL * dxL + dz * dz + lampY * lampY; + float d2R = dxR * dxR + dz * dz + lampY * lampY; + float pool = lampY * lampY * (rsqrt(d2L) / d2L + rsqrt(d2R) / d2R) * 26.0; + // The low band under the sodium, so the pools have the same pulse the + // bass does. Floor stays put: base * (1 + k*s), never k*s. + lit += float3(1.00, 0.58, 0.20) * pool * (1.0 + 0.20 * gSync.x); + // Kept close to neutral, and deliberately LOW. This is the term that + // sets how dark the carriageway is between two lamps, and between two + // lamps is most of the road: at 0.20 it lifted the whole surface onto + // the same quantiser step as the pools and the road went flat and + // concrete-coloured. The pool rhythm running away to the vanishing + // point is the shot; the ambient only has to stop the gaps collapsing + // into the dither floor. + lit += float3(0.115, 0.114, 0.128); + + // Water darkens asphalt - it fills the pores and stops them + // scattering - and then gives all of it back as a mirror. Doing both + // is what separates a wet road from a road with lights painted on it: + // the diffuse has to drop for the reflections to have anything to be + // brighter than. + surf = surf * lit * (1.0 - abk * 0.97) * lerp(1.0, 0.74, wetAmt); + + // Paint. Lane lines on the shot's own lane pitch, dashed, plus a + // solid line at each edge of the tarmac. Four lanes on the motorway, + // two on the moor - and because the lines are generated from the + // pitch rather than written out, the count costs nothing. + // + // As the abstraction opens, the dashes join up into a continuous line + // and the paint stops being paint and starts emitting - the road + // markings are the last thing to survive, as long streaks. + // + // The albedo here is 0.46, not the 0.9 a real road marking has. That + // is not a mistake and it is not timidity: at 0.9 the paint sits at + // 1.4 under every lamp in the row, `stripe()` returns a coverage the + // quantiser then rounds to white anyway, and the antialiasing that + // the whole function exists to provide is thrown away. At 0.46 the + // line clips only where it should - in the headlight wedge, at the + // bumper - and stays a properly resolved line everywhere else. + float q = (xw + roadW) / lanePitch; + float dl = abs(q - floor(q + 0.5)) * lanePitch; + float inner = smoothstep(roadW - 0.30, roadW - 1.30, abs(xw)); + float ph = abs(frac(zw / dashP) - 0.5) * 2.0; + float dash = lerp(smoothstep(0.52 + wz / (dashP * 0.5), + 0.52 - wz / (dashP * 0.5), ph), 1.0, abk); + // Paint that has stopped being paint does not stay 170 mm wide. Once + // the line is emitting rather than reflecting it blooms, and letting + // it do so is what keeps the abstract appearance's two long streaks + // as the mass at the bottom of the frame - the thing that whole shot + // is made of - on a carriageway only four metres to the side. + // Twice normal paint at full abstraction and no more. At three times + // it stops being a line becoming light and becomes a magenta slab up + // the middle of the frame, wide enough at the bumper to cover the + // instruments - which is the one thing at the bottom of the picture + // worth seeing by then. + float paintW = lerp(0.17, 0.34, abk); + float cen = stripe(dl, paintW, wx) * dash * inner; + float edg = stripe(abs(abs(xw) - (roadW - 0.42)), paintW * 0.85, wx); + float3 paint = float3(0.46, 0.46, 0.42) * lit + 0.012; + surf = lerp(surf, lerp(paint, float3(1.00, 0.20, 0.62) * 1.5, abk), cen * onRoad); + surf = lerp(surf, lerp(paint, float3(0.24, 0.95, 1.00) * 1.3, abk), edg * onRoad); + + // Wet road. Schlick against a flat normal: a mirror at grazing + // incidence, plain asphalt straight down. It is computed BEFORE the + // reflection rather than after because it is also the cheapest + // possible reason not to trace one - across the bottom third of the + // frame, where the ray goes almost straight down, this term is worth + // five per cent and the second light loop is worth nothing. + float fres = 0.03 + 0.97 * pow(1.0 - saturate(-rd.y), 5.0); + + // The reflection is the same lamp row traced from the road point along + // the mirrored ray, with the normal wobbled by two octaves of ripple. + // Doing it this way rather than smearing the lamps downward is what + // gives the long vertical streaks their shape for free: at a grazing + // angle a tiny wobble in the normal throws the reflected ray a very + // long way, which is exactly why a wet road smears and a wet wall + // does not. + [branch] if (wetAmt * fres > 0.05) { + float3 rr = float3(rd.x, -rd.y, rd.z); + // Both octaves are band-limited. They are the highest frequency + // detail in the shot and they live in the part of the frame with + // the worst footprint, so unfiltered they do not read as ripple - + // they read as a boiling grey haze over the far carriageway, and + // they take the streaks down with them. + float f1 = 1.0 - max(bandlimit(wx, 0.55), bandlimit(wzg, 3.6)); + float f2 = 1.0 - max(bandlimit(wx, 0.20), bandlimit(wzg, 1.2)); + float rip1 = (vnoise(float2(xw * 1.8, zw * 0.28 - t * 1.6)) - 0.5) * f1; + float rip2 = (vnoise(float2(xw * 5.1 + 11.0, zw * 0.85 - t * 3.2)) - 0.5) * f2; + // The mid band ruffles the water. Same house rule, and the term + // it scales is a normal offset rather than a brightness, so the + // floor cannot move at all. + float ripK = (0.4 + wetAmt) * (1.0 + 0.40 * gSync.y); + rr.y += (rip1 * 0.060 + rip2 * 0.024) * ripK; + rr.x += rip2 * 0.045 * wetAmt; + rr = normalize(rr); + + // Five lamps is plenty in a reflection - the far ones are already + // below the ripple. Twice the pixel angle softens it, which is + // both cheaper and closer to what a rough mirror does. It takes + // the SAME trail, radius, lamp pitch, lead car and oncoming + // stream as the direct pass, so the reflection is of the scene + // rather than of a near miss of it. + float3 rl = lightPass(P, rr, zs, ro.z, lampP, carP, oncP, + trail * 0.88, pixAng * 2.2, fogK, 5, 2, 2); + // Asphalt is a ROUGH mirror, and the Schlick term above is a + // smooth one - at six degrees below the horizontal it returns + // 0.60, and at 0.60 x wetAmt x 1.70 the reflected lamp row was + // being added at nearly full strength across the whole middle of + // the carriageway. That washed the road into one flat pale slab + // and took the lamp pools, which are the rhythm of the shot, out + // with it. 1.35 keeps the vertical streaks - which are the reason + // to trace a reflection at all - and gives the road back its + // structure. + surf += float3(1.00, 0.60, 0.22) * rl.x * fres * wetAmt * 1.35; + surf += float3(1.00, 0.11, 0.06) * rl.y * fres * wetAmt * 0.95; + // And the traffic coming the other way, smeared the length of the + // carriageway. This is the single most recognisable thing about a + // wet road at night and the shot did not have it. + surf += float3(0.82, 0.88, 1.00) * rl.z * fres * wetAmt * 1.25; + } + + // Aerial haze, on the road's own distance. Everything solid dies into + // the same near-black the sky already is, so there is no visible end + // to the road - which is the only way an infinite road can be built + // out of thirteen lamps. + float fg = 1.0 - exp(-tg * fogK * 1.4); + surf = lerp(surf, float3(0.016, 0.019, 0.032) * (1.0 - abk * 0.94), fg); + + col = lerp(col, surf, gcov); + } + + // ---- guard rail ------------------------------------------------------ + // A second ray/plane intersect, against whichever of the two rails the + // ray is heading for. It is a thin horizontal ribbon converging on the + // vanishing point, and in a frame that is otherwise black and amber it + // is the line that tells you the road is going somewhere. + // + // It is also, at 360 lines, the single worst aliasing risk in the shot: a + // near-horizontal line that is four pixels tall at twenty metres and a + // quarter of a pixel tall at three hundred, carrying a periodic + // brightness pulse along its length. The original drew it with a hard + // `if (ry > 0.40 && ry < 1.02)` and a raw cos pulse, which is a recipe + // for a crawling dashed ladder. Everything below is coverage. + float tR = min((sxr * railX - ro.x) / rdx, 4000.0); + float ry = ro.y + rd.y * tR; + float rz = ro.z + rd.z * tR + zs; + + // Same closed form, stood on its end. d(tR)/d(pixel) = tR^2/railX * + // pixAng, so the rail's vertical footprint grows as tR^2 and passes the + // 0.62 m height of the ribbon at about three hundred metres - which is + // precisely where it should stop being drawn. The sub-pixel term inside + // stripe() then retires it on its own, smoothly, and no arbitrary + // distance cutoff is needed. + float dTr = tR * tR * pixAng / railX; + float wry = max(tR * pixAng + abs(rd.y) * dTr, 1e-4); + float wrz = max(abs(rd.z) * dTr, 1e-4); + float railCov = stripe(abs(ry - 0.71), 0.31, wry) + * step(0.25, tR) * step(tR, tG) + * saturate((900.0 - tR) * 0.01); + + [branch] if (railCov > 0.002) { + // Two ridges with a groove between them. That W section is the whole + // silhouette of a crash barrier, and it is the only detail here that + // still reads when the thing is four pixels tall. The smoothstep + // edges open with the footprint so the ridges soften into the body + // rather than strobing once they go sub-pixel. + float prof = saturate(smoothstep(0.055 + wry, 0.0, abs(ry - 0.56)) + + smoothstep(0.055 + wry, 0.0, abs(ry - 0.88))); + float body = 0.28 + 0.72 * prof; + + // The lamp pulse along the rail. Its period is the lamp pitch in rz, + // and rz's footprint passes that pitch well before the rail itself + // goes sub-pixel, so it has to fade to its own mean - + // E[(0.5 + 0.5 cos)^3] = 5/16 - or the rail arrives at the vanishing + // point as a bead curtain. + float pulse = 0.5 + 0.5 * cos(rz * (6.2831853 / lampS)); + pulse = pulse * pulse * pulse; + pulse = lerp(pulse, 0.3125, bandlimit(wrz, lampS)); + + // Reflector studs on the posts, one every four metres, which is a + // faster rhythm than the lamps and reads as speed on its own. + float stud = 0.5 + 0.5 * cos(rz * (6.2831853 / 4.0)); + stud = stud * stud; + stud = stud * stud; + stud = lerp(stud, 0.375, bandlimit(wrz, 4.0)) + * smoothstep(0.055 + wry, 0.0, abs(ry - 0.72)); + + float head = exp(-tR * 0.060) * 0.55; // your own headlights on it + float3 rc = float3(0.50, 0.44, 0.36) * body * (0.06 + pulse * 0.55 + head); + rc += float3(1.00, 0.80, 0.42) * stud * (0.10 + head * 0.85); + rc = lerp(rc, float3(1.00, 0.26, 0.72) * body * 1.1, abk); + + float fgr = 1.0 - exp(-tR * fogK * 1.4); + rc = lerp(rc, float3(0.016, 0.019, 0.032) * (1.0 - abk * 0.94), fgr); + + col = lerp(col, rc, railCov); + } + + // ---- the signs ------------------------------------------------------- + // The two nearest gantries. They are the only horizontal edges in the + // frame, so they are the only thing that reads as ARRIVING rather than as + // converging, and one of them sweeping up out of the top of the picture + // is the shot's biggest gesture. Spacing is per shot: every 148 m on the + // motorway, every 260 on the moor. + float gz = ro.z + zs; + float gph = frac(gz / gsp); + float gsd = floor(gz / gsp); + float gw = roadW + 3.6; + { + // Seeded by the gantry's own index, not by the slot it is in. Seed it + // by the slot and the far one rewrites its legend at the instant it + // becomes the near one. + float3 gl2; + float4 g2 = gantryHit(ro, rd, (2.0 - gph) * gsp, pixAng, gw, + fogK, abk, frac((gsd + 2.0) * 0.317), gl2); + col = lerp(col, g2.xyz, g2.w); + col += gl2 * (1.0 + 0.45 * gVoice.w); + + float3 gl1; + float4 g1 = gantryHit(ro, rd, (1.0 - gph) * gsp, pixAng, gw, + fogK, abk, frac((gsd + 1.0) * 0.317), gl1); + col = lerp(col, g1.xyz, g1.w); + col += gl1 * (1.0 + 0.45 * gVoice.w); + } + + // ---- the vehicle ahead ------------------------------------------------ + // Its tail lights were always there; its body never was, so the lights + // hung in empty air twenty metres out. One more plane intersect, two + // boxes - a body and a cabin - and there is something on the road with + // you. It also throws spray, which is the reason the rain reads as rain + // out in the world and not only on the glass. + float Dc = carZ - ro.z; + [branch] if (abk < 0.86) { + float rzc = max(rd.z, 0.05); + float tc = Dc / rzc; + float xc = ro.x + rd.x * tc; + float yc = ro.y + rd.y * tc; + float wc = max(tc * pixAng, 0.005); + + float lowb = stripe(abs(xc), 1.06, wc) * stripe(abs(yc - 0.73), 0.43, wc); + float cab = stripe(abs(xc), 0.86, wc) * stripe(abs(yc - 1.39), 0.23, wc); + float bcov = saturate(max(lowb, cab)) * saturate(rd.z * 8.0 - 0.2) + * saturate((Dc - 4.0) * 0.4) * saturate(1.0 - abk * 1.15); + + // Black bodywork with your own headlights on the back of it, and a + // red wash out of its own lamps. Nothing else: at this size that is + // all a car is. + float3 bc = float3(0.040, 0.038, 0.044) * (0.5 + 2.2 * exp(-Dc * 0.055)); + float2 dl1 = float2(abs(xc) - 0.98, yc - 0.80); + bc += float3(0.60, 0.07, 0.03) * exp(-dot(dl1, dl1) * 5.0) * (0.35 + brake); + float fgc = 1.0 - exp(-tc * fogK * 1.4); + bc = lerp(bc, float3(0.016, 0.019, 0.032) * (1.0 - abk * 0.94), fgc); + col = lerp(col, bc, bcov); + + // The spray, as an angular lobe around the vehicle rather than a + // volume - it is a metre and a half of haze twenty metres out and + // there is nothing a march would tell us that this does not. + float3 cv = normalize(float3(0.0, 0.85, Dc)); + float ca = 1.0 - dot(rd, cv); + float swb = 0.62 + 0.38 * vnoise(float2(uv.x * 5.0, uv.y * 5.0 - t * 2.6)); + col += float3(0.44, 0.42, 0.46) * exp(-ca * 190.0) * swb + * rain * (0.30 + 0.70 * traffic) * 0.60 * (1.0 - abk * 0.7); + } + + // ---- the lamps themselves -------------------------------------------- + // Added over everything, including over the rail, the gantry and the + // road. They are above the ground plane so nothing can occlude them + // anyway, and letting the glow bleed through the near rail is what bloom + // does. + float3 lgt = lightPass(ro, rd, zs, ro.z, lampP, carP, oncP, + trail, pixAng, fogK, 13, nCar, nOnc); + + col += float3(1.00, 0.62, 0.24) * lgt.x * lerp(1.15, 2.0, ab) + * (1.0 + 0.22 * gSync.x); + // The tail lights are kept deliberately below the top of the ramp. + // Pushed any brighter the quantiser lands them on the white step and the + // post pass hands back two pink dots, and the only other colour in the + // frame stops being red. + col += float3(1.00, 0.11, 0.06) * lgt.y * 0.85 * (1.0 - abk * 0.65); + // The traffic coming the other way. Cooler than the sodium on purpose - + // it is the only cold light in the frame and it is what makes the amber + // read as amber. + col += float3(0.80, 0.88, 1.00) * lgt.z * lerp(0.90, 1.60, ab) + * (1.0 + 0.25 * gSync.z); + + // ---- rain in the beam ------------------------------------------------ + // Falling rain seen from a moving car does not fall: it streams out of + // the vanishing point, and the faster you go the longer the strokes get. + // Radial noise in log polar is exactly that shape for two lookups, and it + // is the last thing that separates a downpour from a drizzle once the + // glass has been dealt with. + [branch] if (rain > 0.06) { + float2 qv = float2(uv.x, sy + 0.06); + float rr2 = length(qv) + 0.05; + float aa = atan2(qv.y, qv.x); + float lr = log(rr2) * lerp(2.4, 1.1, saturate(speed / 34.0)); + float n1 = vnoise(float2(aa * 26.0, lr * 6.0 - t * (2.0 + speed * 0.10))); + float n2 = vnoise(float2(aa * 45.0 + 13.0, lr * 9.0 - t * (3.0 + speed * 0.16))); + float st = smoothstep(0.66, 0.98, n1) * 0.9 + smoothstep(0.74, 1.00, n2) * 0.6; + // Only where your headlights actually reach the drops: the lower + // wedge of the frame, falling off away from the beam. + float litr = exp(-rr2 * 1.7) * smoothstep(0.45, -0.35, sy); + col += float3(0.60, 0.65, 0.78) * st * litr * rain * 0.20 + * (1.0 + 0.40 * gSync.z); + } + + // ---- back to the glass ----------------------------------------------- + // The drops have already bent the ray, so they are visible wherever they + // have something to bend. This adds the little bit of scattered light + // that makes them visible against the parts of the frame that are black. + col += float3(0.30, 0.33, 0.40) * saturate(wfilm) * 0.130 * rain + * (1.0 + 0.30 * gVoice.w); + col = lerp(col, col * 0.10 + float3(0.030, 0.028, 0.032), blade); + + // ---- the bonnet, and what is on the dash ----------------------------- + // A sliver of car along the bottom edge, catching a little of the + // headlight bounce and a little of the instruments. It is the difference + // between "a road" and "seen from inside a car". It goes away as the shot + // abstracts, because by then there is no car. + // + // The curvature term is small and it has to stay small: uv.x runs to + // +-1.78 on a 32:9 display, so a coefficient that looks right at 16:9 + // squares up and swallows the bottom corners all the way to the horizon. + // That is exactly what the first version of this line did. + float by = -0.66 + 0.045 * uv.x * uv.x; + float hsh = smoothstep(by + 0.010, by - 0.010, sy); + float hood = hsh * (1.0 - abk); + float3 hc = float3(0.055, 0.050, 0.058) * (0.20 + 0.80 * smoothstep(-1.0, by, sy)); + // The lip of the bonnet catching the headlight spill. It is pushed up + // from 0.155 because it is the line that separates the car from the road, + // and at 0.13 luminance it and the black body of the bonnet landed on the + // same quantiser step - which made the bottom of the frame one flat shape + // instead of two. + hc += float3(0.80, 0.55, 0.25) * 0.210 * smoothstep(by - 0.055, by, sy); + hc += float3(0.90, 0.30, 0.10) * 0.075 * smoothstep(-0.78, -1.0, sy); + col = lerp(col, hc, hood); + + // The instruments. Two dials with only their top arcs above the dash - + // which is all you ever see of your own - a rev counter that answers to + // the guitar, a speedometer that sits where gTune.x put it, and an + // indicator ticking away on its own clock. They are LIGHT, so unlike the + // bodywork they survive the abstraction: by the third appearance the + // dashboard is two glowing rings at the bottom of the frame, which is the + // right amount of car for a shot that no longer has a road in it. + // + // How much of them you get is also a matte question and therefore a + // per-appearance one: the first row is still in 2.39:1 and sees a sliver, + // the last two are in open frame and see the whole cluster. + [branch] if (hsh > 0.002) { + float3 inst = float3(0.0, 0.0, 0.0); + + // The pool of light the cluster throws onto the dash top. + float2 gp = float2(uv.x, sy + 0.95); + inst += float3(0.95, 0.55, 0.20) * exp(-gp.x * gp.x * 2.2 - gp.y * gp.y * 9.0) * 0.30; + + // Rev counter, left. Idle sits at a fifth of the sweep and the guitar + // takes it round - base + k*s, and the needle is four pixels wide, so + // nothing here can move a meaningful area of the frame. + float nd1 = lerp(-2.10, 1.50, saturate(0.22 + 0.60 * gVoice.x + + 0.05 * sin(t * 5.3))); + float3 d1 = dial(float2(uv.x + 0.42, sy + 0.93), 0.27, nd1); + inst += float3(1.00, 0.42, 0.16) * d1.x * 0.16 + + float3(1.00, 0.72, 0.36) * d1.y * 0.42 + + float3(1.00, 0.30, 0.16) * d1.z * 0.85; + + // Speedometer, right. It reads what gTune.x actually is, so the three + // appearances genuinely show three different speeds. + float nd2 = lerp(-2.10, 1.50, saturate(speed / 44.0 + 0.02 * sin(t * 0.9))); + float3 d2 = dial(float2(uv.x - 0.42, sy + 0.93), 0.27, nd2); + inst += float3(1.00, 0.42, 0.16) * d2.x * 0.16 + + float3(1.00, 0.72, 0.36) * d2.y * 0.42 + + float3(1.00, 0.30, 0.16) * d2.z * 0.85; + + // Telltales between them: an indicator ticking at its own tempo - + // the one thing in the frame that is not synchronised to anything - + // and a steady lamp beside it. + float2 w1 = float2(uv.x + 0.075, sy + 0.735); + float2 w2 = float2(uv.x - 0.075, sy + 0.735); + float tick = step(0.5, frac(t * 0.74)); + inst += float3(0.30, 1.00, 0.35) * exp(-dot(w1, w1) * 2600.0) * tick * 0.75; + inst += float3(1.00, 0.55, 0.10) * exp(-dot(w2, w2) * 2600.0) + * (0.35 + 0.55 * gVoice.z); + + col += inst * hsh * (1.0 - abk * 0.55); + } + + // And the cluster in the windscreen, which is the detail that says there + // is glass between you and the road at all. It is the dash pool mirrored + // about the dash line, an eighth as bright, and it costs three ops. + float mry = 2.0 * by - sy + 0.95; + col += float3(0.85, 0.55, 0.28) + * exp(-uv.x * uv.x * 2.0 - mry * mry * 7.0) * 0.055 + * smoothstep(by - 0.02, by + 0.30, sy) * (1.0 - abk * 0.6); + + // No dither, no scanlines, no grade, no vignette, no aberration and no + // tonemap. All of that belongs to post.hlsl and doing any of it here + // would be doing it twice. This shader's only job is to hand the post + // pass a linear frame with a value ladder worth quantising. + return float4(col * gTime.z, 1.0); +} diff --git a/fx/eye.hlsl b/fx/eye.hlsl new file mode 100644 index 0000000..8927345 --- /dev/null +++ b/fx/eye.hlsl @@ -0,0 +1,433 @@ +// Shot 01 - COLD OPEN: CAT EYE MACRO. And ten more after it. +// +// The demo opens on an eye that fills the frame, close enough that you read +// it as a landscape before you read it as an eye. No geometry: this is a +// two dimensional field, which is the point - not every shot has to be a +// space you fly through, and the cheapest shot in the demo is also the one +// that has to earn the first ten seconds. +// +// It is also the scene the edit leans on hardest: ELEVEN appearances, more +// than any other. That was fine as a plan and a disaster as a picture, +// because the first version of this file read gTune for a pupil width, a +// rotation and a glow, and drew the same photograph eleven times. Measured +// pairwise as luminance thumbnails the eleven shots sat 12.7 apart - the +// second most repetitive scene in the production. +// +// gTune.x pupil width (0 = a slit, 1 = fully round and blown) +// gTune.y lid closure (0 = open, 1 = shut) - arms the blink +// gTune.z iris rotation +// gTune.w glow strength +// +// Those four now carry a fifth thing nobody asked them for: an IDENTITY. +// The shot table hands eleven distinct (x, z, w) triples, so seven decorrelated +// hashes of them give every appearance a stable fingerprint, and the fingerprint +// decides the things a photographer decides - how tight the crop is, where the +// eye sits in frame, how far the lids are apart, what shape the eye is, how +// dense the veins are, and, most of all, WHAT THE CORNEA IS REFLECTING. The +// multipliers below are not decoration: they were solved so that across the +// eleven real triples every seed lands at least 0.056 from its nearest +// neighbour, against a theoretical best of 0.1, with a worst pairwise +// correlation of 0.02. That is what stops two appearances colliding. +// +// Nothing here is on a wall clock that outlives a shot. Every animation reads +// gTime.x, which restarts at each cut, so a four bar hold plays a saccade +// pattern, a dilation and a lid settle instead of being a still frame. + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look, w roll + float4 gTune; +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +float hash12(float2 p) +{ + float3 q = frac(float3(p.xyx) * 0.1031); + q += dot(q, q.yzx + 33.33); + return frac((q.x + q.y) * q.z); +} + +float hash11(float n) +{ + n = frac(n * 0.1031); + n *= n + 33.33; + n *= n + n; + return frac(n); +} + +float noise2(float2 p) +{ + float2 i = floor(p), f = frac(p); + f = f * f * (3.0 - 2.0 * f); + return lerp(lerp(hash12(i), hash12(i + float2(1, 0)), f.x), + lerp(hash12(i + float2(0, 1)), hash12(i + float2(1, 1)), f.x), f.y); +} + +// Value noise whose X axis is genuinely periodic, for anything wrapped around +// a circle. +// +// The version above is not, and the comment further down used to claim it was +// enough to use an integer number of cells around the turn. An integer count +// does make the INTERPOLATION parameter meet itself at the atan2 +-pi line - +// frac() lands on the same value from both sides - but hash12(i) and +// hash12(i + period) are unrelated numbers, so the VALUE still jumps. +// +// Measured before this existed, by reimplementing hash12 and noise2 exactly +// and evaluating across the seam: the iris fibre field jumped 0.18 to 0.20 of +// its 0..1 range across a single pixel boundary, against 0.004 to 0.008 for a +// typical neighbouring pixel. A 23x to 49x discontinuity - a hard radial tear +// running from the pupil to the limbus, in all ten appearances of the eye. +// +// The fix is to wrap the lattice INDEX too, so the cell after the last one is +// the first one again. period must be a whole number. +float noise2p(float2 p, float period) +{ + float2 i = floor(p), f = frac(p); + f = f * f * (3.0 - 2.0 * f); + + float x0 = i.x - period * floor(i.x / period); + x0 = (x0 >= period) ? 0.0 : x0; // guard the rounding case + float x1 = x0 + 1.0; + x1 = (x1 >= period) ? 0.0 : x1; + + return lerp(lerp(hash12(float2(x0, i.y)), + hash12(float2(x1, i.y)), f.x), + lerp(hash12(float2(x0, i.y + 1.0)), + hash12(float2(x1, i.y + 1.0)), f.x), f.y); +} + +#define TAU 6.28318531 +#define ITAU 0.15915494 + +float4 main(VSOut i) : SV_Target +{ + float2 p = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y flipped: the fullscreen triangle puts uv.y = 0 at the TOP, so + // without this the catch-light sat below centre and the lids + // closed the wrong way round. + p.x *= gTime.w; + float t = gTime.x; + + // ---- which of the eleven is this? ------------------------------------ + float k0 = frac(gTune.w * 10.00 + gTune.x * 14.20 + gTune.z * 2.60 + 0.143); + float k1 = frac(gTune.w * 9.50 + gTune.x * 7.10 + gTune.z * 2.80 + 0.083); + float k2 = frac(gTune.w * 12.50 + gTune.x * 13.10 + gTune.z * 6.10 + 0.587); + float k3 = frac(gTune.w * 13.60 + gTune.x * 9.40 + gTune.z * 12.20 + 0.959); + // three more, folded out of the first four rather than out of gTune, so + // they stay independent of the framing they decorate. + float k4 = frac(k0 * 3.6 + k2 * 3.2 + 0.415); + float k5 = frac(k1 * 5.6 + k3 * 1.4 + 0.258); + float k6 = frac(k0 * 3.5 + k3 * 2.6 + 0.346); + + // ---- the crop -------------------------------------------------------- + // This is the single biggest lever in the file. At mag 0.74 the whole + // eye sits in the frame with sclera and veins around it; at 1.52 we are + // inside the iris and the limbus only clips the corners. Eleven shots + // spread across that range stop being eleven of the same photograph. + float mag = 0.72 + 0.80 * k0; + float2 ctr = float2((k1 - 0.5) * 0.42, (k2 - 0.5) * 0.30); + + // A slow drift, so the eye is alive without ever looking like it is + // being animated at you. Rate and throw are per appearance now. + ctr -= float2(sin(t * (0.19 + k4 * 0.17)) * (0.018 + k5 * 0.030), + cos(t * (0.13 + k5 * 0.16)) * (0.014 + k4 * 0.026)); + + // ---- the gaze -------------------------------------------------------- + // A real eye never holds still: it darts and then sits. Step-and-hold, + // fast on the way out and long on the hold, with the targets hashed off + // this appearance's own seed so the pattern is reproducible per shot. + // This is what makes a CAM_HOLD shot not a freeze frame. + float sacR = 0.42 + k2 * 1.05; // darts per second + float sacA = 0.015 + k3 * 0.085; // and how far each one goes + float sp = t * sacR + k0 * 13.0; + float si = floor(sp); + float sf = smoothstep(0.0, 0.13, frac(sp)); + float sd = k1 * 57.0; + float2 gA = float2(hash11(si * 2.13 + sd), hash11(si * 2.13 + sd + 19.7)) - 0.5; + float2 gB = float2(hash11(si * 2.13 + 2.13 + sd), + hash11(si * 2.13 + 2.13 + sd + 19.7)) - 0.5; + float2 gaze = lerp(gA, gB, sf) * sacA; + + // Two frames, and the difference between them is the whole trick: the + // eyeball (sclera, veins, lids, the reflected world) does not move when + // the eye looks somewhere - only the iris and pupil do. + float2 eb = (p - ctr) / mag; // eyeball + float2 e = eb - gaze; // iris + float r = length(e); + float rb = length(eb); + float rs = length(p - ctr); // screen, for glow and vignette + + // ---- iris ------------------------------------------------------------ + float irisEdge = 0.86; + float spin = gTune.z + t * ((k3 - 0.45) * 0.16); + float a01 = (atan2(e.y, e.x) + spin) * ITAU; // turns, not radians + + // Radial fibres, two scales, so it holds up at this magnification. The + // counts are INTEGERS in turns AND the noise is wrapped to them, which is + // what actually closes the seam at the +-pi line - the integer count on + // its own does not, see noise2p. + float sect = floor(7.0 + k4 * 13.0); // 7..19 coarse spokes + float fineN = floor(23.0 + k5 * 26.0); // 23..48 fine ones + float strc = 3.6 + k6 * 5.4; // how far they smear radially + float fib = noise2p(float2(a01 * sect, r * strc), sect) * 0.62 + + noise2p(float2(a01 * fineN, r * strc * 2.3), fineN) * 0.38; + + // The collarette: the ruff where the iris changes character, and where + // the crypts live. Its radius moves per appearance, which redraws the + // whole interior structure rather than recolouring it. + float colR = 0.30 + k6 * 0.30; + float cb = (r - colR) * 5.5; + float band = exp(-cb * cb); + float cryN = floor(5.0 + k5 * 9.0); + float crypt = smoothstep(0.60, 0.88, + noise2p(float2(a01 * cryN, r * 3.0 + 11.0), cryN)) * band; + + float iris = smoothstep(irisEdge + 0.055, irisEdge - 0.02, r); + + // A cat pupil is a vertical slit that opens into an ellipse. + // The pupil breathes with the low band. 0.045 is a ceiling, not a + // starting point: it is 2.8% of the iris, and the band's 420 ms release + // against a 454 ms beat means it is always still falling when the next + // kick lands. It physically cannot pulse. + // On top of that it now DILATES OR CONTRACTS across the shot - direction + // from the seed - so the pupil is a movement and not a setting. + float dilDir = (k2 < 0.5) ? -1.0 : 1.0; + float dil = saturate(gTune.x + dilDir * (0.05 + 0.20 * gTune.x) + * smoothstep(0.15, 2.40, t)); + float pw = max(0.052 + dil * 0.62 + gSync.x * 0.045 + gVoice.y * 0.012, 0.020); + float ph = 0.62 + k4 * 0.22; // how tall the slit is + float lean = (k1 - 0.5) * 0.22; // and how far it leans + float2 q = float2((e.x - e.y * lean) / pw, e.y / ph); + // A perfect ellipse reads as vector art, so the rim borrows the iris + // texture it is cut out of. Free: fib is already in hand. + // Bounded by the iris mask: a tall slit on a wide seed would otherwise + // run past the limbus and cut a hole in the sclera, which is an eye + // with a slot milled through it. + float pupil = smoothstep(1.04, 0.94, length(q) + (fib - 0.5) * 0.05) * iris; + + float hueK = k2 * 0.55; // green cat to amber cat + float3 dark = lerp(float3(0.10, 0.30, 0.16), float3(0.27, 0.21, 0.07), hueK); + float3 lite = lerp(float3(0.55, 0.95, 0.40), float3(0.97, 0.82, 0.34), hueK); + // Contrast of the fibres rides the mid band. base + k*s: the floor is + // 1.20 and the music can only ever add to it. + float3 gold = lerp(dark, lite, saturate(fib * (1.20 + gSync.y * 0.18))); + gold = lerp(gold, lite * 1.15, pow(saturate(1.0 - r / irisEdge), 2.4) * 0.85); + gold *= 1.0 - 0.55 * crypt; // the lacunae, punched dark + + // Flecks. Four of them, at hashed positions in the iris, and only some + // appearances get any at all - the reward for looking twice. + float fleckAmt = saturate((k5 - 0.35) / 0.40); + float fleck = 0.0; + [unroll] for (int fi = 0; fi < 4; fi++) + { + float fh = hash11(float(fi) * 3.7 + k0 * 91.0); + float fr = 0.26 + hash11(float(fi) * 5.1 + k2 * 77.0) * 0.50; + float da = a01 - fh; + da -= floor(da + 0.5); // shortest way round + float2 fd = float2(da * TAU * max(r, 0.12), r - fr); + fleck += exp(-dot(fd, fd) * 900.0); + } + gold += lite * fleck * fleckAmt * 0.55; + + // The limbal ring: the dark edge that makes an iris read as an iris. + float ringW = 0.13 + k6 * 0.17; + gold *= 1.0 - (0.62 + 0.28 * k3) * smoothstep(irisEdge - ringW, irisEdge, r); + + float3 col = gold * iris; + col *= 1.0 - pupil; // punch the pupil out + + // ---- the sclera ------------------------------------------------------ + // Only exists in the wider framings, and when it does it brings the veins + // with it. A whole extra half of the picture that six of the eleven shots + // have and the other five do not. + float scl = smoothstep(irisEdge - 0.01, irisEdge + 0.07, rb); + float3 sclera = float3(0.20, 0.215, 0.205) * (1.0 - 0.62 * smoothstep(0.95, 1.80, rb)); + [branch] if (mag < 1.30) + { + float ab = atan2(eb.y, eb.x) * ITAU; + float vN = floor(13.0 + k5 * 17.0); + float vN2 = floor(vN * 2.4); // integer, so it can wrap + float vr = rb * 2.4 - t * 0.015; + float vv = noise2p(float2(ab * vN, vr), vN) * 0.68 + + noise2p(float2(ab * vN2, vr * 2.4 + 7.3), vN2) * 0.32; + vv = saturate(1.0 - abs(vv * 2.0 - 1.0)); + vv *= vv; vv *= vv; vv *= vv; // ridged, ^8, no pow call + float vmask = smoothstep(irisEdge + 0.02, irisEdge + 0.50, rb); + sclera += float3(0.26, 0.09, 0.09) * vv * vmask + * (0.18 + 0.70 * k5) * (1.0 + gSync.x * 0.25); + } + col += sclera * scl; + + // ---- what the cornea is reflecting ----------------------------------- + // A cat's eye is a mirror before it is anything else, and the thing in + // the mirror is the only element that can be a different OBJECT without + // the shot stopping being an eye. Four of them, chosen by k1. + float2 rc = eb + gaze * 0.30; // the world does not turn with the eye + rc *= (1.4 + k3 * 1.5) / max(1.0 - 0.55 * dot(rc, rc), 0.30); // convex: the rim eats the horizon + float ref = 0.0; + int variant = min(3, (int)(k1 * 4.0)); + if (variant == 0) + { + // a lit window, four panes, drifting + float2 wp = rc * float2(1.0, 1.30) - float2(sin(t * 0.09) * 0.20, 0.55); + float pane = smoothstep(1.00, 0.74, max(abs(wp.x), abs(wp.y))); + float mull = smoothstep(0.05, 0.11, abs(wp.x)) * smoothstep(0.05, 0.11, abs(wp.y)); + ref = pane * (0.30 + 0.70 * mull); + } + else if (variant == 1) + { + // a low sun over a banded horizon: the demo's own synthwave shot, + // seen from the wrong side of the glass + float2 sn = rc - float2(0.0, -0.30 + sin(t * 0.07) * 0.06); + float sun = smoothstep(0.46, 0.20, length(sn * float2(1.0, 1.15))); + float band = 0.5 + 0.5 * sin(sn.y * 26.0 - t * 0.7); + float hz = smoothstep(0.06, 0.0, abs(rc.y + 0.64)); + ref = sun * (0.45 + 0.55 * band) + hz * 0.55; + } + else if (variant == 2) + { + // a tube. scanlines and a roll bar, because of course it is a tube + float2 sn = rc * float2(0.92, 1.20); + float scr = smoothstep(1.05, 0.86, max(abs(sn.x), abs(sn.y))); + float lines = 0.55 + 0.45 * sin(sn.y * 52.0); + float roll = smoothstep(0.30, 0.0, abs(frac(sn.y * 0.4 - t * 0.13) - 0.5)); + ref = scr * (lines * 0.75 + roll * 0.40); + } + else + { + // doorframes receding: the corridor, six deep, walking outward. The + // ratio is exactly 2 so the sequence at frac()==1 is the sequence at + // frac()==0 shifted by one frame, and the loop never pops. + float s = 1.05 * exp2(frac(t * 0.11)); + [unroll] for (int ai = 0; ai < 6; ai++) + { + float dr = max(abs(rc.x) / s, abs(rc.y) / (s * 0.80)); + ref += smoothstep(0.075, 0.0, abs(dr - 1.0)); + s *= 0.5; + } + ref *= 0.55; + } + ref = saturate(ref); + + // The pupil is not black: it is a hole with the world in it. Over the + // iris the same reflection sits far fainter, because there is already + // something under it. + float corn = smoothstep(irisEdge + 0.16, irisEdge - 0.04, rb); + col += pupil * float3(0.020, 0.028, 0.034); + col += ref * corn * float3(0.72, 0.92, 0.85) + * (pupil * (0.16 + 0.14 * k3) + (0.030 + 0.035 * k6)); + + // ---- the wet specular ------------------------------------------------ + // The catchlight is a reflection of the KEY LIGHT, so it barely follows + // the gaze - which is exactly why it slides across the iris when the eye + // darts. Three shapes, and its position moves per appearance. + int cls = min(2, (int)(k2 * 3.0)); + float2 cp = float2(lerp(-0.40, 0.34, k1), lerp(0.14, 0.46, k3)) + + gaze * 0.12 + float2(gSync.z * 0.010, 0.0); + float2 cd = eb - cp; + float csz = 0.13 + k6 * 0.17; + float spec; + if (cls == 0) + { + spec = smoothstep(csz * 2.1, 0.0, length(cd)); // one soft disc + } + else if (cls == 1) + { + spec = smoothstep(csz * 1.5, 0.0, length(cd)) // and a companion + + smoothstep(csz * 0.85, 0.0, length(cd - float2(0.21, -0.14))) * 0.70; + } + else + { + float2 wd = cd / csz; // a window, four panes + float box = smoothstep(1.65, 1.15, max(abs(wd.x), abs(wd.y * 1.25))); + float mull = smoothstep(0.10, 0.24, abs(wd.x)) * smoothstep(0.10, 0.24, abs(wd.y)); + spec = box * (0.35 + 0.65 * mull); + } + col += spec * float3(0.85, 0.95, 0.90) * (0.42 + 0.20 * k0) * (1.0 + gSync.z * 0.22); + // the bounce off the wet lower lid + col += smoothstep(0.28, 0.0, length(eb - float2(cp.x * 0.4, -0.54))) + * float3(0.30, 0.42, 0.38) * 0.11; + + // ---- glow ------------------------------------------------------------ + // The eye is its own light source. This is the reason the shot works in + // a black frame. It falls off in SCREEN radius, not eyeball radius, or a + // tight crop would flood the frame with it. + float glow = exp(-rs * 1.9) * gTune.w * (1.0 + gVoice.z * 0.10); + col += float3(0.28, 0.75, 0.34) * glow; + col += float3(0.10, 0.30, 0.14) * exp(-rs * 0.7) * gTune.w * 0.35; + + // ---- the third eyelid ------------------------------------------------ + // A cat has a nictitating membrane and it sweeps in from the inner + // corner. Four of the eleven get one, once, early in the shot. It is a + // film, not a shutter: it lifts the blacks under it and leaves a bright + // leading edge, and it takes 0.9 s to cross, so nothing about it snaps. + [branch] if (k6 > 0.62) + { + float sweep = sin(saturate((t - 0.55) / 0.90) * 3.14159265); + float xe = -2.10 + sweep * 2.55; + float memb = smoothstep(xe, xe - 0.14, p.x); + float lead = smoothstep(0.10, 0.0, abs(p.x - xe + 0.07)) * sweep; + col = lerp(col, col * 0.62 + float3(0.09, 0.13, 0.10) * (0.30 + 0.70 * sweep), + memb * 0.45); + col += float3(0.18, 0.26, 0.21) * lead * 0.35; + } + + // ---- the lid --------------------------------------------------------- + // Two arcs closing on the eye. A blink is a shape, not a fade - and it + // is a movement, so the shader plays it from the shot's own clock. + // gTune.y only arms it; holding the lid shut for a whole shot would just + // be a shot of black, which is what the first version of this was. + // + // The APERTURE is now per appearance, and it is the second biggest lever + // after the crop: 0.68 is a half lidded stare and 1.37 is wide open, and + // an eye is a different shape at each end of that. The curve and the + // slant move with it, so some of the eleven are almonds and some are + // rounds. + float apert = (1.38 - 0.70 * k3) + sin(t * 0.7 + k0 * 6.0) * 0.015; + float cvx = 0.16 + k6 * 0.30; + float tilt = (k4 - 0.5) * 0.30; + + float lid = 0.0; + if (gTune.y > 0.5) { + float u = saturate(t / 0.18); // 180 ms, shut then open + lid = u < 0.38 ? (u / 0.38) : saturate(1.0 - (u - 0.38) / 0.62); + } + // and a settle, on every shot: the lids come the last few percent of the + // way open over the first second, so frame one is never where we stay. + lid = saturate(lid + (1.0 - smoothstep(0.0, 1.10, t)) * 0.10 * (0.40 + k5)); + + // The lid edge carries fur. Smoothed value noise along x, so the + // silhouette is a different ragged line in every appearance. + float2 d = p - ctr * 0.7; + float lx = d.x * (30.0 + k5 * 34.0); + float lf = frac(lx); lf = lf * lf * (3.0 - 2.0 * lf); + float ls = 5.0 + k0 * 9.0; + float fur = (lerp(hash12(float2(floor(lx), ls)), + hash12(float2(floor(lx) + 1.0, ls)), lf) - 0.5) * 0.034; + + float lo = -apert + lid * (apert + 0.30) + cvx * d.x * d.x + tilt * d.x + fur; + float hi = apert - lid * (apert + 0.30) - cvx * d.x * d.x + tilt * d.x - fur; + float open = smoothstep(lo, lo + 0.045, d.y) * smoothstep(hi, hi - 0.045, d.y); + // the shadow the lid casts on the globe under it + float inner = smoothstep(lo + 0.02, lo + 0.17, d.y) * smoothstep(hi - 0.02, hi - 0.17, d.y); + col *= open * (0.30 + 0.70 * inner); + + // Vignette down to nothing, so the eye floats in the dark. + col *= 1.0 - 0.55 * smoothstep(0.72, 1.75, rs); + + return float4(col * gTime.z, 1.0); +} diff --git a/fx/fullscreen.hlsl b/fx/fullscreen.hlsl new file mode 100644 index 0000000..8e52dc2 --- /dev/null +++ b/fx/fullscreen.hlsl @@ -0,0 +1,18 @@ +// One triangle that covers the screen, built from the vertex id alone. +// Nothing is bound: no vertex buffer, no input layout, no index buffer. +// Draw(3, 0) with topology TRIANGLELIST is the whole call. + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +VSOut main(uint id : SV_VertexID) +{ + VSOut o; + o.uv = float2((id << 1) & 2, id & 2); // (0,0) (2,0) (0,2) + o.pos = float4(o.uv.x * 2.0 - 1.0, + 1.0 - o.uv.y * 2.0, 0.0, 1.0); + return o; +} diff --git a/fx/horizon.hlsl b/fx/horizon.hlsl new file mode 100644 index 0000000..93e93a3 --- /dev/null +++ b/fx/horizon.hlsl @@ -0,0 +1,1254 @@ +// Shot 34 - SYNTHWAVE HORIZON. +// +// A plane, a sun and a ridge line is the most photographed image in the +// genre, so all of the work here is in refusing the postcard. Three things +// do that. +// +// The sun is not a friendly disc. It has a dark limb and a black halo +// around it, so it reads as a hole punched out of the sky rather than as a +// light, and the slats that cut it widen downwards until the bottom third +// is more gap than sun - it is going out, not rising. +// +// The ridge is not scenery. The near range is the darkest thing above the +// horizon, taller than the sun, and it bites into it. The two ranges behind +// it are hazed towards the sky colour, which is the only depth cue the +// image gets. +// +// And slabs stand on the grid and stream past. A grid on its own has no +// scale - one cell is one cell. A cell only becomes a unit of distance once +// something sixty units tall is standing in one. +// +// There is no march loop anywhere in this file. The ground is a single +// ray-plane intersection, the slabs are exact ray-box tests, the lamp posts +// are two ray-plane tests carrying a periodic picket, and the sun, the +// stars, the moon, the clouds and the mountains are all closed-form +// functions of the ray's azimuth and elevation. +// +// Two things in the world are nailed to +Z: the sun, and the direction the +// grid scrolls from. A shot that looks along +Z faces the sun and has the +// world coming at it. A shot that looks along -Z has its back to the sun, +// which is a real and useful image, but the grid then recedes and the slabs +// are behind the camera. +// +// The eye must stay above the plane. There is nothing under y = 0 and the +// shader does not pretend otherwise: it clamps the eye height rather than +// spending instructions on a view no shot is going to take. +// +// ---- WHY THIS FILE IS BUILT AROUND A SHOT FINGERPRINT -------------------- +// +// The horizon is on screen eight times. Measured as 32x18 luminance +// thumbnails, its eight shots sat 10.09 apart on average and two of them - +// fifty seconds apart in the edit - were 3.16 apart, which is the same +// picture twice. The reason was not the edit. The edit hands this shader +// four different numbers every time: +// +// bar 9 scroll 26 sunR 0.30 ridge 0.55 haze 0.25 hold +// bar 15 scroll 30 sunR 0.32 ridge 0.70 haze 0.30 track +// bar 26 scroll 0 sunR 0.34 ridge 0.80 haze 0.40 hold +// bar 47 scroll 38 sunR 0.34 ridge 0.65 haze 0.20 push 15 +// bar 69 scroll 34 sunR 0.34 ridge 0.75 haze 0.25 crane 7 +// bar 79.3 scroll 0 sunR 0.38 ridge 0.85 haze 0.15 pull +// bar 80 scroll 8 sunR 0.38 ridge 0.85 haze 0.15 crane 30 +// bar 87 scroll 0 sunR 0.30 ridge 0.90 haze 0.55 hold +// +// The old shader spent those four numbers on a scroll rate, a disc radius, +// one height multiplier and a fog density - four continuous dials on ONE +// fixed layout. Every appearance therefore had the same mountains in the +// same places, the same four towers at the same four x positions, the same +// grid pitch and the same empty sky. Turning a dial does not make a +// different shot; it makes the same shot slightly warmer. +// +// So the four numbers are now read twice. They still mean exactly what they +// meant - scroll, radius, ridge height, haze - and every one of those +// behaviours is unchanged. But the four together are also hashed into a +// SHOT FINGERPRINT, and the fingerprint decides layout: +// +// which mountains three ridge seeds and three base frequencies, so no +// two appearances share a silhouette, plus a terrace +// fold that turns jagged peaks into flat-topped mesas +// the grid cell pitch 2.0-6.2 units, heavy line every 4, 8 or +// 16, and on two of the eight shots the grid opens +// into a road with edge lines, centre dashes and two +// marching rows of lamp posts +// the skyline one to six slabs, procedurally placed, sized and +// phased, half of them with lit windows +// the sun WHERE ROUND IT IS - up to two thirds of the way to +// the edge of frame, either side - how high it sits in +// its own radius, how many slats cut it and how far +// the erasure has already climbed at the cut +// the sky a cloud deck, an aurora, a banded ringed moon, or +// the clean starfield - weighted independently, so +// combinations happen - over a horizon band that +// either reaches all the way round or dies within a +// disc's width of the sun +// +// Because the fingerprint is a hash of the tuple and no two rows of the +// table share a tuple, no two appearances share a configuration. And +// because nothing above is random per FRAME - only per shot - a held shot +// is still a stable picture. +// +// Measured the way the complaint was measured - one frame per shot, +// compared pairwise as 32x18 luminance thumbnails - the eight appearances +// went from a mean gap of 9.09 to 11.6, and, which matters more, the two +// closest of them went from 1.61 apart to 4.97: there is no longer a pair +// in this scene that is the same picture twice. The two the complaint +// named, fifty seconds apart, went from 3.17 to 9.04. +// +// The second half of the complaint was that a held shot is a still. Three +// of the eight have scroll 0, and two of those are on a locked-off camera, +// so the old frame had literally nothing moving in it. Everything that can +// honestly move without the grid moving now does: the clouds drift, the +// aurora waves and its striations run, the stars twinkle at their own +// rates, the sun's erasure climbs over the length of the shot, the +// reflection in the plane ripples, the lamp caps and the tower windows +// breathe with the guitar and the hat, and the grid's heavy lines lift with +// the bass. Same measurement: the bar 26 hold used to differ from itself by +// 0.00 over four seconds, which is what a still is. It is 3.38 now. +// +// All of the music terms are base + k*s, never base*s, so no floor ever +// drops out. Checked rather than asserted: rendering every shot twice, once +// with all eight envelopes pinned at 0 and once at 1, and stepping between +// them in a single frame - which is louder than anything the song can +// actually do - moves more than a tenth of full scale on at most 2.2% of +// the screen. The guideline counts a transition at 25%, so there is no +// qualifying transition anywhere in this scene no matter what the music +// does. +// +// ---- THE VALUE LADDER ---------------------------------------------------- +// +// Everything in here is graded against what post.hlsl actually does, which +// is not "eleven levels" in the abstract - it is: +// +// lum = dot(col, float3(0.299, 0.587, 0.114)) +// q = floor(saturate(lum + dither) * 10 + 0.5) / 10 +// col = lerp(rampColour(q), rampColour(q) * hue, 0.62) +// +// Three consequences drive every constant below. +// +// One: the quantiser sees LUMINANCE, and hue only comes back at 62% over a +// cool ramp. So two colours that differ in hue but not in luminance are the +// same colour by the time anyone sees them. All separation has to be built +// in value. +// +// Two: the band edges sit at lum = 0.05, 0.15, 0.25, ... so a value has to +// land inside a band, not on a boundary, or the ordered dither swings it +// between two steps across a flat area and it boils. +// +// Three: rampColour(0) is (0.030, 0.034, 0.048). Everything below lum 0.05 +// is that one navy floor. So "black" is a single step, and the sky, the +// ground, the near ridge and the slabs cannot all be black or they are all +// literally the same pixel. +// +// So the frame is built as an explicit ladder, at the sun, in clear air: +// +// step 8 sun crown lum ~ 0.84 +// step 6 sky immediately outside the limb ~ 0.63 +// step 5 heavy grid line, near ~ 0.47 +// step 4 sky at the horizon / sun's foot ~ 0.43 +// step 3 far range ~ 0.28 +// step 2 mid range, dark halo ring, cloud ~ 0.18 +// step 1 near range, aurora ~ 0.08 +// step 0 ground, slabs, lamp posts, sky ~ 0.02 +// +// The near range is one step off the floor rather than on it. That is a +// deliberate departure from "pure black": the slabs are on the floor, and +// if the ridge were too then a slab crossing the ridge would be invisible, +// which is the whole point of having put it there. Everything added to the +// frame here was placed on that ladder too - the clouds at band 2 near the +// sun and band 1 away from it, the aurora at band 1-2 so it never competes +// with the sun, the lamp posts on the floor with a band 4 cap. +// +// gTune.x grid scroll speed, world units per second. 0 freezes it. +// gTune.y sun angular radius, radians. +// gTune.z ridge height. 0 is a dead flat empty horizon, 1 is a range. +// gTune.w haze. 0 is a clear night, 1 is a storm. +// +// ...and the four of them together are the shot's identity, see above. + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect (w/h) + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look direction, w roll + float4 gTune; // per shot knobs +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +static const float TAU = 6.28318530718; + +// ---- small maths --------------------------------------------------------- + +float wrapPi(float a) { return a - TAU * floor(a / TAU + 0.5); } + +// Positive modulo. HLSL's fmod keeps the sign of the dividend, which is the +// wrong half of the answer when the ridge coordinate goes negative. +float pmod(float x, float m) { return x - m * floor(x / m); } + +// A reciprocal that keeps its sign. The ray-box test needs 1/rd, and a ray +// exactly parallel to a slab face has a zero component; replacing it with a +// positive epsilon regardless of sign, which is the obvious way to write +// this, flips one slab face inside out along that axis. +float safeRcp(float v) { return 1.0 / (v < 0.0 ? min(v, -1e-5) : max(v, 1e-5)); } + +// Multiply-and-fract hashes rather than frac(sin(x) * large). +// +// This is a correctness fix, not a taste one. The ridge's top octave is up +// to 376 cycles around the circle, so the sin hash was being asked for +// sin(248 * 91.3) = sin(22653). At that magnitude an fp32 argument has +// about two digits left below the point, the periodic reduction inside sin +// throws most of what remains, and the "random" values come out in short +// repeating runs - the ridge grows visible tiling and the stars form the +// diagonal streaks that always give this hash away. The star hash was worse +// still: its dot product reached 1.2e5. +// +// These have no such cliff over the ranges used here, and they cost about +// half what a sincos does. +float hash11(float n) +{ + float p = frac(n * 0.1031 + 0.1367); + p *= p + 33.33; + p *= p + p; + return frac(p); +} + +float hash21(float2 v) +{ + float3 p = frac(float3(v.xyx) * float3(0.1031, 0.1030, 0.0973) + 0.1174); + p += dot(p, p.yzx + 33.33); + return frac((p.x + p.y) * p.z); +} + +// Value noise that repeats exactly every `period` units. The ridges are +// indexed by azimuth, and atan2 has a seam directly behind the camera; if +// the noise did not close on itself there would be a visible vertical join +// in the mountains whenever a shot looks away from the sun. Making the +// noise periodic costs two floors and hides the seam completely, because +// the values either side of it are then identical - which also means the +// screen-space derivative across the seam is zero rather than infinite, so +// the silhouette anti-aliasing does not blow up there either. +// +// A constant added to x - which is how the per-shot seed and the wind drift +// both get in - shifts the lattice without breaking that, because the +// wrapping is done on the integer cell index and the period is unchanged. +float pnoise(float x, float period) +{ + float i = floor(x), f = frac(x); + f = f * f * (3.0 - 2.0 * f); + return lerp(hash11(pmod(i, period)), hash11(pmod(i + 1.0, period)), f); +} + +// The same idea in two dimensions, periodic in x only. The clouds need it: +// they are a field over (azimuth, elevation) and only the azimuth wraps. +float pnoise2(float2 p, float px) +{ + float2 i = floor(p), f = frac(p); + f = f * f * (3.0 - 2.0 * f); + float x0 = pmod(i.x, px), x1 = pmod(i.x + 1.0, px); + float a = hash21(float2(x0, i.y)); + float b = hash21(float2(x1, i.y)); + float c = hash21(float2(x0, i.y + 1.0)); + float d = hash21(float2(x1, i.y + 1.0)); + return lerp(lerp(a, b, f.x), lerp(c, d, f.x), f.y); +} + +// ---- the analytic grid filter ------------------------------------------- +// +// This is the part that has to be right. A grid line drawn with step() or +// smoothstep() on a fixed width crawls and moires horribly at 360 lines, +// because out towards the horizon one pixel covers dozens of cells and the +// shader is point sampling a square wave. The CRT pass point samples this +// target, so whatever aliasing is left here is the aliasing that ships, and +// then the ordered dither amplifies it. +// +// So instead of sampling the line pattern, integrate it. lineIntegral is +// the antiderivative of "1 within hw of an integer, 0 elsewhere"; the +// difference of two of them, divided by the filter width, is the exact box +// filtered average of the pattern over that width. Near the camera it +// returns a crisp line. Far away, where the filter is wider than a cell, it +// returns the pattern's mean - the grid dissolves into a flat dim wash +// instead of into noise. The filter width comes from fwidth() of the grid +// coordinate itself, so it is whatever the perspective actually is. +// +// It earns its keep four more times below: the sun's slats, the road's +// centre dashes and the two rows of lamp posts are all converging periodic +// patterns and would all strobe exactly as badly. + +float lineIntegral(float x, float hw) +{ + float u = x - floor(x + 0.5); // signed distance to the line + return (x - u) * 2.0 * hw + clamp(u, -hw, hw); +} + +float filteredLine(float x, float w, float hw) +{ + float h = 0.5 * max(w, 1e-5); + + // Recentre on the nearest line before integrating. At the horizon x is + // in the thousands while the difference we want is around 1e-4, and in + // fp32 those two evaluations would agree to every bit they have. The + // antiderivative shifts exactly by an integer, so this is free. + float b = floor(x + 0.5); + float y = x - b; + return (lineIntegral(y + h, hw) - lineIntegral(y - h, hw)) / (2.0 * h); +} + +// ---- the world ---------------------------------------------------------- + +// One mountain layer. Four octaves, folded so the noise's valleys become +// creases and its peaks become points - a plain fbm ridge is a rolling hill +// and reads as friendly. Frequencies double, and every one of them is an +// integer number of cycles around the circle so the periodic noise closes. +// +// `mesa` is the second fold, and it is the strongest single lever on the +// silhouette. At 0 the profile is n*n, which is the crease-and-point range +// this scene has always had. At 1 it is a smoothstep with a narrow ramp, +// which clips every crest flat: the range becomes a run of plateaus with +// vertical sides and horizontal tops - a mesa country rather than an alp. +// The 0.55 rescale is there because smoothstep averages about twice what +// n*n does, and without it turning mesa up would also double the height. +// +// It is applied at full strength only to the two octaves that carry the +// shape, and at a quarter to the two that carry the detail. Terracing all +// four clips the fine relief off the top of a plateau as well as the +// coarse, and what comes back is not a mesa, it is a rectangle - which is +// exactly what the first render of this looked like. +float ridgeLine(float xn, float seed, float f0, float mesa) +{ + float r = 0.0, a = 0.55, f = f0; + [loop] for (int i = 0; i < 4; i++) { + float n = pnoise(xn * f + seed, f); + n = 1.0 - abs(n * 2.0 - 1.0); + n = lerp(n * n, smoothstep(0.34, 0.52, n) * 0.55, + mesa * ((i < 2) ? 1.0 : 0.25)); + r += n * a; + f *= 2.0; + + // A slow amplitude rolloff on purpose. At the usual half-per-octave + // the first octave owns the shape and the crests come out as long + // flat plateaus with a horizontal top edge, which reads as a wall + // rather than as a mountain. + a *= 0.58; + } + return r; +} + +// The sky without the sun in it. Used twice: once for the sky itself, and +// once as the target colour of the distance fog, because aerial haze is the +// sky scattered into the line of sight and fogging towards anything else +// puts a grey rim around every silhouette. +// +// `warm` is how close this direction is to the sun. The horizon band is only +// lit where the sun is; everywhere else it goes to almost nothing. A horizon +// that glows evenly all the way round is the postcard, and it also throws +// away the one thing the black ridge needs, which is somewhere bright to be +// black against. +// +// The three values are picked against the quantiser, not by eye. `hor` is +// lum 0.43 at the sun, which is band 4 and leaves four whole bands of room +// underneath it for the three ranges and the floor - the old value of 0.43 +// red gave lum 0.22, which is band 2, and three ranges will not fit into +// two bands no matter how they are mixed. `mid` sits at lum 0.029 in clear +// air, safely inside band 0 rather than on the 0.05 boundary where it would +// have boiled across the whole upper sky, and lifts to 0.073 - band 1 - in +// haze, so haze visibly raises the sky by exactly one step. +// +// `floor_` is how much of the horizon band survives all the way round, and +// it is a per shot number now. At 0.07 the sky is lit only where the sun +// is, the far edges of frame go to night and the ridge there is black +// against black; at 0.30 the band carries right across and the mountains +// are cut out of it edge to edge. Between them that is most of the upper +// half of the picture, which is why it is worth a knob. +float3 skyGradient(float el, float haze, float warm, float floor_) +{ + float t = saturate(el * 1.30); + float3 hor = lerp(float3(0.780, 0.320, 0.095), float3(0.600, 0.300, 0.160), haze); + float3 mid = lerp(float3(0.030, 0.022, 0.062), float3(0.075, 0.065, 0.105), haze); + float3 zen = lerp(float3(0.008, 0.012, 0.030), float3(0.026, 0.026, 0.042), haze); + hor *= floor_ + (1.0 - floor_) * warm; + float3 c = lerp(hor, mid, smoothstep(0.0, 0.30, t)); + return lerp(c, zen, smoothstep(0.20, 1.00, t)); +} + +// ---- shading ------------------------------------------------------------- + +float4 main(VSOut i) : SV_Target +{ + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y is FLIPPED here on purpose. The fullscreen triangle emits + // uv.y = 0 at the top of the screen, so i.uv*2-1 gives -1 up + // there and the top of the frame ended up looking DOWNWARD - + // the cathedral vault springs at y=+12 and was rendering in the + // lower half, the monolith hung from the ceiling. + uv.x *= gTime.w; + + float3 fw = normalize(gDir.xyz); + + // cross(up, fw) is degenerate for a look direction straight up or down + // and normalize() of it is a NaN that then eats the whole frame. No + // shot does that on purpose, but a CAM_CRANE overshooting its `at` can, + // and one NaN pixel becomes a black square after the CRT pass. + float3 rt = cross(float3(0.0, 1.0, 0.0), fw); + float rl = length(rt); + rt = (rl > 1e-4) ? rt / rl : float3(1.0, 0.0, 0.0); + float3 up = cross(fw, rt); + + float cr = cos(gDir.w), sr = sin(gDir.w); + + // Clamp the eye once, here, rather than in each place that needs it. + // The grid used a clamped height while the slabs used the raw one, so + // an eye that dipped below 0.15 got a plane drawn from one place and + // towers standing in another. + float3 ro = gCam.xyz; + ro.y = max(ro.y, 0.15); + float camY = ro.y; + + float3 rd = normalize(fw + (uv.x * (rt * cr + up * sr) + + uv.y * (up * cr - rt * sr)) * gCam.w); + + // Seconds since this SHOT started, not since the demo did - gfx.c + // writes the shot-local clock here. Nothing in the file runs longer + // than about seven and a quarter seconds, which is what every animation + // rate below is scaled against. + float t = gTime.x; + + float haze = saturate(gTune.w); + float sunR0 = max(gTune.y, 0.02); + float ridge = max(gTune.z, 0.0); + float scrSpd = gTune.x; + + // ---- the shot fingerprint ------------------------------------------- + // + // Eight channels, each a hash of all four knobs but weighting them + // differently. Weighting them differently is the whole trick: two shots + // that happen to sit close in one channel - bar 9 and bar 15 differ by + // 4 in scroll and 0.02 in radius - are far apart in the channels that + // lean on the knobs they actually differ in, so they end up sharing at + // most one or two decisions out of a dozen instead of all of them. + // + // These are pure functions of a constant buffer, so they are the same + // for every pixel and for every frame of a shot: the layout is fixed + // the instant the shot cuts, and only the animation moves after that. + float vid = sunR0 * 91.7 + ridge * 53.3 + haze * 31.1 + scrSpd * 1.37; + float c0 = hash11(vid); + float c1 = hash11(ridge * 77.3 + haze * 61.9 + scrSpd * 0.53 + sunR0 * 13.1); + float c2 = hash11(haze * 88.1 + scrSpd * 2.11 + sunR0 * 37.7 + ridge * 9.3); + float c3 = hash11(scrSpd * 3.71 + sunR0 * 57.3 + ridge * 23.9 + haze * 11.7); + float c4 = hash11(sunR0 * 29.1 - ridge * 44.7 + haze * 72.3 + scrSpd * 0.91); + float c5 = hash11(scrSpd * 1.93 + ridge * 88.7 - haze * 17.3 + sunR0 * 41.1); + float c6 = hash11(haze * 43.9 + sunR0 * 66.1 + scrSpd * 0.71 - ridge * 19.7); + float c7 = hash11(ridge * 35.1 - scrSpd * 1.19 + haze * 97.3 + sunR0 * 21.7); + float c8 = hash11(sunR0 * 74.9 + haze * 52.7 - scrSpd * 1.63 + ridge * 15.3); + float c9 = hash11(sunR0 * 45.3 + ridge * 67.9 + haze * 13.7 - scrSpd * 2.53); + + // -- the floor. Pitch and bar spacing, and whether it is a field or a + // road. The pitch is squared off c1 so the distribution leans short: + // a fine grid is the default look and a coarse one is the exception. + float cell = 2.0 + 4.2 * c1 * c1; + float hvyN = (c5 < 0.42) ? 4.0 : ((c5 < 0.78) ? 8.0 : 16.0); + float roadOn = (c6 > 0.58) ? 1.0 : 0.0; + float roadHW = 5.0 + c3 * 4.0; + + // -- the mountains. Three seeds and three base frequencies, all of them + // integer counts of cycles around the whole circle so the periodic + // noise still closes at the atan2 seam. The near range's lowest + // octave runs 5 to 11, which is between one and three masses across + // a frame - the difference between two enormous shoulders and a + // serrated wall. + float mesa = saturate(c4 * 1.55 - 0.05); + float f0Nr = 5.0 + 2.0 * floor(c0 * 4.0); // 5, 7, 9, 11 + float f0Md = 11.0 + 2.0 * floor(c2 * 5.0); // 11 .. 19 + float f0Fr = 23.0 + 6.0 * floor(c7 * 5.0); // 23 .. 47 + float sdFr = c0 * 61.0 + c2 * 89.0; + float sdMd = c5 * 71.0 + c1 * 53.0; + float sdNr = c3 * 83.0 + c4 * 67.0; + + // Which of the three ranges dominates, and how far each one sits down + // on the horizon. Raising the subtracted floor drowns the valleys and + // leaves fewer, more separated peaks; dropping it gives a continuous + // wall. It is the difference between an island chain and a barrier. + float aFar = 0.100 * (0.65 + 0.80 * frac(c0 * 3.3)); + float aMid = 0.260 * (0.60 + 0.90 * frac(c2 * 5.1)); + float aNear = 0.620 * (0.45 + 1.25 * frac(c4 * 7.7)); + float kMid = 0.07 + 0.16 * frac(c1 * 4.7); + float kNear = 0.09 + 0.18 * frac(c3 * 6.3); + + // How far each range is hazed towards the sky. Written as a chain of + // products rather than three independent numbers so that the ordering + // - far palest, near darkest - is true by construction at every + // setting. Three free values could and did invert it, and a mid range + // paler than the one behind it is not a depth cue, it is a mistake. + float hFarK = 0.52 + 0.26 * frac(c7 * 9.1) + 0.10 * haze; + float hMidK = hFarK * (0.52 + 0.18 * frac(c5 * 11.3)); + float hNearK = hMidK * (0.28 + 0.24 * frac(c6 * 13.7)); + + // -- the sun. Where it sits in its own radius, how many slats cut it, + // how far down the visible face the first one starts, and how fast + // that start climbs while the shot is held. + float lift = 0.26 + 0.36 * c3; + float slatK = 5.0 + floor(c3 * 5.0); + float slatF = 0.14 + 0.26 * c5; + float eatRt = 0.006 + 0.016 * c6; + + // -- and, the single biggest change to the composition available here, + // WHERE ROUND it is. The sun used to be nailed to +Z, which is also + // the direction the world streams in from, and seven of the eight + // shots look straight down +Z - so seven of the eight had the same + // bright disc in the same place in the middle of frame, and no + // amount of new detail elsewhere was going to change that. Nothing + // outside this file depends on where it is: the grid still scrolls + // from +Z, the towers still stream in from +Z, and a sun half a + // radian off that is just a road heading north with the sunset in + // the north-west. + // + // The offset is a fraction of the SHOT'S OWN half field of view, + // less the disc's radius, so that "two thirds of the way to the + // edge" means the same thing at fov 0.55 and at fov 0.86 and the sun + // can never be pushed off frame by a wide lens. And it is damped by + // how far the camera is already looking off the axis: the bar 26 + // shot is aimed thirty-one degrees left of +Z and already has the + // sun hard against the right edge, so giving that one the full + // offset as well would put it out of shot altogether. + float camAz = atan2(fw.x, fw.z); + float hfov = atan(gCam.w * gTime.w); + float aimL = max(hfov - sunR0 * 0.55, hfov * 0.25); + float sunAz = (c8 - 0.5) * 1.70 * aimL * saturate(1.0 - abs(camAz) * 1.3); + + // -- how far round the horizon the sun's light reaches. This covers + // more of the frame than anything else in the file: at 0.85 the + // whole width of the sky carries the band and the ridge is a + // silhouette from edge to edge, at 2.4 the light falls off within a + // disc's width of the sun and the rest of the sky is night with + // stars in it. Same sun, same mountains, completely different + // photograph. It feeds the reflection in the plane and the rim on + // the near crest as well, so all three narrow together. + float warmK = 0.85 + 1.55 * frac(c9 * 7.1); + + // -- and how hot the floor is. Multiplies both line weights together so + // the fine-to-heavy ratio, which is what makes the grid read as a + // grid, is untouched. + float gGain = 0.68 + 0.67 * frac(c8 * 5.3); + + // -- how much of the horizon band survives away from the sun, and how + // thickly the stars are sown. Both cover a large area of frame for + // almost no instructions, which is exactly what a thumbnail-sized + // difference between two shots is made of. + float skyFloor = 0.07 + 0.24 * frac(c2 * 3.9); + float starThr = 0.865 + 0.10 * frac(c1 * 8.3); + + // -- how far out the floor lights up, and how far it reaches before it + // goes out. Both in units of the eye height, both per shot: a short + // reach leaves a dark plane with a lit patch under the camera, a + // long one carries the grid all the way to the mountains' feet, and + // that is most of the lower half of the frame either way. + float gNear = 1.4 + 1.9 * c9; + float gFar = 26.0 + 74.0 * frac(c9 * 3.7); + + // -- the sky. Weighted, not switched, so a shot can have a cloud deck + // AND a moon behind it. Haze pushes cloud up on its own, which is + // the one place a knob feeds a layout decision directly: an overcast + // is what haze looks like when you can see the sky it came from. + // Aurora is suppressed by both haze and cloud, because it is the + // clear-night option and stacking all three is soup. + float cloudA = saturate(smoothstep(0.38, 0.78, c7) + haze * 0.55); + float auroraA = smoothstep(0.30, 0.62, c0) * (1.0 - haze) * (1.0 - cloudA * 0.7); + float moonA = (c6 < 0.30) ? 1.0 : 0.0; + float winOn = (c1 > 0.60) ? 1.0 : 0.0; + + // -- the skyline. One tower on an empty plane is a different image from + // six, and both are better than always four. + float slabN = 1.0 + floor(c2 * 6.0); + float baseZ = 10.0 + c4 * 26.0; + + // The scroll is wrapped, in two different periods, before it is used. + // + // gTune.x is tens of units per second, so over a long shot the raw + // scroll reaches three figures and keeps climbing. floor(x + 0.5) on a + // large fp32 loses the sub-cell precision the analytic grid filter is + // built on, and frac() on the slab phase quantises to visible steps - + // the towers stop streaming and start ratcheting. + // + // The grid pattern repeats every hvyN cells, so wrapping at that is + // exact and nothing moves; the dashes are one per cell and the lamp + // posts one per two cells, and hvyN is always even, so both of those + // divide the same wrap. The slabs repeat every 300. + float scrollRaw = scrSpd * t; + float scroll = pmod(scrollRaw, cell * hvyN); + float slabPhase = pmod(scrollRaw, 300.0) * (1.0 / 300.0); + + // Everything above the horizon is measured as an azimuth and a tangent + // of elevation. Putting the sun, the stars and the mountains in one + // space means roll turns the world under them rather than sliding them + // across the screen, which is what roll is supposed to do. + float az = atan2(rd.x, rd.z); + float el = rd.y / max(length(rd.xz), 1e-4); + float xn = az / TAU; + + // The sun breathes on the low band. This is a GEOMETRIC response, not a + // brightness one: the disc swells by three and a half percent, its + // luminance never moves, and the quantiser therefore sees an edge shift + // rather than a level change. Multiplying brightness by an envelope is + // what makes a demo fail a photosensitivity check. + float sunR = sunR0 * (1.0 + 0.035 * gSync.x); + + // How high the sun sits inside its own radius is the single cheapest + // compositional change available: at 0.30 it is a slice cut off by the + // ground with the ridge biting the rest, at 0.82 it is a whole disc + // clear of the mountains with the plane empty under it. + float elSun = sunR * lift; + float3 sunDir = normalize(float3(sin(sunAz), elSun, cos(sunAz))); + + // Signed azimuth from the sun. Everything that has to know where the + // sun is reads this one number - the horizon glow, the disc, the light + // pillar, the reflection in the plane, the cloud lighting, the ridge + // rim and the moon's phase - so moving the sun moves all of them + // together and the picture stays lit by one thing. + float dAz = wrapPi(az - sunAz); + + // How close this direction is to the sun, along the ground. + float warm = exp(-abs(dAz) * warmK); + + // Kept, because the ground fog, the slab fog, the lamp posts and the + // cloud deck all want the sky at the horizon and it is a dozen + // instructions to recompute. + float3 hz = skyGradient(0.0, haze, warm, skyFloor); + + // ---- mountains ------------------------------------------------------ + // + // Three ranges. The far one is high frequency and low, which is what + // distance does to a ridge; the near one is coarse and tall enough to + // cross the sun. Heights are in tangent-of-elevation units, so the near + // range's crests reach about half the upper frame at the fov these use. + // + // The frequencies are counts of cycles around the WHOLE circle, and a + // shot only ever sees about a sixth of it, so they have to be much + // higher than they look. Subtracting a floor before scaling is what + // turns a lumpy field into a range: the valleys clamp flat onto the + // horizon and only the crests stand up, so the silhouette has feet + // instead of hovering. + float hFar = (ridgeLine(xn, sdFr, f0Fr, mesa * 0.45) * aFar + 0.002) * ridge; + float hMid = max(ridgeLine(xn, sdMd, f0Md, mesa) - kMid, 0.0) * aMid * ridge; + float hNear = max(ridgeLine(xn, sdNr, f0Nr, mesa) - kNear, 0.0) * aNear * ridge; + + // Silhouette coverage. saturate(0.5 + d/w) is a linear approximation to + // the box filter over the edge and is enough here, because these edges + // are near vertical and the ridge is one flat colour on both sides. + float dFar = hFar - el; + float dMid = hMid - el; + float dNear = hNear - el; + float covFar = saturate(0.5 + dFar / max(fwidth(dFar), 1e-5)); + float covMid = saturate(0.5 + dMid / max(fwidth(dMid), 1e-5)); + float covNear = saturate(0.5 + dNear / max(fwidth(dNear), 1e-5)); + + // ---- ground --------------------------------------------------------- + // + // One ray-plane intersection. The denominator is clamped rather than + // branched on so that the grid coordinate, and therefore fwidth() of + // it, stays defined for every pixel including the ones in the sky - + // a derivative taken inside a branch is a derivative of nonsense. + float tG = camY / max(-rd.y, 1e-4); + float3 pg = ro + rd * tG; + + // The half cell offset puts x = 0 in the middle of a cell rather than on + // a line. Without it a camera sitting on the world axis - which is where + // most shots put it - has a heavy grid line running straight out of the + // bottom of frame, and it reads as a searchlight rather than as floor. + float2 g = float2(pg.x, pg.z + scroll) / cell + 0.5; + float2 w = fwidth(g); + + // Capping the half width at a small multiple of the filter width is the + // other half of the analytic grid, and the one that is easy to leave + // out. A line has a fixed width in WORLD units, so three metres from the + // camera it is fifty pixels across and lies over the bottom of frame + // like a painted road marking. Clamped this way it holds roughly two + // pixels from here to the middle distance and then dissolves into the + // wash on its own, which is what a grid line is supposed to look like. + // + // Both half widths are now specified in WORLD units and converted, so + // that changing the cell pitch or the bar spacing changes how many + // lines there are without also changing how thick they are. + float hwFs = 0.042 / cell; + float2 hwF = min(float2(hwFs, hwFs), w * 1.10); + float fine = max(filteredLine(g.x, w.x, hwF.x), + filteredLine(g.y, w.y, hwF.y)); + + // Every hvyN'th line is heavy. Without it the grid is a texture; with + // it the grid has bars, and the eye can count them going away. Four + // gives a tight ladder, sixteen gives widely spaced beams with a fine + // mesh between them, and they do not read as the same floor at all. + float rHvy = 1.0 / hvyN; + float2 G = g * rHvy; + float2 W = w * rHvy; + float hwHs = 0.145 / (cell * hvyN); + float2 hwH = min(float2(hwHs, hwHs), W * 1.70); + float heavy = max(filteredLine(G.x, W.x, hwH.x), + filteredLine(G.y, W.y, hwH.y)); + + // ---- the road ------------------------------------------------------- + // + // On the shots that get it, the middle of the plane stops being a field + // and becomes a carriageway: the mesh is cut away over the tarmac, two + // solid lines mark the edges and a run of centre dashes streams in at + // exactly the cell rate, so the road and the grid keep time with each + // other instead of beating against each other. + // + // Every one of these widths is in world units and every coverage is + // divided by the screen-space derivative of the world coordinate, so a + // line is a real painted stripe near the camera and correctly fades to + // nothing rather than to noise once it is thinner than a pixel. + float wx = max(fwidth(pg.x), 1e-5); + float ax0 = abs(pg.x); + float onRd = saturate((roadHW - ax0) / wx + 0.5) * roadOn; + float exd = abs(ax0 - roadHW); + float edgeL = saturate((0.18 - exd) / wx + 0.5) * saturate(0.36 / wx) * roadOn; + float zc = (pg.z + scroll) / cell; + float wzc = max(fwidth(zc), 1e-5); + float ctrL = filteredLine(zc, wzc, 0.26) + * saturate((0.16 - ax0) / wx + 0.5) * saturate(0.32 / wx) * roadOn; + + fine *= 1.0 - onRd; + heavy *= 1.0 - onRd * 0.90; + + // Dark immediately under the camera and gone before the horizon. The + // near fade is what stops the two or three cells at the bottom of frame + // from becoming the brightest thing in the shot. + // + // Both distances scale with the eye height, and they have to. On a plane + // the distance to a given row of the screen is proportional to how high + // the camera is, so a fade written in fixed world units covers half the + // frame from two units up and none of it from one - which is exactly + // what happened here, and it made the grid vanish out of the low shots. + float gfade = saturate(tG / (camY * gNear)) * exp(-tG / (camY * gFar)); + + // The reflection of the sun in the plane. Strongest at grazing angles, + // which is to say strongest at the horizon, which is where a low sun + // actually glitters off a wet road. The ripple runs along it at a + // couple of hertz, which is the one thing in the lower half of frame + // that still moves when gTune.x is zero and the grid is frozen. Its + // floor is 0.86, so it is a shimmer, not a blink. + float glit = pow(warm, 3.5) * exp(-abs(el) * 9.0); + float rip = 0.86 + 0.14 * sin(pg.z * 0.20 - t * 2.3 + pg.x * 0.55); + + // Cool and warm are within 0.11 of each other in luminance, so this + // transition survives the quantiser as a hue shift over one band rather + // than as a jump. That is the intent: the road should change colour, + // not brightness. + float3 gridCool = float3(0.20, 0.52, 0.92); // lum 0.47 + float3 gridWarm = float3(0.98, 0.46, 0.14); // lum 0.58 + float3 gridCol = lerp(gridCool, gridWarm, saturate(glit * 2.4)); + + // Weights land a lone fine line in band 2 and a heavy crossing in band + // 4-5, against a floor in band 0. The old 0.55/1.05 put a heavy + // crossing at lum 1.6, which is not a bright grid line, it is a flat + // white one with every value inside it thrown away. + // + // The two music terms are both base + k*s. The hat pushes the fine mesh + // by at most a quarter and the low band pushes the bars by at most a + // third; neither can take the floor away, so the grid pulses without + // the frame ever going dark between hits. + float3 gnd = float3(0.007, 0.009, 0.019) * (1.0 - onRd * 0.35); + gnd += gridCol * (fine * 0.40 * (1.0 + 0.28 * gVoice.w) + + heavy * 0.58 * (1.0 + 0.30 * gSync.x)) * gfade * gGain; + gnd += gridCol * (edgeL * 0.52 + ctrL * 0.46) * gfade * gGain; + gnd += float3(1.00, 0.40, 0.13) * glit * 0.75 * rip; + + // ---- sky ------------------------------------------------------------ + + // Kept, because the slab fog and the lamp post fog both want the sky at + // this exact elevation. + float3 skyRaw = skyGradient(el, haze, warm, skyFloor); + float3 sky = skyRaw; + + // Stars, thinned out by haze and by the sun's glow. Sized at roughly a + // virtual pixel and a half - any smaller and the ordered dither in the + // post pass eats them alive. + // + // The horizontal cell count is an integer number of cells around the + // whole circle and the cell index is wrapped, so the star field closes + // across the atan2 seam the same way the ridge noise does. Scaling a + // raw azimuth by 110 put a hard vertical join through the star field at + // exactly the azimuth the ridge had been so carefully made continuous + // at. + // + // Each star twinkles at its own rate, seeded off its own hash, with a + // floor of 0.72 - so the field is alive on a locked-off camera but no + // individual star ever switches on or off. + { + float2 sp = float2(xn * 704.0, rd.y * 96.0); + float2 ic = floor(sp), fc = frac(sp) - 0.5; + ic.x = pmod(ic.x, 704.0); + float h = hash21(ic); + float s = smoothstep(starThr, starThr + 0.09, h) + * saturate(1.0 - length(fc) * 2.2); + float tw = 0.72 + 0.28 * sin(h * 61.0 + t * (1.4 + h * 3.2)); + + // 1.05, not 1.6. At 1.6 the blue channel reached 1.5, the quantiser + // clipped it flat and every star came out the same white as the sun + // crown. At 1.05 a star is band 7 and keeps its colour. + sky += float3(0.62, 0.70, 0.95) * s * s * 1.05 * tw + * saturate(rd.y * 3.0 - 0.1) * (1.0 - haze) * (1.0 + 0.35 * gVoice.w); + } + + // ---- the second body ------------------------------------------------ + // + // A banded, ringed planet, low but clear of the ridge, and always at + // least 0.62 radians off the sun's azimuth so it never sits on top of + // it. It is the cheapest possible "this is a different place" cue: one + // extra disc in the upper sky and the shot cannot be confused with the + // seven others. Lit from the sun's side, so the terminator points the + // right way and it belongs in the same picture rather than looking + // pasted on. + // + // Deliberately dim - the crown of it is band 3, well under the sun's + // band 8 - because there is only one light in this world and it is not + // this. + float3 moonC; + float moonCov, ringM; + { + float mAz0 = (c7 - 0.5) * 2.2; + mAz0 += (mAz0 < 0.0 ? -0.62 : 0.62); // never on top of the sun + float mEl = 0.24 + c6 * 0.28; + float mR = sunR * (0.40 + 0.30 * c1); + float2 mp = float2(wrapPi(az - sunAz - mAz0), el - mEl); + float mrr = length(mp) / mR; + + // Same seam taper as the sun: at the atan2 join fwidth(mrr) is the + // width of the whole sky, and without the second factor a ghost of + // the disc appears on that one column. + moonCov = saturate(0.5 + (1.0 - mrr) / max(fwidth(mrr), 1e-5)) + * saturate((1.6 - mrr) * 4.0) * moonA; + + float bandv = pnoise((mp.y / mR) * 3.4 + 11.0, 64.0); + moonC = lerp(float3(0.15, 0.17, 0.25), float3(0.40, 0.42, 0.53), bandv); + moonC *= 0.34 + 0.66 * saturate(0.55 - (mp.x / mR) * (mAz0 > 0.0 ? 0.85 : -0.85)); + + // The ring is the same circle squashed 3.4:1 and taken as an + // annulus. Drawn only outside the disc, which costs the far side of + // it but saves an ordering test and a depth compare on something + // that is four pixels wide. + float2 rp = float2(mp.x, mp.y * 3.4) / mR; + float rl2 = length(rp); + ringM = smoothstep(1.28, 1.40, rl2) * (1.0 - smoothstep(1.68, 1.80, rl2)) + * moonA * (1.0 - moonCov); + } + sky = lerp(sky, moonC, moonCov); + sky += float3(0.34, 0.36, 0.46) * ringM * 0.55; + + float ca = dot(rd, sunDir); + + // A dark ring just outside the disc, then the glow beyond it. This is + // the single most important line in the file: without the ring the sun + // is a sticker on a gradient, and with it the sun is a hole with + // something burning behind the sky around it. + // + // It starts at 0.99 rather than 1.00. At 1.00 there was a band of + // un-darkened sky sitting between the limb and the ring, and with the + // glow on top of it that band was the brightest thing in frame after + // the crown - a white halo directly outside a disc whose whole idea is + // that it is surrounded by darkness. + float rr = length(float2(dAz, el - elSun)) / sunR; + float ring = smoothstep(0.99, 1.32, rr) * (1.0 - smoothstep(1.45, 2.60, rr)); + sky *= 1.0 - 0.72 * ring; + + // Glow coefficients set so the sky just outside the limb lands in band + // 6 - brighter than the horizon at band 4, darker than the sun's foot + // at band 4-5 and its crown at band 8. The old 0.55/0.22 put it past + // 1.0, which is band 10, which is the same band as the crown: the sun + // and the sky around it were one flat white blob. + float3 glow = float3(1.00, 0.44, 0.15) * exp(-(1.0 - ca) * 34.0) * 0.24 + + float3(0.90, 0.30, 0.12) * exp(-(1.0 - ca) * 4.0) * 0.14; + + // A light pillar straight up out of it. Cheap, and it is the detail + // that stops the sky reading as a gradient with a circle on it. Kept + // faint on purpose - at the strength it wants to be it stops being a + // pillar and becomes a searchlight. The solo lengthens it and lifts it + // by half, from a floor of 0.09 that is always there. + glow += float3(0.85, 0.34, 0.12) + * exp(-abs(dAz) * 34.0) + * exp(-max(el, 0.0) * (3.0 - 1.4 * gVoice.y)) + * 0.09 * (1.0 + 0.50 * gVoice.y); + + sky += glow * (1.0 - 0.85 * ring); + + // ---- aurora --------------------------------------------------------- + // + // A single ribbon, not a curtain wall: its centre elevation waves along + // the azimuth on one octave of periodic noise, its thickness on + // another, and a third at 121 cycles gives the vertical striations that + // are the only reason anyone recognises the thing. All three drift, at + // different rates and in different directions, so the ribbon crawls + // rather than sliding - which is what makes this the animation that + // carries a shot with a frozen grid. + // + // Amplitude is set so the brightest part lands at band 2: it is the + // second event in the sky and must not compete with the sun. The organ + // swells it from a floor of 0.6. + { + float rib = pnoise(xn * 9.0 + t * 0.100, 9.0); + float thk = 0.055 + 0.045 * pnoise(xn * 19.0 - t * 0.155, 19.0); + float dd = (el - (0.20 + rib * 0.34)) / thk; + float str = 0.45 + 0.55 * pnoise(xn * 121.0 + t * 0.70, 121.0); + sky += float3(0.24, 0.58, 0.50) * exp(-dd * dd) * str + * auroraA * saturate(el * 8.0) * 0.55 * (0.60 + 0.40 * gVoice.z); + } + + // ---- the sun -------------------------------------------------------- + // + // by runs -1 at the top of the disc to +1 at the bottom. The slats start + // at bs = 0, which is a little above the centre, because the ground has + // already taken everything below by = +0.55 and slats that only live in + // the bottom half would be a band four pixels tall. + // + // The phase is bs*K - bs*bs*0.2375*K, whose derivative falls with + // depth, so the slats get further apart going down while the gaps get + // wider: the disc is being erased from below rather than striped + // evenly. K is per shot: at 5 the visible part of the disc gets three + // slats, which is a striped circle rather than a sun going out, and at + // 9 it gets six thin ones and reads as a venetian blind. Both are + // right, in different shots, which is the point. + // + // And the erasure CLIMBS while the shot is held. slatF is how far down + // the visible disc the first slat sits at the cut - so a later + // appearance can open with the sun further gone than an earlier one did + // - and that start creeps upward at up to 0.022 of the disc a second, + // floored at 0.03 so it can never quite eat the crown. On the four-bar + // hold at bar 87 that is a visible slow extinction over seven seconds + // with nothing else in the frame moving. + // + // Both of those are measured as a FRACTION OF THE VISIBLE DISC, not in + // absolute disc radii, and that is a fix rather than a flourish. The + // ground cuts the disc at by = lift, so the part anyone sees runs from + // by = -1 to by = lift and is span = lift + 1 tall. The old code + // started the slats at a fixed by = -0.62, which is a quarter of the + // way down when lift is 0.55 - and with lift now free to sit at 0.26 + // it was most of the way down instead, so the low-sun shots came out + // with a plain white ball and one bar across the bottom. The slat count + // is normalised the same way, so a low sun and a high one get the same + // number of bars across the face rather than the same bars with most of + // them buried. + float span = lift + 1.0; + float sfr = max(slatF - t * eatRt, 0.03); + float by = (elSun - el) / sunR; + float bs = by + 1.0 - sfr * span; + float kk = slatK * (1.55 / span); + float bp = bs * kk - bs * bs * (kk * 0.2375); + float gap = saturate(0.06 + 0.58 * saturate(bs)); + + // Filtered with the same integral as the grid, because these slats + // converge exactly the way grid lines do and would strobe just as badly. + float slat = 1.0 - filteredLine(bp - gap * 0.5, fwidth(bp), gap * 0.5); + slat = lerp(1.0, slat, saturate(bs * 6.0)); // the crown stays solid + + // The crown is lum 0.84 and the foot lum 0.45, so the disc spans four + // bands top to bottom and the dark limb takes another two off the edge. + // The old top colour had a red channel of 1.10: everything above lum + // 0.95 is one band, so the entire upper half of the disc was flat. + float3 sunCol = lerp(float3(0.98, 0.83, 0.50), + float3(0.95, 0.27, 0.06), saturate(by * 0.5 + 0.5)); + sunCol *= 1.0 - 0.42 * smoothstep(0.70, 1.00, rr); // dark limb + sunCol *= slat; + + // The extra taper kills a faint ghost of the disc that otherwise shows + // up on the single pixel column at the atan2 seam, where fwidth(rr) is + // the width of the whole sky. + float discCov = saturate(0.5 + (1.0 - rr) / max(fwidth(rr), 1e-5)) + * saturate((1.5 - rr) * 4.0); + + float3 col = lerp(sky, sunCol, discCov); + + // ---- the cloud deck ------------------------------------------------- + // + // Two octaves of noise that is periodic around the azimuth and free in + // elevation, at 12 cycles horizontally against 26 vertically. That + // ratio is the whole effect: the cells come out four times wider than + // they are tall, so the threshold cuts them into long horizontal bars + // rather than into blobs, and a bank of horizontal bars stacked over a + // low sun is the one cloud formation this genre has. + // + // Drawn OVER the sun on purpose. A bar crossing the disc is worth more + // than the disc is, and it is the difference between a sky and a + // backdrop. Lit from underneath towards the sun - band 2 there, band 1 + // at the edges of frame - so it reads as cloud and not as smoke. + { + float cd = t * (0.03 + 0.06 * c4); + float2 cp = float2(xn * 12.0 + cd, el * 26.0 + 1.7); + float n1 = pnoise2(cp, 12.0) * 0.62 + pnoise2(cp * 2.0, 24.0) * 0.38; + float lo = 0.54 - 0.18 * cloudA; + float cov = smoothstep(lo, lo + 0.15, n1) + * exp(-max(el, 0.0) * 3.2) * saturate(el * 26.0) * cloudA * 0.88; + float3 cc = lerp(hz * 0.34, (hz + glow * 0.55) * 1.20, pow(saturate(warm), 0.75)); + col = lerp(col, cc, saturate(cov)); + } + + // ---- composite: mountains over the sky ------------------------------ + + // Three haze mixes chosen so the ranges land on bands 3, 2 and 1 with + // the horizon sky above them at band 4 - and, importantly, so they stay + // in those bands across the whole haze range rather than only at one + // setting. + // + // The old mixes were 0.65 / 0.32 / 0.04 against a horizon of lum 0.22. + // That put the far range at lum 0.16 against a sky at 0.22 - six + // hundredths apart, which is a little over half of one band, so the + // dither merged them and the far range simply was not in the picture. + // The near range and the slabs were both under 0.05 and were therefore + // the same navy floor as each other and as the empty sky. + float3 far0 = float3(0.006, 0.008, 0.015); + col = lerp(col, lerp(far0, hz, hFarK), covFar); // band 3 + col = lerp(col, lerp(far0, hz, hMidK), covMid); // band 2 + col = lerp(col, lerp(far0, hz, hNearK), covNear); // band 1 + + // A rim on the near crest, only where the crest is in front of the sun. + // A black shape with a hot line along its top edge is the whole reason + // this scene survives being dithered down to a handful of levels: the + // rim is band 6 sitting directly on band 1, which is the largest step + // anywhere in the frame that is not the sun itself. The guitar drives + // it from a floor of 0.90 - the rim is never absent, it just gets + // hotter when the riff is under it. + col += float3(1.00, 0.46, 0.16) + * exp(-max(dNear, 0.0) * 90.0) * step(0.0, dNear) + * pow(warm, 1.4) * 0.90 * ridge * (1.0 + 0.32 * gVoice.x); + + // A fog bank sitting in the mountains' feet. This is the structural half + // of what gTune.w means: at haze 0.15 it is nothing, at 0.55 it is a + // layer that drowns the bottom of the near range and leaves the crests + // as islands, which is a materially different silhouette rather than a + // greyer version of the same one. Squared, so only the genuinely hazy + // shots get it, and pulled back off the sun so the disc keeps its foot. + float bank = saturate(exp(-max(el, 0.0) * (30.0 - 14.0 * haze)) * haze * haze * 1.4) + * (1.0 - discCov * 0.75) * step(0.0, el); + col = lerp(col, hz * (0.80 + 0.30 * warm), bank); + + // ---- ground over everything below the horizon ------------------------ + + // The ground's fog scales with the eye height for the same reason the + // grid fade does. The slabs get their own constant in world units, + // because they are objects at real distances and how far away a tower + // looks must not change when the camera comes down a metre. + float fogK = (1.0 + 2.6 * haze) / (camY * 62.0); + + // 0.0030 with a shallow haze response, not 0.0055 with a steep one. + // Swept against the quantiser at every haze setting, this holds the + // slab depth ramp inside bands 0, 1 and 2 for the whole 15-to-315 unit + // stream: a near slab is always on the floor and a far one is always a + // ghost, and neither ever climbs into band 3 where the far range lives + // and disappears against it. + float fogW = 0.0030 * (1.0 + 1.6 * haze); + + // Two deliberate weaknesses in the ground fog. It never reaches 1, and + // it hazes towards a little over half the sky rather than towards the + // sky itself. Let the plane haze all the way and the bottom third of + // frame becomes one warm wash with the mountains floating in it: the + // plane and the sky arrive at the same value and the horizon stops + // being an edge, it becomes a place where two browns happen to touch. + float fogG = (1.0 - exp(-tG * fogK)) * (0.58 + 0.30 * haze); + gnd = lerp(gnd, (hz + glow * 0.18) * 0.55, fogG); + + float gcov = saturate(0.5 - el / max(fwidth(el), 1e-5)); + col = lerp(col, gnd, gcov); + + // ---- slabs over everything ------------------------------------------ + // + // Exact ray-box, no march. They are camera-relative in z so that they + // stream past forever, but world-fixed in x so that a lateral dolly + // still gets real parallax off them. + // + // The four hand-placed towers this scene used to have are gone, because + // four hand-placed towers are the same four towers every time the scene + // comes back. What survives from them is the DISCIPLINE, encoded rather + // than typed: index parity puts consecutive slabs on opposite sides of + // the axis, and none of them is allowed within fifteen units of it, so + // whatever the hash produces still frames the sun instead of standing + // in front of it. Heights are squared off the hash so most of them are + // low and a rare one is enormous - a skyline of equal towers has no + // skyline in it. + float cS = 0.0, tBest = 1e9; + { + float3 ir = float3(safeRcp(rd.x), safeRcp(rd.y), safeRcp(rd.z)); + + // dv is the thickness of ONE slab along this ray: positive inside + // it, negative outside, and continuous across the silhouette + // because tN and tF are both continuous functions of rd. That makes + // it a signed field the screen-space derivative can be taken of, + // which is the only way a ray-box gets an anti-aliased edge without + // supersampling it. + // + // This matters more than it sounds. These are the highest contrast + // edges in the frame - band 0 against band 4 - they are near + // vertical, and they MOVE. A hard-tested box gives them stair steps + // that crawl a pixel at a time down the edge, and the ordered + // dither turns each step into a little rectangle of noise. + // + // THE DERIVATIVE HAS TO BE TAKEN PER SLAB, which is why this loop + // is unrolled and why it no longer keeps a running maximum to + // filter at the end. max() of two continuous fields is continuous + // but it has a CREASE where the winner changes, fwidth() at a + // crease reports the size of the crease rather than the local + // gradient, and saturate(0.5 + small/large) is one half - so the + // locus where two slabs' thicknesses happen to be equal was drawn + // as a half-covered line right across the frame, whether or not + // either slab was anywhere near being hit. With four slabs nailed + // to four hand-picked positions that locus happened to fall outside + // the frame. With six of them placed by a hash it did not, and it + // put a wire across the sun. Each slab is filtered on its own field + // now and the coverages are unioned afterwards, which has no crease + // in it anywhere. + [unroll] for (int b = 0; b < 6; b++) { + // Two hashes, four numbers. frac() of a scaled hash is a + // decorrelated second draw for nothing, and a third hash per + // slab was measurably more expensive than the whole ray-box + // test it was feeding. + float sd = vid * 0.0173 + (float)b * 3.719; + float ha = hash11(sd); + float hb = hash11(sd + 1.31); + + float sgn = ((b & 1) != 0) ? 1.0 : -1.0; + float sx = sgn * (15.0 + ha * 92.0); + float shw = 3.0 + frac(ha * 7.31) * 5.5; + float sht = 13.0 + hb * hb * 66.0; + float u = frac(hb * 5.17 + 0.31 - slabPhase); + + // Centre at half the height with a half extent of the same, so + // the box spans y = 0 to y = height. It was centred AT the + // height with a half extent of the height, which stood every + // slab in a box from 0 to twice its height. + float hh = sht * 0.5; + float3 bc = float3(sx, hh, ro.z + baseZ + u * 300.0); + float3 bh = float3(shw, hh, shw); + + float3 n = ir * (ro - bc); + float3 k = abs(ir) * bh; + float3 t1 = -n - k, t2 = -n + k; + float tN = max(max(t1.x, t1.y), t1.z); + float tF = min(min(t2.x, t2.y), t2.z); + + // Clamped before the derivative is taken. Unclamped, a ray that + // misses carries a thickness of minus several million world + // units, fwidth of it is comparable, and the coverage comes out + // at a flat one half over the entire sky. Sixteen units is + // wider than any silhouette gradient at these distances, so + // inside and outside both pin to a constant, fwidth goes to + // zero there and the coverage is exactly 1 or exactly 0 - the + // feather only exists in the pixel or two where the real + // gradient lives. A slot this shot does not use is pinned to + // the same constant, so it is exactly as free as not running. + float dvC = ((float)b < slabN) + ? clamp(tF - max(tN, 0.0), -16.0, 16.0) : -16.0; + float cv = saturate(0.5 + dvC / max(fwidth(dvC), 1e-5)); + float th = max(tN, 0.0); + + if (cv > 0.002 && th < tBest) tBest = th; + cS = max(cS, cv); + } + } + cS *= (tBest < ((rd.y < 0.0) ? tG : 1e9)) ? 1.0 : 0.0; + + // Nothing but haze separates a near slab from a far one, so the fog is + // doing all the depth work here. The near ones come out on the floor, + // band 0, the far ones as band 2 ghosts, and that difference is the + // only thing telling you how big the plane is. The target is kept at + // 0.72 of the sky so even the farthest slab stays a silhouette rather + // than dissolving into the band the sky is in. + float fS = 1.0 - exp(-tBest * fogW); + float3 slabCol = lerp(float3(0.006, 0.007, 0.014), + (skyRaw + glow * 0.25) * 0.72, fS); + col = lerp(col, slabCol, cS); + + // Windows, on the shots that get them. A monolith is a shape; a tower + // with a hundred lit cells in it is a building, and the two read as + // completely different objects standing in the same grid. The hit point + // is folded on x+z so one pattern wraps the corner without needing to + // know which face was hit. + // + // The lod term is not optional. These cells are about a metre and a + // half and the far towers are three hundred units out, so without it + // they are sub-pixel noise that the ordered dither turns into a + // shimmering rectangle. It fades them out the moment a cell stops being + // resolvable, and the fog fades them again on top of that, so only the + // near towers are ever lit. + { + float3 hp = ro + rd * min(tBest, 3000.0); + float2 wq = float2((hp.x + hp.z) * 0.62, hp.y * 0.40); + float2 fq = abs(frac(wq) - 0.5); + float wm = step(fq.x, 0.30) * step(fq.y, 0.32) * step(0.63, hash21(floor(wq))); + float lod = saturate(1.5 - max(fwidth(wq.x), fwidth(wq.y)) * 2.4); + col += float3(1.00, 0.62, 0.26) * wm * lod * cS * winOn + * (1.0 - fS * 0.85) * 0.40 * (0.62 + 0.38 * gVoice.x); + } + + // ---- lamp posts down the road --------------------------------------- + // + // Two vertical planes at x = +-poleX, each carrying a picket that + // repeats every two cells. Because every post on one side lies in the + // SAME plane, this is one ray-plane intersection per side and then a + // filtered line in z and a window in y - an infinite row of posts for + // about the cost of one box, and correctly anti-aliased by the same + // integral the grid uses. + // + // They are what a road needs that a grid does not: a rhythm with a + // beat, streaming past at the scroll rate, with a hot cap on each one + // that gives the lower half of frame a run of moving highlights. On the + // bar 80 crane, which climbs thirty units a second, they are also the + // only thing in the picture that tells you the camera is rising. + // + // The distance falloff is aggressive on purpose. A post is a few + // centimetres of world at two hundred units and there is no honest way + // to draw it, so it is faded out well before it can become a row of + // grey dots along the horizon. + float poleX = roadHW + 3.2; + float poleH = 7.5 + c2 * 4.0; + float poleSp = cell * 2.0; + float poleCov = 0.0, poleT = 1e9, capM = 0.0; + [unroll] for (int s2 = 0; s2 < 2; s2++) { + float px = (s2 == 0) ? -poleX : poleX; + float tp = (px - ro.x) * safeRcp(rd.x); + float tc = clamp(tp, 0.0, 900.0); + float3 pp = ro + rd * tc; + + float zp = (pp.z + scroll) / poleSp; + float wzp = max(fwidth(zp), 1e-5); + float post = filteredLine(zp, wzp, 0.055); + + float wyp = max(fwidth(pp.y), 1e-5); + float vext = saturate((poleH - pp.y) / wyp + 0.5) * saturate(pp.y / wyp + 0.5); + + // Both derivatives gate it. Once a pixel spans more than a couple + // of world units vertically or more than half a post period along + // z, there is no post left to draw and what remains is the mean of + // the pattern smeared over the sky - which is exactly the flat grey + // band this term exists to prevent. + float lod = saturate(1.4 - wyp * 0.50) * saturate(1.6 - wzp * 2.0); + float on = step(0.4, tp) * step(tp, 850.0) * lod * vext * post; + + float cov = on * exp(-tc * 0.011); + float cap = on * saturate((pp.y - (poleH - 1.5)) * 1.1) * exp(-tc * 0.020); + + if (cov > poleCov) { poleCov = cov; poleT = tc; } + capM = max(capM, cap); + } + { + float3 poleCol = lerp(float3(0.010, 0.011, 0.020), + (skyRaw + glow * 0.22) * 0.62, + 1.0 - exp(-poleT * fogW)); + float pv = saturate(poleCov) * roadOn * ((poleT < tBest) ? 1.0 : 0.0); + col = lerp(col, poleCol, pv); + col += float3(1.00, 0.58, 0.22) * saturate(capM) * roadOn + * 0.50 * (0.62 + 0.38 * gVoice.w); + } + + // Shot fade only. No dither, no scanlines, no tone curve, no mask - + // post.hlsl owns every one of those and doing any of them twice is how + // a demo ends up with moire between its own two dither grids. + return float4(col * gTime.z, 1.0); +} diff --git a/fx/inscriptions.hlsli b/fx/inscriptions.hlsli new file mode 100644 index 0000000..85f0a7d --- /dev/null +++ b/fx/inscriptions.hlsli @@ -0,0 +1,68 @@ +#ifndef INSCRIPTIONS_HLSLI +#define INSCRIPTIONS_HLSLI + +uint2 inscriptionGlyph(uint c) +{ + switch (c) { + case 97u: return uint2(0x83800u, 0x7a3eu); + case 98u: return uint2(0x8bc21u, 0x3e31u); + case 100u: return uint2(0x8fa10u, 0x7a31u); + case 101u: return uint2(0x8b800u, 0x383fu); + case 102u: return uint2(0x38a4cu, 0x842u); + case 103u: return uint2(0x8c7c0u, 0x3a1eu); + case 105u: return uint2(0x21804u, 0x3884u); + case 108u: return uint2(0x21086u, 0x3884u); + case 109u: return uint2(0xaac00u, 0x56b5u); + case 110u: return uint2(0x8bc00u, 0x4631u); + case 111u: return uint2(0x8b800u, 0x3a31u); + case 114u: return uint2(0x9b400u, 0x421u); + case 115u: return uint2(0xf800u, 0x3e0eu); + case 116u: return uint2(0x11c42u, 0x3242u); + case 117u: return uint2(0x8c400u, 0x5b31u); + case 121u: return uint2(0x8c620u, 0x3a1eu); + case 46u: return uint2(0x0u, 0x1080u); + default: return uint2(0u, 0u); + } +} + +static const uint inscriptionRemember[12] = {114,101,109,101,109,98,101,114,46,46,46,32}; +static const uint inscriptionSignal[12] = {115,105,103,110,97,108,32,102,111,117,110,100}; +static const uint inscriptionStrange[12] = {115,116,97,121,32,115,116,114,97,110,103,101}; + +float inscriptionPixel(int2 p, uint label) +{ + uint count = label == 1u ? 11u : 12u; + if (p.x < 0 || p.y < 0 || p.x >= int(count * 6u - 1u) || p.y >= 7) return 0.0; + uint slot = uint(p.x) / 6u; + uint x = uint(p.x) % 6u; + if (x == 5u) return 0.0; + uint c = label == 1u ? inscriptionRemember[slot] + : label == 2u ? inscriptionSignal[slot] : inscriptionStrange[slot]; + uint2 bits = inscriptionGlyph(c); + uint row = uint(p.y); + return float(((row < 4u ? bits.x : bits.y) >> ((row % 4u) * 5u + x)) & 1u); +} + +float inscriptionMask(float2 uv, float2 footprint, float selector) +{ + if (selector < 0.5 || selector >= 3.5 || any(uv < 0.0) || any(uv > 1.0)) return 0.0; + uint label = uint(selector + 0.5); + float2 size = float2(label == 1u ? 65.0 : 71.0, 7.0); + float2 p = uv * size; + float2 fw = max(footprint * size, 0.001); + float resolved = 1.0 - smoothstep(1.0, 1.8, max(fw.x, fw.y)); + if (resolved <= 0.0) return 0.0; + int2 base = int2(floor(p)); + float value = 0.0; + [loop] for (int y = -1; y <= 1; ++y) { + [loop] for (int x = -1; x <= 1; ++x) { + int2 cell = base + int2(x, y); + float2 overlap = max(0.0, min(p + fw * 0.5, float2(cell) + 1.0) + - max(p - fw * 0.5, float2(cell))); + value += inscriptionPixel(cell, label) * overlap.x * overlap.y; + } + } + return saturate(value / (fw.x * fw.y)) * resolved; +} + +#endif diff --git a/fx/kaleido.hlsl b/fx/kaleido.hlsl new file mode 100644 index 0000000..e600968 --- /dev/null +++ b/fx/kaleido.hlsl @@ -0,0 +1,722 @@ +// KALEIDOSCOPIC IFS TUNNEL. +// +// You are falling down a folded throat: a lattice mirrored around the axis +// and repeated down it, streaming past close enough to blur and converging on +// a light you never reach. It belongs under a solo, and it is built so that +// the same shader is several different rooms - a tight four-sided pipe you +// can barely fit through, a twelve-fold cathedral of glass with irises you +// fly through - on the shot knobs alone. +// +// The composition, which is what everything below is in service of: the light +// is a lamp ON THE AXIS, a fixed distance AHEAD of the camera, travelling +// with it. So the far end of the tunnel is blown out, the walls rushing past +// your ear are nearly black, and the relief between the two is raked by a +// light that is always at the vanishing point. That is the entire value +// structure - white core, amber-to-green relief, black near walls - and it +// is the structure that survives a 4x4 Bayer dither at 640x360, where +// anything subtler would not. +// +// ---- WHY THIS FILE IS SHAPED THE WAY IT IS ------------------------------- +// +// Eight shots in the edit draw this scene. Measured as thumbnails they were +// too close to each other: the knobs only ever changed the fold ANGLE, the +// IFS scale and the pipe diameter, and at 360 lines through a four-stop ramp +// those three read as the same picture with the contrast nudged. What the +// eye actually sorts a frame by, in the half second a two-bar shot gets, is +// SYMMETRY ORDER and SILHOUETTE - how many arms the star at the vanishing +// point has, and whether the tunnel is a bare pipe or has rings coming at +// you. So the knobs now quantise into a small set of discrete layouts and +// the continuous values ride on top of those. +// +// gTune.x fold amount - 0..1. Two jobs. Quantised into five bands it picks +// the SYMMETRY ORDER: 4, 6, 8, 10 or 12 sectors +// around the axis, and the parity of that band sets +// which way the whole thing spins. Continuously it +// still drives the IFS rotation between iterations +// (blocky and architectural at 0, sheared into +// spines at 1), the spin rate, the rate the throat +// breathes at, and the colour band spacing. +// gTune.y IFS scale - space expansion per iteration, clamped 1.15..2.30. +// Low is a dense fine weave, high is a few big +// slabs. Its FRACTIONAL part is a second, unrelated +// knob: the coarseness floor, which caps how fine +// the lattice is allowed to get regardless of how +// close it is. A coarse shot is carved out of +// slabs, a fine one is filigree. +// gTune.z openness - 0 is a tight bore threaded onto the camera, 1 +// blows the pipe out into a hall and turns on the +// glass rim light. Quantised into four bands it also +// decides whether the tunnel carries IRIS PLATES - +// annular bulkheads rushing past with a hole in the +// middle - and how close together they sit. +// Continuously it sets cell length down the axis, +// rail thickness, twist pitch and fog density. +// gTune.w hurtle - EXTRA units per second the structure flows toward +// you, on top of a baseline the shot always gets. +// The baseline exists because half these shots are +// held or whip-panned cameras: with a still camera +// and no flow the shot is a photograph, and this is +// the scene under the solo. +// +// The eight rows in the table land on eight different (symmetry, structure) +// pairs, which is the point: no two appearances are the same room. +// +// ---- OFF-AXIS SAFETY ----------------------------------------------------- +// +// The tunnel used to be nailed to the world axis, so any shot that walked the +// camera sideways buried the lens in solid lattice and the frame went black. +// The axis now follows the camera once the camera has used up its clearance +// inside the bore - free parallax within the throat, and a tow after that. +// It is a translation that depends only on the constant buffer, so it costs +// nothing per step and every argument below about toTube being a pure +// translation still holds. +// +// ---- THE THING THAT MAKES THIS SHADER WORK AT 360 LINES ------------------ +// +// A five level IFS with a scale of 2.3 puts its finest strut at th/2.3^5, a +// hair over seven thousandths of a unit. At the demo's fixed 360 virtual +// lines that is sub-pixel from about four units out - which is to say, over +// essentially the whole frame. Rendered flat, that detail does not read as +// detail. It reads as a boiling stipple that the post pass's ordered dither +// then latches onto and amplifies, and in a dark cinema it is the only thing +// anybody sees. +// +// So the field is CONE AWARE. The march carries the radius of the pixel +// footprint at the current depth, and the IFS uses it twice: any level whose +// struts are thinner than one pixel is fattened out to a pixel, and once a +// level falls below that it stops iterating entirely. Fattening keeps the +// strut visible instead of stippling; stopping keeps the compiler from +// spending five iterations resolving something that lands inside one sample. +// The normal and the occlusion are handed the same radius, so all three agree +// on which surface they are looking at. The per-shot coarseness floor rides +// in through exactly the same door - it is a minimum footprint - so it costs +// nothing and it saves iterations on the shots that use it hardest. +// +// Cost: up to 56 march steps, a four tap normal and a five tap occlusion at +// the hit, so 65 distance field evaluations for a pixel that hits and fewer +// for one that does not. Each evaluation is two to five IFS iterations after +// the cone cutoff, plus a rail, plus - on the shots that turn them on - one +// iris plate. No shadow rays: the only real light sits on the axis at the +// vanishing point, so a shadow ray from any visible surface would trace back +// along the camera's own sightline and buy almost nothing off the most +// expensive map in the demo. Occlusion does that job instead, and in a folded +// lattice it is doing most of the shaping. +// +// In wall clock, at the 640x360 the scene target actually runs at: order of +// a millisecond on a mid-range discrete part, a few tenths on a fast one, +// and several milliseconds on integrated graphics. + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect (w/h) + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look direction, w roll + float4 gTune; // per shot knobs +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// ---- distance field ------------------------------------------------------ + +#define LAMP 13.0 // how far ahead of the camera the light sits +#define TAU 6.28318531 + +// Half the vertical field, in tangent units, divided by half the virtual +// height: the scene target is a fixed 360 lines (gfx.c, VH), so this is the +// tangent subtended by one virtual pixel. Multiplied by depth it is the +// radius of the pixel's footprint out there, which is the only number the +// level-of-detail cutoff needs. It is deliberately biased a little coarse - +// the output is dithered into a narrow ramp, and half a pixel of detail is +// not detail there, it is noise. +#define PIXTAN (1.25 / 180.0) + +float2 rot2(float2 v, float a) +{ + float c = cos(a), s = sin(a); + return float2(v.x * c - v.y * s, v.x * s + v.y * c); +} + +// The two radii the tunnel lives between. Shots need the inner one as well - +// it is the safety margin that stops a camera ending up buried in a solid - +// so it is worth having in one place. +float boreRadius(float op) { return 0.80 + 1.70 * op; } +float wallRadius(float op) { return 3.20 + 2.60 * op; } + +// ---- the per shot layout ------------------------------------------------- +// +// Everything in here reads the constant buffer and nothing else, so it is +// uniform over the whole frame and fxc hoists the lot out of the march. It +// is written as one struct rather than sprinkled through mapT so that the +// eight rows in the table can be read off against it in one place. +struct Cfg +{ + float folds, sec, isec; // symmetry order and the wedge it implies + float spin, zTwist; // rotation in time, and pitch of the spiral + float cell, coarse; // axial repeat, and the detail floor + float boreAmp, boreFrq; // the throat breathing as it comes at you + float railGap, railW; // the spars that ride the lip of the throat + float ringOn, ringSp, ringISp, ringTh, ringGap; // the iris plates + float bandFrq, fogK, flow; +}; + +Cfg cfg() +{ + Cfg c; + + float fx = saturate(gTune.x); + float fz = saturate(gTune.z); + float fy = clamp(gTune.y, 1.15, 2.30); + + // The two discrete axes. Five symmetry bands against four structure + // bands is twenty layouts for eight shots, and the table's values land + // on eight distinct pairs - which is the whole reason the appearances + // stop looking like each other. + float vi = min(floor(fx * 5.0), 4.0); // 0..4 + float ri = min(floor(fz * 4.0), 3.0); // 0..3 + + // Spin direction off the parity of the symmetry band. Two shots that + // happen to share a fold count will at least be turning opposite ways. + float par = (frac(vi * 0.5) < 0.25) ? 1.0 : -1.0; + + // Four sectors reads as a square shaft, six as the snowflake everybody + // already knows as "kaleidoscope", twelve as a rose window. Nothing else + // in this file changes the frame as hard as this one number. + c.folds = 4.0 + 2.0 * vi; + c.sec = TAU / c.folds; + c.isec = c.folds * (1.0 / TAU); + + c.spin = par * (0.22 + 0.50 * fx); + c.zTwist = par * (0.018 + 0.045 * fz); + + c.cell = 2.30 + 2.60 * fz; + + // The coarseness floor. gTune.y's integer part is the IFS scale, which + // the table sweeps monotonically; its fraction is free, and it is worth + // more here than anywhere else because it decides whether the wall is a + // filigree or a set of slabs. + c.coarse = 0.004 + 0.030 * frac(fy); + + // The throat is not a constant diameter. A long wave travelling with the + // structure opens and closes the aperture as it comes at you, which is + // motion the camera cannot supply and which reads even on a shot held + // dead still. The gSync term pumps it with the kick; it is an addition + // to a base, never a multiply of the picture. + c.boreAmp = 0.08 + 0.16 * fz + 0.05 * saturate(gSync.x); + c.boreFrq = 0.055 + 0.075 * fx; + + // Spars riding the lip of the throat, one per sector, silhouetted + // against the bright core and converging on it. They are what makes the + // symmetry order legible at a glance instead of something you have to + // work out from the relief. + c.railGap = 0.10 + 0.28 * fz; + c.railW = 0.045 + 0.055 * fz; + + // Iris plates: annular bulkheads with a hole in the middle, rushing past. + // Off for the tightest shots, where the pipe wants to be bare, and + // getting denser as the room opens up. + c.ringOn = (ri >= 1.0) ? 1.0 : 0.0; + c.ringSp = 11.0 - 2.2 * ri; + c.ringISp = 1.0 / c.ringSp; + c.ringTh = 0.06 + 0.12 * fz; + // The hole is only a little wider than the throat, so these read as + // apertures you fall through rather than as ribs on a distant wall. Any + // wider and the eye files them as wall detail; the whole reason they are + // here is to put something between the camera and the vanishing point. + c.ringGap = 0.06 + 0.26 * fz; + + c.bandFrq = 0.16 + 0.16 * fx; + + // Fog is a composition control, not an atmosphere: it sets how deep the + // tunnel reads. A tight bore wants to close in a few units, a cathedral + // wants to run away from you. + c.fogK = 0.020 + 0.022 * (1.0 - fz); + + // Baseline flow, so a held or whip-panned camera is not a photograph. + // It is a RATE multiplied by absolute shot time, so nothing audio driven + // is allowed in it - a rate that changed would jump the phase. + c.flow = gTune.w + 2.0 + 7.0 * fz; + + return c; +} + +// World space into tube space, where the axis of the tunnel is exactly +// x = y = 0. This is a pure translation at every depth - never a rotation - +// so length(q.xy) is a true world distance to the axis and a direction built +// out of q.xy is a true world direction. The lighting leans on both of those, +// which is why the twist lives in mapT and not in here. +float3 toTube(float3 p) +{ + // The structure comes at the camera rather than the other way round, so + // a shot can hold the camera dead still and still be moving at speed. + p.z += gTime.x * cfg().flow; + + float op = saturate(gTune.z); + + // The bore is not a straight pipe. A long shallow serpentine drifts the + // vanishing point off centre and back, which is the whole difference + // between falling down a hole and riding something. + // + // The amplitude is a FRACTION OF THE BORE and not a constant. It used to + // be a constant 0.60 and 0.48, whose combined swing is 0.77 units - wider + // than the tightest bore, so a shot that put the camera on the world axis + // with tune.z at 0 had the tunnel walk over the top of it and bury the + // lens in solid lattice, on a schedule, some seconds into the shot. At + // 0.42 of the bore the worst case swing is a little over half the + // clearance, and it gets grander as the room does, which is also the + // better look. + float amp = boreRadius(op) * 0.42; + p.x -= sin(p.z * 0.021) * amp; + p.y -= cos(p.z * 0.017) * amp * 0.80; + + // ...and the tunnel is threaded onto the CAMERA, not onto the world + // origin. A crane or a lateral track used to walk the lens straight into + // the solid outside the wall and the frame went black; now the camera + // gets free parallax for the first half of its clearance and tows the + // axis after that. Depends on gCam alone, so it is a constant offset for + // the whole frame and the pure-translation argument above survives. + float2 co = gCam.xy; + float cl = length(co); + float free = boreRadius(op) * 0.50; + p.xy -= co * (max(cl - free, 0.0) / max(cl, 1e-4)); + + return p; +} + +// `lod` is the radius of the pixel footprint at the point being sampled. +// Pass 0 for the exact field; the march, the normal and the occlusion all +// pass the real thing, and all three must pass the SAME thing at a given +// point or the normal ends up being the gradient of a discontinuity. +float mapT(float3 q, float lod) +{ + Cfg c = cfg(); + + float op = saturate(gTune.z); + float bore = boreRadius(op); + float wall = wallRadius(op); + float mid = (bore + wall) * 0.5; + + float r = length(q.xy); + + // The breathing throat. Only the CUT is modulated, never `mid` - the + // lattice stays exactly where it was and the aperture opens over it and + // closes back, so the wave reveals and hides relief instead of dragging + // the whole wall around. Amplitude times frequency is under a tenth, so + // the field stays comfortably Lipschitz and the march does not overstep. + float bth = bore * (1.0 + c.boreAmp * sin(q.z * c.boreFrq)); + + // Twist about the axis. Being a rotation about that axis it leaves r + // alone, so it is free to happen after the radius has been taken. Rate + // and sign are both per shot: a spiral climbing left at one pitch and a + // spiral climbing right at three times that are different rooms. + float2 tw = rot2(q.xy, q.z * c.zTwist + gTime.x * c.spin); + + // The kaleidoscope. Fold the plane down to one sector, then mirror that + // wedge about its own centre line. The mirror is the part that matters: + // it makes the two edges of the wedge identical, so the copies meet with + // no visible seam no matter what the IFS then does inside. + // + // The usual failure of a polar fold is the pinch at the axis, where the + // sectors converge and the arc length metric collapses to nothing. It + // cannot happen here: the bore hollows out everything inside the throat, + // so the pinch is never rendered. + float a = atan2(tw.y, tw.x); + a -= c.sec * floor(a * c.isec); + a = abs(a - c.sec * 0.5); + + // Arc length around the tunnel. A true world length, which is what makes + // a box built on it a valid distance estimate. + float arc = a * r; + + // Unroll the wedge into a flat slab: x is arc length around the tunnel, + // y is how far out from the middle of the annulus, z is depth, repeated + // so the tunnel has no end. + // + // This unrolling is the fix for the thing that makes a naive version of + // this shader fail. The attractor of a contracting IFS is COMPACT - a + // blob a couple of units across - so running one on raw world space + // gives a small clump of struts floating in an otherwise empty frame. + // Run it in the slab instead and the fold and the repeat tile that one + // blob into a relief that covers the whole wall. + float3 p; + p.x = arc; + p.y = r - mid; + p.z = frac(q.z * (1.0 / c.cell)) * c.cell - c.cell * 0.5; + + // The iterated function system. Fold into the positive octant, rotate, + // then scale about an offset point - five times. Everything but the + // scale is an isometry, so working in units of 1/scale is a correct + // distance estimate for the original point. + // + // A shot row that left tune.y at zero would otherwise divide by zero and + // paint the frame with NaN, so the scale is clamped rather than trusted. + float s = clamp(gTune.y, 1.15, 2.30); + float is = 1.0 / s; + + // The fold angle is taken as a 0..1 amount and remapped onto the band + // where the lattice actually exists. Past about 1.5 radians the two + // rotations between them walk the whole structure out of the slab and + // the shot becomes an empty pipe - a silent, total failure that looks + // like a bug in the engine rather than a bad number in a row. Rather + // than leave that trap in the table, the knob only reaches the good band. + float ang = 0.18 + 1.30 * saturate(gTune.x); + float ca = cos(ang), sa = sin(ang); + + // Opening the tunnel up moves the IFS fixed point outward with the wall, + // so the relief keeps hugging the surface instead of being left behind + // floating in the middle of the new, larger hall. + float3 off = float3(0.62 + 0.55 * op, 0.48 + 0.75 * op, 0.40); + + // A square tube down the current z at every level of the iteration, all + // unioned together. Measuring only the LAST iterate is the obvious way to + // write this and it is a trap: past about a radian of fold angle the + // rotations walk the attractor clean out of the slab we sample, the + // union collapses to nothing, and the shot becomes an empty pipe. Taking + // a primitive at each level instead guarantees material at every scale, + // so the fold angle is free to sweep its whole range without the + // structure ever disappearing. Each term is a valid estimate at its own + // scale, so the minimum of them is still a valid estimate. + // + // Square rather than round because at 640x360 a flat face that catches + // the light in one value beats a cylinder that ramps across four pixels. + // + // The strut swells with the solo. It is base * (1 + k * envelope), so the + // lattice thickens on a peak and never thins away to nothing. + float th = (0.45 + 0.25 * op) * (1.0 + 0.20 * saturate(gVoice.y)); + float d = 1e9; + float isc = 1.0; // 1 / (total scale so far) + + // The footprint the iteration is actually allowed to resolve: the pixel, + // or the shot's coarseness floor, whichever is blunter. + float lodI = max(lod, c.coarse); + + [loop] for (int i = 0; i < 5; i++) { + p = abs(p); + p.xy = float2(p.x * ca - p.y * sa, p.x * sa + p.y * ca); + p = p * s - off * (s - 1.0); + p.yz = float2(p.y * ca - p.z * sa, p.y * sa + p.z * ca); + isc *= is; + + // This level's strut half-width, back in world units. + float w = th * isc; + + // Never let a strut be thinner than the pixel it lands in. Below that + // width it stops being geometry and starts being a coin flip per + // frame, and the dither in the post pass turns a coin flip into + // crawling salt. Fattening it costs nothing - the max() was already + // there - and it is exactly the original expression when the strut is + // wider than a pixel, so near geometry is untouched. + float wf = max(w, lodI); + d = min(d, max(abs(p.x) * isc - wf, abs(p.y) * isc - wf)); + + // ...and once a level is finer than a pixel, nothing below it can be + // resolved either, because every subsequent level is smaller again. + // Stop. This is where the cost saving lives: at the far wall this + // runs twice, not five times. + if (w < lodI) break; + } + + // The rails. One square spar per sector, sitting a fixed gap outside the + // CURRENT throat radius - so they ride the breathing aperture rather than + // being swallowed by it at the wide phase - and running the length of the + // tunnel. Because the fold that produced `arc` is taken from the twisted + // coordinates, they are helices, and their pitch is the shot's. + // + // They cost five instructions and they do three jobs: they draw the + // symmetry order in one unmistakable read, they give the eye something + // with real speed on it near the camera where the lattice is just a blur, + // and they guarantee material in the frame for any combination of fold + // angle and scale. + float railR = bth + c.railGap; + float rw = max(c.railW, lod); + d = min(d, max(arc - rw, abs(r - railR) - rw)); + + // The iris plates. A bulkhead every few units with a hole punched in it + // just outside the rails, so the shot is a series of apertures you fall + // through rather than a smooth pipe. Their inner rims face the lamp and + // catch it, which puts a stack of concentric bright rings down the + // tunnel - a completely different silhouette from the bare configuration + // for eight instructions, on the shots that ask for it. + if (c.ringOn > 0.5) { + float zr = frac(q.z * c.ringISp + 0.5) * c.ringSp - c.ringSp * 0.5; + d = min(d, max(abs(zr) - max(c.ringTh, lod), + (railR + c.ringGap) - r)); + } + + // The pipe the relief is carved into. Anything past `wall` is solid, so + // the frame is never black holes with a few struts in them, and rays + // that would otherwise fly off into nothing terminate - which makes the + // march cheaper, not more expensive. + d = min(d, wall - r); + + // Hollow out the throat we fly down. Parts of the lattice that reach too + // far inward are simply cut off here, so the fold angle and the scale + // can be pushed hard without ever walling the camera in. + d = max(d, bth - r); + + return d; +} + +float map(float3 p, float lod) { return mapT(toTube(p), lod); } + +// Tetrahedron sampling: four map evaluations rather than the usual six. This +// is the most expensive map in the demo, and at 640x360 through a dithered +// ramp the slightly noisier normal is invisible while the two evaluations it +// saves are not. +// +// The offset grows with the pixel footprint. A fixed 0.0018 is right at two +// units and wrong at forty, where it samples inside one strut of a lattice +// finer than the pixel and returns a normal that changes completely between +// one frame and the next - which is a shimmer no amount of fogging hides. +// Sampling at the scale you can actually see is the whole trick. +float3 mapNormal(float3 p, float lod) +{ + float2 e = float2(1.0, -1.0) * max(0.0018, lod * 0.60); + float3 g = e.xyy * map(p + e.xyy, lod) + e.yyx * map(p + e.yyx, lod) + + e.yxy * map(p + e.yxy, lod) + e.xxx * map(p + e.xxx, lod); + // Safe normalize: four taps of a folded field can in principle cancel, + // and one NaN pixel here is a NaN the CRT pass then smears across a + // neighbourhood. Three instructions to never find out. + return g * rsqrt(max(dot(g, g), 1e-12)); +} + +// Cheap ambient occlusion: how much of the field crowds in along the normal. +// With one distant lamp doing all the lighting, this is what actually carves +// the relief - without it the wall reads as a smooth painted cylinder. +// +// The probe distances stretch with the footprint for the same reason the +// normal offset does: probing 5 cm into a field that has been coarsened to +// 20 cm measures nothing. The final scale divides the stretch back out so +// the amount of occlusion is the same at every depth. +float occlusion(float3 p, float3 n, float lod) +{ + float sca = 1.0 + lod * 6.0; + float o = 0.0, s = 1.0; + [loop] for (int i = 0; i < 5; i++) { + float d = (0.05 + 0.15 * float(i)) * sca; + o += (d - map(p + n * d, lod)) * s; + s *= 0.62; + } + return saturate(1.0 - (1.35 / sca) * o); +} + +// ---- shading ------------------------------------------------------------- + +float4 main(VSOut i) : SV_Target +{ + Cfg c = cfg(); + + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y is FLIPPED here on purpose. The fullscreen triangle emits + // uv.y = 0 at the top of the screen, so i.uv*2-1 gives -1 up + // there and the top of the frame ended up looking DOWNWARD - + // the cathedral vault springs at y=+12 and was rendering in the + // lower half, the monolith hung from the ceiling. + uv.x *= gTime.w; + + float3 fw = normalize(gDir.xyz); + float3 rt = normalize(cross(float3(0.0, 1.0, 0.0), fw)); + float3 up = cross(fw, rt); + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 ro = gCam.xyz; + float3 rd = normalize(fw + (uv.x * (rt * cr + up * sr) + + uv.y * (up * cr - rt * sr)) * gCam.w); + + float op = saturate(gTune.z); + + // Radius of this pixel's footprint per unit of depth. Uniform across the + // frame, so it costs one multiply and never a derivative. + float lodRate = gCam.w * PIXTAN; + + // The lamp, in tube space: on the axis, down the tunnel from wherever the + // camera is. It travels with the shot, so the bright core stays pinned to + // the vanishing point however the camera moves. + // + // Its distance grows as the tunnel opens up. A fixed distance is right for + // a tight bore and washes a wide one out to flat amber, because an open + // tunnel puts far more wall within a few units of the lamp; scaling it + // with the room keeps the lit core about the same size on screen whatever + // the shot does with tune.z. + float lampD = LAMP * (1.0 + 0.45 * op); + float3 tube0 = toTube(ro); + float3 lamp = float3(0.0, 0.0, tube0.z + lampD); + + // The ray, in tube space, as the secant across exactly the distance the + // lamp sits at. toTube is a translation that varies slowly with depth, so + // the tube-space ray is not quite the world ray, and over thirteen units + // the difference is enough to slide the bright core visibly off the hole + // it is supposed to be sitting in. + float3 trd = normalize(toTube(ro + rd * lampD) - tube0); + + float t = 0.05; + float glow = 0.0; + float3 q = tube0; + bool hit = false; + + // The march. 0.62 of a step rather than a full one, because a polar fold, + // a domain repeat and an IFS all overestimate distance near their fold + // planes, and at a full step they all show up as the same artifact: a + // hard bitten edge where a strut should be. + [loop] for (int k = 0; k < 56; k++) { + q = toTube(ro + rd * t); + + float lod = lodRate * t; + float h = mapT(q, lod); + + // Haze around the lamp, accumulated along the ray. This is what fills + // the throat with air, and it costs no extra map evaluations because + // the march is already here. It is the AIR, though, not the core - + // see the core term below for why that distinction is worth the + // handful of instructions it costs. + float dl = length(q - lamp); + glow += min(h, 0.7) * exp(-t * c.fogK) / (1.0 + dl * dl * 0.09); + + // Stop when the remaining distance is under half a pixel. Tying the + // threshold to the footprint rather than to a hand-tuned slope means + // it tracks the field of view: a wide shot spends fewer steps + // resolving something it is going to draw coarsely anyway. + if (h < lod * 0.45 + 0.0008) { hit = true; break; } + t += h * 0.62; + if (t > 80.0) break; + } + + float lodHit = lodRate * t; + + // Bands of colour sweeping up the tunnel toward the camera. This is the + // psychedelic half of the brief, and it is deliberately only two hues - + // the demo's warm amber and its phosphor green - because a full rainbow + // would fight both the palette and the narrow ramp the post pass dithers + // into. Banding it along z rather than by time means the colour is + // attached to the structure and arrives with it. + // + // The period is per shot, twenty to thirty-six units, which is one to + // three bands across the depth the fog leaves visible. At the 0.085 it + // used to be, the period was seventy-four units - longer than you can + // see - so there were never any bands, only one slowly changing wash, + // and the one thing the shot promised the eye it never delivered. + float band = sin(q.z * c.bandFrq + gTime.x * 0.7) * 0.5 + 0.5; + float3 hot = lerp(float3(1.00, 0.63, 0.26), + float3(0.36, 1.00, 0.56), + smoothstep(0.28, 0.80, band)); + float3 deep = float3(0.021, 0.027, 0.046); // near black, slightly blue + + // The air gets its hue from the lamp's depth, not from wherever this + // pixel's march happened to stop. Sampling the band at the end of the + // ray means two neighbouring pixels that terminated a step apart pick up + // different hues, and the throat - which is nothing BUT air - fills with + // colour noise that the dither then locks into a crawling pattern. One + // hue for the whole haze, drifting as the structure passes. + float bandA = sin(lamp.z * c.bandFrq + gTime.x * 0.7) * 0.5 + 0.5; + float3 hotA = lerp(float3(1.00, 0.63, 0.26), + float3(0.36, 1.00, 0.56), + smoothstep(0.28, 0.80, bandA)); + + float3 col = deep; + + if (hit) { + float3 p = ro + rd * t; + float3 n = mapNormal(p, lodHit); + + // Toward the lamp. Legal as a world direction only because toTube is + // a translation at every depth - see the note on it. + float3 lv = lamp - q; + float dl = length(lv); + float3 ld = lv / max(dl, 0.001); + + // The key. Falloff is gentler than inverse square on purpose: true + // inverse square over a 60 unit tunnel puts everything past the first + // few metres into the black, and then there is no relief left to look + // at, only a bright hole. The master peak lifts it - base * (1 + k), + // so the floor never drops out from under the picture. + float key = saturate(dot(n, ld)) * (8.5 / (1.0 + dl * dl * 0.032)) + * (1.0 + 0.30 * saturate(gSync.w)); + + // A dim fill from the axis itself, so the walls sliding past the + // camera are dark rather than absent. Without it the near field is a + // black frame around a bright hole and the sense of speed goes. + // At 0.40 over a 0.30 falloff it was worth about one extra unit of + // `deep` on the near wall, which is to say it was not doing its job; + // this reaches the far side of the open configuration as well. + float ra = length(q.xy); + float fil = saturate(dot(n, normalize(float3(-q.x, -q.y, 0.25)))) + * (0.55 / (1.0 + ra * ra * 0.22)); + + float ao = occlusion(p, n, lodHit); + float fre = pow(1.0 - saturate(dot(n, -rd)), 3.0); + + // The key is the warm-to-green lamp light; the fill is deliberately + // COLD. Lighting both with the same hue washes the entire frame one + // colour and the palette collapses - the walls have to fall back to + // the demo's blue-black for the amber core to read as warm at all. + col = hot * key * ao; + col += float3(0.30, 0.42, 0.68) * fil * ao; + col += deep * 1.6 * ao; + // The glass. Grazing angles pick up the light, which is what turns + // the open configuration from a dark lattice into something lit from + // inside. Mostly off when the tunnel is tight, because a tight tunnel + // wants to be a silhouette. + col += hot * fre * (0.18 + 1.15 * op) * (1.0 + 0.45 * saturate(gVoice.y)) * ao; + + // A hard specular, off the same lamp. The lattice is glass in the + // open configuration and stone in the tight one, and one exponent + // scaled by openness is the whole difference between those two + // materials. It lands on the flat faces the IFS is built out of, so + // it reads as facets catching the light rather than as a sheen. + float3 hv = normalize(ld - rd); + float spe = pow(saturate(dot(n, hv)), 26.0) * (0.20 + 1.30 * op); + col += hot * spe * (0.40 + 0.60 * ao); + } + + // Fog the surfaces back into the dark, then add the air on top: the glow + // IS the depth cue, so it must not be faded by the thing it is cueing. + float f = exp(-t * c.fogK); + col = col * f + deep * (1.0 - f); + col += hotA * glow * 0.13; + + // The lamp itself. The accumulated haze above cannot do this on its own: + // it is a sum over march steps, so its brightness depends on how many + // steps a pixel happened to take, and down the middle of a hollow bore + // that count changes by one between neighbouring pixels. The result is a + // core that is grey rather than blown out AND that flickers a step at a + // time. This term is closed form - the perpendicular distance from the + // ray to the lamp - so it is smooth across the frame and dead steady in + // time, and it is the white in "white core, amber relief, black walls". + // Same construction as the source down the nave in scene.hlsl. + { + float3 lw = lamp - tube0; + float tc = max(dot(lw, trd), 0.0); + float3 pv = lw - trd * tc; + float perp = length(pv); + float core = 1.0 / (1.0 + perp * perp * 2.2); + + // ...and it wears the tunnel's own symmetry. The core is the single + // brightest thing in the frame and it was a featureless dot in every + // one of the eight appearances; giving it the shot's fold count turns + // it into a four, six, eight, ten or twelve armed star that turns + // with the structure. It is a modulation of a small bright region + // about its own mean, not a gate on it, so nothing here flashes. + // The 1e-6 is not decoration. pv.xy is exactly zero for the pixel + // whose ray points straight down the barrel at the lamp, atan2 of + // (0,0) is not defined, and one NaN in the brightest pixel in the + // frame is a NaN the CRT pass then smears over a neighbourhood. + float pa = atan2(pv.y + 1e-6, pv.x + 1e-6) + gTime.x * c.spin; + float star = 1.0 + 0.45 * cos(pa * c.folds); + + // Squared so it is a small hard core rather than a broad grey wash, + // and scaled past 1.0 so the middle of it clips to the top of the + // post pass ramp instead of dithering somewhere in the mid greys. + col += hotA * core * core * 3.2 * star * (tc < t ? 1.0 : 0.30); + } + + return float4(col * gTime.z, 1.0); +} diff --git a/fx/logo.hlsl b/fx/logo.hlsl new file mode 100644 index 0000000..d606a19 --- /dev/null +++ b/fx/logo.hlsl @@ -0,0 +1,310 @@ +// SC_LOGO: one solid CRTKAFA sculpture, then a flight into its conduit. +// Scene layout: word centred at (0,0,0), width 11.6, height 2.2, +// depth 1.1. Tunnel axis +Z, inner radius approximately 8. +// gTime.x is absolute intro seconds, 0..7.25; no modulo / logo repeats. +// gTune = (twist adjustment, metal warmth, emission adjustment, camera mode). +// Recommended (0,0,0,0). w < .5 uses introCamera below; w >= .5 uses +// gCam.xyz/gDir verbatim. gCam.w remains tan(vertical FOV/2) in BOTH modes; +// recommend .48, with .48 fallback for an unset value. gTime.w = aspect. +// For CPU meshes/depth compositing, mirror introCamera on CPU or use w=1. +// All other Scene fields have the same layout as scene.hlsl. +cbuffer Scene : register(b0) +{ + float4 gTime; + float4 gCam; + float4 gDir; + float4 gTune; + float4 gSync; + float4 gVoice; +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +float segment(float2 p, float2 a, float2 b) +{ + float2 v = b - a; + return length(p - a - v * saturate(dot(p - a, v) / dot(v, v))); +} + +// Stroke centre lines, NOT a bitmap: closed contours have real open counters. +// Each glyph is subsequently extruded, including the counter walls. +float glyph(float2 p, int id) +{ + float d = 100.0; + if (id == 0) // C: chamfered bowl, right side visibly open. + { + d = segment(p, float2(.52,.84), float2(.25,1.0)); + d = min(d, segment(p, float2(.25,1), float2(-.35,1))); + d = min(d, segment(p, float2(-.35,1), float2(-.58,.72))); + d = min(d, segment(p, float2(-.58,.72), float2(-.58,-.72))); + d = min(d, segment(p, float2(-.58,-.72), float2(-.35,-1))); + d = min(d, segment(p, float2(-.35,-1), float2(.25,-1))); + d = min(d, segment(p, float2(.25,-1), float2(.52,-.84))); + } + else if (id == 1) // R: closed upper bowl AND a diagonal lower leg. + { + d = segment(p, float2(-.55,-1), float2(-.55,1)); + d = min(d, segment(p, float2(-.55,1), float2(.28,1))); + d = min(d, segment(p, float2(.28,1), float2(.55,.76))); + d = min(d, segment(p, float2(.55,.76), float2(.55,.30))); + d = min(d, segment(p, float2(.55,.30), float2(.28,.08))); + d = min(d, segment(p, float2(.28,.08), float2(-.55,.08))); + d = min(d, segment(p, float2(-.08,.08), float2(.60,-1))); + } + else if (id == 2) // T + { + d = segment(p, float2(-.60,1), float2(.60,1)); + d = min(d, segment(p, float2(0,1), float2(0,-1))); + } + else if (id == 3) // K: two diagonals meet the upright at its middle. + { + d = segment(p, float2(-.55,-1), float2(-.55,1)); + d = min(d, segment(p, float2(-.55,-.08), float2(.55,1))); + d = min(d, segment(p, float2(-.25,.23), float2(.60,-1))); + } + else if (id == 5) // F: no lower arm, unlike the R bowl. + { + d = segment(p, float2(-.55,-1), float2(-.55,1)); + d = min(d, segment(p, float2(-.55,1), float2(.58,1))); + d = min(d, segment(p, float2(-.55,.12), float2(.35,.12))); + } + else // A, at indices 4 and 6; triangular open counter. + { + d = segment(p, float2(-.62,-1), float2(0,1)); + d = min(d, segment(p, float2(0,1), float2(.62,-1))); + d = min(d, segment(p, float2(-.38,-.24), float2(.38,-.24))); + } + return d - .125; +} + +float box3(float3 p, float3 b) +{ + float3 q = abs(p) - b; + return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0); +} + +float word(float3 p) +{ + // Bounding box is a conservative step outside the glyph neighbourhood. + float bound = box3(p, float3(5.91,1.19,.57)); + if (bound > .65) return bound; + // Evaluate adjacent cells too: nearest-centre alone can skip a thinner + // glyph at cell boundaries. There is NO repetition in world Z. + int cell = (int)clamp(floor(p.x / 1.70 + 3.5),0.0,6.0); + float d = 100.0; + [loop] for (int j = -1; j <= 1; ++j) + { + int id = cell + j; + if (id < 0 || id > 6) continue; + float2 q = p.xy - float2((id - 3) * 1.70,0); + float2 e = float2(glyph(q,id),abs(p.z) - .49); + // Minkowski bevel gives lit edge curvature and .55 half thickness. + float solid = min(max(e.x,e.y),0.0) + length(max(e,0.0)) - .06; + d = min(d,solid); + } + return d; +} + +float2 conduit(float3 p) +{ + float radius = length(p.xy); + float twist = .115 + .035 * clamp(gTune.x,-1.0,1.0); + float a = twist * p.z + .10 * sin(gTime.x * .40); + float2 q = float2(cos(a)*p.x-sin(a)*p.y, + sin(a)*p.x+cos(a)*p.y); + // Four solid helical rails inside a continuous enclosing wall. + float rail = length(float2(abs(q.x)-7.65,q.y)) - .24; + rail = min(rail,length(float2(q.x,abs(q.y)-7.65))-.24); + // Twist metric bound for r <= 9; wall dominates outside that radius. + rail *= .57; + float wall = 8.55 - radius; + float ringZ = (frac((p.z-2.5)/5.0+.5)-.5)*5.0; + float ring = length(float2(radius-8.08,ringZ))-.30; + float d = min(wall,ring); + float material = 2.0; + if (rail < d) { d = rail; material = 3.0; } + return float2(d,material); +} + +/* Small, diegetic title stamped into the lower lip of the conduit. This is + intentionally a bitmap-sized mark rather than another giant overlay: the + opening names itself once, then the journey owns the frame. */ +static const uint titleRows[56] = { + 14,17,16,16,16,17,14, 30,17,17,30,20,18,17, + 4,10,17,17,31,17,17, 17,18,20,24,20,18,17, + 31,4,4,4,4,4,4, 14,17,17,17,17,17,14, + 10,10,31,10,31,10,10, 4,12,4,4,4,4,14 +}; +uint tinyRow(int id, int y) { return titleRows[clamp(id,0,7)*7+clamp(y,0,6)]; } + +float tinyTitle(float2 p) +{ + // A finite luminous label on the z=-.65 face. Rows go DOWN the page. + float2 q = float2(p.x + 3.465, -1.55-p.y) / .105; + if (q.x < 0 || q.y < 0 || q.x >= 66 || q.y >= 7) return 0; + int ci = (int)floor(q.x / 6); + int co = (int)floor(q.x) - ci*6; + int ro = clamp((int)floor(q.y),0,6); + if (ci == 8 || co >= 5) return 0; + int id = ci == 0 ? 0 : ci == 1 ? 1 : ci == 2 ? 2 : + ci == 3 ? 0 : ci == 4 ? 3 : ci == 5 ? 4 : + ci == 6 ? 1 : ci == 7 ? 5 : ci == 9 ? 6 : 7; + uint bits = tinyRow(id,ro); + return float((bits >> (4-co)) & 1u); +} + +float2 scene(float3 p) +{ + float2 h = conduit(p); + float w = word(p); + if (w < h.x) h = float2(w,1.0); + return h; +} + +float3 normalAt(float3 p, float eps) +{ + float2 e = float2(1,-1)*eps; + float3 n = e.xyy*scene(p+e.xyy).x + e.yyx*scene(p+e.yyx).x + + e.yxy*scene(p+e.yxy).x + e.xxx*scene(p+e.xxx).x; + return n * rsqrt(max(dot(n,n),1e-12)); +} + +float ambientOcclusion(float3 p, float3 n) +{ + float occ = 0.0; + float weight = 1.0; + [loop] for (int k=0; k<4; ++k) + { + float r = .09 + .18*k; + occ += max(r-scene(p+n*r).x,0.0)*weight; + weight *= .55; + } + return saturate(1.0-1.5*occ); +} + +float shadow(float3 p, float3 l) +{ + float visibility = 1.0; + float t = .035; + [loop] for (int k=0; k<16; ++k) + { + float h = scene(p+l*t).x; + visibility = min(visibility,12.0*max(h,0.0)/t); + if (h < .002 || t > 7.0) break; + t += clamp(h,.025,.85); + } + return saturate(visibility); +} + +void introCamera(out float3 ro, out float3 fw, out float roll) +{ + // Read for 1.5 seconds, then pass the word before the 3.63s cut. + float t = min(gTime.x,1.5)*.60 + max(gTime.x-1.5,0.0)*3.0; + float rush = max(t-2.0,0.0); + float lift = smoothstep(1.9,3.35,t); + float settle = smoothstep(4.3,6.4,t); + ro = float3(1.5*cos(t*.60)*(1.0-settle), + .35 + 2.45*lift*(1.0-settle), + -14.0+1.1*t+2.4*rush*rush); + // Aim at word first, then turn forward BEFORE crossing its plane. + float3 aim = float3(0,0,0)-ro; + float3 readDir = normalize(float3(aim.xy,max(aim.z,3.0))); + fw = normalize(lerp(readDir,float3(0,0,1),smoothstep(2.4,3.65,t))); + roll = .025*sin(t*.9) + .12*settle; +} + +float4 main(VSOut input, out float depth : SV_Depth) : SV_Target +{ + float3 ro, fw; + float roll; + introCamera(ro,fw,roll); + if (gTune.w >= .5) + { + ro = gCam.xyz; + fw = gDir.xyz * rsqrt(max(dot(gDir.xyz,gDir.xyz),1e-10)); + if (dot(fw,fw) < .5) fw = float3(0,0,1); + roll = gDir.w; + } + float3 upHint = abs(fw.y) > .98 ? float3(0,0,1) : float3(0,1,0); + float3 rt = normalize(cross(upHint,fw)); + float3 up = cross(fw,rt); + float2 uv = float2(input.uv.x*2-1,1-input.uv.y*2); + uv.x *= max(gTime.w,.1); + float fov = max(gCam.w > .01 ? gCam.w : .48, .55/max(gTime.w,.25)); + float3 rd = normalize(fw + fov*(uv.x*(rt*cos(roll)+up*sin(roll)) + +uv.y*(up*cos(roll)-rt*sin(roll)))); + float t = .05; + float material = 0.0; + bool hit = false; + depth = 0.0; + [loop] for (int k=0; k<112; ++k) + { + float2 h = scene(ro+rd*t); + if (h.x < max(.0015,t*.00055)) + { + hit = true; + material = h.y; + break; + } + t += max(h.x*.82,.001); + if (t > 145.0) break; + } + float3 deep = float3(.009,.017,.026); + float3 col = deep; + if (hit) + { + float3 p = ro+rd*t; + depth = saturate(.05/max(t*dot(rd,fw),.001)); + float3 n = normalAt(p,max(.002,t*.00022)); + float ao = ambientOcclusion(p,n); + float3 key = normalize(float3(-.45,.65,-.75)); + float diffuse = saturate(dot(n,key)); + float shade = shadow(p+n*.018,key); + float fresnel = pow(1.0-saturate(dot(n,-rd)),3.0); + float spec = pow(saturate(dot(n,normalize(key-rd))),48.0); + float3 phosphor = lerp(float3(.35,.95,.69),float3(1,.65,.29), + saturate(.20+gTune.y)); + float pulse = 1.0+.12*saturate(gSync.x)+.07*saturate(gVoice.z); + if (material < 1.5) + { + // Front and side are different alloys; bevel joins them visibly. + float front = smoothstep(.65,.97,abs(n.z)); + float3 metal = lerp(float3(.12,.23,.26),float3(.65,.77,.71),front); + col = metal*(.15*ao+1.05*diffuse*shade); + col += float3(.65,.79,1)*spec*.75*shade; + col += phosphor*fresnel*.38*ao; + col += phosphor*front*(.08+.09*saturate(gTune.z))*pulse; + col += float3(.16,.28,.42)*saturate(dot(n,normalize(float3(1,-.1,.5))))*.32*ao; + } + else + { + float3 lamp = float3(0,0,ro.z+16.0)-p; + float lampDist = length(lamp); + float lit = saturate(dot(n,lamp/max(lampDist,.001))); + float band = .5+.5*sin(p.z*.19); + float3 tint = lerp(float3(.21,.57,.48),float3(.61,.36,.16),band); + col = tint*(.12+lit*2.2/(1.0+lampDist*lampDist*.008))*ao; + col += float3(.20,.30,.42)*diffuse*.18; + col += tint*fresnel*.18*ao; + if (material > 2.5) + col += phosphor*(.36+.18*saturate(gTune.z))*pulse; + } + col = lerp(deep,col,exp(-t*.010)); + } + // Ray/plane hit is bounded in X, Y AND depth. No infinite extrusion. + if (rd.z > .001 && ro.z < -.65) { + float labelT = (-.65-ro.z)/rd.z; + float2 xy = (ro+rd*labelT).xy; + if (labelT > .05 && (!hit || labelT < t) && + abs(xy.x) < 3.7 && xy.y < -1.37 && xy.y > -2.48) { + float ink = tinyTitle(xy); + col = lerp(float3(.018,.038,.047),float3(.48,.91,.79),ink); + depth = saturate(.05/(labelT*dot(rd,fw))); + } + } + return float4(max(col,0.0)*saturate(gTime.z),1.0); +} diff --git a/fx/mesh.hlsl b/fx/mesh.hlsl new file mode 100644 index 0000000..ad1bec5 --- /dev/null +++ b/fx/mesh.hlsl @@ -0,0 +1,294 @@ +// Meshes, pulled rather than fed. +// +// Nothing is bound to the input assembler - no vertex buffer, no index +// buffer, no input layout. The vertex shader reads straight out of two typed +// buffers laid over the `const short` arrays in cats_mesh.h, and the draw is +// Draw(NT*3, 0) with the index fetched here. +// +// That is not stylistic. Three things fall out of it: +// +// There is no DXGI_FORMAT_R16G16B16_SINT, so an input layout would have to +// pad the 6-byte vertices to 8 - about 19 KB on the run cat alone, in an +// executable where the shader blobs are already the biggest thing. +// +// A typed R16_SINT buffer has a 2-byte stride, so the baked arrays upload +// verbatim. pSysMem points at the header array: no copy, no allocation, +// nothing a C runtime would have to be present for. +// +// Drawing non-indexed means the triangle id is just vid/3, so a per-triangle +// flat colour needs no vertex duplication, and the barycentric is +// float3(vid%3 == 0, 1, 2) - which is a wireframe-to-solid morph for free. +// +// The cost is one dependent load per vertex on a 790-triangle mesh. +// +// --------------------------------------------------------------------------- +// W H Y T H E R E I S A R I G D O W N T H E R E +// +// The 24 baked frames are the whole of the cat's motion, and the rate they +// play at is not free: actRate in the shot table is tuned so a stride covers +// about two body lengths of the from -> to travel, because anything else makes +// the feet skate. Run the numbers on bar 35 - 10.33 units of travel over 32 +// rows at 0.63 frames a row - and the cat gets through FOUR FIFTHS of one +// stride in the entire shot. Bar 59 gets through half of one. That is the +// honest reason the cat reads as furniture: not that the animation is bad, but +// that at a skate-free rate there is barely any of it inside a two-bar shot. +// +// The rate cannot go up without the feet sliding. So everything below is +// SECONDARY motion - the parts of an animal that are not carrying its weight +// and are therefore free to move as fast as they like. A tail that lashes, a +// head that turns, ears that flick, a spine that flexes, a ribcage that +// breathes, and a body that sinks into its knees when the kick lands. The legs +// are deliberately left alone: they are doing the one job the baked frames +// already do well, and anything added to a planted paw is a skate. +// +// The rig weights are plain thresholds in the mesh's own baked integer space, +// which is why fetchVert no longer applies gAnim.z - the quantiser scale is +// applied once, at the end, and until then a number in this file can be read +// straight against cats_mesh.h. Measured off the array: the cat faces +X, the +// animation is entirely in XY (z is constant per vertex to within a unit or +// two, which is what a sagittal run cycle looks like), the tail runs from +// x=-9800 out to x=-15200, the paws bottom out around y=-6400, and the head is +// everything past x=+6600. +// +// THE THRESHOLDS CANNOT BE TAKEN OFF THE ANIMATED POSE. A paw swings 8500 +// units of x during a stride - a front paw reaches x=+8800, which is deeper +// into head territory than the head is, and a rear paw reaches x=-11600, which +// is halfway down the tail. Segment on that and the rig grabs a foot. So the +// weights are read off `rf`: the midpoint of the current frame and the one +// half a cycle away. A pair of legs is in antiphase, so the midpoint very +// nearly cancels the swing - it drops the worst per-vertex x wander over the +// cycle from 15387 units to 3169, and the median from 2643 to 599 - and what +// is left is a bind pose in all but name. Three extra loads, and after it the +// paws never once cross a threshold: they stay inside x = [-9740, +6515] while +// the tail sits below -9770 and the head above +6680. +// +// Four appearances, all ACT_CAT_RUN in the corridor, and gTune.z happens to +// track the energy of each one exactly: 0.20 at bar 59 (the sparse solo +// entry, the slowest gait in the demo), 0.30 at bar 35, 0.50 at bar 43, 0.60 +// at bar 77 (the tension phase, the fastest). So gTune.z is read here as +// AGITATION and it drives every amplitude in the rig. The bar 59 cat prowls; +// the bar 77 cat is coming apart. That is one animal with four moods rather +// than the same silhouette pasted in four times. +// --------------------------------------------------------------------------- + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look direction, w roll + float4 gTune; + float4 gSync; // x low, y mid, z high, w master peak + float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +cbuffer Mesh : register(b1) +{ + float4 gModel; // xyz world position, w uniform scale + float4 gAnim; // x fractional frame, y frame count (1 = rigid), z quant scale, w yaw + float4 gMisc; // x vertices per frame, y wireframe 0..1, z shatter, w tint +}; + +Buffer gPos : register(t0); +Buffer gIdx : register(t1); + +struct VSOut +{ + float4 pos : SV_Position; + float3 wp : TEXCOORD0; // world position, for the face normal + float3 bary : TEXCOORD1; + float vz : TEXCOORD2; // view depth, for the fog +}; + +// Raw baked units. gAnim.z is deliberately NOT applied here - see above. +float3 fetchVert(uint vtx, uint frame) +{ + uint o = (frame * (uint)gMisc.x + vtx) * 3u; + return float3(gPos.Load((int)o), gPos.Load((int)(o + 1u)), + gPos.Load((int)(o + 2u))); +} + +VSOut main(uint vid : SV_VertexID) +{ + VSOut o; + uint vtx = gIdx.Load((int)vid); + + // The baked run cycle is 24 frames of plain vertex positions, and the + // frame index is FRACTIONAL because it comes from the song's row clock + // rather than a wall clock - the old demo stepped them against wall-clock + // time and the cat never once landed on the beat. + // + // It used to lerp two frames. At the skate-free rate the cat is only + // getting about 5.6 frames a second, so a straight line between them holds + // each limb at a constant velocity for 180 ms and then turns a corner, and + // the corner is visible - the legs tick. Catmull-Rom over four frames + // costs six more loads on a 396-vertex mesh and makes the velocity + // continuous, which is the difference between a flipbook and an arc. The + // cycle loops, so the neighbours wrap; at frac == 0 it returns frame f1 + // exactly, which is what ACT_CAT_STILL depends on. + uint nf = max((uint)gAnim.y, 1u); + float3 p; + if (nf == 1u) + { + // Static props have their own proportions: no cat rig or interpolation. + p = fetchVert(vtx, 0u) * gAnim.z; + } + else + { + uint f1 = (uint)gAnim.x % nf; + uint f0 = (f1 + nf - 1u) % nf; + uint f2 = (f1 + 1u) % nf; + uint f3 = (f1 + 2u) % nf; + float t = frac(gAnim.x); + + float3 P0 = fetchVert(vtx, f0), P1 = fetchVert(vtx, f1); + float3 P2 = fetchVert(vtx, f2), P3 = fetchVert(vtx, f3); + float3 g = 0.5 * (2.0 * P1 + + (P2 - P0) * t + + (2.0 * P0 - 5.0 * P1 + 4.0 * P2 - P3) * t * t + + (3.0 * (P1 - P2) + P3 - P0) * t * t * t); + + // The stand-in bind pose: this frame averaged with its opposite. Nothing + // below reads a threshold off `g`, only off `rf`. nf>>1 is 0 for a + // single-frame mesh, which degenerates to the frame itself, correctly. + float3 rf = 0.5 * (P1 + fetchVert(vtx, (f1 + (nf >> 1)) % nf)); + + // ---- the rig ------------------------------------------------------- + // ag agitation, the one number that separates the four appearances + // sd a phase seed, so two cats never start a sway on the same beat + // st seconds into THIS shot, for the drifts that must not be periodic + // gp gait phase, 2*pi per stride, off the row clock and so beat-locked + // dr whichever of the two loud voices is actually playing + float ag = saturate(gTune.z * 1.6); + float sd = gTune.x * 31.0 + gTune.y * 17.0 + gTune.z * 57.0; + float st = gTime.x; + float gp = 6.2831853 * gAnim.x / (float)nf; + float dr = gVoice.x * 0.45 + gVoice.y * 0.55; + + // The three weights the whole rig is built out of, all in bind space. + // tail past the root of it, and thin with it - a tail stays inside + // |z| = 788 where a hind leg reaches 1578, so the second gate + // holds even where the first one is tight + // head starts at 6550, which is 35 units clear of the furthest a paw + // ever reaches, so the head rotation can never catch a foot + // torso how far above the feet: 0 at every paw (they top out at -1510), + // 1 through the spine. This is what keeps the legs out of the + // weave and turns the body's sink into knee compression. + float tail = (1.0 - smoothstep(-14800.0, -9755.0, rf.x)) + * (1.0 - smoothstep(900.0, 1400.0, abs(rf.z))); + float head = smoothstep(6550.0, 9200.0, rf.x); + float torso = smoothstep(-1400.0, 900.0, rf.y); + + float3 d = float3(0.0, 0.0, 0.0); + + // TAIL. A travelling wave with the phase lagging further the further out + // along it you go, which is what makes a tail read as a whip rather than a + // rod, and the lag itself opens up with agitation. Amplitude is + // base + k*voice, never base*voice: a silent bar still has a moving tail. + float lag = tail * (2.2 + 1.7 * ag); + float tp = gp * 2.0 + sd - lag; + float ta = 320.0 + 820.0 * ag + 420.0 * dr; + d.z += tail * ta * sin(tp); + d.y += tail * ta * 0.40 * sin(tp * 0.5 + 1.1); + d.y += tail * (200.0 + 450.0 * ag) * sin(st * 0.55 + sd); + + // SPINE. A lateral S about one wavelength along the body, plus an arch + // that peaks between the two leg pairs at x = -1750 - the mid-back is the + // part of a running cat that actually bends. + d.z += torso * (110.0 + 320.0 * ag) * sin(rf.x * 0.00022 + gp * 2.0 + sd); + float mid = 1.0 - saturate(abs(rf.x + 1750.0) / 9000.0); + d.y += torso * mid * (150.0 + 470.0 * ag) * sin(gp * 2.0 + 0.7); + + // The kick lands in the knees. The body sinks and the paws do not, because + // `torso` is zero down there, so the legs compress instead of the cat + // teleporting. base + k*s, so muting the drums leaves a deliberate stance. + d.y -= torso * (0.35 + 0.65 * ag) * (90.0 + 520.0 * gSync.x); + + // BREATHING. Ribcage only - not the head, not the tail, not the legs. It + // is small on purpose: this is the thing that stops a held frame from + // being a still, and it should reward a second look rather than announce + // itself. Proportional to the vertex's own offset, so it is a scale. + float rib = torso * (1.0 - head) * (1.0 - tail) + * saturate(1.0 - abs(rf.x + 1000.0) / 8000.0); + float br = sin(st * 1.7 + sd * 0.3); + d.z += rib * g.z * (0.030 + 0.045 * gVoice.z) * br; + d.y += rib * g.y * (0.020 + 0.030 * gVoice.z) * br; + + // HEAD. Yawed and pitched about the base of the neck, on two slow + // incommensurable periods so it never repeats inside a shot, and lifted by + // the guitar. The rotation is applied to the live pose but blended in by a + // bind-space weight, so the shoulders carry the shear and nothing else in + // the mesh can wander into it. + float hyaw = (0.05 + 0.15 * ag) * sin(st * 0.83 + sd) + + 0.04 * sin(st * 2.70 + sd * 1.7); + float hpit = (0.04 + 0.10 * ag) * sin(st * 0.61 + sd * 0.5) + + 0.07 * gVoice.x; + float3 hv = g - float3(6550.0, 500.0, 0.0); + float cy = cos(hyaw), sy = sin(hyaw); + float3 hr = float3(hv.x * cy - hv.z * sy, hv.y, hv.x * sy + hv.z * cy); + float cp = cos(hpit), sp = sin(hpit); + hr = float3(hr.x * cp - hr.y * sp, hr.x * sp + hr.y * cp, hr.z); + d += (hr - hv) * head; + + // EARS. The top of the head only, each one on its own phase, flicking on + // the hat. Tiny in world units - the ears are tiny - but they are the + // outermost points of the silhouette and the rim light finds them. + float ear = head * smoothstep(1900.0, 3100.0, rf.y); + float side = rf.z >= 0.0 ? 1.0 : -1.0; + float fk = sin(gp * 6.0 + sd + side * 1.9); + d.z += ear * side * (60.0 + 400.0 * gVoice.w) * fk; + d.y -= ear * (30.0 + 180.0 * gVoice.w) * (0.5 + 0.5 * fk); + + // WEAVE. The whole body yaws a few degrees as it runs, pinned at the + // paws again, so head and tail swing opposite ways. This is the one that + // makes it look like it has mass. + float wv = (0.015 + 0.045 * ag) * sin(gp + sd * 0.7) + + 0.020 * sin(st * 0.47 + sd); + float cw = cos(wv), sw = sin(wv); + d += (float3(g.x * cw - g.z * sw, g.y, g.x * sw + g.z * cw) - g) * torso; + + // Out of the baked integer space, once. + p = (g + d) * gAnim.z; + } + + // Shatter: push each triangle out along its own pseudo-random axis. The + // seed is the triangle id, so a face stays whole while it flies. + if (nf > 1u && gMisc.z > 0.001) + { + uint tri = vid / 3u; + float s = frac(sin(float(tri) * 12.9898) * 43758.5453); + float s2 = frac(sin(float(tri) * 78.2330) * 24634.6345); + float s3 = frac(sin(float(tri) * 39.4260) * 13875.9284); + p += (float3(s, s2, s3) - 0.5) * gMisc.z * gMisc.z * 14.0; + } + + float ca = cos(gAnim.w), sa = sin(gAnim.w); + p = float3(p.x * ca - p.z * sa, p.y, p.x * sa + p.z * ca); + float3 wp = p * gModel.w + gModel.xyz; + + // The camera basis, built the same way every scene shader builds it, so + // a mesh and the distance field behind it agree about where things are. + float3 fw = normalize(gDir.xyz); + float3 rt = normalize(cross(float3(0.0, 1.0, 0.0), fw)); + float3 up = cross(fw, rt); + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 r2 = rt * cr + up * sr; + float3 u2 = up * cr - rt * sr; + + float3 dv = wp - gCam.xyz; + float z = dot(dv, fw); + + // Clip space by hand. The scene shaders cast normalize(fw + (uv.x*r2 + + // uv.y*u2) * gCam.w), so a point lands at uv = (x, y) / (z * gCam.w) - + // and w = z with 0.05 in the z slot reproduces the SDF passes' own + // saturate(0.05 / vz), which is what puts them on the same depth scale. + // Keep signed w: hardware clips at z = 0.05 and rejects geometry behind + // the eye instead of projecting it onto a tiny positive denominator. + o.pos = float4(dot(dv, r2) / (gCam.w * gTime.w), + dot(dv, u2) / gCam.w, + 0.05, z); + o.wp = wp; + o.vz = z; + uint c = vid % 3u; + o.bary = float3(c == 0u ? 1.0 : 0.0, c == 1u ? 1.0 : 0.0, c == 2u ? 1.0 : 0.0); + return o; +} diff --git a/fx/meshps.hlsl b/fx/meshps.hlsl new file mode 100644 index 0000000..79cebc5 --- /dev/null +++ b/fx/meshps.hlsl @@ -0,0 +1,283 @@ +// The pixel half of the mesh path. +// +// No baked normals are read. The face normal comes from the screen-space +// derivatives of the world position, which is exact for a flat triangle and +// costs two instructions - and it means the 900-odd bytes of baked normals in +// the mesh headers never have to be uploaded at all. +// +// The lighting deliberately matches what the distance-field scenes do: one +// warm key, a cool fill from above, and the same exponential fog. A mesh that +// is lit by different rules than the space it is standing in reads as a +// sticker on the frame, however good it looks on its own. +// +// --------------------------------------------------------------------------- +// W H Y T H I S F I L E S H A D E S I N B O D Y S P A C E +// +// Every appearance of the cat is a broadside pass across a corridor. Yaw is 0 +// in all four shots, the mesh's long axis is world X, and the camera sits near +// the middle of the corridor looking down Z - so what reaches the screen is +// the animal's FLANK, nearly face on. A flat-shaded flank is one continuous +// surface whose face normal barely moves across it, so ANYTHING BUILT OUT OF N +// IS VERY NEARLY A CONSTANT over the two thousand-odd pixels that matter. That +// is the exact mechanism by which a cat becomes a black lozenge with a green +// edge on it, and no amount of relighting fixes it. +// +// It is also physically true rather than a rendering accident: the only lights +// in a corridor are the ceiling tubes and the two side walls, and all three +// are edge-on to a vertical flank. A real cat crossing a real corridor really +// is nearly unlit on the side we can see. So the form cannot be lit into +// existence - it has to be DRAWN, out of where a point sits in the animal. +// +// Which meant measuring the animal. The 24 baked frames were dumped and +// rasterised offline; the cycle turned out to be in place (mean X moves 0.118 +// over the whole cycle), so a body-space constant stays put on the body. The +// landmarks below are the same ones the rig in mesh.hlsl weights against - +// same numbers, converted out of baked integer space - so the two halves of +// the mesh path cannot drift apart about where the cat's parts are: +// +// x -0.96 ....................................... +0.77 (mesh units) +// |<-- tail, -0.89 to -0.56 -->| +// haunch -0.44 waist -0.12 shoulder +0.30 +// head, +0.39 to +0.59 and beyond +// y -0.41 paws/floor ... 0 spine ... +0.24 ear roots ... +0.33 arch +// z +-0.15 flanks +// +// P E R A P P E A R A N C E. The cat runs four times, always inside +// SC_CORRIDOR, so gTune is the corridor's own knob set and the honest thing is +// to obey it rather than invent a second, disagreeing one: +// +// gTune.x corridor width -> how much the two side walls bounce back on it +// gTune.y ceiling height -> how high the fitting that lights it hangs +// gTune.z bend -> the coat: band count, contrast, edge width +// +// gTune.z is 0.30 / 0.50 / 0.20 / 0.60 across the four, which is the widest +// spread on offer, and mesh.hlsl already reads it as the animal's AGITATION. +// This file reads the same axis as how GRAPHIC the animal is: four wide soft +// bands and a thin hard edge at the calm end, ten fine bands and a fat edge at +// the frantic one. One quantity, two halves of the same idea, so the bar 59 +// cat prowls and looks plain and the bar 77 cat is coming apart and looks it. +// +// Legibility is NOT keyed off gModel.w. It is keyed off fwidth of the body +// coordinate - how many mesh units one pixel is worth on this draw - so a cat +// that is small because it is far away reads the same as one that is small +// because it was scaled down, and neither can be got wrong by the shot table. +// +// Measured against the shader this replaces, on the bar 35 pass at 1080p: +// mean level 0.172 -> 0.182 (the exposure is meant to be unchanged), but the +// standard deviation across the animal 0.087 -> 0.118, and the mean pairwise +// gap between the four appearances 17.97 -> 21.90. Wider tonal range on one +// cat, and four cats that no longer look like the same picture. +// --------------------------------------------------------------------------- + +cbuffer Scene : register(b0) +{ + float4 gTime; + float4 gCam; + float4 gDir; + float4 gTune; + float4 gSync; + float4 gVoice; +}; + +cbuffer Mesh : register(b1) +{ + float4 gModel; + float4 gAnim; + float4 gMisc; // x verts/frame, y wireframe 0..1, z shatter, w tint +}; + +struct VSOut +{ + float4 pos : SV_Position; + float3 wp : TEXCOORD0; + float3 bary : TEXCOORD1; + float vz : TEXCOORD2; +}; + +// Original per-triangle RGB, expanded to RGBA8_UNORM once at upload. +// Only single-frame models use this resource; the run cat keeps its coat. +Buffer gFaceColor : register(t2); + +// One fitting every four metres, the same spacing corridor.hlsl uses. The two +// files have to agree about this or the cat lights up in the dark half of a +// bay and stops belonging to the room it is running through. +#define FIX_PERIOD 4.0 + +float bump(float x, float c, float k) { float d = (x - c) * k; return exp(-d * d); } + +float4 main(VSOut i, uint tri : SV_PrimitiveID) : SV_Target +{ + float3 n = normalize(cross(ddx(i.wp), ddy(i.wp))); + + // Face the light rather than the camera: a flat-shaded mesh whose normals + // point away just goes black, and a demo prop is never seen from behind. + float3 vdir = normalize(gCam.xyz - i.wp); + if (dot(n, vdir) < 0.0) n = -n; + + if (gAnim.y == 1.0) + { + float3 base = gFaceColor.Load((int)tri).rgb; + float key = saturate(dot(n, normalize(float3(-0.35, 0.80, -0.45)))); + float fill = saturate(dot(n, normalize(float3(0.65, 0.30, 0.55)))); + // Neutral light preserves the blue paint, pale windows and dark trim. + // Ambient keeps a distant face readable even when the key is behind it. + float3 col = base * (0.60 + 0.55 * key + 0.15 * fill); + float fog = exp(-max(i.vz, 0.0) * 0.0075); + col = col * fog + float3(0.055, 0.050, 0.062) * (1.0 - fog); + return float4(col * gTime.z, 1.0); + } + + // ---- body space ------------------------------------------------------ + // Undo the vertex shader exactly: translate, uniform scale, yaw. The yaw + // is 0 in every shot the cat appears in, but doing it properly costs one + // sine pair on two thousand pixels and means none of the landmarks below + // can come adrift if the C side ever turns the animal. + float iw = 1.0 / max(gModel.w, 1e-4); + float3 rp = (i.wp - gModel.xyz) * iw; + float cy = cos(gAnim.w), sy = sin(gAnim.w); + float3 op = float3(rp.x * cy + rp.z * sy, rp.y, rp.z * cy - rp.x * sy); + float3 on = float3( n.x * cy + n.z * sy, n.y, n.z * cy - n.x * sy); + + // One pixel, measured in mesh units. Every fine feature below is floored + // at this, so nothing crawls on the near cat or vanishes on the far one. + float pw = max(max(fwidth(op.x), fwidth(op.y)), fwidth(op.z)); + + // The same three weights the rig in mesh.hlsl deforms by, so the shading + // and the animation agree about which end of the cat is which. + float head = smoothstep( 0.3875, 0.5875, op.x); // 0 shoulders .. 1 head + float tail = 1.0 - smoothstep(-0.89, -0.56, op.x);// 0 hip .. 1 tail tip + float hgt = saturate((op.y + 0.41) * 1.35); // 0 paws .. 1 crown + + // ---- the light the corridor actually has ----------------------------- + float ceilH = lerp(2.30, 4.60, saturate(gTune.y)); + float cn = floor(i.wp.z / FIX_PERIOD + 0.5); + float cz = cn * FIX_PERIOD; + float dz = max(abs(i.wp.z - cz) - 0.60, 0.0); // metres to the tube + float3 lp = float3(0.0, ceilH - 0.07, + clamp(i.wp.z, cz - 0.60, cz + 0.60)); + float3 ld = normalize(lp - i.wp); + + // Two metres is the darkest a bay gets, and a quarter is as far as the + // level is allowed to move: base + k*s and never base*s, so the animal + // dims between fittings instead of blinking out. + float bay = 1.0 - 0.24 * saturate(dz * dz * 0.52); + + // The demo's standing key, tilted toward whichever fitting is currently + // overhead. The tilt is most of the animation of a held shot: as the cat + // crosses a bay the highlight walks along it and the head turns into and + // out of the light, so two bars on one camera stop being a still. + float3 kd = normalize(lerp(normalize(float3(-0.35, 0.80, -0.45)), ld, 0.24)); + float dif = saturate(dot(n, kd)); + + // Both walls, not the nearer one: picking the nearer one puts a seam down + // the middle of the corridor that the cat pops across at x = 0. A narrow + // corridor throws a great deal back onto the flanks, a wide one almost + // nothing, which is one more thing that separates the four passes. + float hw = lerp(1.15, 3.80, saturate(gTune.x)); + float3 wl = float3(-hw, 1.10, i.wp.z) - i.wp; + float3 wr = float3( hw, 1.10, i.wp.z) - i.wp; + float wl2 = max(dot(wl, wl), 0.30), wr2 = max(dot(wr, wr), 0.30); + // Capped, not for the corridor - the widest it gets there is 0.83 - but + // for the mesh-dump path in gfx.c, which draws the cat at the origin with + // gTune all zeroes and would otherwise stand it inside both walls at once. + float bnc = min(saturate(dot(n, wl * rsqrt(wl2))) * (2.4 / (1.4 + wl2)) + + saturate(dot(n, wr * rsqrt(wr2))) * (2.4 / (1.4 + wr2)), 1.0); + + // ---- the shape, drawn rather than lit -------------------------------- + // Three masses and the gaps between them, on the landmarks measured off + // the mesh: the shoulder over the front legs, the haunch over the rear + // ones, the waist that makes those two read as two, and the head - so the + // front of the animal is brighter than the back and the silhouette has a + // direction even when it is sixty pixels long. + float mass = 1.0 + + 0.28 * bump(op.x, 0.30, 4.0) // shoulder, over the forelegs + + 0.24 * bump(op.x, -0.44, 3.6) // haunch, over the hind legs + + 0.20 * bump(op.x, 0.60, 5.0) // head + - 0.17 * bump(op.x, -0.12, 5.2); // the tuck between them + + // Back lit, belly not. On a flank that shares one normal this ramp is the + // whole difference between a cat and a stain. + float form = (0.55 + 0.85 * pow(max(hgt, 1e-4), 0.70)) + * mass * (1.0 - 0.22 * tail); + + // ...but only where the normal is not already doing the job. An up-facing + // back under an overhead tube is genuinely lit and does not want drawing + // on top of; the flank is not lit at all and is nothing else. Without this + // line the two cues stack and the spine blows out to a white stripe. + form = lerp(1.0, form, 1.0 - 0.72 * dif); + + // Tabby banding, across the body the way a real one runs. Resolved rather + // than faded by hand: the amplitude dies the moment a band is thinner than + // a pixel, so the far cat is plain instead of a field of crawling noise. + float bands = lerp(4.5, 10.5, saturate(gTune.z)); + float ph = (op.x + 0.96) * bands + op.y * 1.25; + float coat = sin(ph * 6.2831853) * saturate(1.0 - fwidth(ph) * 1.8) + * (0.55 + 0.45 * saturate(gTune.z)) * (1.0 + 0.30 * gSync.z); + + // The gait, off the same fractional frame the vertex shader poses with, so + // the ripple down the coat lands on the footfalls and not on a wall clock. + // Two waves a cycle, which is the two-beat lope. + float ripp = sin(op.x * 3.2 - (gAnim.x / max(gAnim.y, 1.0)) * 12.566371); + + // ---- assemble -------------------------------------------------------- + // The cat is a silhouette with a rim on it. It is meant to be read as a + // shape first and a creature second, so the body stays close to black and + // the light lives on the edge. + float3 body = lerp(float3(0.020, 0.024, 0.032), + float3(0.55, 0.72, 0.42), gMisc.w); + + float lum = (0.20 + 0.80 * dif + 0.16 * bnc) * form * bay * 1.12; + lum *= 1.0 + 0.20 * coat; + lum *= 1.0 + 0.09 * ripp; + + float3 col = body * lum; + + // The edge carries the shaping too: fat over the two masses, thin at the + // waist, and pushed up at the two places a cat is recognised from - the + // ears and the last hand's width of tail. A cat that lands small on screen + // gets more of it, because sixty pixels of animal is all outline; one + // pixel is worth about 0.005 mesh units on the near draw and 0.03 on the + // far one, which is what the threshold below is set against. + float small = saturate((pw - 0.008) * 50.0); + float ears = head * smoothstep(0.16, 0.244, op.y); + float rim = pow(1.0 - saturate(dot(n, vdir)), + lerp(4.4, 2.6, saturate(gTune.z))); + float edge = rim * (0.70 + 0.30 * mass) + * (1.0 + 0.45 * ears + 0.45 * tail + 0.45 * small) + * (1.0 + 0.20 * coat); + + col += float3(0.42, 0.86, 0.46) * edge * (0.35 + 0.65 * gMisc.w) * bay; + col += float3(0.20, 0.26, 0.38) * saturate(n.y) * 0.15 * (0.60 + 0.40 * form); + + // Two catchlights, at the eyes, measured off the mesh. A black cat at + // sixty pixels is read from its eyes before it is read from its outline, + // and this demo has a scene that is nothing but a cat's eye, so a green + // spark here is a callback and not a decoration. Two per cent of body + // length is a real cat's eye; the pixel floor under that radius means the + // spark neither crawls on the near draw nor disappears on the far one. + // It is gated by the same `head` weight the rig turns the head with, so it + // can never strand itself on a shoulder, and it holds a base level so the + // solo lifts it rather than switching it on. + float3 ep = float3(0.645, 0.070, 0.098); + float ed = min(length(op - ep), length(op - float3(ep.x, ep.y, -ep.z))); + float er = max(0.038, pw * 1.15); + float eye = exp(-(ed * ed) / (er * er)) * head + * saturate(abs(on.z) * 1.5 + 0.20); + col += float3(0.58, 1.00, 0.62) * eye + * (0.55 + 0.40 * gVoice.y + 0.35 * gMisc.w); + + // Wireframe-to-solid, out of the barycentric the vertex shader got for + // nothing. At gMisc.y = 1 only the edges survive. + if (gMisc.y > 0.001) + { + float3 d = fwidth(i.bary); + float3 a = smoothstep(float3(0,0,0), d * 1.5, i.bary); + float wf = 1.0 - min(min(a.x, a.y), a.z); + col = lerp(col, float3(0.55, 0.95, 0.60) * wf, gMisc.y); + } + + float fog = exp(-max(i.vz, 0.0) * 0.0075); + col = col * fog + float3(0.055, 0.050, 0.062) * (1.0 - fog); + + return float4(col * gTime.z, 1.0); +} diff --git a/fx/orbital.hlsl b/fx/orbital.hlsl new file mode 100644 index 0000000..f13e9bf --- /dev/null +++ b/fx/orbital.hlsl @@ -0,0 +1,321 @@ +// ORBITAL / original analytic worlds. D3D11 ps_5_0, no resources. +// gTune = (floor variant 0..2, animation rate 0..2, emission 0..2, tilt radians). +// Zero rate still moves. gTime.x is seconds; camera is never replaced. +// Suggested eye / at / vertical FOV degrees / camera speed / tune: +// 0: (0,3.8,-12) / (0,0,0) / 44 / TRACK .35 / (0,1,1,.22), ONE flyby. +// 1: (0,2.9,-13) / (0,0,0) / 46 / ORBIT .07 / (1,.8,1,-.18). +// 2: (0,1,-11) / (0,0,0) / 48 / TRACK .30 / (2,1,1,.15). +// Bounds centered at origin: 0 sphere R=5.7 (planet R=2.6), +// 1 sphere R=6.5 (opaque core R=1.48); 2 sphere R=5.3. +// Keep camera outside those bounds; slow track/orbit gives strong parallax. +// Transparent sheets composite in ray order, behind nearest opaque surface. +// Depth is nearest visible layer at .05/viewZ, sky is infinite (zero). + +cbuffer Scene : register(b0) +{ + float4 gTime; + float4 gCam; + float4 gDir; + float4 gTune; + float4 gSync; + float4 gVoice; +}; +struct VSOut { float4 pos : SV_Position; float2 uv : TEXCOORD0; }; +struct PSOut { float4 color : SV_Target; float depth : SV_Depth; }; +static const float PI = 3.14159265359; +static const float FAR_T = 1e5; +static const float3 CYAN = float3(.12,.78,1.0); +static const float3 AMBER = float3(1.0,.43,.105); +static const float3 PINK = float3(.78,.095,.45); +static const float3 SUN = float3(-.57735,.57735,-.57735); + +float hash31(float3 p) +{ + p = frac(p * .1031); + p += dot(p, p.yzx + 33.33); + return frac((p.x + p.y) * p.z); +} +float noise3(float3 p) +{ + float3 b = floor(p), f = frac(p); + f = f*f*(3.0-2.0*f); + return lerp(lerp(lerp(hash31(b), hash31(b+float3(1,0,0)), f.x), + lerp(hash31(b+float3(0,1,0)), hash31(b+float3(1,1,0)), f.x), f.y), + lerp(lerp(hash31(b+float3(0,0,1)), hash31(b+float3(1,0,1)), f.x), + lerp(hash31(b+float3(0,1,1)), hash31(b+1.0), f.x), f.y), f.z); +} +float2 rotate2(float2 p, float a) +{ + float s, c; sincos(a,s,c); + return float2(c*p.x-s*p.y, s*p.x+c*p.y); +} +float3 localPoint(float3 p) +{ + p.xy = rotate2(p.xy, -.32-clamp(gTune.w,-.8,.8)); + return p; +} +// Analytic interval also handles a camera inside a sphere. +float2 sphere(float3 ro, float3 rd, float radius) +{ + float b = dot(ro,rd), h = b*b-dot(ro,ro)+radius*radius; + if (h < 0.0) return float2(FAR_T,-FAR_T); + h = sqrt(h); + return float2(-b-h,-b+h); +} +float firstHit(float2 hit) +{ + return hit.x > .001 ? hit.x : (hit.y > .001 ? hit.y : FAR_T); +} +// World-fixed far stars on cube faces; filtered footprints, no screen noise. +float3 sky(float3 d, float footprint) +{ + float3 a = abs(d); + float2 uv; float face; + if (a.x >= a.y && a.x >= a.z) { uv=d.yz/a.x; face=d.x>0?1.0:2.0; } + else if (a.y >= a.z) { uv=d.xz/a.y; face=d.y>0?3.0:4.0; } + else { uv=d.xy/a.z; face=d.z>0?5.0:6.0; } + float2 st = uv*125.0, cell=floor(st), f=frac(st); + float h=hash31(float3(cell,face)); + float2 center=.25+.5*float2(hash31(float3(cell,face+12.0)),hash31(float3(cell,face+31.0))); + float w=max(.035,footprint*125.0); + float star=exp(-dot(f-center,f-center)/(w*w)) * min(1.0,.008/(w*w)); + star *= smoothstep(.986,.999,h); + float dust=pow(saturate(1.0-abs(d.y+.22*d.x-.13)*3.0),6.0); + float n=noise3(d*7.0); + return float3(.004,.007,.016) + dust*n*float3(.021,.012,.034) + + lerp(float3(.42,.68,1.0),float3(1.0,.7,.4),h)*star*1.8; +} +float annulus(float r, float lo, float hi, float aa) +{ + return smoothstep(lo-aa,lo+aa,r)*(1.0-smoothstep(hi-aa,hi+aa,r)); +} +// Fade frequencies beyond the ray footprint before evaluating narrow grooves. +float wave(float phase, float frequency, float width) +{ + return .5+.5*sin(phase)*saturate(1.0-frequency*width*.32); +} +float ringDensity(float r, float width) +{ + float bands=.38+.28*wave(r*39.0,39.0,width)+.30*wave(r*113.0,113.0,width); + float gap=1.0-.94*exp(-pow((r-4.23)/max(.055,width),2.0)); + return bands*gap*annulus(r,3.22,5.55,max(.012,width)); +} +float3 planet(float3 p, float3 rd, float time, float px) +{ + float3 n=p/2.6, q=n; + q.xz=rotate2(q.xz,time*.095); + float turbulence=noise3(q*8.0)+.38*noise3(q*23.0); + float lat=q.y*33.0+(turbulence-.65)*3.0; + float bands=.5+.5*sin(lat); + float fine=wave(lat*3.6+q.x*5.0,120.0,px); + float3 alb=lerp(float3(.12,.22,.31),float3(.83,.49,.22),bands); + alb=lerp(alb,float3(.76,.72,.56),.32*fine); + // A local oval storm travels with the rotating atmosphere. + float2 storm=float2(q.x+.37,(q.y+.19)*2.8); + float vortex=exp(-dot(storm,storm)*95.0)*smoothstep(.15,.6,-q.z); + alb=lerp(alb,float3(.70,.16,.095),vortex*.85); + float light=saturate(dot(n,SUN)); + float shadow=1.0; + float ts=-p.y/SUN.y; + if (ts>0.0) shadow=1.0-.83*ringDensity(length((p+SUN*ts).xz),.02); + float rim=pow(1.0-saturate(dot(n,-rd)),3.5); + float3 col=alb*(.10+.94*light*shadow); + col+=CYAN*rim*(.12+.48*light)*(.97+.03*saturate(gSync.y)); + return col; +} +// Three differently oriented sheets; true plane intersections, no screen halos. +float3 sheetNormal(int id) +{ + if (id==0) return float3(0,1,0); + if (id==1) return normalize(float3(.13,.24,1.0)); + return normalize(float3(-.64,1.0,.31)); +} +float4 sheet(float3 p, float3 rd, int id, int mode, float time, float px) +{ + float3 normal=sheetNormal(id); + float3 u=normalize(cross(normal,float3(1,0,0))); + float3 v=cross(normal,u); + float2 q=float2(dot(p,u),dot(p,v)); + float r=length(q), angle=atan2(q.y,q.x); + float width=max(.008,px/max(.12,abs(dot(rd,normal)))); + float4 result=float4(0,0,0,0); + if (mode==0) { + float density=ringDensity(r,width); + float3 c=lerp(float3(.22,.40,.48),float3(.85,.60,.31),wave(r*9.0,9.0,width)); + float shadow=firstHit(sphere(p+SUN*.01,SUN,2.6)).98?float3(0,0,1):float3(0,1,0); + float3 right=normalize(cross(worldUp,fw)), up=cross(fw,right); + float2 uv=(input.uv*2.0-1.0)*float2(gTime.w,-1.0); + uv=rotate2(uv,gDir.w); + float3 worldRay=normalize(fw+(right*uv.x+up*uv.y)*gCam.w); + float footprint=max(length(ddx(worldRay)),length(ddy(worldRay))); + float3 ro=localPoint(gCam.xyz), rd=localPoint(worldRay); + int mode=(int)clamp(floor(gTune.x),0.0,2.0); + float time=gTime.x*(.55+.45*clamp(gTune.y,0.0,2.0)); + float exposure=.65+.35*clamp(gTune.z,0.0,2.0); + float3 col=sky(worldRay,footprint); + float nearest=FAR_T; + + [branch] if (mode<2) { + float radius=mode==0?2.6:1.48; + float t=firstHit(sphere(ro,rd,radius)); + if (t=5) break; + float h=hash31(float3(float(j),7.0,2.0)); + float orbit=mode==0?5.55:(3.0+2.85*h); + float a=float(j)*2.39996+time*(.085+.06*h); + float3 center=float3(cos(a)*orbit,(mode==0?.11:1.15)*sin(a*2.0+float(j)),sin(a)*orbit); + float size=(mode==0?.035:.065)+h*h*(mode==0?.075:.15); + float3 axes=float3(size,size*(.55+.45*h),size*.76); + float3 er=(ro-center)/axes, ed=rd/axes; + float len=length(ed); + float hit=firstHit(sphere(er,ed/len,1.0)); + t=hit1e-5?-dot(ro,normal)/denom:FAR_T; + distances[k]=(hit>.001 && hit.02) nearest=min(nearest,t); + } + } + // Analytic atmosphere shell: only its segment in FRONT of opaque matter. + if (mode==0) { + float2 atmo=sphere(ro,rd,2.66); + float start=max(.001,atmo.x), end=min(opaque,atmo.y); + if (end>start) { + float mid=(start+end)*.5; + float3 p=ro+rd*mid; + float light=.12+.88*saturate(dot(normalize(p),SUN)); + // A foreground ring attenuates the atmosphere as well. + float visibility=1.0; + float planeT=abs(rd.y)>1e-5?-ro.y/rd.y:FAR_T; + if (planeT>0.0 && planeTend) break; + float3 p=ro+rd*t; + float2 field=filaments(p,time); + float eps=max(.0015,footprint*t*.42); + if (field.x 2x+y = 2.00 +// bar 18, push, 3 bars (x,y) = (1.00, 0.40) -> 2x+y = 2.40 +// bar 21, crane, 3 bars (x,y) = (1.10, 0.60) -> 2x+y = 2.80 +// +// which is three values evenly spaced 0.4 apart, so one floor turns them +// into a programme index 0, 1, 2 - and the wrap at the end means any future +// row is legal rather than out of range. x and y keep their old meanings on +// top of that, so nothing about the table's intent is thrown away. +// +// A programme is not a colour. It rebuilds the room: +// +// P0 OFF AIR 38 small tubes in 5 rows, all of them snowing. A tight +// deep colonnade of tall ragged plinths, some of them gone +// and only their floor plate left, thick air, wet floor. +// P1 TEST CARD 26 tubes in 3 rows showing the card. The establishing +// version: the room as it is described above, full rows, +// even plinths, dry floor, clear air. +// P2 WAVEFORM 17 big tubes in 2 rows, each one a scope tracing the +// music. A wide sparse room, low broad plinths, deep clear +// air and a hard mirror floor. +// +// At thumbnail scale what separates them is how tall the band of tubes +// stands, whether the bottom half of frame carries a mirrored copy of it, +// and how bright the room runs. None of those is a hue. +// +// WHAT THIS COSTS. Three programmes is three bodies in one shader, and the +// blob goes from 29,192 to 44,848 bytes - it was already the largest in the +// demo and it still is. That is +3.8% of the executable, and it is the whole +// price: the branch on PROG is uniform across the draw, so only one body +// ever executes, and a measured A/B of the frame time could not separate the +// two versions from run-to-run noise (124-127 us either way, with the worst +// frame in the demo still belonging to shot 22, the kaleido, not to this +// scene). The cost is bytes on disk, not microseconds. +// ========================================================================== +// +// WHAT IS MARCHED AND WHAT IS NOT +// +// Only the plinths are marched. The floor, the ceiling and the monitor wall +// are all planes or one axis aligned box, and every one of them is solved +// analytically in main(). That is not a micro-optimisation, it is the whole +// reason the shot works: +// +// * a grazing plane is the one thing a fixed step budget handles badly. +// Neighbouring rays run out of steps at different distances and the +// bottom of the frame fills with concentric arcs. +// * a sphere traced hit lands up to eps SHORT of the surface, and eps has +// to grow with distance or the march never terminates. Any "which +// surface did I hit" test written against a fixed epsilon therefore +// misfires at range. Solving the wall exactly means there is no test to +// get wrong. +// * marching only the plinths leaves a compact, well separated field. It +// converges in 8 steps on average instead of 21, and the map() body is +// a third shorter because the floor and the wall are no longer in it - +// and map() is inlined seven times, so every instruction is paid seven +// times. +// +// The monitor grid - bezels, screens, programme, per-tube flicker - is a +// function evaluated at the hit point, not geometry. That costs two calls +// per pixel (the wall, and the floor's reflection of it) instead of putting +// eighty boxes inside the march. +// +// NOTHING NEW IS ALLOWED A TRANSCENDENTAL IN THE FIELD +// +// mapPlinths is inlined about thirty seven times per pixel (march, normal, +// shadow, occlusion), so one sin added there costs more than everything in +// monitorWall put together. The two animations that live in the field are +// built to that budget: +// +// * the objects HOVER on a bob driven by frac and a smoothstep, not a +// sine. smoothstep of a triangle wave has zero derivative where the +// triangle has its corners, so the composite is C1 and reads as a +// perfectly smooth float - for two mads and a frac. +// * the objects SPIN on one angle shared by the whole room, whose cos and +// sin are computed ONCE into a static, with a per-object sign flip so +// they do not all turn the same way. Rotation is an isometry, so the +// field stays exact. +// +// EVERYTHING IS FILTERED BY ITS OWN FOOTPRINT +// +// This renders at 640x360 and hands off to a CRT pass that quantises +// luminance to ten levels through a 4x4 ordered dither. Ten levels is not +// much to work with. Two consequences run through the whole shader: +// +// * nothing may be thinner than about two virtual pixels, because the +// dither will turn a one pixel line into crawling noise. The convergence +// ring, the blanking bar, the scope trace, the snow blocks and the bezel +// rim are all deliberately fat, and the ones whose width is in screen +// units are clamped against the pixel footprint rather than fixed. +// * the chequers and every programme are box filtered against the real +// screen space footprint of the hit, computed analytically from t and +// the grazing angle - no ddx/ddy, which would be undefined in this much +// divergent flow control anyway. Far detail converges to ITS OWN MEAN +// instead of aliasing, which is what makes the floor "run out into +// nothing" rather than boil. Each programme therefore has to declare +// what its mean is; get that number wrong and the far half of the wall +// steps to a different brightness than the near half. +// +// The value structure is three tiers and nothing in between, because ten +// levels cannot hold more: near black for the ceiling, the dark chequers and +// every plinth face turned away from the wall (levels 0-1); a clear mid grey +// for the light chequers and the lit plinth edges (levels 2-8, brightening +// as the floor runs towards the wall); and the tubes themselves at the top +// of the ramp (levels 3-10, the bright bars clipping). In a dark cinema the +// wall is the only thing that is allowed to be white, so the room's gain is +// set from that constraint down rather than from a look upwards. +// +// The wall runs floor to CEILING, not to the top of the grid. It costs +// nothing - it is one box - and it buys two things: there is no hard +// horizontal seam across the top of frame, and the empty upper third is the +// wall's own plaster, which has a spill gradient falling away from the +// tubes, instead of a completely flat ceiling plane. +// +// No dither, no scanlines, no grade, no vignette here. The post pass owns +// all four. This shader outputs linear HDR and one shot fade. +// +// NOTHING HERE STROBES. Every music term is base + k*s or base * (1 + k*s), +// so no floor can drop out. The three things that move fastest - the snow, +// the per-tube flicker and the blanking bars - are all DESYNCHRONISED +// per tube by the tube's own hash, so the frame mean of seventy-odd +// independent oscillators barely moves even when any one tube is swinging +// hard. The only whole-wall event, the refresh wipe, only ever ADDS light, +// and only to about three columns at a time. +// +// gTune.x room exposure / how hard the monitors burn. 1.0 is the room. +// gTune.y vertical hold: the base roll rate. 0 frozen, 1 a slow crawl up. +// gTune.z EXTRA flicker depth on top of whatever the programme already has, +// so the table can push a shot further but can no longer switch the +// room's only light source off by leaving a field at zero. +// gTune.w EXTRA floor gloss, on the same terms. +// gTune.x and gTune.y together also pick THE PROGRAMME - see above. + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect (w/h) + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look direction, w roll + float4 gTune; // per shot knobs +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +float4 gCaption; +}; + +#include "inscriptions.hlsli" + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// The room, in world units. The wall front face is the plane the reflection +// and the lights are both aimed at, so it gets a name. +// +// WALL_HW is 62 and not 40 because at 40 the wall is exactly as wide as a +// 0.62 fov frame from twelve units behind the front row - which means the +// bare end of it swings into shot on any pan. The wall is a function, so +// widening it is free; running out of it is not. It has to clear the widest +// programme's grid too: P2 is 9 columns of 6.4, which reaches 57.6. +#define WALL_Z 30.0 +#define WALL_HW 62.0 +#define WALL_TH 1.6 +#define CEIL_Y 18.0 +#define FAR 160.0 + +// The shadow ray's cutoff: the height above which nothing in the room can +// possibly occlude anything. It has to bound the TALLEST programme - P0's +// plinths run to 1.855 half height, so a cap top of 3.71 plus a hover of +// 0.36 plus a 1.1 tall object reaches 5.17. Over-estimating this only costs +// a step or two on a shadow ray over open floor; under-estimating it cuts +// the tops off shadows, so it is rounded up. +#define PLINTH_TOP 5.50 + +// Lighting constants. The falloff is deliberately much slower than inverse +// square and the ambient bounce is deliberately large: these are not three +// bulbs, they are three samples of a lit surface a hundred units wide. A +// real inverse square puts the far floor at level 0 and the near floor at +// level 10, which is six of the ten levels spent on one gradient. +#define L_FALLOFF 0.00045 +#define L_GAIN 1.05 +#define L_BOUNCE 0.62 + +// ---- the programme, and everything it decides ---------------------------- + +// Three constants, chosen by index. Two compares and two selects - cheaper +// than indexing a const array, and it reads as a table: the three columns +// are P0, P1, P2 in that order, everywhere in this file. +float pick3(float a, float b, float c, float k) +{ + return (k < 0.5) ? a : ((k < 1.5) ? b : c); +} + +// 2x + y separates the three rows of the shot table into 2.00 / 2.40 / 2.80. +// Scaling by 2.5 and dropping 4.5 lands those on exactly 0.5 / 1.5 / 2.5 - +// the CENTRE of each bin - so the floor is 0 / 1 / 2 with half a step of +// slack on both sides: a table edit of +-0.10 on x, or +-0.20 on y, cannot +// change channel by accident. The wrap keeps any other tune legal. +float programme() +{ + float k = floor(gTune.x * 5.0 + gTune.y * 2.5 - 4.5); + return k - 3.0 * floor(k / 3.0); +} + +// These are statics and not macros on purpose: fxc emits a global static's +// initialiser once in the entry point's preamble, so each of these costs a +// handful of instructions per PIXEL rather than per inlined copy of the +// distance field. That distinction is the whole reason the field can afford +// to be programme-dependent at all. +static float PROG = programme(); + +// -- the monitor wall's geometry. +// +// HOW TALL THE BAND IS, not how finely it is divided. That distinction cost +// a rewrite: the first version of this table varied the tube COUNT - 38 fine +// ones against 17 coarse ones - and the three shots measured no further +// apart than they had before. A wall of many small bright rectangles and a +// wall of few large bright rectangles have the SAME average brightness, so +// once the frame is squashed to a 32x18 thumbnail they are the same picture. +// Texture is for the viewer; only large-scale luminance layout survives the +// measurement. So what actually varies here is where the light SITS: +// +// P0 band 0.95 .. 7.45 a low strip, and a lot of dark wall above it +// P1 band 1.30 .. 11.50 the room as it was +// P2 band 1.10 .. 13.50 giant screens, nearly up to the ceiling +// +// The tube count still varies, because it is what tells a human eye these +// are three different rooms - it just is not what earns the score. +// +// The aperture is 0.68 x 0.60 of the cell, so a 4:3 tube wants CELLX/CELLY +// near 1.18. All three are within a few percent of that: these stay CRTs. +// P0 P1 P2 +static float CELLX = pick3( 1.91, 4.20, 7.30, PROG); // cell width +static float CELLY = pick3( 1.625, 3.40, 6.20, PROG); // cell height +static float MONY0 = pick3( 0.95, 1.30, 1.10, PROG); // bottom of the grid +static float GRIDX = pick3(29.00, 13.00, 8.00, PROG); // columns are -G..G-1 +static float GRIDY = pick3( 4.00, 3.00, 2.00, PROG); // rows + +// Where the light lives. Derived rather than hardcoded, because the grid's +// vertical centre moves with the programme and the three light samples have +// to stay inside the lit band or the room tips over. +static float LIGHT_Y = MONY0 + GRIDY * CELLY * 0.5 + 0.45; + +// -- what is ON the tubes. +static float FLICK = pick3( 0.55, 0.30, 0.22, PROG); // per-tube breathing +static float ROLLB = pick3( 0.55, 0.45, 0.30, PROG); // roll band strength +static float WIPEA = pick3( 1.10, 0.55, 0.85, PROG); // refresh wipe gain +static float WIPER = pick3( 0.190, 0.085, 0.130, PROG); // and its rate +static float TUBEG = pick3( 1.85, 2.00, 2.90, PROG); // tube output + +// How hot the ROOM runs, on top of gTune.x. This pulls in the same direction +// the table already does - the table's own exposures are 0.85, 1.00, 1.10 - +// it just pulls harder, because a third of a stop is not enough to separate +// three shots of one room and the post pass regrades the hue away anyway. +// Together they give 0.68 / 1.00 / 1.49: a dark shot, a normal one, and a +// bright one, which is the one difference a luminance thumbnail cannot miss. +static float ROOMG = pick3( 0.80, 1.00, 1.35, PROG); + +// -- the plinth field. +static float ROW_DX = pick3( 4.60, 5.60, 7.40, PROG); // half aisle width +static float ROW_DZ = pick3( 5.60, 6.50, 8.20, PROG); // spacing in z +static float ROW_HI = pick3( 4.00, 3.00, 2.00, PROG); // last row index +static float PW = pick3( 0.55, 0.62, 0.78, PROG); // plinth half width +static float PRAG = pick3( 0.40, 0.14, 0.28, PROG); // height raggedness +static float PGAP = pick3( 0.22, 0.00, 0.30, PROG); // fraction missing +static float PSEED = pick3( 1.00, 4.30, 9.70, PROG); // which objects +static float PBOB = pick3( 0.170, 0.115, 0.230, PROG); // hover rate +static float SPINR = pick3( 0.55, 0.22, 0.38, PROG); // spin rate + +// -- the room around them. These four are the ones that actually move the +// measurement, because between them they own the whole bottom half of the +// frame and the overall level of the top half: +// +// P0 a dry, dark floor in clear air. The frame is nearly black with +// one low strip of snowing tubes across it - which is also what the +// orbit at bar 63 wants, three seconds into the darkest part of the +// solo. +// P1 the room as described: a mid grey chequered floor, a little +// gloss, clear air. +// P2 a wet floor carrying a full second copy of the wall, a pale +// chequer, and thick enough haze that the room glows. The brightest +// this scene ever gets, and it is the crane, on the widest lens. +static float CHEQK = pick3( 0.70, 0.50, 0.36, PROG); // 1 / chequer size +static float FLR_HI = pick3( 0.400, 0.700, 0.880, PROG); // light chequer +static float FLR_LO = pick3( 0.042, 0.062, 0.095, PROG); // dark chequer +static float FOGK = pick3(0.0090,0.0115,0.0210, PROG); // haze density + +// The floor's gloss and the flicker depth are FLOORS that gTune adds to, +// never fields that gTune can zero. That is the fix for the bug at the top +// of this file: the table passes 0.00 for both in all three rows, and under +// the old reading that switched off the two effects the scene most needs. +static float GLOSSF = saturate(pick3(0.06, 0.14, 0.85, PROG) + gTune.w); +static float FLICKF = saturate(FLICK + gTune.z); + +// One rotation for the whole room, evaluated once. The per-object sign flip +// in mapPlinths is what stops it reading as a turntable. +static float2 gSpin = float2(cos(gTime.x * SPINR), sin(gTime.x * SPINR)); + +// Room exposure. A static rather than a macro because it now carries a slow +// mains sag, and a macro would have paid for that sine at all six use sites. +// +// The wall of monitors IS the light in this room, so it is the one thing +// here that should answer the music. Six percent on the mid band and five on +// the organ: the room brightens fractionally when the chords move, which +// reads as the tubes being fed by the same mains as everything else. Both +// are 1 + k*s, so the floor never drops out. +static float ROOM_EXP = gTune.x * ROOMG + * (1.0 + gSync.y * 0.06 + gVoice.z * 0.05) + * (1.0 + 0.030 * sin(gTime.x * 0.37)); + +// ---- distance field ------------------------------------------------------ + +float vmax3(float3 v) { return max(v.x, max(v.y, v.z)); } + +float sdBox(float3 p, float3 b) +{ + float3 q = abs(p) - b; + return length(max(q, 0.0)) + min(vmax3(q), 0.0); +} + +float sdBox2(float2 p, float2 b) +{ + float2 q = abs(p) - b; + return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0); +} + +float hash11(float n) { return frac(sin(n * 12.9898) * 43758.5453); } + +float hash21(float2 p) { return frac(sin(dot(p, float2(27.13, 61.71))) * 43758.5453); } + +// A smooth 0..1 bob with no transcendental in it. tri has corners at 0 and +// 1; smoothstep's derivative is zero at both ends; so the composite is C1 +// everywhere and the hover has no visible kick at the top or the bottom of +// its travel. This is called once per inlined copy of the field, which is +// why it is worth not being a sine. +float bob01(float phase) +{ + float tri = abs(frac(phase) * 2.0 - 1.0); + return tri * tri * (3.0 - 2.0 * tri); +} + +// The plinths, and nothing else. No floor, no wall, no ceiling - they are all +// analytic in main(). This is also exactly the right field for the shadow and +// occlusion rays: the plinths are the only things in the room that can +// occlude anything. A light that sits ON the wall cannot be occluded by the +// wall, and a floor lit from seven units above it cannot shadow itself, so +// putting either of them in the occluder field can only produce a wrong +// answer - and it did: with the wall in it, every shadow ray ended up close +// to the wall at the far end of its travel and the penumbra estimator read +// that proximity as an occluder, dimming a fifth of the floor for no reason. +float mapPlinths(float3 p) +{ + // Two rows, mirrored in x, then repeated in z with the index CLAMPED - + // an unbounded repeat would put plinths behind the camera and out past + // the wall, and the clamp costs one round and one clamp. The count of + // rows is the programme's, so P0 is a deep colonnade and P2 is three + // widely spaced pairs. + float3 q = p; + float side = step(0.0, p.x); + q.x = abs(q.x) - ROW_DX; + float row = clamp(round(q.z / ROW_DZ), -1.0, ROW_HI); + q.z -= row * ROW_DZ; + + // This plinth's identity. One hash, two channels: sel picks the object, + // sel2 sets the height and decides whether the plinth is there at all. + // The second channel is a frac of the first, which is free - a second + // hash11 would be a second sine in the hot field. + float sel = hash11(row * 3.0 + side * 17.0 + PSEED); + float sel2 = frac(sel * 137.719 + 0.37); + + // Some plinths are gone. Rather than removing them - which would mean + // adding a constant to the field, and adding to an SDF OVERSTATES the + // distance and lets rays step through their neighbours - the plinth + // collapses to the bare floor plate it stood on, with whatever was on + // top of it shrunk to a marble. That stays an exact distance field, and + // a gap in a gallery row with only the footprint left is worth more to + // this scene than a clean row anyway. + float pres = step(PGAP, sel2); + + // Ragged heights, so a row reads as a skyline and not a picket fence. + // P1 keeps this small: it is the establishing shot and wants the even + // gallery the header describes. + float hh = 1.325 * (1.0 + PRAG * (sel2 - 0.5) * 2.0); + hh = lerp(0.055, hh, pres); + + float d = sdBox(q - float3(0.0, hh, 0.0), float3(PW, hh, PW)); + + // Base slab and cap slab in one box. They are the same size and they sit + // symmetrically about the middle of the plinth, so folding y about that + // midpoint gets both of them for the price of one. When the plinth is + // gone the fold degenerates harmlessly into the single low plate. + float3 s2 = float3(q.x, abs(q.y - hh) - (hh - 0.10), q.z); + d = min(d, sdBox(s2, float3(PW + 0.23, 0.10, PW + 0.23)) - 0.03); + + // One object per plinth, HOVERING above the cap. Which object is decided + // by the plinth's index and by which side of the room it is on, so the + // two rows never mirror each other - a mirrored gallery reads as a + // pattern, and a pattern is not uncanny. The seed is the programme's, so + // the same plinth carries a different object in a different shot. + // The three shapes are three different NORMS of the same offset vector - + // length is a sphere, the sum of the components is an octahedron, the + // largest component is a cube - which is far cheaper than three separate + // primitives and a rotation, and reads exactly the same at 640x360. + // (Max-norm underestimates the true distance to a cube near its corners, + // which is safe: a march that underestimates just takes a smaller step. + // The select on side makes the field discontinuous across x = 0, by + // at most 0.23, at a place where the field is 5.1 - so no ray can ever + // step through anything on the strength of it.) + bool sph = (sel < 0.34), oct = (sel >= 0.34 && sel < 0.67); + + // rad is the norm's threshold, hlf that shape's half height. A removed + // plinth's object shrinks to a marble rather than vanishing, for the + // same reason the plinth does. + float rad = sph ? 0.52 : (oct ? 0.318 : 0.44); + float hlf = sph ? 0.52 : (oct ? 0.550 : 0.44); + rad = lerp(0.050, rad, pres); + hlf = lerp(0.050, hlf, pres); + + // The hover. Always positive, so the object never sinks back into its + // cap - it is a float, not a bounce - and every one of them is on its + // own phase off sel2, so the gallery has forty independent slow clocks + // in it and a locked-off camera still has something to watch. + float lift = 0.16 + 0.20 * bob01(gTime.x * PBOB + sel2); + + // The spin. One shared angle, one per-object sign. Rotation about y is + // an isometry, so all three norms stay exact under it. + float3 a0 = q - float3(0.0, 2.0 * hh + lift + hlf, 0.0); + float sg = (sel2 < 0.5) ? -1.0 : 1.0; + float3 a = abs(float3(a0.x * gSpin.x - a0.z * gSpin.y * sg, + a0.y, + a0.x * gSpin.y * sg + a0.z * gSpin.x)); + + float ds = (sph ? length(a) + : (oct ? (a.x + a.y + a.z) * 0.5773 + : max(a.x, max(a.y, a.z)))) - rad; + return min(d, ds); +} + +// The top of this cell's cap. The shading needs to know whether it hit the +// object or the plinth under it, and now that heights are ragged it can no +// longer ask "is p.y above 2.70". Repeating the cheap half of the field is +// far less work than evaluating the field twice, and it is only called on a +// plinth hit. +float capTopAt(float3 p) +{ + float side = step(0.0, p.x); + float row = clamp(round(p.z / ROW_DZ), -1.0, ROW_HI); + float sel = hash11(row * 3.0 + side * 17.0 + PSEED); + float sel2 = frac(sel * 137.719 + 0.37); + float pres = step(PGAP, sel2); + return 2.0 * lerp(0.055, 1.325 * (1.0 + PRAG * (sel2 - 0.5) * 2.0), pres); +} + +// Four taps on a tetrahedron rather than the six taps the other scenes use. +// It is the same normal to within a rounding error, and it removes two whole +// inlined copies of the field - worth about 4 KB of shader blob here, which +// is real money in an 85 KB executable. Only ever called on a plinth hit; +// the floor and the wall have known normals. +float3 mapNormal(float3 p) +{ + float2 k = float2(1.0, -1.0) * 0.0022; + return normalize(k.xyy * mapPlinths(p + k.xyy) + k.yyx * mapPlinths(p + k.yyx) + + k.yxy * mapPlinths(p + k.yxy) + k.xxx * mapPlinths(p + k.xxx)); +} + +// tmax is handed in already clipped to the height above which nothing in the +// room can possibly occlude, so a shadow ray over open floor finishes in four +// or five steps instead of walking all the way to the wall. +float softShadow(float3 ro, float3 rd, float tmax) +{ + float res = 1.0, t = 0.30; + [loop] for (int i = 0; i < 20; i++) { + if (t > tmax) break; + float h = mapPlinths(ro + rd * t); + if (h < 0.004) return 0.0; + res = min(res, 9.0 * h / t); + t += clamp(h, 0.12, 3.0); + } + return saturate(res); +} + +float occlusion(float3 p, float3 n) +{ + float o = 0.0, s = 1.0; + [loop] for (int i = 0; i < 5; i++) { + float d = 0.09 + 0.26 * float(i); + o += (d - mapPlinths(p + n * d)) * s; + s *= 0.62; + } + return saturate(1.0 - 1.5 * o); +} + +// ---- the monitor wall ---------------------------------------------------- + +// Colour of the wall's front face at a point on it, in world x and y. Called +// twice per pixel at most: once where a ray lands on the wall, once for the +// floor's reflection of it. Returns linear HDR - the screens are meant to +// come out at the top of the post pass's ramp, they are the light source. +// +// `aa` is the width, in world units, of one screen pixel at this point. It +// is the whole antialiasing strategy: every feature narrower than aa is +// faded out towards ITS OWN MEAN rather than being drawn and left to fight +// the ordered dither. Each of the three programmes therefore states its mean +// explicitly in the lerp that fades it - those numbers are not decoration, +// they are what stops the far half of the wall stepping to a different level +// than the near half. +float3 monitorGrid(float2 w, float t, float aa) +{ + // Dead plaster, with a soft spill where it is closest to the tubes. This + // is also what you get above and below the grid, and it must stay very + // dark or the wall stops reading as a grid of lit rectangles. + float3 dead = float3(0.030, 0.036, 0.050) + * (0.30 + 0.70 * exp(-abs(w.y - LIGHT_Y) * 0.16)); + + float2 g = float2(w.x / CELLX, (w.y - MONY0) / CELLY); + float2 id = floor(g); + float2 f = g - id - 0.5; // -0.5 .. 0.5 inside the cell + + // The grid runs out past the edge of frame in every shot, which is the + // whole point of the room - but how MANY tubes that is, and how tall the + // band of them stands, is the programme's decision. + if (id.y < 0.0 || id.y > GRIDY - 1.0 || id.x < -GRIDX || id.x > GRIDX - 1.0) return dead; + + float ac = max(aa / CELLX, 2e-4); // footprint, in cell units + float fine = saturate(1.0 - ac * 5.0); // 1 when detail is resolvable, 0 when it is not + + // Rounded screen aperture, in cell-relative units, so it scales with the + // programme's monitor size for free. Everything outside it is bezel: near + // black plastic with one thin lighter rim catching its own tube. The + // value gap between screen and bezel is what survives the dither at + // 640x360, so it is a big gap on purpose - and the aperture edge itself + // is crossfaded over one pixel, because seventy eight hard edged + // rectangles crawling in unison is the single most obvious artefact this + // scene can produce. + float sd = sdBox2(f, float2(0.34, 0.30)) - 0.06; + float rim = exp(-max(sd, 0.0) * 9.0) * fine; + float3 bez = dead + float3(0.055, 0.062, 0.075) * (0.35 + 1.6 * rim); + if (sd > ac) return bez; // all bezel: skip the programme + + float h = hash21(id); // this tube's identity + float h2 = frac(h * 137.719 + 0.37); // and a second channel of it + float2 s = f / float2(0.40, 0.36); // -1 .. 1 across the screen + + float3 pat; + + if (PROG < 0.5) { + // ---- P0 OFF AIR ------------------------------------------------ + // The station is gone and the wall is snowing. Blocky snow: five by + // three cells per tube, which is about three virtual pixels a block + // in a wide shot - one more subdivision and it would be dither food. + // Every tube runs its own frame counter at its own rate, so the wall + // CRAWLS rather than pulsing in unison, and the frame's mean over a + // thousand-odd independent blocks does not move at all. That is what + // makes a wall of static safe: the picture changes everywhere and + // the level changes nowhere. + // + // Underneath it is a dim ghost of whatever the card used to be, two + // flat quadrants, so a tube never loses its shape entirely and the + // wall still reads as rectangles rather than as grey mush. + float by = floor(s.y * 1.5); + float tear = smoothstep(0.86, 1.0, sin(t * (0.31 + 0.19 * h2) + h * 13.0) * 0.5 + 0.5); + float shear = tear * (hash21(float2(by, floor(t * 14.0))) - 0.5) * 4.0; + float2 bq = float2(floor(s.x * 2.5 + shear), by); + + float fr = floor(t * (7.0 + 4.0 * h) + h * 53.0); + float n = hash21(bq * 3.1 + fr + id * 0.37); + float gh = 0.30 + 0.14 * step(0.0, s.x) + 0.10 * step(0.0, -s.y); + + // 0.47 is the mean of ghost + snow, and it is what a tube collapses + // to once the blocks stop being resolvable. + pat = lerp(0.47, gh + 0.34 * (n - 0.5) * 2.0, fine) * float3(0.80, 0.98, 0.92); + } + else if (PROG < 1.5) { + // ---- P1 TEST CARD ---------------------------------------------- + // The room as the header describes it. Four bars, not the six a real + // test card has: a monitor is about twenty virtual pixels across in + // a wide shot, and six bars in twenty pixels is not a test card, it + // is noise for the dither to chew on. The staircase is scaled so its + // four steps land on four DIFFERENT levels of the post pass's ten + // level quantiser - at the old 0.20 + 0.26i the top two both clipped + // to white on any tube brighter than average, and a test card whose + // top half is one flat block is not reading as a test card. + float bi = clamp(floor((s.x * 0.5 + 0.5) * 4.0), 0.0, 3.0); + float lvl = lerp(0.485, 0.20 + bi * 0.19, fine); // 0.485 is the mean of the four + pat = lvl * float3(0.74, 1.00, 0.86); // phosphor bias + + // One amber bar, so the wall is not entirely one hue and the warm + // light in this demo gets a foothold here too. + pat = lerp(pat, float3(1.00, 0.64, 0.26) * 0.95, step(abs(bi - 1.0), 0.5) * 0.85 * fine); + + // The convergence circle. Deliberately fat: a one pixel ring would + // be eaten alive by the ordered dither, and at forty six units the + // old 0.18/0.06 ring was exactly one pixel. + float r = length(s * float2(1.0, 1.06)); + pat = lerp(pat, float3(0.90, 1.00, 0.94), + smoothstep(0.20, 0.07, abs(r - 0.60)) * 0.55 * saturate(1.0 - ac * 4.0)); + } + else { + // ---- P2 WAVEFORM ----------------------------------------------- + // Seventeen big tubes, and every one of them is a scope showing the + // music. Three harmonics whose amplitudes come off three different + // sources - the low band, the solo voice and the high band - so the + // wall's PICTURE is genuinely different when the music is different, + // which is the one kind of variation a post-pass regrade cannot + // flatten out. + // + // Nothing here can flash: the trace is a thin bright thing on a dark + // face, so the tube's mean barely moves however hard the trace + // swings, and every amplitude is base + k*s with a floor of its own. + float tr = 0.30 * sin(s.x * 4.3 + t * 2.6 + h * 6.283) * (0.30 + 0.85 * gSync.x) + + 0.19 * sin(s.x * 11.0 - t * 4.4 + h2 * 6.283) * (0.22 + 0.85 * gVoice.y) + + 0.11 * sin(s.x * 23.0 + t * 7.7) * (0.18 + 0.85 * gSync.z); + + // Both widths are clamped against the pixel footprint, so neither + // the trace nor the graticule can ever go sub-pixel at range. + float2 gl = abs(frac(s * 1.5 + 0.5) - 0.5) / 1.5; + float gw = max(0.075, ac * 4.0); + float grat = saturate(1.0 - min(gl.x, gl.y) / gw); + + float tw = max(0.155, ac * 4.5); + float trace = saturate(1.0 - abs(s.y - tr) / tw); + trace *= trace; // a hard core and a soft shoulder + + // The beam. One bright dot per tube running the sweep on its own + // phase - the secondary element with its own timing that makes a + // held shot worth holding. + float bx = frac(t * (0.24 + 0.18 * h) + h2) * 2.0 - 1.0; + float beam = saturate(1.0 - length(float2((s.x - bx) * 0.8, s.y - tr)) + / max(0.20, ac * 5.0)); + + // These are storage scopes with the brightness wound right up, not + // dark instrument faces: this programme is the brightest the room + // ever gets and the tubes have to carry that, not just the floor. + // 0.43 is the face's mean - 0.34 of base, about 0.06 of graticule + // and about 0.03 of trace averaged over the height of the screen - + // and it is what a tube collapses to once it stops being resolvable. + pat = lerp(0.43, 0.34 + 0.26 * grat + 0.70 * trace + 0.62 * beam, fine) + * float3(0.66, 1.00, 0.80); + } + + // The roll, and it is now PER TUBE. A bright band travelling up the frame + // with the black frame blanking bar trailing it - a vertical hold that + // nobody ever fixed. Both are three virtual pixels deep, not one. + // + // The phase and the rate both come off the tube's hash, which is the + // single cheapest thing in this file and one of the most valuable: with + // one shared phase the wall was seventy eight televisions showing the + // same frame at the same instant, which is exactly why it read as one + // flat lattice. Scattered, it reads as a room full of sets. And on a + // very slow timer a tube loses hold completely and rolls through fast - + // at any moment one or two of the seventy eight are doing it. + float rrate = 0.085 * gTune.y * (0.55 + 1.10 * h); + float kick = smoothstep(0.90, 0.985, sin(t * (0.21 + 0.17 * h2) + h * 25.0) * 0.5 + 0.5); + float rp = frac(t * rrate * (1.0 + 7.0 * kick) + h * 7.31); + float ry = 1.0 - 2.0 * rp; + float dy = s.y - ry; + float rf = saturate(1.0 - ac * 3.0); + pat = lerp(pat, float3(1.0, 1.0, 1.0), exp(-abs(dy) * 2.5) * ROLLB * rf); + pat *= 1.0 - 0.85 * rf * smoothstep(0.42, 0.10, abs(dy + 0.30)); + + // The glass. Each tube dims towards its own corners, which is scene + // content and not the post pass's vignette - that one is on the room. + pat *= 1.0 - 0.38 * saturate(dot(s, s) * 0.55); + + // Flicker, out of sync between tubes. Two fast sines whose rates come off + // the tube's hash, so no two monitors ever line up, plus an occasional + // deeper dip on a very slow timer - that dip is the thing the eye + // actually catches, the fast wobble just stops the wall being dead. + // FLICKF has a per-programme FLOOR, so the table can no longer leave the + // wall frozen by passing 0. + float fl = 1.0 + FLICKF * (0.11 * sin(t * (4.7 + h * 4.1) + h * 37.0) + + 0.05 * sin(t * (13.0 + h * 6.0) + h * 11.0)); + fl -= FLICKF * 0.34 * smoothstep(0.84, 1.0, sin(t * (0.61 + h * 0.83) + h * 19.0) * 0.5 + 0.5); + + // The refresh. A column of about three tubes brightening as it sweeps + // across the wall - the one whole-wall event in the room, and the reason + // a locked-off shot is worth three bars. It only ever ADDS, and only to + // roughly an eighth of the grid at a time, so the frame's overall level + // moves by a couple of percent: this is a wipe, not a flash. + float wx = (frac(t * WIPER + 0.13) * 2.2 - 1.1) * GRIDX; + float wip = exp(-abs(id.x - wx) * 0.85) * WIPEA; + + // Some of these tubes are older than others. The spread is 0.86 to 1.06 + // rather than 0.80 to 1.14: a wider spread pushed the bright tubes' whole + // staircase up into the clip and they went flat. + float3 lit = pat * max(fl, 0.0) * (0.86 + 0.20 * h) * (TUBEG + wip); + + // One pixel crossfade across the aperture edge. + return lerp(bez, lit, saturate(0.5 - sd / (2.0 * ac))); +} + +float3 monitorWall(float2 w, float t, float aa, bool captionInk) +{ + float3 base = monitorGrid(w, t, aa); + if (gCaption.x < 0.5 || gCaption.x >= 3.5 || gCaption.y <= 0.0) return base; + float2 p = w - float2(0.0, 7.2); + if (any(abs(p) > float2(6.35, 4.75))) return base; + float edge = sdBox2(p, float2(6.10, 4.50)) - 0.18; + float panel = saturate(0.5 - edge / max(aa, 0.001)); + float glass = saturate(0.5 - (sdBox2(p, float2(5.70, 4.10)) - 0.12) / max(aa, 0.001)); + float2 textSize = float2(gCaption.x < 1.5 ? 9.75 : 10.65, 1.05); + float2 uv = float2(p.x, -p.y) / textSize + 0.5; + float ink = captionInk ? inscriptionMask(uv, aa / textSize, gCaption.x) : 0.0; + float3 tube = lerp(float3(0.025, 0.032, 0.038), + float3(0.065, 0.105, 0.090), glass); + tube += float3(0.72, 0.91, 0.79) * ink * glass; + return lerp(base, tube, panel * saturate(gCaption.y)); +} + +// The room light, sampled as three points spread across the wall rather than +// one. Three is enough to make a plinth's shadow spread and soften as it runs +// towards the camera, which one point light cannot do at any cost. +// +// The amplitude is small on purpose. This modulates the brightness of the +// whole room, and the room is being quantised to ten levels downstream: at +// the old +-14% the entire floor stepped between two ramp colours in unison, +// which reads as the render breaking, not as a tube breathing. The tubes +// themselves still flicker hard - that is where the eye is looking. +float lightFlick(float k, float t) +{ + return 1.0 + FLICKF * (0.035 * sin(t * (5.1 + k * 1.7) + k * 9.0) + + 0.020 * sin(t * (12.0 + k * 3.0) + k * 4.0)); +} + +// Box filtered chequerboard. p is in chequer units (one square = 1), w is the +// screen space footprint in the same units. Converges to a flat 0.5 exactly +// where the squares stop being resolvable, which is what stops the horizon +// turning into a moire fight with the ordered dither. Analytic integral of +// the xor pattern over the footprint - the cost is two frac and two abs more +// than the unfiltered version. +float checker(float2 p, float2 w) +{ + float2 i = 2.0 * (abs(frac((p - 0.5 * w) * 0.5) - 0.5) - + abs(frac((p + 0.5 * w) * 0.5) - 0.5)) / w; + return 0.5 - 0.5 * i.x * i.y; +} + +// ---- shading ------------------------------------------------------------- + +float4 main(VSOut i) : SV_Target +{ + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y is FLIPPED here on purpose. The fullscreen triangle emits + // uv.y = 0 at the top of the screen, so i.uv*2-1 gives -1 up + // there and the top of the frame ended up looking DOWNWARD - + // the cathedral vault springs at y=+12 and was rendering in the + // lower half, the monolith hung from the ceiling. + uv.x *= gTime.w; + + float3 fw = normalize(gDir.xyz); + float3 rt = normalize(cross(float3(0.0, 1.0, 0.0), fw)); + float3 up = cross(fw, rt); + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 ro = gCam.xyz; + float3 rd = normalize(fw + (uv.x * (rt * cr + up * sr) + + uv.y * (up * cr - rt * sr)) * gCam.w); + + float time = gTime.x; + + // The three samples of the wall that light the room. Derived from the + // programme's grid rather than hardcoded, so when P0 stacks five short + // rows of tubes and P2 stacks two tall ones the light still comes from + // where the light actually is. + float3 kL0 = float3(-20.0, LIGHT_Y - 0.35, WALL_Z); + float3 kL1 = float3( 0.0, LIGHT_Y + 0.35, WALL_Z); + float3 kL2 = float3( 20.0, LIGHT_Y - 0.35, WALL_Z); + + // World units subtended by one virtual pixel, per unit of depth. Every + // filter width in the shader is built out of this. + const float px = 2.0 / 360.0; + float pxw = gCam.w * px; + + // ---- the analytic half of the scene --------------------------------- + // Floor plane y = 0, ceiling plane y = CEIL_Y. + float tFlr = (rd.y < -1e-4) ? (-ro.y / rd.y) : FAR; + float tCei = (rd.y > 1e-4) ? ((CEIL_Y - ro.y) / rd.y) : FAR; + tFlr = (tFlr > 0.0) ? min(tFlr, FAR) : FAR; + tCei = (tCei > 0.0) ? min(tCei, FAR) : FAR; + + // The monitor wall slab, as a ray/AABB. Exact, so the hit point is ON the + // wall and "is this the wall" is not a question anyone has to answer with + // an epsilon. sgn keeps the reciprocal finite for an axis aligned ray, so + // a 0 * inf NaN cannot get into the min/max chain. + float3 wc = float3(0.0, CEIL_Y * 0.5, WALL_Z + WALL_TH); + float3 wb = float3(WALL_HW, CEIL_Y * 0.5, WALL_TH); + float3 sgn = float3(rd.x < 0.0 ? -1.0 : 1.0, + rd.y < 0.0 ? -1.0 : 1.0, + rd.z < 0.0 ? -1.0 : 1.0); + float3 iv = sgn / max(abs(rd), 1e-6); + float3 t1 = (wc - wb - ro) * iv; + float3 t2 = (wc + wb - ro) * iv; + float3 tsm = min(t1, t2), tbg = max(t1, t2); + float wN = max(tsm.x, max(tsm.y, tsm.z)); + float wF = min(tbg.x, min(tbg.y, tbg.z)); + float tWal = (wF >= max(wN, 0.0) && wN > 0.0) ? min(wN, FAR) : FAR; + // Which face. The front face is the one the grid is on. + bool wallFront = (tsm.z >= tsm.x) && (tsm.z >= tsm.y) && (rd.z > 0.0); + + // ---- march the plinths, and only up to the first analytic surface ---- + float tCap = min(min(tFlr, tCei), tWal); + float tHit = FAR; + float tm = 0.0; + + [loop] for (int k = 0; k < 72; k++) { + if (tm >= tCap) break; + float h = mapPlinths(ro + rd * tm); + if (h < 0.0018 * tm + 3e-4) { tHit = tm; break; } + tm += h * 0.92; + } + + // 0 plinth, 1 floor, 2 wall, 3 ceiling, 4 nothing. + int surf; + float t; + if (tHit < tCap) { surf = 0; t = tHit; } + else if (tCap >= FAR) { surf = 4; t = FAR; } + else if (tWal <= tFlr && tWal <= tCei) { surf = 2; t = tWal; } + else if (tFlr <= tCei) { surf = 1; t = tFlr; } + else { surf = 3; t = tCei; } + + float3 col = float3(0.0, 0.0, 0.0); + + if (surf == 2) { + // The wall is emissive, nothing lights it. Its side and top faces are + // bare slab. + float3 p = ro + rd * t; + col = wallFront ? monitorWall(p.xy, time, t * pxw / max(abs(rd.z), 0.15), true) + : float3(0.022, 0.026, 0.036); + } + else if (surf == 3) { + // Ceiling. Shaded flat from the middle light with the normal known to + // be straight down. It stays very dark on purpose - it sees as much + // of the monitor wall as the floor does and would otherwise come out + // just as bright, which would put the second brightest thing in the + // frame along the top edge and pull the eye straight out of the room. + float3 dl = kL1 - (ro + rd * t); + float ql = dot(dl, dl); + col = float3(0.045, 0.048, 0.058) * float3(0.62, 0.84, 0.92) + * (saturate(-dl.y * rsqrt(ql)) / (1.0 + L_FALLOFF * ql)) + * 2.6 * ROOM_EXP; + } + else if (surf == 0 || surf == 1) { + bool isFloor = (surf == 1); + float3 p = ro + rd * t; + float3 n = isFloor ? float3(0.0, 1.0, 0.0) : mapNormal(p); + float3 albedo; + + if (isFloor) { + p.y = 0.0; // exact, so say so + // The chequers, at the programme's square size - a tight room + // gets a tight floor. Box filtered against the footprint of this + // pixel on the floor, which at a grazing angle is + // t * pixel / |rd.y| and blows up towards the horizon exactly as + // fast as the moire would have. The old exp(-t) contrast fade + // was doing the opposite of what it needed to: at t = 16 it had + // already thrown away a quarter of the contrast where the + // footprint was still a sixth of a square, and at t = 34, where + // the footprint was four fifths of a square and genuinely + // aliasing, it had only thrown away a third. + float fw2 = t * pxw / max(abs(rd.y), 0.02); + float2 c = p.xz * CHEQK; + float2 wf = float2(fw2, fw2) * CHEQK + 0.004; + // Both stops are the programme's. The floor is the largest thing + // in frame by area, so this pair moves the picture's overall + // level further than anything else in the file. + albedo = lerp(FLR_LO * float3(1.00, 1.08, 1.35), + FLR_HI * float3(1.00, 1.01, 1.07), checker(c, wf)); + } else { + // Plinth, or the object hovering over it. The objects are a + // little paler than the plinths so the row of them reads as a + // row of highlights above a row of silhouettes. The split is + // measured against THIS cell's cap, because the caps are no + // longer all at the same height. + albedo = (p.y > capTopAt(p) + 0.06) ? float3(0.56, 0.57, 0.60) + : float3(0.33, 0.34, 0.37); + } + + // One shadow ray, cast at the middle sample. All three sources sit on + // the same plane, so one ray gets the shape of the shadow right and + // the other two only soften it - three rays would cost 80% more + // marching to move the result by very little. + // + // The ray is cut off at the height above which nothing in the room + // can occlude anything, so from the floor it is about twenty four + // units long rather than the full thirty five to the wall. + float3 sd1 = kL1 - p; + float sl1 = length(sd1); + float3 sdr = sd1 / sl1; + float tex = (sdr.y > 1e-3) ? ((PLINTH_TOP - p.y) / sdr.y) : sl1; + float sh = softShadow(p + n * 0.035, sdr, clamp(tex, 0.0, sl1)); + + float3 d0 = kL0 - p, d1 = kL1 - p, d2 = kL2 - p; + float q0 = dot(d0, d0), q1 = dot(d1, d1), q2 = dot(d2, d2); + float3 lit = float3(0.0, 0.0, 0.0); + lit += saturate(dot(n, d0 * rsqrt(q0))) / (1.0 + L_FALLOFF * q0) * lightFlick(0.0, time); + lit += saturate(dot(n, d1 * rsqrt(q1))) / (1.0 + L_FALLOFF * q1) * lightFlick(1.0, time); + lit += saturate(dot(n, d2 * rsqrt(q2))) / (1.0 + L_FALLOFF * q2) * lightFlick(2.0, time); + lit *= 0.25 + 0.75 * sh; + + float ao = occlusion(p, n); + + // Cold cyan-white, the colour of a room lit by nothing but tubes. + float3 key = float3(0.62, 0.84, 0.92); + + col = albedo * key * lit * L_GAIN * ROOM_EXP; + // Bounce. Not a real GI term - just the fact that a surface turned + // towards the wall sees a great deal of glowing wall. It is large, + // and it is what stops the far end of the floor collapsing to black: + // with only the direct term the light chequers ran from level 8 at + // the camera to level 2 at the horizon, and six of the ten levels + // available went on one floor gradient. + col += albedo * key * L_BOUNCE * (0.30 + 0.70 * saturate(n.z)) * ao * ROOM_EXP; + col *= 0.35 + 0.65 * ao; + + // A rim, and it is doing real work rather than being decoration. The + // camera looks at the room from the near side, so every plinth and + // every object shows the face pointing AWAY from the only light there + // is, and comes out as a flat black cut-out. The wall behind them is + // enormous, so their edges genuinely catch it, and that edge is the + // only thing that says these are solid objects and not holes in the + // floor. + // + // Only on the plinths. A grazing view of the floor has a fresnel of + // nearly one across half the frame, so letting this term touch it + // paints a pale band along the bottom edge of every shot. + if (!isFloor) { + float fres = pow(1.0 - saturate(dot(n, -rd)), 4.0); + col += key * fres * 0.55 * ao * ROOM_EXP; + } + + // The wet floor. The wall is a known plane, so the mirror ray is one + // divide, not a second march. Plinths are missing from the + // reflection; at this gloss and this resolution the dither hides + // that, and a second march would double the cost of the shot. + // + // This is the single largest compositional difference between the + // three appearances, because it decides whether the bottom half of + // the frame carries a second copy of the monitor wall or is bare + // chequers. It had never once run: gTune.w is 0.00 in every row of + // the table, so GLOSSF is a per-programme floor that gTune adds to. + // + // The reflected sample gets a much larger footprint than the direct + // one - the whole wall is squashed into a few rows of pixels near the + // horizon - so monitorWall filters itself down to flat bands there + // instead of drawing a programme into a two pixel strip. + if (isFloor && GLOSSF > 0.001) { + float3 rr = reflect(rd, n); + if (rr.z > 0.02) { + float dR = (WALL_Z - p.z) / rr.z; + float3 wp = p + rr * dR; + if (abs(wp.x) < WALL_HW && wp.y > 0.0 && wp.y < CEIL_Y) { + float fr = pow(1.0 - saturate(dot(n, -rd)), 5.0); + float aaR = (t + dR) * pxw / max(abs(rd.y), 0.03); + col += monitorWall(wp.xy, time, aaR, false) + * GLOSSF * (0.05 + 0.95 * fr) * 0.85; + } + } + } + } + + // Cold haze, at the programme's density. The room has air in it, which is + // why the floor runs out into nothing instead of ending at a visible + // edge, and why there is a glow hanging in front of the monitor wall. + // How thick that air is decides how far back the floor survives, which is + // most of what separates a tight room from a deep one. + float fog = exp(-t * FOGK); + col = col * fog + float3(0.020, 0.024, 0.034) * (1.0 - fog); + col += float3(0.30, 0.42, 0.50) * (1.0 - fog) * saturate(rd.z + 0.25) * 0.14 * gTune.x; + + return float4(col * gTime.z, 1.0); +} diff --git a/fx/post.hlsl b/fx/post.hlsl new file mode 100644 index 0000000..3df4000 --- /dev/null +++ b/fx/post.hlsl @@ -0,0 +1,229 @@ +// The CRT pass. Takes the low resolution scene target and puts it on a tube. +// +// The one thing that matters here and is easy to get wrong: this pass works +// in TWO coordinate spaces at once. +// +// virtual space - the low resolution grid the scene was drawn on +// (gRes.xy, a fixed 360 lines). Scanlines and the ordered +// dither live here, so a scanline is always one virtual +// row and the dither is always one virtual pixel. That is +// what keeps the look identical from 1366x768 to +// 5120x1440 instead of turning into fine grey noise on a +// big display. +// +// output space - real backbuffer pixels (SV_Position). The shadow mask +// and the rim of the glass live here, because an aperture +// grille is a property of the tube, not of the signal. +// +// Mixing the two up is why most CRT shaders look wrong at some resolution. + +cbuffer Post : register(b0) +{ + float4 gRes; // xy virtual size, zw output size + float4 gCrt; // x scanline depth, y mask depth, z aberration, w vignette + float4 gGrade; // x dither, y exposure, z dissolve mix, w fade to black + float4 gFx; // x flash, y whip smear, z bloom level, w letterbox + float4 gRamp[4];// the quantiser's four stops. this is the palette. + float4 gTone; // x hue kept, y quantiser steps, zw unused +}; + +Texture2D gScene : register(t0); +Texture2D gPrev : register(t1); // the outgoing shot, during a dissolve +Texture2D gBloom : register(t2); // half res, already blurred +SamplerState gPoint : register(s0); +SamplerState gLinear: register(s1); + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// 4x4 ordered Bayer, the Marathon look. Values 0..15. +static const float kBayer[16] = { + 0.0, 8.0, 2.0, 10.0, + 12.0, 4.0, 14.0, 6.0, + 3.0, 11.0, 1.0, 9.0, + 15.0, 7.0, 13.0, 5.0 +}; + +// A narrow cold ramp. Quantising into this rather than into plain grey is +// what makes the dither read as an old shaded renderer instead of noise. +float3 rampColour(float v) +{ + // The four stops come from the section's grade rather than from literals. + // Changing them changes what the picture is MADE OF - every pixel passes + // through here - which is a categorically stronger thing than tinting + // the output. Interpolated in C across a dissolve, so two sections in + // different palettes cross-fade as grades and not as a colour switch. + float3 a = gRamp[0].rgb; + float3 b = gRamp[1].rgb; + float3 c = gRamp[2].rgb; + float3 d = gRamp[3].rgb; + float3 lo = lerp(a, b, saturate(v * 3.0)); + float3 md = lerp(b, c, saturate(v * 3.0 - 1.0)); + float3 hi = lerp(c, d, saturate(v * 3.0 - 2.0)); + return v < 0.3333 ? lo : (v < 0.6666 ? md : hi); +} + +float4 main(VSOut i) : SV_Target +{ + float2 uv = i.uv; + + // Gentle barrel distortion. Enough to feel like glass, not enough to + // make anyone think about it. + float2 cc = uv * 2.0 - 1.0; + float r2 = dot(cc, cc); + uv = (cc * (1.0 + 0.028 * r2)) * 0.5 + 0.5; + + // THE RIM OF THE TUBE, feathered over one OUTPUT pixel instead of being a + // hard in/out test. The barrel maps a straight screen edge to a shallow + // curve, and a binary test on a shallow curve is a staircase with steps + // tens of pixels long - which then crawls, one step at a time, whenever + // anything behind it moves. Measured in output pixels rather than virtual + // ones so the feather stays one pixel wide at every resolution instead of + // becoming a three pixel smear at 1080. + float2 inPx = min(uv, 1.0 - uv) * gRes.zw; + float rim = saturate(min(inPx.x, inPx.y) + 0.5); + if (rim <= 0.0) + return float4(0.0, 0.0, 0.0, 1.0); + + // Chromatic aberration, growing towards the edge of the tube. + float2 ab = cc * gCrt.z * (0.4 + 0.6 * r2) / gRes.xy; + float3 col; + col.r = gScene.Sample(gPoint, uv + ab).r; + col.g = gScene.Sample(gPoint, uv).g; + col.b = gScene.Sample(gPoint, uv - ab).b; + + // A whip-in is a lateral smear that settles. It is what lets a fast pan + // hand off into the next shot instead of just stopping dead. + if (gFx.y > 0.002) { + float3 sm = float3(0.0, 0.0, 0.0); + [unroll] for (int k = 1; k <= 6; k++) { + float2 o = float2(gFx.y * 0.085 * float(k), 0.0); + sm += gScene.Sample(gLinear, saturate(uv - o)).rgb; + } + col = lerp(col, sm / 6.0, saturate(gFx.y)); + } + + // A dissolve wants the two shots mixed BEFORE the tube, so the dither + // and the scanlines belong to the composite rather than to each half. + if (gGrade.z > 0.002) + col = lerp(col, gPrev.Sample(gPoint, uv).rgb, saturate(gGrade.z)); + + // BLOOM, added before the quantiser rather than after it. That order is + // the whole reason it works here: added afterwards it would sit on top of + // the dither as a smooth gradient and the two would visibly disagree + // about what resolution the picture is. Added first, the glow is + // dithered along with everything else and belongs to the same image. + col += gBloom.Sample(gLinear, uv).rgb * gFx.z; + + col *= gGrade.y; // exposure + + // --- virtual space: dither ------------------------------------------- + // Quantise the luminance through the ordered matrix, then re-colour + // through the ramp and keep the original hue on top of it. + // + // THE WIDTH OF THE DITHER IS THE ENTIRE ARGUMENT IN THIS BLOCK, and it + // is measured, not tasted. An ordered dither spends NEIGHBOURING pixels + // to buy a value that lies between two quantiser levels, so it has to + // span the gap between two adjacent levels - one step - and the moment + // it spans more than that it stops carrying information and starts + // adding noise to a picture that is already quantised. + // + // As shipped, the Bayer threshold spans +-0.469 and was multiplied by + // gGrade.x = 0.85 against steps of 1/10. That is EIGHT levels wide. + // Two consequences, both of them visible in every single shot: + // + // - at the ends of the range the noise is rectified, because saturate() + // throws away the half of the swing that would have gone below black + // and keeps the half that goes above it. A scene value of exact black + // came off the tube as a 16 cell speckle reaching level 4 of 10; + // empty sky measured 4% mean with 20% peaks in a picture whose black + // stop is 3%. White was crushed the same way from the other side. + // + // - in the midtones a flat 50% grey became a mosaic running from near + // black to near white. Local contrast from the dither was five times + // the local contrast of anything the scene had drawn, so every shot + // converged on the same grey field and any two of them measured + // alike no matter what was in front of the camera. + // + // So the width is expressed in QUANTISER STEPS now, and bounded: + // + // one step at either end of the range. That is the mean preserving + // minimum, and it is the widest dither that provably cannot lift black + // or dirty white: the largest Bayer offset is 0.469 of a step, which + // never rounds up to the next level on its own. Black stays exactly on + // stop 0, white stays exactly on stop 3, and the dark gradients still + // dither rather than band. + // + // about two and a bit steps through the midtones, where a wider dither + // IS the texture the look is made of - scaled by the grade's own + // dither knob, so bleach (7 steps, dither 1.00) still speckles far + // harder than warm (11 steps, 0.82), which is what those numbers were + // chosen for. + // + // The taper between the two is the shape term, and it is smooth, so a + // gradient running down into black narrows its own dither on the way. + float2 vp = floor(uv * gRes.xy); + int bi = int(fmod(vp.y, 4.0)) * 4 + int(fmod(vp.x, 4.0)); + float thr = (kBayer[bi] + 0.5) / 16.0 - 0.5; // +-0.469, evenly spaced + + float lum = dot(col, float3(0.299, 0.587, 0.114)); + float steps = max(gTone.y, 1.0); + + float shape = smoothstep(0.0, 0.15, lum) * smoothstep(0.0, 0.12, 1.0 - lum); + float width = (1.0 + 1.30 * gGrade.x * shape) / steps; + + float q = floor(saturate(lum + thr * width) * steps + 0.5) / steps; + + float3 dithered = rampColour(q); + // The hue divide gets a floor of one 8-bit code rather than 1e-4. The + // scene target is 16 bit float, so it is full of values below that, and + // a 1/lum with a 1e-4 floor makes the difference between a pixel at zero + // and a pixel at one ten-thousandth a factor of 2.6 in the output. That + // was hidden under the old speckle. With black actually black it would + // read as an edge between two shades of nothing. + float3 hue = col / max(lum, 1.0 / 255.0); + col = lerp(dithered, dithered * hue, gTone.x); + + // --- virtual space: scanlines ---------------------------------------- + float sl = sin(uv.y * gRes.y * 3.14159265); + col *= 1.0 - gCrt.x * sl * sl; + + // --- output space: aperture grille ----------------------------------- + // Three-phosphor stripe on real pixels. On a small window this is almost + // invisible, which is correct: so is a real shadow mask. + float phase = fmod(i.pos.x, 3.0); + float3 mask = float3(phase < 1.0 ? 1.0 : 0.72, + (phase >= 1.0 && phase < 2.0) ? 1.0 : 0.72, + phase >= 2.0 ? 1.0 : 0.72); + col *= lerp(float3(1.0, 1.0, 1.0), mask, gCrt.y); + + // Vignette, then the shot fade, then the flash. The flash is added last + // and deliberately after the dither: a cut should blow the quantiser + // out, not be quantised along with everything else. The vignette is + // clamped because 0.26 * r2 * r2 passes 1.0 in the extreme corners and a + // negative multiplier there would subtract from the flash instead of + // being masked by it. + col *= saturate(1.0 - gCrt.w * r2 * r2); + col *= gGrade.w; + col += gFx.x * float3(1.00, 0.97, 0.92); + + // THE MATTE, last, and in the distorted uv rather than in output space: + // the bars are part of the signal, so they bow with the glass like + // everything else on the tube. A straight bar over a curved picture is + // the tell that the letterbox was pasted on afterwards. + // + // Softened over one virtual line, because the edge is a curve crossing a + // pixel grid and a hard threshold on it crawls. + if (gFx.w > 0.0005) + col *= smoothstep(0.0, 1.0 / gRes.y, + (0.5 - gFx.w) - abs(uv.y - 0.5)); + + // The rim goes on after the flash, so a cut blows out the picture and + // not the frame around it. + col *= rim; + + return float4(col, 1.0); +} diff --git a/fx/poweroff.hlsl b/fx/poweroff.hlsl new file mode 100644 index 0000000..79682c5 --- /dev/null +++ b/fx/poweroff.hlsl @@ -0,0 +1,598 @@ +// Shot 39 - CRT POWER-OFF. The last shot of the demo. +// +// Nothing here is marched. This is a two dimensional shader, like the cat eye +// that opens the demo, and for the same reason: the subject is the picture +// itself, so there is no space to fly through. The camera fields are read and +// ignored on purpose - a tube dying is a locked-off frame, and a drifting +// camera would fight it. +// +// The collapse is not a squash, and getting the order right is the whole shot: +// +// 0. Before anything geometric happens the tube is ALREADY failing, and it +// has to be, or the collapse arrives out of nowhere. The vertical hold +// lets go first: the frame walks up the glass and wraps, dragging the +// blanking bar and its retrace streak across the picture. The vertical +// linearity goes with it, so the top of the frame compresses before the +// rest of it does. The three guns stop landing on the same spot, which +// is the red-left/blue-right fringe. That is the anticipation, and it is +// the difference between inevitable and abrupt. +// +// 1. The high voltage sags. The beam is accelerated less, the yoke +// therefore deflects it further, and the picture SWELLS a little, goes +// soft, and brightens before anything else happens. That swell is the +// detail that says "a tube switched off" rather than "a wipe". +// +// 2. The vertical deflection dies, and it dies far faster than the +// horizontal. The picture crushes to a full-width line while the width +// is still untouched. Every scanline in the frame is now landing on top +// of every other one, so the line carries the whole frame's beam energy +// in a single row - it is blown out, and it is brightest at the two +// ends, where the sweep turns around and the beam dwells. Behind it the +// whole picture is still faintly there in the phosphor, unlit. +// +// 3. Only then does the horizontal go, and the line closes to a point. +// +// 4. The point is not the end. Phosphor keeps glowing after the beam that +// lit it has stopped, green longest of the three, which is why the last +// thing left on a dead tube is a small green-white star that takes a +// second to go out - and a horizontal smear either side of it, where the +// line was a moment ago and the phosphor has not caught up. +// +// What is on screen when it dies is the cat eye, one last time, already sick +// with tearing and a rolling hum bar. The demo opened on that eye; ending on +// it and then killing the tube under it is the only cut this shot needs. +// +// gTune.x collapse progress on the FIRST frame of the shot, 0..1 +// gTune.y how fast that progress advances, per second +// gTune.z signal decay the picture ALREADY had before the switch +// gTune.w phosphor persistence of the dot afterwards +// +// gTune.x is the driver the edit steers by, and it runs 0 to 1 - but tune is +// constant for the length of a shot, so the shader needs both the value it +// starts at and the rate it moves. x is where the collapse begins and y is +// how long it takes (rate = 1 / seconds). +// +// SELF-TIMED MODE. y = 0 used to mean "freeze at exactly x", and the shot +// this scene is actually cut into passes {1, 0, 0, 0} - which froze it at +// x = 1, i.e. parked the whole final 1.8 seconds of the demo on one static +// frame of an already-dead tube at full flare. Every envelope in here is a +// function of k, k was constant, and so the last thing the audience saw was +// a still. A held pose is worth having; a held pose is not worth having as +// the default that an unfilled tune falls into. So y = 0 now means "time +// yourself", and x becomes a tempo scale on the shader's own schedule: +// +// rate = 1.14 * x (x = 0 reads as 1, so {0,0,0,0} works too) +// k = (t - 0.62) * rate +// +// which holds the failing picture for the first six tenths, closes the line +// at t = 1.32 and leaves half a second of star. Those numbers are cut against +// what actually reaches the screen: the shot's TR_BLACK fades it up over 14 +// rows, so brightness is under 15% for the first six tenths of a second and +// there is no point spending the collapse down there. In self-timed mode w = +// 0 likewise reads as "the usual persistence" rather than "none". Any shot +// that passes a real rate gets the documented behaviour, unchanged, including +// the freeze. +// +// Landmarks on that 0..1 clock, so a cut can be placed on one: +// +// < 0.00 the pre-roll: rolling frame, tearing, misconvergence +// 0.00 - 0.15 the swell, and the last stretch that is still a picture +// 0.18 - 0.48 the vertical dies (eased, so it is over near 0.36) +// 0.36 - 0.54 the held line, full width. this is the shot +// 0.54 - 0.80 the width closes +// 0.80 the flare, and the point +// 0.80 + the phosphor, on a clock in SECONDS, not in k +// +// The afterglow is deliberately NOT on the k clock. Held there it could never +// outlive k reaching 1, so "a second of glow" was only reachable by making the +// whole collapse four seconds long. It runs on real time now, so the collapse +// is as fast as the edit wants and the star still takes its second. +// +// The yoke ring is a sine in k, and at the rates this shot uses it advances +// most of a radian per frame - so it rings in POSITION, not in height. Height +// feeds the beam-energy gain, which is brightness, and a brightness wobble at +// that rate is a photosensitivity event. A displacement of the same size is +// a shudder, which is what a coil losing its drive actually does. + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look, w roll + float4 gTune; +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// One virtual row, in the [-1,1] units this shader works in. 360 lines, so a +// row is 2/360. Every softness in here is floored at roughly this, because a +// transition narrower than a pixel is not a soft edge, it is a stair that +// crawls - and the post pass dithers on top of it, which makes it worse. +#define ROW 0.00556 + +float hash12(float2 p) +{ + float3 q = frac(float3(p.xyx) * 0.1031); + q += dot(q, q.yzx + 33.33); + return frac((q.x + q.y) * q.z); +} + +// Everything the tube is doing wrong, gathered into one value. All six fields +// are uniform across the draw - they are functions of the collapse clock and +// nothing else - so this is passed by value into a function called up to +// seven times per pixel and the compiler keeps the whole thing in registers. +struct Fault +{ + float sick; // how broken the signal is: tearing, hum, speckle + float hf; // how much authority per-row detail still has + float roll; // vertical hold slip, in picture units + float nlin; // vertical linearity error - the top compresses first + float bar; // blanking bar and retrace streak, at the wrap seam + float lift; // the one voice left in the outro, as a floor-safe lift +}; + +// The picture the tube is showing, in raster coordinates: x in [-aspect, +// aspect], y in [-1,1], regardless of what the deflection is currently doing +// to it. Everything the collapse does is a coordinate change on top of this. +// +// `hf` is how much authority the per-row detail still has. Once the vertical +// has crushed the picture, one output row is an average of dozens of picture +// rows, and five taps cannot average dozens of independent random rows - it +// samples five of them, which is not a filter, it is noise. Physically the +// tear and the speckle DO average away as the frame folds, so the honest fix +// and the cheap fix are the same one: fade them out with the fold. +float3 signalAt(float2 c, Fault f) +{ + // ---- the vertical hold, which is the first thing to go ---------------- + // The frame walks up the glass and wraps. This is a coordinate change on + // the PICTURE, not on the raster: the raster is still where it was, which + // is why the caller's extent test is done before this and not after. + float wr = frac((c.y - f.roll + 1.0) * 0.5); // 0..1 across the frame + float sd = min(wr, 1.0 - wr); // 0 at the wrap seam + float yy = wr * 2.0 - 1.0; + + // Vertical linearity goes before vertical amplitude does: the sawtooth + // starts from the most depleted part of the supply, so the top of the + // picture squashes while the bottom is still the right size. Small, but + // it means the eventual crush is the end of something already happening + // rather than an event on its own. + yy += f.nlin * yy * saturate(yy); + + float row = floor((yy + 1.0) * 180.0); + float tick = floor(gTime.x * 26.0); + + // Line tearing, in BLOCKS of rows. This was per-row, and per-row tearing + // does not read as tearing: on the highest contrast edge in the frame - + // the pupil slit - it reads as a chewed edge, because neighbouring rows + // land on opposite sides of it. Real tearing displaces a contiguous run + // of lines, and it is the intact runs in between that make the broken + // ones read as broken. + float band = floor(row * 0.125); // 8-row blocks + float pick = hash12(float2(band * 0.37, tick + 7.0)); + float jit = hash12(float2(band, tick)) - 0.5; + c.x += jit * 0.17 * f.sick * f.hf * step(0.72, pick); + + // Near-black, slightly blue, a touch heavier at the top and bottom of the + // frame so the raster has a shape even before anything is drawn in it. + float3 col = float3(0.016, 0.019, 0.030) * (1.0 - 0.35 * abs(yy)); + + // The eye. One bold disc, a hard slit, one small specular - three shapes, + // which is all that survives a 4x4 dither at 360 lines. It is framed to + // sit on top of the demo's opening shot: eye.hlsl fills out to r = 0.84, + // so this does too. It was 0.70 and a quarter smaller, which is enough to + // break the rhyme with the shot the demo opened on - and a small disc in + // a black field is not a composition. + float2 e = float2(c.x, yy) - float2(0.0, 0.03); + float r = length(e * float2(1.0, 1.04)); + + float3 grn = lerp(float3(0.34, 0.95, 0.32), float3(0.05, 0.26, 0.09), + smoothstep(0.12, 0.92, r)); + + // The limbal ring. Without it the iris is one flat value across its whole + // area and the disc reads as a light bulb rather than as an eye - and a + // flat disc is also the first thing a ten step dither turns into a + // poster. One smoothstep buys the shape back. + grn *= 1.0 - 0.80 * smoothstep(0.60, 0.86, r); + + float iris = smoothstep(0.95, 0.84, r); + float2 q = float2(e.x / 0.112, e.y / 0.62); + float pup = smoothstep(1.06, 0.90, length(q)); + + col += grn * iris * (1.0 - 0.94 * pup); + + // The ambient glow is masked by the pupil as well. Added flat over the + // top it lifted the slit to a third of full green and the eye stopped + // having a dark centre, which is the one value the composition needs. + col += float3(0.09, 0.30, 0.11) * exp(-r * 1.55) * (1.0 - 0.45 * pup); + + // The pupil is not a hole. It is glass with something behind it. + col += pup * float3(0.020, 0.030, 0.034); + + // The wet specular, in the same place the opening shot puts it. This is + // the brightest thing in the picture and it has to stay that way, or the + // eye is a flat disc. + col += float3(0.60, 0.68, 0.63) * + smoothstep(0.150, 0.040, length(e - float2(-0.220, 0.340))); + + // The hum bar: a soft bright band crawling UP the frame, which is what a + // failing supply looks like before it stops being a supply at all. It + // crawls FASTER as the supply gives up, because the ripple is drifting + // away from the field rate - a hum bar that keeps a constant speed is a + // hum bar on a tube that is fine. + float hum = frac(yy * 0.5 + 0.5 + gTime.x * (0.30 + 0.55 * f.sick)); + hum = exp(-(hum - 0.5) * (hum - 0.5) * 26.0); + col += float3(0.050, 0.056, 0.070) * hum * f.sick * (1.0 + 0.45 * f.lift); + + // Speckle, quantised to pairs of rows so it reads as signal and not as + // film grain, and so it is coarse enough to survive the box filter + // instead of turning into a crawl. + float sp = hash12(float2(floor(row * 0.5) * 1.7, tick * 3.1 + 19.0)); + col += (sp - 0.45) * 0.10 * f.sick * f.hf; + + // The blanking interval, dragged into shot by the roll. This is the one + // piece of the pre-roll that is unambiguous at a glance: a dark band with + // a thin bright retrace streak down the middle of it, crossing the frame. + // Nothing else in the demo looks remotely like it, which is the point - + // the audience has to be told the tube is going before it goes. + float blank = exp(-sd * sd * 900.0) * f.bar; + col *= 1.0 - 0.88 * blank; + col += float3(0.10, 0.15, 0.11) * exp(-sd * sd * 30000.0) * f.bar * 0.60; + + return max(col, 0.0); +} + +float4 main(VSOut i) : SV_Target +{ + float2 p = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y flipped, same reason as every other scene: uv.y = 0 is the + // top of the screen, and a tube collapses towards its centre + // line, not away from it. + p.x *= gTime.w; + float A = gTime.w; + + // ---- the clock -------------------------------------------------------- + // Written as ku0 + t * rate in BOTH branches, so anything that needs to + // know "at what time does the clock read v" can invert it: t = (v - ku0) + // / rate. The roll uses that to stop itself. + float rate, ku0, persist; + if (gTune.y > 1e-4) { + rate = gTune.y; // the edit gave a rate + ku0 = gTune.x; + persist = saturate(gTune.w); + } else { + rate = 1.14 * ((gTune.x > 0.01) ? gTune.x : 1.0); + ku0 = -0.62 * rate; // six tenths of failing first + persist = (gTune.w > 0.001) ? saturate(gTune.w) : 0.60; + } + + // ku is the collapse clock and it is NOT clamped at either end. The + // pre-roll needs it negative, so the failure has somewhere to live before + // the geometry moves; the phosphor after the closure needs it to keep + // running past 1, or the afterglow plateaus at whatever value it had + // reached when k hit its ceiling and the tube never actually goes out. + float ku = ku0 + gTime.x * rate; + float k = saturate(ku); + + // How far into the failure we are, before the switch does anything + // geometric. In authored mode ku starts at gTune.x >= 0, so this is 1 + // from the first frame and the shot behaves exactly as it always did. + float pre = smoothstep(-0.62, 0.02, ku); + + // ---- the three envelopes --------------------------------------------- + // The swell is a bump, not a ramp: the picture grows, and then the thing + // that was making it grow takes the deflection with it. + float swell = smoothstep(0.00, 0.15, k) * smoothstep(0.32, 0.18, k); + + // Vertical first, and fast. Cubic ease-out, so almost all of the travel + // happens in the first third of the window and the last sliver of height + // creeps away - which is what makes the line feel like it snapped shut. + float vv = smoothstep(0.18, 0.48, k); + float iv = 1.0 - vv; + float vc = 1.0 - iv * iv * iv; + + // Horizontal only starts once vertical is done, and the gap between the + // two is the held line. That hold is the shot. This one is NOT eased out + // the way the vertical is: the width has to still be visibly closing + // right up to the last moment, or the point arrives before the line has + // finished being a line and the collapse loses its ending. + float hc = smoothstep(0.54, 0.80, k); + + // ---- what is wrong with the signal ------------------------------------ + // gTune.z is how sick the picture ALREADY was when the shot started. The + // death adds its own on top, and it has to: a tube whose vertical hold + // has let go is tearing, whatever the edit asked for. It peaks exactly on + // the switch, which is where the sync loses lock hardest. + float sick = saturate(gTune.z + + pre * (0.55 + 0.45 * smoothstep(0.02, 0.16, k))); + + // The slip, in picture heights, and it STOPS. There is no rolling once + // there is no vertical sweep left to lose lock on, and a phosphor ghost + // that keeps sliding is a ghost of nothing. Freeze at k = 0.30, halfway + // through the crush, by clamping the time the roll integrates over. + float tStop = max((0.30 - ku0) / rate, 0.0); + float roll = 1.90 * pre * pre * min(gTime.x, tStop); + + // ---- the deflection --------------------------------------------------- + // 0.0030 is roughly half a virtual row at 360 lines, so the line really + // is one scanline tall rather than a thin rectangle. + float h = lerp(1.0, 0.0030, vc) * (1.0 + 0.10 * swell); + float w = lerp(1.0, 0.0026, hc) * (1.0 + 0.06 * swell); + + // The yoke does not stop cleanly, it rings - in POSITION. Peaks in the + // middle of the collapse and is gone by the time the line settles. The + // height keeps a much smaller share of the same ring so the line still + // reads as breathing, held under a tenth so it cannot count as a + // brightness transition however fast it goes. + float ringPh = k * 34.0; + float ringE = vc * (1.0 - vc); + float ring = 0.055 * ringE * sin(ringPh); + h *= 1.0 + 0.07 * ringE * sin(ringPh + 1.6); + + // The raster centre slips down as the vertical hold goes, and the line + // keeps a little of the picture's pincushion, so it is not a ruled edge. + float xn = p.x / A; + float yc = 0.030 * vc * (1.0 - 0.5 * vc) + ring; + float bow = 0.010 * vc * xn * xn; + + float sy = (p.y - yc - bow) / max(h, 1e-5); + float sx = p.x / max(w, 1e-5); + + // ---- the raster ------------------------------------------------------- + // One output row now covers 1/h rows of picture, so it has to be a box + // filter over that span or the collapse turns into aliased stripes + // instead of the picture folding into itself. Five taps is enough because + // by the time the span is wide the result is nearly a flat average. + // + // The floor on span is the HV sag defocus: less accelerating voltage is a + // fatter, softer spot, and without an explicit floor that softness never + // appeared, because span is derived from h and h has barely moved yet. + float span = min((2.0 / 360.0) / max(h, 1e-5), 2.4); + span = max(span * (1.0 + 0.8 * swell), 0.030 * swell); + + // How much of the frame is folding into one output row, 0..1. Drives the + // suppression of per-row detail inside signalAt. + float fold = saturate((span - 0.010) * 26.0); + + Fault F; + F.sick = sick; + F.hf = 1.0 - 0.92 * fold; + F.roll = roll; + F.nlin = 0.42 * pre * (1.0 - vc); + F.bar = pre * (1.0 - vc); + // The outro subtracts a voice a bar until only the organ is left, so the + // organ is the only thing still moving in the music under this shot. It + // lifts, it never scales: base + k*s, so no envelope can take the floor + // out from under the picture. + F.lift = saturate(gVoice.z); + + // span is a function of h and swell, both uniform, so the tap count below + // is the same for every pixel in the draw and the branch never diverges: + // for the first half second, while the picture is still a picture and the + // span is under a row, one tap IS the filter and the other four are paid + // for nothing. The count is a variable rather than two copies of the loop + // because two copies is two copies of signalAt in the blob. + int nt = (span < 1.6 * ROW) ? 1 : 5; + float ctr = 0.5 * float(nt - 1); + float stp = 0.25 * span; + + float3 sig = float3(0.0, 0.0, 0.0); + [loop] for (int t = 0; t < nt; t++) { + float y = sy + (float(t) - ctr) * stp; + if (abs(y) <= 1.0) sig += signalAt(float2(sx, y), F); + } + sig /= float(nt); + + // Misconvergence. As the high voltage sags the three beams stop landing + // on the same triad and the guns separate - red short of where it should + // be, blue past it. It is the single most legible symptom a colour tube + // has, it is the one the swell shares a cause with, and it is worth two + // extra taps: about eight pixels of fringe at 1920 at its widest. + // + // Gated on a UNIFORM value, so the branch is taken by the whole draw or + // by none of it. Two things about that gate are load-bearing: + // + // - It is (1 - 14*vc), not (1 - vc). These two taps sit OUTSIDE the box + // filter, so they are neither extent-tested nor divided by the tap + // count. The moment the fold starts rejecting taps, sig.g is a fifth + // of a partly-rejected sum while a raw tap is not, and replacing r and + // b with raw taps turns the whole screen magenta - the folded picture + // tiling in two channels at full strength behind a dim green one. It + // is a real bug and it cost a contact sheet to find. Zero the fringe + // before the fold can begin: at 14 it is out by k = 0.21, and the + // swell peaks at 0.16, so the widest fringe still lands intact. + // + // - The blend weight, not the sampled value, carries the extent test. + // Multiplying the value instead pulls r and b to black in the two or + // three percent of rows the raster has already lost, which is a green + // seam across the top and bottom of the frame. + float conv = (0.006 * pre + 0.010 * swell) * saturate(1.0 - 14.0 * vc); + [branch] if (conv > 0.0005) { + float m = (abs(sy) <= 1.0) ? 0.90 : 0.0; + float3 lo = signalAt(float2(sx - conv, sy), F); + float3 hi = signalAt(float2(sx + conv, sy), F); + sig.r = lerp(sig.r, lo.r, m); + sig.b = lerp(sig.b, hi.b, m); + } + + // Overscan: the picture runs past the glass at full width, so no edge is + // visible until the horizontal actually starts closing. This is measured + // in SCREEN units with a one-pixel floor on the softness. Written as a + // smoothstep on sx it was a fixed 4% of a half-width that ends up at + // 0.0026, i.e. a transition two thousandths of a pixel wide - a hard + // stair on the two ends of the line, exactly where the eye is looking. + // ROW is the softness floor in x as well as in y: the engine fixes the + // height at 360 lines and lets the width follow the aspect, so a virtual + // pixel is square at every aspect from 4:3 to 32:9 and 2/360 is one of + // them in either direction. + float halfW = A * w; + float xsoft = max(0.02 * halfW, ROW); + float xin = 1.0 - smoothstep(halfW - xsoft, halfW + xsoft, abs(p.x)); + + // Beam energy is conserved, so brightness goes up exactly as fast as the + // area goes down. Both gains are capped - the true 1/(h*w) is about six + // orders of magnitude and there is no point carrying it once the frame + // has clipped to white. + float vGain = min(1.0 / max(h, 0.0030), 42.0); + float hGain = min(1.0 / max(w, 0.0026), 4.5); + + // The ends of a collapsed line are brighter than the middle because the + // sweep decelerates into the turnaround and the beam sits there longer. + // The ramp starts further in than it did: at 0.50 the bright part landed + // under the overscan roll-off and never showed. + float ax = abs(p.x) / max(halfW, 1e-5); + float ends = 1.0 + 2.6 * vc * (1.0 - hc) * smoothstep(0.35, 0.96, ax); + + // The sweep is not steady either - it hunts, and a node travels along the + // line. Without this the held line is a still frame for a fifth of a + // second at the loudest moment of the shot. ax is clamped to 1 inside the + // visible line by xin, so this is always about one cycle across it, + // whatever the width has got down to. + ends *= 1.0 + 0.22 * vc * (1.0 - hc) * + sin(ax * 7.0 - gTime.x * 9.0); + + // The swell is not only bigger, it is brighter: the same beam, less of it + // being thrown away in the shadow mask. And the sag that causes the swell + // starts long before the swell does, so the picture is already lifting + // while it is still a picture - which is also the only reason anything is + // visible under the shot's fade-up for the first six tenths of a second. + float lift = 1.0 + 0.45 * swell + 0.75 * pre * (1.0 - vc); + float3 col = sig * (vGain * hGain * ends * xin * lift); + + // Once the line is carrying the whole frame it is past what any one gun + // can put out, so the colour walks to neutral - which is also why the + // classic power-off line is white and not the colour of the picture. Not + // all the way: the line is a column-average of the picture and holding a + // little of that is what stops it being a ruled white bar. + float lum = dot(col, float3(0.299, 0.587, 0.114)); + col = lerp(col, float3(lum, lum, lum), 0.62 * vc); + + // Bloom off the line. A row this overdriven does not stay inside its row: + // it blooms into its neighbours, white in the core and green at the + // edges, because that is the order the phosphors give up in. + float dl = abs(p.y - yc - bow); + float bloom = exp(-dl * 26.0) * vc * (1.0 - 0.85 * hc); + float bsoft = max(0.06 * halfW, ROW); + float bx = 1.0 - smoothstep(halfW - bsoft, halfW + 2.0 * bsoft, + abs(p.x)); + col += lerp(float3(0.30, 0.95, 0.36), float3(1.00, 1.00, 0.95), + exp(-dl * 90.0)) * bloom * 0.60 * bx; + + // ---- the dot ---------------------------------------------------------- + // Past a certain point the raster is two pixels wide and sampling it is a + // lottery, so the brightest thing in the last second of the demo is drawn + // rather than filtered. Hand over, do not add on top. + // + // The handover happens LATE. Starting it at hc = 0.80 crossfaded a line + // still 45 pixels wide into a round dot, and the flare - the single + // brightest frame of the shot - arrived as a soft green cloud with the + // line dissolved inside it. At 0.93 the line is a dozen pixels across + // when the dot takes over and the cut between the two is invisible. + float dotOn = smoothstep(0.93, 1.00, hc); + col *= 1.0 - dotOn; + + // 0.80 is where the width actually reaches zero, so both the flare and + // the decay are measured from the closure rather than from a round + // number - and they are measured in SECONDS, not in k. + // + // In k, the phosphor could never outlive k reaching 1, so "a second of + // afterglow" was only reachable by making the whole collapse four seconds + // long. Dividing by the rate decouples the two: the collapse is as fast + // as the edit wants and the dot still takes its second to go out. + float tAfter = max(ku - 0.80, 0.0); + float tSec = tAfter / rate; + + float glowE = exp(-tSec * lerp(7.0, 1.3, persist)); + + // The instant the line closes, everything that was spread across a whole + // row arrives in one place. That is a flare, and it is over in a few + // frames - without it the line does not so much close as fade out. + float flare = exp(-tSec * 22.0) * smoothstep(0.93, 1.00, hc); + + // p is isotropic - x was already scaled by the aspect - so a plain length + // is a circle on screen. The core shrinks as it fades, the way a real dot + // does; it does not simply dim in place. + // + // The flare widens the CORE, not the halo. Widening the halo made the + // single brightest frame of the shot a green ball with a pale pip lost + // inside it; a tube flashing off flashes WHITE, with green only at the + // fringe. The floor is two virtual rows: below that the gaussian is + // narrower than the pixel grid and the last image of the demo scintillates + // on and off as the dot lands between sample points. + float coreR = max(lerp(0.030, 0.0075, hc) * (0.55 + 0.45 * glowE) + + 0.055 * flare, 2.0 * ROW); + float rd = length(p - float2(0.0, yc)); + float cq = rd / coreR; + float core = exp(-cq * cq); + + // The halo is phosphor that was lit a moment ago and is still coming + // down, so it is not tied to the core's radius - but it was a fixed 0.075 + // wide, which is four times the core for the dot's whole life, and a green + // cloud that size does not read as a star, it reads as an out of focus + // smudge. It is tight now, and it comes down on the SLOW clock so the glow + // does not snap shut the instant the flare ends. + float halo = exp(-rd / (0.050 + 0.045 * flare + 0.030 * glowE)); + + // Underneath both, one very wide and very faint field, so the glow still + // has somewhere to sit rather than ending at the halo's edge. + float mist = exp(-rd / 0.17); + + // Green longest of the three. The core is white while the beam is still + // arriving and walks green as it dies, which is what leaves a green-white + // star rather than a white one that turns off. + float3 coreC = lerp(float3(0.62, 1.00, 0.66), float3(1.00, 1.00, 0.96), + saturate(flare + 0.6 * glowE)); + + col += (coreC * core * (3.2 + 6.0 * flare) + + float3(0.24, 0.85, 0.32) * halo * (0.80 + 1.60 * flare + + 0.35 * F.lift) + + float3(0.16, 0.55, 0.22) * mist * (0.28 + 0.50 * flare)) + * dotOn * glowE; + + // The line does not vanish when the dot takes over. It was there a + // hundredth of a second ago and the phosphor it lit is still coming down, + // so a horizontal smear survives the closure, narrowing and dimming on + // its own clock - slower than the flare, faster than the star. This is + // the last thing in the demo that MOVES, and it is what turns the point + // into the end of a collapse rather than a dot that was always there. + float trW = A * 0.62 * exp(-tSec * 2.4); + float trD = p.y - yc; + float trail = exp(-trD * trD * 5200.0) * + (1.0 - smoothstep(trW * 0.55, trW, abs(p.x))); + col += float3(0.20, 0.62, 0.24) * trail * dotOn * exp(-tSec * 3.0) * 0.85; + + // ---- what is left ----------------------------------------------------- + // Phosphor does not stop glowing because deflection did. For a while + // after the raster leaves, the whole picture is still faintly there, + // hanging in the dark behind the line with nothing driving it. The decay + // was fast enough that it was gone before the line had even settled; it + // is halved now, so the held line has the ghost of the eye behind it, + // which is the one moment in the shot where you can see both at once. + float ghost = vc * exp(-max(k - 0.24, 0.0) * 7.0) * 0.30; + [branch] if (ghost > 0.002) { + Fault G = F; + G.sick = sick * 0.3; + G.hf = 1.0; + G.bar = F.bar * 0.4; + col += signalAt(p, G) * ghost; + } + + // The glass itself, lit by the room. It is almost nothing, but a frame of + // exactly zero gives the dither nothing to bite on, and the demo should + // end on a dark tube rather than on a hole. + // exp, not a linear falloff: at 32:9 the corner is 3.7 units out and a + // linear term would have gone negative long before it got there. + col += float3(0.008, 0.010, 0.016) * exp(-length(p) * 0.45); + + return float4(col * gTime.z, 1.0); +} diff --git a/fx/reactor.hlsl b/fx/reactor.hlsl new file mode 100644 index 0000000..ef3e503 --- /dev/null +++ b/fx/reactor.hlsl @@ -0,0 +1,205 @@ +// SOLO orders 16-39. Bounded, opaque sculpture; host owns camera and grading. +// Tune: variant [0,3], motion [0,2], cyan->magenta accent [0,1], deform [0,1]. +cbuffer Scene : register(b0) +{ + float4 gTime; + float4 gCam; + float4 gDir; + float4 gTune; + float4 gSync; + float4 gVoice; +}; + +struct VSOut { float4 pos : SV_Position; float2 uv : TEXCOORD0; }; + +static float sClock, sExpand, sFly, sCollapse, sDeform; +static float2 sCoreSpin; +static float2 sRingSpin[3]; +static float2 sRingTilt[3]; +static float sRadius[3]; +static float sLift[3]; + +float3 safeUnit(float3 v, float3 fallback) +{ + float d = dot(v, v); + return d > 1e-10 ? v * rsqrt(max(d, 1e-10)) : fallback; +} + +float2 turn(float2 p, float2 cs) +{ + return float2(cs.x*p.x - cs.y*p.y, cs.y*p.x + cs.x*p.y); +} + +float box3(float3 p, float3 b) +{ + float3 q = abs(p) - b; + return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); +} + +float box2(float2 p, float2 b) +{ + float2 q = abs(p) - b; + return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0); +} + +// x conservative distance, y material: porous alloy / mechanical / hot heart. +float2 field(float3 p) +{ + float3 q = p; + q.xz = turn(q.xz, sCoreSpin); + q.xy = turn(q.xy, float2(0.9393727, 0.3428978)); + float collapse = 1.0 - 0.48*sCollapse; + float3 axes = float3(1.85, 1.34, 1.12) + + sExpand*float3(0.90, 0.66, 0.65); + axes *= collapse; + // Ellipsoid bound and gyroid both use lower distance bounds. + float shell = (length(q / axes) - 1.0) * axes.z; + float frequency = lerp(2.65, 2.05, sExpand); + float3 u = q*frequency + float3(0.0, sClock*0.27, 0.0); + float gy = dot(sin(u), cos(u.yzx)); + // Gradient norm <= 2*sqrt(3)*frequency. No spatial warp hidden in steps. + float thickness = 0.36 + 0.15*sDeform + + 0.08*sin(sClock*1.1); + float porous = (abs(gy) - thickness) / (3.465*frequency); + float2 result = float2(max(shell, porous), 0.0); + + // An elongated counter-rotating heart is visible through real holes. + float3 heart = p; + heart.yz = turn(heart.yz, sCoreSpin.yx); + float h = (dot(abs(heart), float3(1.0, 0.72, 1.0)) + - (0.65 - 0.22*sCollapse)) / 1.588; + if (h < result.x) result = float2(h, 2.0); + + [loop] for (int j = 0; j < 3; ++j) + { + float3 r = p; + r.yz = turn(r.yz, sRingTilt[j]); + r.xy = turn(r.xy, sRingSpin[j]); + r.z -= sLift[j]; + float radial = length(r.xy) - sRadius[j]; + // Bevelled rectangular cross-section, open quadrant and severed ends. + float arc = box2(float2(radial, r.z), float2(0.18, 0.13)) - 0.075; + arc = max(arc, -max(r.x, r.y)); + arc = max(arc, 0.22 + 0.28*sFly - abs(r.y)); + if (arc < result.x) result = float2(arc, 1.0); + + // Broad pieces at the outer radius cross IN FRONT of the lattice. + // Opposed pieces are displaced in depth, not camera-facing sprites. + float3 shard = r; + float side = r.x < 0.0 ? -1.0 : 1.0; + shard.x = abs(shard.x) - (sRadius[j] + 0.12); + shard.y -= side * (0.75 + 0.25*sFly); + shard.z -= side * (0.25 + 0.38*sFly); + shard.yz = turn(shard.yz, float2(0.8, 0.6)); + float piece = box3(shard, float3(0.25, 0.55, 0.18)) - 0.075; + if (piece < result.x) result = float2(piece, 1.0); + } + // Hard guarantee for the analytic ray interval, including all tune values. + result.x = max(result.x, length(p) - 5.95); + return result; +} + +float3 normalAt(float3 p, float e) +{ + float3 a = float3(1.0, -1.0, -1.0); + float3 b = float3(-1.0, -1.0, 1.0); + float3 c = float3(-1.0, 1.0, -1.0); + float3 d = float3(1.0, 1.0, 1.0); + return safeUnit(a*field(p+a*e).x + b*field(p+b*e).x + + c*field(p+c*e).x + d*field(p+d*e).x, + float3(0.0, 1.0, 0.0)); +} + +float4 main(VSOut i, out float depth : SV_Depth) : SV_Target +{ + depth = 0.0; + float variant = clamp(gTune.x, 0.0, 3.0); + sExpand = saturate(1.0 - abs(variant - 1.0)); + sFly = saturate(1.0 - abs(variant - 2.0)); + sDeform = saturate(gTune.w); + sClock = gTime.x * clamp(gTune.y, 0.0, 2.0); + // Collapse cycles continuously; neither beat resets nor full-frame flashes. + sCollapse = saturate(variant - 2.0) + * (0.58 + 0.32*sin(sClock*0.72)); + float angle = sClock*0.29; + sCoreSpin = float2(cos(angle), sin(angle)); + [loop] for (int j = 0; j < 3; ++j) + { + float fj = float(j); + float a = sClock*(0.22 + fj*0.09)*(j == 1 ? -1.0 : 1.0) + fj*2.1; + float b = 0.55 + fj*0.83 + (0.22 + 0.40*sFly)*sin(sClock*0.31 + fj); + sRingSpin[j] = float2(cos(a), sin(a)); + sRingTilt[j] = float2(cos(b), sin(b)); + sRadius[j] = 2.95 + fj*0.39 + 0.48*sFly + 0.22*sExpand + - 0.75*sCollapse + (0.10 + 0.12*sDeform)*sin(sClock*0.83 + fj*2.0); + sLift[j] = (0.18 + 0.38*sFly)*sin(sClock*0.57 + fj*2.3); + } + + float2 uv = float2(i.uv.x*2.0 - 1.0, 1.0 - i.uv.y*2.0); + uv.x *= max(gTime.w, 0.01); + float3 fw = safeUnit(gDir.xyz, float3(0.0, 0.0, 1.0)); + float3 reference = abs(fw.y) > 0.98 ? float3(0.0, 0.0, 1.0) : float3(0.0, 1.0, 0.0); + float3 rt = safeUnit(cross(reference, fw), float3(1.0, 0.0, 0.0)); + float3 up = cross(fw, rt); + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 rd = safeUnit(fw + max(gCam.w, 0.001) + * (uv.x*(rt*cr + up*sr) + uv.y*(up*cr - rt*sr)), fw); + float3 ro = gCam.xyz; + float3 col = float3(0.003, 0.005, 0.012); + float bRay = dot(ro, rd); + float discriminant = bRay*bRay - dot(ro, ro) + 36.0; + if (discriminant < 0.0) return float4(col*gTime.z, 1.0); + float root = sqrt(max(discriminant, 0.0)); + float t = max(0.0, -bRay - root); + float end = -bRay + root; + bool hit = false; + float2 sampleHit = float2(0.0, 0.0); + [loop] for (int step = 0; step < 80; ++step) + { + if (t > end) break; + sampleHit = field(ro + rd*t); + float epsilon = max(0.0025, t*0.00040); + if (sampleHit.x < epsilon) { hit = true; break; } + t += max(sampleHit.x*0.88, 0.001); + } + if (hit) + { + float3 p = ro + rd*t; + float3 n = normalAt(p, max(0.002, t*0.00022)); + float3 key = safeUnit(float3(-5.0, 7.0, -4.0)-p, fw); + float3 fill = safeUnit(float3(5.0, 1.0, 4.0)-p, fw); + // Broad off-axis camera fill keeps lattice faces readable throughout + // an orbit. World-space key and opposing cyan fill retain modelling. + float3 cameraFill = safeUnit(ro + rt*4.0 + up*3.0 - p, -rd); + float3 warm = float3(1.0, 0.52, 0.24); + float3 cyan = float3(0.08, 0.73, 0.90); + float3 accent = lerp(cyan, float3(0.91, 0.09, 0.43), saturate(gTune.z)); + float mechanical = sampleHit.y > 0.5 && sampleHit.y < 1.5 ? 1.0 : 0.0; + float heart = sampleHit.y > 1.5 ? 1.0 : 0.0; + float ao = 1.0; + // Conservative field is deliberately scaled; this is contact shading. + [loop] for (int k = 1; k <= 3; ++k) + { + float reach = 0.12*float(k); + ao -= max(0.0, reach - field(p+n*reach).x)*0.48; + } + ao = clamp(ao, 0.35, 1.0); + float diffuse = saturate(dot(n, key)); + float back = saturate(dot(n, fill)); + float front = saturate(dot(n, cameraFill)); + float fresnel = pow(1.0-saturate(dot(n, -rd)), 3.0); + float spec = pow(saturate(dot(n, safeUnit(key-rd, n))), 42.0); + float spec2 = pow(saturate(dot(n, safeUnit(fill-rd, n))), 28.0); + float3 alloy = lerp(accent*0.66 + float3(0.055, 0.045, 0.06), + float3(0.36, 0.43, 0.50), mechanical); + col = alloy*(0.20 + diffuse*1.80 + front*0.85)*ao; + col += cyan*back*0.60*ao + warm*spec*0.85 + accent*spec2*0.60; + col += accent*fresnel*(0.15 + 0.13*saturate(gVoice.y))*ao; + float flow = 0.5 + 0.5*sin(p.y*3.0 + p.x*1.7 - sClock*2.0); + col += accent*(1.0-mechanical)*(0.05 + 0.12*flow); + col += heart*lerp(warm, accent, 0.35) + * (0.60 + 0.22*saturate(gVoice.y) + 0.10*saturate(gSync.y)); + depth = saturate(0.05 / max(t*dot(rd, fw), 0.001)); + } + return float4(col*gTime.z, 1.0); +} diff --git a/fx/scene.hlsl b/fx/scene.hlsl new file mode 100644 index 0000000..f58c443 --- /dev/null +++ b/fx/scene.hlsl @@ -0,0 +1,504 @@ +// Shot 05 - CATHEDRAL / ORGAN HALL. +// +// The one room in the demo that IS an organ, and it gets five seconds under +// the cold open with the camera barely moving - 1.15 units per second down a +// nave whose key light stays 74 units ahead forever. A push that never +// arrives is a still frame unless the ROOM moves, so everything here that +// reads is either built out of the bay rhythm the camera slides past, or it +// is animated on gTime.x on its own clock: +// +// the arcade real arches spanning every bay, so the colonnade is an +// arcade and not a row of posts. It is what turns the push +// into travel: arches pass overhead, one per bay. +// the aisle glass tall leaded lancets in the outer wall, one per bay, +// seen THROUGH the arcade. A slow wave of brightness runs +// down the row, and the whole rank breathes on gSync.y. +// the aisle light cool daylight pooling in the side aisles under those +// windows, against the warm key down the axis. The band it +// fills drifts vertically as if the sun were moving. +// the organ a rank of pipes on a gallery at |x| = 13.6, running the +// length of the hall behind the arcade, tips lit by +// gVoice.z. The organ is playing alone; the pipes should +// know that. +// the rose the light at the end of the nave is now a rose window +// with tracery, an oculus and a shimmer, still a bright +// disc dead centre so the MATCH CUT still has its shape. +// the lamps three lamps on chains down the axis, swaying on their +// own periods. Pure parallax: they are what tells you the +// camera is moving at all. +// the floor flagstones, and the key light smeared the length of the +// polished stone. The eye sits 6.5 above the floor on this +// shot, so that reflection is a third of the frame. +// +// NOTHING in here multiplies the whole picture by an audio envelope. Every +// music-driven term is base + k*s, so no floor ever drops out. +// +// Everything is [loop], never [unroll]. An unrolled march is the one thing +// that would blow the executable size out on its own. +// +// The volumetric is deliberately built out of LOW frequencies only. March +// steps in the open nave are five or six units long; anything with detail +// finer than that would flicker as the camera pushed through it. The +// per-bay rhythm you see in the air is a shallow 30% modulation on a smooth +// lobe - the sharp per-bay detail lives in the window SURFACES, where there +// is no sampling problem at all. + +cbuffer Scene : register(b0) +{ + float4 gTime; // x time, y beat, z shot fade, w aspect (w/h) + float4 gCam; // xyz eye, w tan(vfov/2) + float4 gDir; // xyz look direction, w roll + float4 gTune; // per shot knobs +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// ---- the per-shot dials, decoded once at the top of main() --------------- +// +// gTune.x the arcade. bay pitch and column proportion. 0 gives 13 unit +// bays and fat 1.3 columns; 1 gives 8 unit bays, +// slimmer columns and finer fluting. Everything +// that repeats down the nave - arches, ribs, +// windows, the shafts in the air - follows the bay, +// so this one float relays the whole hall. +// gTune.y the organ loft. -0.6 and below deletes the pipe rank and its +// gallery outright and you get a bare hall; 0 is a +// medium rank; +0.4 a full one with a tall skyline. +// gTune.z the air. haze, fog density, how much volumetric there is. +// gTune.w the glass. stained glass brightness, how much daylight pools +// in the aisles, the rake of the shafts, and the +// spoke count of the rose window (8 to 16). +static float sBay = 13.0; +static float sBayInv = 0.076923077; +static float sColR = 1.30; +static float sFlute = 18.0; +static float sLoft = 0.60; +static float sSpread = 0.55; + +// ---- distance field ------------------------------------------------------ + +float vmax3(float3 v) { return max(v.x, max(v.y, v.z)); } + +float sdBox(float3 p, float3 b) +{ + float3 q = abs(p) - b; + return length(max(q, 0.0)) + min(vmax3(q), 0.0); +} + +float sdBox2(float2 p, float2 b) +{ + float2 q = abs(p) - b; + return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0); +} + +// One column, centred on the origin, standing on the floor. +float column(float3 p) +{ + float2 q = p.xz; + float d = length(q) - sColR; + d += 0.055 * sin(atan2(q.y, q.x) * sFlute); // flutes + d = max(d, p.y - 13.2); // stop under the capital + d = min(d, sdBox(p - float3(0.0, -5.3, 0.0), float3(sColR + 0.65, 0.75, sColR + 0.65)) - 0.10); + d = min(d, sdBox(p - float3(0.0, 13.0, 0.0), float3(sColR + 0.80, 0.85, sColR + 0.80)) - 0.14); + return d; +} + +float map(float3 p) +{ + float d = p.y + 6.0; // floor + + // Walls, and a barrel vault springing from y = 12. Below the spring + // line the max() collapses this to a flat wall at |x| = 16.5. + float2 v = float2(p.x, max(p.y - 12.0, 0.0)); + float lv = length(v); + d = min(d, 16.5 - lv); + + // Two colonnades, repeated down the nave. cz is zero AT a column. + float3 c = p; + c.x = abs(c.x) - 10.0; + c.z = frac(p.z * sBayInv + 0.5) * sBay - sBay * 0.5; + d = min(d, column(c)); + + // A transverse rib over each bay, on the column line. Without them the + // vault is one smooth surface that takes the fill light evenly and reads + // as a flat grey ceiling rather than as a curve. It reuses lv, which is + // the same length() the wall already paid for. + d = min(d, max(15.75 - lv, abs(c.z) - 0.62)); + + // A ridge rib along the crown, running the length. It is the only line + // in the room that points where the camera is going, and on a five + // second push down the axis that is most of the composition. + d = min(d, max(15.80 - lv, abs(p.x) - 0.50)); + + // THE ARCADE. A semicircular arch spanning each bay between the columns, + // springing just above the capitals. Below the springing the same + // expression collapses to a 0.62 shaft standing exactly at the column + // centres, which is buried inside the columns and costs nothing. + { + float ax = abs(p.x) - 10.0; + float az = frac(p.z * sBayInv) * sBay - sBay * 0.5; // zero MID bay + float2 rq = float2(az, max(p.y - 13.9, 0.0)); + float ring = length(rq) - sBay * 0.5; + d = min(d, length(float2(ring, ax)) - 0.62); + } + + // THE ORGAN. A rank of pipes on a gallery behind the arcade, heights + // walked by a golden-ratio sequence so the skyline reads as random + // without a single transcendental. The 0.65 is a deliberate + // underestimate: neighbouring cells have different heights, which breaks + // the Lipschitz bound near a tip, and marching short is cheaper than + // marching a punched-through pipe. + if (sLoft > 0.02) { + float ax = abs(p.x) - 13.60; + float pu = p.z * 1.0526316; // period 0.95 + float g = frac(floor(pu) * 0.61803399 + 0.137); + float pz = (frac(pu) - 0.5) * 0.95; + float top = 15.40 + (2.20 + 2.40 * sSpread) * g; + + float pipe = length(float2(ax, pz)) - 0.32; + pipe = max(pipe, p.y - top); + pipe = max(pipe, 13.95 - p.y); + + float ledge = sdBox2(float2(ax, p.y - 13.55), float2(1.15, 0.40)) - 0.08; + d = min(d, min(pipe * 0.65, ledge)); + } + + return d; +} + +// Tetrahedral, not central-difference. The room got two new solids this +// pass and map() is inlined once per tap, so four taps instead of six is +// twenty percent off the compiled blob for a normal nobody can tell apart. +float3 mapNormal(float3 p) +{ + const float e = 0.0022; + float3 k = float3(1.0, -1.0, 1.0); + return normalize(k.xyy * map(p + k.xyy * e) + + k.yyx * map(p + k.yyx * e) + + k.yxy * map(p + k.yxy * e) + + k.xxx * map(p + k.xxx * e)); +} + +float softShadow(float3 ro, float3 lightPos) +{ + float3 toLight = lightPos - ro; + float lightDist = length(toLight); + float tMax = min(55.0, lightDist); + float res = 1.0, t = 0.35; + if (tMax <= t) return res; + float3 rd = toLight / lightDist; + [loop] for (int i = 0; i < 24; i++) { + // Test before map(): an overshooting step must not sample past the light. + if (t >= tMax) break; + float h = map(ro + rd * t); + if (h < 0.002) return 0.0; + res = min(res, 11.0 * h / t); + t += clamp(h, 0.12, 2.5); + } + return saturate(res); +} + +// Cheap ambient occlusion: how much of the field is nearby along the normal. +float occlusion(float3 p, float3 n) +{ + float o = 0.0, s = 1.0; + [loop] for (int i = 0; i < 5; i++) { + float d = 0.10 + 0.28 * float(i); + o += (d - map(p + n * d)) * s; + s *= 0.62; + } + return saturate(1.0 - 1.4 * o); +} + +// ---- shading ------------------------------------------------------------- + +float4 main(VSOut i, out float depth : SV_Depth) : SV_Target +{ + depth = 0.0; // far, under reversed Z + + // ---- dials ---- + float a = saturate(gTune.x); + sBay = 13.0 - 5.0 * a; + sBayInv = 1.0 / sBay; + sColR = 1.30 - 0.42 * a; + sFlute = 18.0 + 10.0 * a; + sLoft = saturate(0.60 + gTune.y); + sSpread = saturate(0.55 + gTune.y); + + float air = saturate(0.55 + gTune.z); + float glass = saturate(0.70 + gTune.w); + float spokes = 8.0 + floor(glass * 8.0); + float tilt = 0.42 + 0.55 * glass; // rake of the daylight + float drift = gTime.x * 0.020 * (0.4 + air); + // The sun, moving. It is the slowest thing in the demo on purpose: over + // five seconds it lifts the lit band on the aisle wall by about a metre, + // which you do not see happen but do see the result of. + float sunTop = 12.6 + 1.7 * sin(gTime.x * 0.155); + + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y is FLIPPED here on purpose. The fullscreen triangle emits + // uv.y = 0 at the top of the screen, so i.uv*2-1 gives -1 up + // there and the top of the frame ended up looking DOWNWARD - + // the cathedral vault springs at y=+12 and was rendering in the + // lower half, the monolith hung from the ceiling. + uv.x *= gTime.w; + + float3 fw = normalize(gDir.xyz); + float3 rt = normalize(cross(float3(0.0, 1.0, 0.0), fw)); + float3 up = cross(fw, rt); + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 r2 = rt * cr + up * sr; + float3 u2 = up * cr - rt * sr; + float3 ro = gCam.xyz; + float3 rd = normalize(fw + (uv.x * r2 + uv.y * u2) * gCam.w); + + // The light at the end of the nave, and a much dimmer one over the + // camera so the nearest columns are not pure silhouette. Inverse square + // over 70 units eats almost everything, so the key is genuinely huge. + float3 lp = float3(0.0, 15.0, ro.z + 74.0); + float3 lp2 = float3(0.0, 16.0, ro.z + 4.0); + // The organ breathing the key. Small on purpose: the key lights the far + // end of a 74 unit nave, so this is a swell and not a flash. + float keyM = 1.0 + 0.25 * gVoice.z + 0.10 * gSync.x; + + float t = 0.0; + float shaft = 0.0; + float beams = 0.0; + bool hit = false; + + [loop] for (int k = 0; k < 104; k++) { + float3 p = ro + rd * t; + float h = map(p); + float w = min(h, 1.2); + + // Volumetric: distance from this sample to the light, accumulated. + // It is what makes the hall read as full of air rather than vacuum. + float dl = length(p - lp); + shaft += 900.0 / (1.0 + dl * dl) * w; + + // DAYLIGHT IN THE AISLES, on every other step. Trace the sample back + // along the rake to the outer wall and ask how far up that wall it + // came from: below the lit band you get light, above it you get the + // dark haunch of the vault. The per-bay lobe is only a 30% ripple on + // top - see the note at the head of the file about step length. + if ((k & 1) == 0) { + float ax = abs(p.x); + float s = max(16.5 - ax, 0.0); + float wy = p.y + tilt * s; + + float bw = abs(frac(p.z * sBayInv + drift) - 0.5); + float lobe = saturate(1.0 - bw * 2.2); lobe *= lobe; + + float pool = saturate((ax - 6.5) * 0.15) // aisles, not the nave + * saturate((sunTop - wy) * 0.22) // under the lit band + * saturate((p.y + 7.5) * 0.30); // and off the floor + + // The air itself, turning over. Twenty unit wavelengths, so the + // march resolves it however long the steps get. + float swirl = 0.86 + 0.14 * sin(p.z * 0.29 + gTime.x * 0.47) + * sin(p.y * 0.25 - gTime.x * 0.31); + + beams += pool * (0.70 + 0.30 * lobe) * swirl * w; + } + + if (h < 0.0016 * t) { + hit = true; + // rd is normalised, so t is RAY LENGTH. Projecting it onto the + // forward axis is what makes this VIEW depth - without the dot + // the mesh punches through the field towards the screen edges + // while looking perfectly correct dead centre. + depth = saturate(0.05 / max(t * dot(rd, fw), 0.001)); + break; + } + t += h * 0.86; + if (t > 190.0) break; + } + + float3 col = float3(0.0, 0.0, 0.0); + float3 warm = float3(1.00, 0.74, 0.46); + float3 cool = float3(0.30, 0.38, 0.56); + float3 day = float3(0.60, 0.71, 0.96); + + if (hit) { + float3 p = ro + rd * t; + float3 n = mapNormal(p); + float3 ld = normalize(lp - p); + float dl = length(lp - p); + + float dif = saturate(dot(n, ld)) * (5200.0 * keyM / (1.0 + dl * dl)); + dif *= softShadow(p + n * 0.03, lp); + + float3 ld2 = normalize(lp2 - p); + float dl2 = length(lp2 - p); + float fil = saturate(dot(n, ld2)) * (260.0 / (1.0 + dl2 * dl2)); + + float ao = occlusion(p, n); + float fres = pow(1.0 - saturate(dot(n, -rd)), 4.0); + + // ---- who am I standing on -------------------------------------- + // Position is a cheaper material id than carrying one through the + // march, and in a room this rigid it is exact. + float ax = abs(p.x); + float onFloor = saturate((-5.80 - p.y) * 40.0) * saturate(n.y * 4.0 - 2.0); + float onPipe = saturate((p.y - 14.00) * 6.0) + * saturate((ax - 13.05) * 8.0) * saturate((14.15 - ax) * 8.0); + float onWall = saturate((ax - 15.75) * 5.0) * saturate((12.20 - p.y) * 4.0); + + // Flagstones. A grid this coarse survives the CRT quantiser, which a + // finer one would not. + float3 alb = float3(1.0, 1.0, 1.0); + { + float2 fg = abs(frac(p.xz * 0.24) - 0.5); + float grout = saturate(min(fg.x, fg.y) * 26.0); + alb = lerp(alb, float3(0.90, 0.88, 0.85) * (0.55 + 0.45 * grout), onFloor); + } + alb = lerp(alb, float3(0.78, 0.80, 0.86), onPipe); // tin, not stone + + col = warm * dif * alb; + col += cool * fil * 0.42 * alb; + col += cool * ao * 0.075 * (0.35 + 0.65 * saturate(n.y)); + col += warm * fres * 0.30 * ao; + col *= 0.35 + 0.65 * ao; + + // Specular. Polished stone and tin only; the rest of the hall is + // rough and gets essentially none. + { + float3 hv = normalize(ld - rd); + float spc = pow(saturate(dot(n, hv)), 60.0); + col += warm * spc * (0.08 + 2.4 * onPipe + 1.4 * onFloor) * saturate(dif * 4.0); + } + + // The key light smeared the length of the polished floor. The eye + // sits 6.5 above the flagstones on the one shot this scene has, so + // this reflection runs from the bottom of the frame to the horizon + // and is doing as much work as the light itself. + { + float rk = saturate(dot(reflect(rd, n), ld)); + col += warm * onFloor * (pow(rk, 30.0) * 0.60 + pow(rk, 5.0) * 0.09) + * (0.55 + 0.45 * keyM); + } + + // The pipes catching the organ. Fresnel on a cylinder is an edge + // light, which is exactly how a rank of tin pipes reads at sixty + // units - two bright verticals per pipe and nothing in between. + col += float3(0.95, 0.80, 0.58) * onPipe * fres * ao * (0.30 + 0.85 * gVoice.z) * 1.6; + + // ---- THE AISLE GLASS ------------------------------------------- + // One tall leaded lancet per bay in the outer wall, centred MID bay + // so it sits under an arch rather than behind a column. Emissive, so + // it goes on after the ao multiply. + { + float bz = frac(p.z * sBayInv) * sBay - sBay * 0.5; + float bi = floor(p.z * sBayInv); + float wt = (p.y - 4.0) * 0.142857; // 0..1 up the window + float hw = 0.185 * sBay * saturate(1.0 - saturate((wt - 0.58) * 2.4)); + + float inw = saturate((hw - abs(bz)) * 6.0) + * saturate(wt * 8.0) * saturate((1.0 - wt) * 8.0); + float mull = saturate(abs(frac(bz * 1.30) - 0.5) * 7.0 - 0.55); // mullions + float tran = saturate(abs(frac(p.y * 0.95) - 0.5) * 7.0 - 0.55); // transoms + float cell = frac(floor(bz * 1.30) * 0.6180 + floor(p.y * 0.95) * 0.3125 + + bi * 0.271); + + float3 tint = lerp(float3(0.98, 0.63, 0.28), float3(0.32, 0.57, 0.95), + frac(bi * 0.6180 + 0.31)); + tint = lerp(tint, float3(0.82, 0.91, 1.00), 0.35 * cell); + + // A wave of brightness running down the row, plus the mid band. + // Both are base + k*s; the rank never goes out. + float wave = 0.84 + 0.16 * sin(p.z * 0.085 - gTime.x * 0.62); + float lum = inw * mull * tran * (0.60 + 0.65 * cell) + * glass * (0.55 + 0.45 * gSync.y) * wave; + + col += tint * lum * onWall * 1.5; + } + } + + col += float3(1.00, 0.76, 0.50) * shaft * 0.0016 * (1.0 + 0.30 * gVoice.z); + // The aisles, cool, against the warm key down the axis. w is capped at + // 1.2 while steps in the open run to five units, so this accumulator + // under-weights open space by roughly four to one - which is where the + // 0.22 comes from and why it looks nothing like the 0.0016 above it. + col += day * beams * 0.220 * (0.35 + 0.65 * glass) * (0.5 + 0.5 * air); + + // ---- THE ROSE WINDOW --------------------------------------------------- + // The source at the end of the nave used to be a featureless blob. It is + // still a bright disc dead centre - the MATCH CUT at bar 9 depends on + // that - but now it has tracery, an oculus and a shimmer, and it swells + // with the organ. The disc is a coherent region of screen, so the atan2 + // behind the rr < 1.25 test is paid for by almost no wave that is not + // looking straight at it. + { + float3 tol = lp - ro; + float proj = dot(tol, rd); + if (proj > 0.0) { + float perp = length(tol - rd * proj); + float vis = (proj < t) ? 1.0 : 0.0; + + col += float3(1.00, 0.80, 0.55) * (2.4 / (1.0 + perp * perp * 0.55)) + * (proj < t ? 1.0 : 0.30) * (0.72 + 0.42 * keyM); + + if (rd.z > 0.05 && vis > 0.5) { + float tz = (lp.z - ro.z) / rd.z; + float2 wp = float2(ro.x + rd.x * tz - lp.x, ro.y + rd.y * tz - lp.y); + float rr = length(wp) * 0.19231; // window radius 5.2 + if (rr < 1.25) { + float an = atan2(wp.y, wp.x); + float sp = an * spokes * 0.15915494; + float sw = abs(frac(sp) - 0.5) * 2.0; + float rg = abs(frac(rr * 3.0) - 0.5) * 2.0; + + float lead = saturate(sw * 5.0 - 0.45) * saturate(rg * 5.0 - 0.35); + float cell = frac(floor(sp) * 0.6180 + floor(rr * 3.0) * 0.317); + float disc = saturate((1.0 - rr) * 7.0); + float rim = saturate(1.0 - abs(rr - 1.0) * 14.0); + float ocu = saturate(1.0 - rr * 7.0); + + float3 gc = lerp(float3(1.00, 0.72, 0.36), + float3(0.55, 0.72, 1.00), cell); + float shm = 0.90 + 0.10 * sin(rr * 16.0 - gTime.x * 0.9 + an * 2.0); + float bri = (0.58 + 0.52 * gVoice.z) * shm; + + col += gc * disc * lead * (0.55 + 0.90 * cell) * bri * 2.1; + col += float3(1.00, 0.86, 0.62) * rim * 1.3 * bri; + col += float3(1.00, 0.92, 0.72) * ocu * 2.6 * bri; + } + } + } + } + + // ---- LAMPS ON CHAINS --------------------------------------------------- + // Three of them down the axis, each on its own period so they never line + // up. They are the parallax: at 1.15 units per second the columns barely + // move, but a lamp passing the frame at eight units does. + [loop] for (int L = 0; L < 3; L++) { + float fl = float(L); + float lz = ro.z + 17.0 + fl * sBay * 1.5; + float sway = 1.05 * sin(gTime.x * (0.52 + 0.09 * fl) + fl * 2.4); + float3 lm = float3(sway, 6.2 - 0.10 * abs(sway), lz); + float3 tl = lm - ro; + float pj = dot(tl, rd); + if (pj > 0.0 && pj < t) { + float pe = length(tl - rd * pj); + col += float3(1.00, 0.72, 0.38) * (0.42 / (1.0 + pe * pe * 9.0)) + * (1.0 + 0.50 * gSync.z); + } + } + + // Exponential distance fog, so the far end of the nave dissolves. + float fog = exp(-t * (0.0042 + 0.0060 * air)); + col = col * fog + float3(0.055, 0.050, 0.062) * (1.0 - fog) * (0.85 + 0.35 * glass); + + return float4(col * gTime.z, 1.0); +} diff --git a/fx/signoff.hlsl b/fx/signoff.hlsl new file mode 100644 index 0000000..1e9e648 --- /dev/null +++ b/fx/signoff.hlsl @@ -0,0 +1,315 @@ +// The only text in the demo. +// +// crt.fyi +// hi@crt.fyi +// +// Two lines, small, once. The author was explicit about that and it is the +// right call: a demo that tells you who made it in forty-point letters is +// advertising, and a demo that puts it in the corner once at the end is a +// signature. So this file is not allowed to grow the type or centre it. What +// it IS allowed to do is make the eleven seconds it owns worth watching: +// the line writes itself, character by character, with a caret ahead of it +// and the phosphor still hot behind it. +// +// No glyph atlas. Between them these two lines use eleven distinct +// characters, and a monoline stroke distance field draws them from about +// thirty line segments - less code than the loader for an atlas would be, +// and it stays sharp at any size because it is analytic. The strokes are +// deliberately even-weight and slightly wide: at 360 lines a typographic +// contrast would just alias into mush. +// +// gTune.x typing speed, 1.0 = the tuned default (about eleven glyphs a +// second). The reveal used to be an absolute 0..1 fed from the shot +// table, and the shot table fed it a constant 1.0 - so the "typing" +// the comment promised never happened once. It is derived from +// gTime.x now, and gTune.x only scales the rate. +// gTune.y size +// gTune.z which line: 0 both, 1 only the address +// gTune.w the matte, as a fraction of the height hidden at each edge + +cbuffer Scene : register(b0) +{ + float4 gTime; + float4 gCam; + float4 gDir; + float4 gTune; + float4 gSync; + float4 gVoice; +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +// distance to a line segment, the primitive everything here is built from +float sdSeg(float2 p, float2 a, float2 b) +{ + float2 pa = p - a, ba = b - a; + float h = saturate(dot(pa, ba) / dot(ba, ba)); + return length(pa - ba * h); +} + +// distance to an arc of a circle, for the round letters. a0..a1 in radians. +float sdArc(float2 p, float2 c, float r, float a0, float a1) +{ + float2 q = p - c; + float a = atan2(q.y, q.x); + // wrap the angle into [a0, a0+2pi) so the comparison is unambiguous + float w = a - a0; + w -= 6.28318530718 * floor(w / 6.28318530718); + if (w <= a1 - a0) return abs(length(q) - r); + // outside the sweep: the nearer of the two end points + float2 e0 = c + float2(cos(a0), sin(a0)) * r; + float2 e1 = c + float2(cos(a1), sin(a1)) * r; + return min(length(p - e0), length(p - e1)); +} + +// Each glyph is drawn in a box 0.62 wide and 1.0 tall, with the baseline at +// y = 0 and the x-height at 0.58 - the proportions of a plain grotesque. +float glyph(float2 p, int c) +{ + float d = 1e9; + if (c == 0) { // c + d = sdArc(p, float2(0.31, 0.31), 0.27, 0.55, 5.73); + } else if (c == 1) { // r + // The arm used to sweep from three o'clock, which hooks it down past + // the middle of the x-height and makes the r read as a curl. A + // grotesque r lets go earlier and higher: the shoulder tops out AT + // the x-height and the terminal stops just under it. + d = min(sdSeg(p, float2(0.14, 0.0), float2(0.14, 0.58)), + sdArc(p, float2(0.34, 0.40), 0.19, 0.30, 2.60)); + } else if (c == 2) { // t + d = min(sdSeg(p, float2(0.24, 0.10), float2(0.24, 0.86)), + sdSeg(p, float2(0.06, 0.58), float2(0.44, 0.58))); + // the tail bottoms out ON the baseline and lifts again, instead of + // ending nose-down under it + d = min(d, sdArc(p, float2(0.38, 0.14), 0.14, 3.14, 4.95)); + } else if (c == 3) { // . + d = length(p - float2(0.16, 0.065)) - 0.048; + } else if (c == 4) { // f + d = min(sdSeg(p, float2(0.26, 0.0), float2(0.26, 0.72)), + sdSeg(p, float2(0.08, 0.58), float2(0.46, 0.58))); + d = min(d, sdArc(p, float2(0.40, 0.72), 0.14, 1.57, 3.14)); + } else if (c == 5) { // y + d = min(sdSeg(p, float2(0.08, 0.58), float2(0.30, 0.06)), + sdSeg(p, float2(0.52, 0.58), float2(0.20, -0.22))); + } else if (c == 6) { // i + d = sdSeg(p, float2(0.16, 0.0), float2(0.16, 0.58)); + // The dot has to clear the STROKE, not the centreline. Ink extends + // w = 0.070 glyph units past the geometry on both sides, so a dot at + // 0.775 with r 0.048 left 0.775-0.048-0.070-0.58 = 0.007 units of + // gap: a sixth of a virtual pixel. The two merged and the demo spelt + // the author's own domain "crt.fyl". At 0.88 with a slightly smaller + // dot the gap is 0.120 units, two and a half virtual lines of real + // dark, and it reads as an i. + d = min(d, length(p - float2(0.16, 0.88)) - 0.040); + } else if (c == 7) { // h + // shoulder raised so the h reaches the same x-height as the c and + // the r; it was sitting two hundredths low and the word looked like + // it had a dip in it + d = min(sdSeg(p, float2(0.12, 0.0), float2(0.12, 0.90)), + sdSeg(p, float2(0.44, 0.0), float2(0.44, 0.42))); + d = min(d, sdArc(p, float2(0.28, 0.42), 0.16, 0.0, 3.14)); + } else if (c == 8) { // @ + // The inner ring has to clear the outer one by more than the ink + // reaches, or the counter fills in and the @ reads as a solid disc. + // The comment that used to sit here claimed this was measured. It was + // not, and it was wrong: at r 0.095 the annulus is + // (0.31 - 0.070) - (0.095 + 0.070) = 0.075 glyph units + // which at this size is 1.46 virtual pixels - BELOW the threshold the + // i's dot just established at 2.34. So the c beside it kept its + // counter and the @ filled in, and the demo signed off with a blob in + // the middle of the author's email address. 0.05 puts the annulus at + // 0.120 units, the same gap that works for the i. + d = sdArc(p, float2(0.31, 0.34), 0.31, 0.60, 6.10); + d = min(d, sdArc(p, float2(0.31, 0.34), 0.05, 0.0, 6.28)); + d = min(d, sdSeg(p, float2(0.405, 0.34), float2(0.405, 0.15))); + } + return d; +} + +// crt.fyi -> c r t . f y i (index 0, seven glyphs) +// hi@crt.fyi -> h i @ c r t . f y i (index 7, ten glyphs) +// One table, two offsets, so the loop can only ever index inside it. +static const int kText[17] = { 0, 1, 2, 3, 4, 5, 6, + 7, 6, 8, 0, 1, 2, 3, 4, 5, 6 }; + +// METRICS. Every glyph used to advance 0.72 whatever it was, which is a +// teletype and not a grotesque: the period and the i sat in the middle of +// slots twice their width and "crt.fyi" came out with two holes punched in +// it. kLsb pulls each glyph over so its ink starts at the origin, kAdv is +// the ink width plus about 0.30 of tracking - the round letters tucked in by +// the usual optical hundredths, and the i and the @ given back what the ring +// and the bare stem need so "hi@" does not collide. The address ends up +// fifteen percent tighter than it was, which is the opposite of enlarging it. +// c r t . f y i h @ +static const float kLsb[9] = { 0.04, 0.14, 0.06, 0.112, 0.08, 0.08, 0.16, 0.12, 0.00 }; +static const float kAdv[9] = { 0.80, 0.70, 0.68, 0.40, 0.66, 0.70, 0.34, 0.62, 0.94 }; + +// One line of type, written on. Returns +// x ink coverage 0..1 +// y tight phosphor glow the beam spot +// z halation light scattered in the glass +// w the write head, in glyph units, for the caret +// +// prog is measured in glyph slots: 3.4 means three characters are down and +// the fourth is 40 percent of the way through its slot. +float4 drawLine(float2 p, int base, int n, float prog, float fw, float w) +{ + float ink = 0.0, hot = 0.0, hal = 0.0; + float x = 0.0, head = 0.0; + + [loop] for (int k = 0; k < 10; k++) + { + if (k >= n) break; + float age = prog - (float)k; + if (age <= 0.0) break; // not typed yet + + int c = kText[base + k]; + float d = glyph(p - float2(x - kLsb[c], 0.0), c) - w; + + // A character does not fade in over a fifth of a second - it is + // struck. Two frames from nothing to solid, and then the phosphor + // where it landed stays hot for about a sixth of a second after. + float f = saturate(age * 3.2); + float s = exp(-max(age - 1.0, 0.0) * 0.55); + + ink = max(ink, (1.0 - smoothstep(0.0, fw, d)) * f); + hot = max(hot, exp(-max(d, 0.0) / 0.22) * f * (1.0 + 1.20 * s)); + hal = max(hal, exp(-max(d, 0.0) / 0.45) * f); + + head = x + kAdv[c] * saturate(age * 1.8); + x += kAdv[c]; + } + return float4(ink, hot, hal, head); +} + +float4 main(VSOut i) : SV_Target +{ + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + uv.x *= gTime.w; + + // Derivatives before any branch: below this point whole waves get thrown + // away by the bounding box and a gradient taken in there is undefined. + float px = fwidth(uv.x); + + // 0.055 was chosen on paper and measured wrong on screen: at 360 lines + // the strokes came out under two pixels and the letterforms turned to + // mush. Small enough to be a signature, big enough to be legible, is + // about twice that. + float sz = 0.115 * max(gTune.y, 0.15); + // 0.16 of the glyph box is not a monoline, it is a marker pen: adjacent + // strokes merged and every letter turned into a blob. A real monoline + // grotesque sits nearer 7 percent, and at that weight the counters in + // the c, the r and the @ survive. + float w = 0.070; // stroke half width, glyph units + + // Bottom left, with a margin. Not centred: centred type is a title card, + // and this is meant to be found rather than presented. + // + // TITLE SAFE. The bottom of the PICTURE is not the bottom of the frame: + // the outro closes a letterbox over this shot, and bars of b hide + // everything below y = -1 + 2b. Placed at a fixed -0.62 the second line + // sat at 0.896 in screen uv against a matte cutting at 0.872, so the + // author's own email address was sliced through the middle. Deriving the + // position from the matte instead of from a constant means the type + // stays legible whatever the edit does to the aspect ratio. + float bottom = -1.0 + gTune.w * 2.0; + // o0 is the FIRST line, so it clears the edge by the margin, plus the + // second line's descender, plus the line spacing. + float2 o0 = float2(-gTime.w + 0.20, bottom + 0.10 + sz * (0.22 + 1.5)); + float2 o1 = o0 - float2(0.0, sz * 1.5); + + // ---- bounding box ----------------------------------------------------- + // Seventeen glyphs were being evaluated at every pixel of a 1920x1080 + // frame to draw two words in one corner. Ninety-odd percent of the frame + // is nowhere near the type; skipping it there is what pays for the + // second and third light terms below. The window is smooth to the edge + // so the halation cannot leave a rectangle behind it. + float2 imin = float2(o1.x - 0.30 * sz, o1.y - 0.42 * sz); + float2 imax = float2(o1.x + 6.25 * sz, o0.y + 1.00 * sz); + // The margin is not slack, it is the only thing standing between the + // halation and a visible rectangle: the window has to finish its work + // out where the glow is already under the dither floor, or the place it + // cuts reads as a straight edge in the dark. + float marg = 2.00 * sz; + float2 dd = max(imin - uv, uv - imax); + float e = max(max(dd.x, dd.y), 0.0); + if (e > marg) return float4(0.0, 0.0, 0.0, 1.0); + float win = 1.0 - smoothstep(marg * 0.55, marg, e); + + // ---- the reveal ------------------------------------------------------- + // Eleven glyphs a second: fast enough that it is a signature being + // written and not a crawl, slow enough that you can read it happening. + // The first line clears in nine tenths of a second, the address is down + // by two, and the rest of the shot is the caret blinking on it. + float rate = 11.0 * max(gTune.x, 0.20); + float t = gTime.x - 0.26; // let the dissolve land first + float lead = (gTune.z < 0.5) ? 9.0 : 0.0;// seven slots of line, two of pause + float p0 = t * rate; + float p1 = t * rate - lead; + + // one virtual scanline of tracking error, so the type sits IN the signal + // rather than on top of it. Tenths of a pixel - it must never be legible + // as a wobble, only as the picture not being perfectly still. + float2 q = uv; + q.x += 0.0009 * sin(uv.y * 88.0 + gTime.x * 3.7) * (0.45 + gSync.x); + + float fw = px * 1.5 / sz; // one pixel, in glyph units + float4 L0 = float4(0, 0, 0, 0), L1; + if (gTune.z < 0.5) L0 = drawLine((q - o0) / sz, 0, 7, p0, fw, w); + L1 = drawLine((q - o1) / sz, 7, 10, p1, fw, w); + + float ink = max(L0.x, L1.x); + float hot = max(L0.y, L1.y); + float hal = max(L0.z, L1.z); + + // ---- the caret -------------------------------------------------------- + // It leads the writing, drops to the second line at the break, and once + // the address is down it stays behind it and blinks at one hertz. A + // secondary element with its own clock, which is what stops the last + // second of a held shot from being a still. + bool onSecond = (p1 > 0.0) || (gTune.z >= 0.5); + float2 co = onSecond ? o1 : o0; + float ch = onSecond ? L1.w : L0.w; + float cp = onSecond ? p1 : p0; + float done = saturate((p1 - 10.0) * 1.5); + // solid while it is writing, blinking once it has nothing left to write + float blink = lerp(1.0, smoothstep(-0.22, 0.22, cos(gTime.x * 6.28)), done); + // and it only exists at all once the first character has landed + float cOn = saturate(cp * 2.0) * blink; + + float2 cq = (q - co) / sz - float2(ch + 0.13, 0.0); + float cd = sdSeg(cq, float2(0.0, -0.02), float2(0.0, 0.60)) - w * 0.85; + ink = max(ink, (1.0 - smoothstep(0.0, fw, cd)) * cOn * 0.85); + hot = max(hot, exp(-max(cd, 0.0) / 0.22) * cOn * 0.60); + + // ---- the row the beam is writing -------------------------------------- + // A tube does not draw a letter, it draws a line and turns the beam on + // in the middle of it. So the row under the active text carries a little + // warmth, brightest just after a character lands, and it decays once the + // line is finished. It has to hug the x-height: at a lazier falloff it + // stops being a scan line and becomes a lit rectangle behind the type, + // which is a caption box, which is the one thing this must not look like. + float2 bp = (q - co) / sz; + float bx = max(max(-0.20 - bp.x, bp.x - ch - 0.20), 0.0); + float bt = saturate(cp * 2.0) * (0.30 + 0.70 * exp(-max(cp - 10.0, 0.0) * 1.2)); + float band = 0.030 * bt + * exp(-abs(bp.y - 0.29) / 0.17) * exp(-bx / 0.30) + * (1.0 + 0.80 * exp(-frac(max(cp, 0.0)) * 6.0)); + + // ---- phosphor --------------------------------------------------------- + // Three terms, not one: the beam spot tight around the stroke, the + // halation that scatters through the glass and puts a soft field around + // the whole word, and the ink itself. The glow used to be doing the job + // of the first two at once, which is why it read as a second, fatter + // stroke instead of as light. Only the two GLOW terms move with the + // music - the ink never does, so nothing here can flash. + float lift = 1.0 + 0.34 * gVoice.z + 0.22 * gSync.w; + float3 col = float3(0.68, 0.94, 0.72) + * (ink + (hot * 0.18 + hal * 0.020 + band) * lift) * win; + + return float4(col * gTime.z, 1.0); +} diff --git a/fx/voidshot.hlsl b/fx/voidshot.hlsl new file mode 100644 index 0000000..29dfa6a --- /dev/null +++ b/fx/voidshot.hlsl @@ -0,0 +1,483 @@ +// Shot 30 - MONOLITH IN THE WHITE VOID. +// +// The contrast shot. Everything else in this demo is dark, so one shot is +// almost entirely white, and the subject is a black shape standing in it. +// The dither in the post pass does most of the work here: against a flat +// white field the Bayer pattern is the texture, which is exactly the +// Marathon loading screen look. +// +// It appears ONCE, at bar 82, on the loudest flash cut in the piece: a stab +// on beat one and then 1.36 s of real silence. So it was never a repetition +// problem, it was a nothing-happens problem - a rectangle standing still for +// two seconds, seen through a flash it did not use. +// +// What it does now is the only thing that moment wants: the stab HITS it. +// The monolith comes apart under the white of the flash, and by the time +// the flash has decayed the pieces are already slowing. They decelerate +// asymptotically, so by the middle of the shot everything has stopped dead +// and hangs in the air, unfallen, for the whole of the silence. The picture +// stops when the music does - it just stops somewhere the eye has never +// seen it stop before. The base stays planted, so it still reads as the +// monolith and not as a pile of boxes. +// +// gTune.x monolith lean, and which way the break throws +// gTune.y how far the horizon has been pushed back +// gTune.z fracture the shot STARTS at (0 = enters whole, 1 = already open) +// gTune.w strike delay in seconds (0 = struck on the cut, which is the cut +// this shot is written against) +// +// gSync.w a tremor in the pieces that dies out with the sound itself +// gVoice.y the solo, riding the dust ring the strike throws along the floor + +cbuffer Scene : register(b0) +{ + float4 gTime; + float4 gCam; + float4 gDir; + float4 gTune; +/* What the music is doing at this exact sample. The three bands + each drive a different SCALE of thing - low for big slow shapes, + mid for mid-sized ones, high for fine texture - so the image ends + up with the same frequency structure as the music instead of just + reacting to it. */ +float4 gSync; // x low, y mid, z high, w master peak +float4 gVoice; // x guitar, y solo, z organ, w hat +}; + +struct VSOut +{ + float4 pos : SV_Position; + float2 uv : TEXCOORD0; +}; + +#define NSLAB 9 +#define NCHIP 26 + +// The sun sits BEHIND the monolith relative to the camera and low, so the +// shadow runs forward out of the frame towards the viewer instead of hiding +// behind the shape. On a white floor the shadow is the only other graphic +// element in the picture; it should not be pointing away from us. +static const float3 SUN = float3(-0.4870, 0.7190, 0.4953); // normalised + +// Where the monolith is cut. Deliberately uneven - equal bands read as a +// stack of pancakes, uneven ones read as something that broke. +static const float kEdge[NSLAB + 1] = { + 0.000, 0.082, 0.171, 0.268, 0.352, 0.463, 0.576, 0.701, 0.848, 1.000 +}; + +// Three more of them, standing out in the haze. They are almost entirely +// fogged out - a tenth of a stop each - and that is the point: they give +// the void a scale, so the white is a distance and not a backdrop. +static const float4 kFar[3] = { + float4(-46.0, 70.0, 2.30, 20.0), // x, z, half width, height + float4( 74.0, 138.0, 3.40, 34.0), + float4(-96.0, 232.0, 2.90, 22.0) +}; + +static float4 gSA[NSLAB]; // xyz: piece centre in monolith space, w: half height +static float4 gSB[NSLAB]; // cos/sin of its two tumble angles +static float3 gBmin, gBmax; // world box that contains every piece +static float3 gCmin, gCmax; // and one that contains every chip +static float gLeanS, gLeanC; +static float gA; // how far the break has got. 0 whole, 1 open + +float sdBox(float3 p, float3 b) +{ + float3 q = abs(p) - b; + return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); +} + +// Slab test against an axis aligned box. Used to BOUND the raymarch rather +// than to draw anything: the pieces occupy a small part of the frame, so +// most pixels never take a march step at all, and the shader ends up +// cheaper than the one box version it replaces despite carrying nine. +bool hitAABB(float3 o, float3 d, float3 bn, float3 bx, out float t0, out float t1) +{ + float3 inv = 1.0 / (d + 1e-9); + float3 a = (bn - o) * inv; + float3 b = (bx - o) * inv; + float3 lo = min(a, b), hi = max(a, b); + t0 = max(max(lo.x, lo.y), lo.z); + t1 = min(min(hi.x, hi.y), hi.z); + return t1 > max(t0, 0.0); +} + +/* ---- the break ------------------------------------------------------- + Everything about a piece depends only on constants and on uniforms, so + it is solved once per pixel here and the marcher just reads it. Doing it + inside map() would put nine normalize() and eighteen sincos inside a + sixty step loop, which is the difference between a shot and a stall. */ +void setupSlabs(float A, float trem, float ts) +{ + gLeanS = sin(gTune.x * 0.14); + gLeanC = cos(gTune.x * 0.14); + + [unroll] for (int k = 0; k < NSLAB; k++) { + float e0 = kEdge[k], e1 = kEdge[k + 1]; + float cy = (e0 + e1) * 4.6 - 4.6; // band centre, monolith space + float hy = (e1 - e0) * 4.6; // and its half height + + // R3 low discrepancy, so nine pieces are well spread without a + // hash and without a table to maintain. + float3 r = frac(float3(0.7548776, 0.5698402, 0.3819660) * (float(k) + 2.0)); + + // Thrown outward and slightly up. The lean biases the throw, so a + // monolith that leans is one that comes apart the way it leans. + float3 dir = normalize(float3((r.x - 0.5) * 2.00 + 0.55 * gTune.x, + (r.y - 0.18) * 0.80, + (r.z - 0.5) * 2.00) + float3(0.0, 0.02, 0.0)); + + // The bottom of the shape barely moves and does not turn AT ALL. + // What is left standing is what keeps this readable as the monolith + // rather than as a pile of boxes - and a stump that spun in place + // would both look wrong and swing geometry down through the floor, + // so the hold gates the tumble exactly as it gates the throw. + float hold = smoothstep(-0.02, 0.30, e0); + float spd = (0.55 + 2.35 * r.z) * hold; + + // A tremor that lives and dies with the sound, not with the clock. + // Three centimetres of it - a vibration, never a flash. + float wob = sin(ts * 26.0 + float(k) * 2.1) * trem * 0.035 * hold; + + float3 c = float3(0.0, cy, 0.0) + dir * (A * spd + wob); + + float a = (r.x - 0.5) * 2.10 * A * hold; + float b = (r.y - 0.5) * 1.55 * A * hold; + + gSA[k] = float4(c, hy); + gSB[k] = float4(cos(a), sin(a), cos(b), sin(b)); + } + + /* The bound is fitted to the exact swept extent of the nine oriented + pieces with about fifteen percent to spare, because it is doing two + jobs at once: a bound that clips is geometry that silently vanishes, + and a bound that is loose is march steps spent on empty white. */ + gBmin = float3(-(1.55 + 2.60 * A), -0.50, -(1.80 + 1.95 * A)); + gBmax = float3( 1.55 + 2.60 * A, 9.60 + 1.70 * A, 1.80 + 1.95 * A); + + // The chips throw much further than the pieces do, and twenty six + // analytic spheres is the one cost this shader pays on every single + // pixel. Same trick: bound them, and the white two thirds of the frame + // stops paying for debris it cannot see. + gCmin = float3(-(1.20 + 6.60 * A), 0.50, -(1.60 + 5.70 * A)); + gCmax = float3( 1.20 + 6.60 * A, 9.40 + 5.50 * A, 1.60 + 5.70 * A); +} + +// Just the pieces. The floor is solved analytically, so it is not in here. +float mapObj(float3 p) +{ + float3 m = p - float3(0.0, 4.6, 0.0); + m.xy = float2(m.x * gLeanC - m.y * gLeanS, m.x * gLeanS + m.y * gLeanC); + + float d = 1e9; + [unroll] for (int k = 0; k < NSLAB; k++) { + float3 q = m - gSA[k].xyz; + float4 R = gSB[k]; + q.xy = float2(q.x * R.x + q.y * R.y, -q.x * R.y + q.y * R.x); + q.yz = float2(q.y * R.z + q.z * R.w, -q.y * R.w + q.z * R.z); + d = min(d, sdBox(q, float3(0.608, gSA[k].w - 0.012, 1.538)) - 0.012); + } + return d; +} + +float3 objNormal(float3 p) +{ + const float2 e = float2(1.0, -1.0) * 0.0022; + return normalize(e.xyy * mapObj(p + e.xyy) + e.yyx * mapObj(p + e.yyx) + + e.yxy * mapObj(p + e.yxy) + e.xxx * mapObj(p + e.xxx)); +} + +/* Is this surface one of the faces the strike MADE? The monolith is + polished black on the four faces it was born with; a fracture is raw. + Finding which piece we are on costs one extra pass of nine boxes at a + single point, and it buys the whole reason to break something on camera - + you have to be able to see the inside. + + The test is on the normal turned back into the piece's own frame, not on + the position: a cut face is the one whose local normal is +-y. Testing + position instead wraps a grey stripe around the top edge of every side + face, which reads as a bug rather than as an exposed section. */ +float cutFace(float3 p, float3 n) +{ + float3 m = p - float3(0.0, 4.6, 0.0); + m.xy = float2(m.x * gLeanC - m.y * gLeanS, m.x * gLeanS + m.y * gLeanC); + float3 nl0 = float3(n.x * gLeanC - n.y * gLeanS, n.x * gLeanS + n.y * gLeanC, n.z); + + float best = 1e9, ny = 0.0; + [unroll] for (int k = 0; k < NSLAB; k++) { + float4 R = gSB[k]; + float3 q = m - gSA[k].xyz; + q.xy = float2(q.x * R.x + q.y * R.y, -q.x * R.y + q.y * R.x); + q.yz = float2(q.y * R.z + q.z * R.w, -q.y * R.w + q.z * R.z); + float d = sdBox(q, float3(0.608, max(gSA[k].w - 0.012, 0.02), 1.538)) - 0.012; + if (d < best) { + best = d; + float2 t = float2(nl0.x * R.x + nl0.y * R.y, -nl0.x * R.y + nl0.y * R.x); + ny = t.y * R.z + nl0.z * R.w; + } + } + return smoothstep(0.55, 0.86, abs(ny)); +} + +// The fine stuff the strike threw off. Analytic spheres rather than SDF +// geometry: at this distance a chip is three to ten pixels across, so a +// silhouette is all of it, and twenty six of them cost less than one more +// box inside the march. +float4 chipAt(int k) +{ + float3 r = frac(float3(0.7548776, 0.5698402, 0.3819660) * (float(k) + 5.0)); + float3 r2 = frac(float3(0.8191725, 0.6710436, 0.5497004) * (float(k) + 11.0)); + + float3 rest = float3((r.x - 0.5) * 1.10, (r.y - 0.5) * 9.00, (r.z - 0.5) * 2.70); + float3 dir = normalize(float3(rest.x * 1.70 + (r2.x - 0.5) * 1.90, + (r2.y - 0.15) * 1.20, + rest.z * 0.90 + (r2.z - 0.5) * 1.90) + + float3(0.0, 0.03, 0.0)); + + // Same rule as the pieces: what is low down stays where it was. + float3 lp = rest + dir * (gA * (1.6 + 6.4 * r2.x) + * smoothstep(-3.6, -1.8, rest.y)); + + float3 w = float3(lp.x * gLeanC + lp.y * gLeanS, + 4.6 - lp.x * gLeanS + lp.y * gLeanC, + lp.z); + float rad = 0.035 + 0.095 * r.z * r.z; + w.y = max(w.y, rad * 0.92); // nothing sinks into the floor + return float4(w, rad); +} + +float shadowSlabs(float3 o, float3 d) +{ + float t0, t1; + if (!hitAABB(o, d, gBmin, gBmax, t0, t1)) return 1.0; + + float t = max(t0, 0.05), te = min(t1, 90.0), res = 1.0; + [loop] for (int i = 0; i < 28; i++) { + if (t > te) break; + float h = mapObj(o + d * t); + if (h < 0.0022) return 0.0; + res = min(res, 7.5 * h / t); + t += clamp(h, 0.05, 2.0); + } + return saturate(res); +} + +// Twenty six little shadows scattered across a white floor. This is most of +// what sells the freeze: without them the chips are stickers. +float chipShade(float2 g) +{ + float s = 1.0; + [unroll] for (int k = 0; k < NCHIP; k++) { + float4 c = chipAt(k); + float lift = max(c.y, 0.0) / SUN.y; + float2 gp = c.xz - SUN.xz * lift; + float rad = c.w * (1.7 + 0.55 * lift); + float dd = length(g - gp); + s *= 1.0 - 0.55 * (1.0 - smoothstep(rad * 0.25, rad, dd)); + } + return s; +} + +// Ambient contact under the hanging pieces. The sun shadow is directional +// and long; this is the short dark bloom directly beneath a mass, and it is +// the difference between a piece that hangs and a piece that was pasted on. +float slabContact(float2 g) +{ + float o = 1.0; + [unroll] for (int k = 0; k < NSLAB; k++) { + float3 lp = gSA[k].xyz; + float2 w = float2(lp.x * gLeanC + lp.y * gLeanS, lp.z); + float h = max(4.6 - lp.x * gLeanS + lp.y * gLeanC, 0.35); + float rad = 1.5 + h * 0.42; + o *= 1.0 - 0.30 * exp(-dot(g - w, g - w) / (rad * rad)) / (1.0 + h * 0.22); + } + return o; +} + +float4 main(VSOut i) : SV_Target +{ + float2 uv = float2(i.uv.x * 2.0 - 1.0, 1.0 - i.uv.y * 2.0); + // y is FLIPPED here on purpose. The fullscreen triangle emits + // uv.y = 0 at the top of the screen, so i.uv*2-1 gives -1 up + // there and the top of the frame ended up looking DOWNWARD - + // the cathedral vault springs at y=+12 and was rendering in the + // lower half, the monolith hung from the ceiling. + uv.x *= gTime.w; + + float3 fw = normalize(gDir.xyz); + float3 rt = normalize(cross(float3(0.0, 1.0, 0.0), fw)); + float3 up = cross(fw, rt); + float cr = cos(gDir.w), sr = sin(gDir.w); + float3 ro = gCam.xyz; + float3 rd = normalize(fw + (uv.x * (rt * cr + up * sr) + + uv.y * (up * cr - rt * sr)) * gCam.w); + + /* ---- the strike clock ---------------------------------------------- + 1 - exp(-kt) is the whole idea. Velocity is highest at the instant of + the hit and decays from there, so the break is violent under the + flash and has visibly stopped by the time the silence is half over. + The tiny linear term on the end means it never becomes a literal + still frame - the pieces creep about six centimetres across the rest + of the shot, which you do not see so much as fail to be bored by. */ + float ts = max(gTime.x - gTune.w, 0.0); + float A = saturate(gTune.z) + (1.0 - exp(-ts * 3.4)) + ts * 0.030; + gA = min(A, 1.35); + setupSlabs(gA, saturate(gSync.w), ts); + + // The void. Not pure white at the top, or there is nothing for the + // dither to bite on. The last term is a one percent swell across the + // whole field - far too small to see as a gradient, just enough to + // walk the dither threshold so the white is alive rather than dead. + float3 sky = lerp(float3(0.972, 0.972, 0.962), + float3(0.780, 0.800, 0.840), + saturate(rd.y * 1.6 + 0.10)); + sky += 0.009 * sin(rd.x * 2.7 + gTime.x * 0.30) * sin(rd.y * 3.3 - gTime.x * 0.21); + + float3 col = sky; + float tScene = 1e9; + + // ---- the floor, solved rather than marched ---- + float tG = (rd.y < -1e-5) ? (-ro.y / rd.y) : 1e9; + if (tG < 0.0) tG = 1e9; + + // ---- the pieces, marched only inside their own bound ---- + float tObj = 1e9; + { + float t0, t1; + if (hitAABB(ro, rd, gBmin, gBmax, t0, t1)) { + float t = max(t0, 0.05); + float te = min(t1, min(tG, 300.0)); + [loop] for (int k = 0; k < 60; k++) { + if (t > te) break; + float h = mapObj(ro + rd * t); + if (h < 0.0010 * t) { tObj = t; break; } + t += h; + } + } + } + + if (tObj < 1e8) { + float3 p = ro + rd * tObj; + float3 n = objNormal(p); + float sh = shadowSlabs(p + n * 0.02, SUN); + float di = saturate(dot(n, SUN)); + + // Polished black where the shape was born, raw grey where the stab + // opened it. The grain is coarse on purpose: it has to survive a + // seven step quantiser to be a texture rather than a smudge. + float cut = cutFace(p, n); + float3 gp = floor(p * 21.0); + float gr = frac(sin(dot(gp, float3(12.9898, 78.233, 37.719))) * 43758.5453); + float3 albedo = lerp(float3(0.030, 0.032, 0.038), + float3(0.400, 0.394, 0.380) * (0.72 + 0.50 * gr), + cut); + + // The void is a lit dome, so upward faces pick up a lot of it. + float amb = 0.30 + 0.42 * saturate(n.y * 0.5 + 0.5); + col = albedo * (amb + 0.95 * di * sh); + + // A white world puts a hard rim on a black edge. On the quantiser + // this becomes a one pixel outline, which is exactly the look. + float fres = pow(1.0 - saturate(dot(n, -rd)), 4.0); + col += (0.13 + 0.10 * cut) * fres * float3(0.95, 0.96, 1.00); + + tScene = tObj; + } + else if (tG < 400.0) { + float3 p = float3(ro.x + rd.x * tG, 0.0, ro.z + rd.z * tG); + float2 g = p.xz; + float r = length(g); + + // Every chip shadow lands inside r = 18.8 and every contact bloom + // inside r = 22, measured against the throw. Past that the floor is + // clean white and there is nothing to compute. + float sh = shadowSlabs(p + float3(0.0, 0.015, 0.0), SUN); + if (r < 20.0) sh = min(sh, chipShade(g)); + + /* The ground opened where it was hit. The crack FRONT races out on + its own envelope and then halts, so for the first third of a + second there is something travelling across the floor even after + the pieces have slowed - a second thing with its own timing, + which is what keeps a held shot from being one idea. */ + float front = 2.5 + 58.0 * (1.0 - exp(-ts * 4.6)); + float a = atan2(g.y, g.x); + + float u = a * (9.0 / 6.28318) + sin(r * 0.21) * 0.55 + + sin(r * 0.065 + 1.7) * 0.85; + float arc = abs(frac(u) - 0.5) * r * 0.698; // ~world width + float crack = 1.0 - smoothstep(0.045, 0.24 + r * 0.02, arc); + + float ru = r * 0.155 + sin(a * 5.0) * 0.22 + sin(a * 2.0 + 0.9) * 0.15; + float ring = abs(frac(ru) - 0.5) * 6.45; + crack = max(crack, (1.0 - smoothstep(0.10, 0.42 + r * 0.02, ring)) * 0.55); + + crack *= 1.0 - smoothstep(front * 0.80, front, r); // the front + crack *= smoothstep(0.6, 2.2, r); // not under the stump + crack *= saturate(1.0 - r / 150.0); + + // Keep the fracture front subordinate to the standing silhouette. + // Near-black rings used to compete with every suspended slab. + float3 albedo = lerp(float3(0.940, 0.940, 0.930), + float3(0.085, 0.088, 0.095), crack * 0.32); + + // Dust swept out along the front. Written base + k*voice so the + // floor never drops out from under the ring - this is a local + // eleven percent smudge, never a frame that changes brightness. + float dust = exp(-abs(r - front) * 0.30); + albedo *= 1.0 - 0.11 * dust * (0.55 + 0.45 * saturate(gVoice.y)); + + if (r < 22.0) albedo *= slabContact(g); + + col = albedo * (0.20 + 0.90 * SUN.y * sh); + tScene = tG; + } + + // ---- the chips ---- + { + float ct0, ct1; + if (hitAABB(ro, rd, gCmin, gCmax, ct0, ct1) && ct0 < tScene) { + [unroll] for (int c = 0; c < NCHIP; c++) { + float4 ch = chipAt(c); + float3 oc = ro - ch.xyz; + float b = dot(oc, rd); + float cc = dot(oc, oc) - ch.w * ch.w; + float hh = b * b - cc; + if (hh > 0.0) { + float tt = -b - sqrt(hh); + if (tt > 0.05 && tt < tScene) { + float3 cn = (ro + rd * tt - ch.xyz) / ch.w; + float cd = saturate(dot(cn, SUN)); + col = float3(0.045, 0.047, 0.053) * (0.34 + 0.90 * cd) + + 0.10 * pow(1.0 - saturate(dot(cn, -rd)), 4.0); + tScene = tt; + } + } + } + } + } + + // ---- the others, standing out in the haze ---- + [unroll] for (int f = 0; f < 3; f++) { + float4 F = kFar[f]; + float t0, t1; + if (hitAABB(ro, rd, float3(F.x - F.z, 0.0, F.y - F.z * 2.3), + float3(F.x + F.z, F.w, F.y + F.z * 2.3), t0, t1) + && t0 > 0.0 && t0 < tScene) { + col = float3(0.055, 0.058, 0.066); + tScene = t0; + } + } + + // Aerial haze back into the void, so the ground has no visible end. + // Fogging towards the sky rather than towards a constant is what makes + // the horizon a seam you cannot find. + if (tScene < 1e8) { + // Clear foreground air preserves black faces through the bleach + // quantiser. The old zero-distance haze lifted the subject about + // 0.4 towards white; the remote monoliths still dissolve normally. + float fog = 1.0 - exp(-max(tScene - 18.0, 0.0) * 0.021); + col = lerp(col, sky, fog * (0.55 + 0.45 * gTune.y)); + } + + return float4(col * gTime.z, 1.0); +} diff --git a/gfx.c b/gfx.c new file mode 100644 index 0000000..67aea48 --- /dev/null +++ b/gfx.c @@ -0,0 +1,2594 @@ +/* ========================================================================== + C R T k a f a - D 3 D 1 1 E N G I N E + -------------------------------------------------------------------------- + Single translation unit, no C runtime (/NODEFAULTLIB, /ENTRY:entry). + The only DLLs are ones that ship with Windows, and d3d11.dll is loaded by + hand rather than imported, so a machine without it gets our message + instead of the loader's. + + Scene geometry is a pixel shader and the edit is a table. A new look is a + .hlsl file plus a row in shots.h - not a new rasteriser path in C, and not + another term added to a camera integral. + ========================================================================== */ + +#define COBJMACROS +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include +#include + +/* -------------------------------------------------------------------------- + no-CRT glue + -------------------------------------------------------------------------- */ +int _fltused = 1; + +#pragma function(memset) +void *memset(void *d, int c, size_t n) +{ + unsigned char *p = (unsigned char *)d; + while (n--) *p++ = (unsigned char)c; + return d; +} + +#pragma function(memcpy) +void *memcpy(void *d, const void *s, size_t n) +{ + unsigned char *a = (unsigned char *)d; + const unsigned char *b = (const unsigned char *)s; + while (n--) *a++ = *b++; + return d; +} + +/* /O2 emits calls to this for some struct copies even though nothing in the + source says memmove. Without it the link fails with an unresolved symbol + that points at no line of code - and without the pragma the compiler + refuses to let us define it at all (C2169). */ +#pragma function(memmove) +void *memmove(void *d, const void *s, size_t n) +{ + unsigned char *a = (unsigned char *)d; + const unsigned char *b = (const unsigned char *)s; + if (a == b || n == 0) return d; + if (a < b) { while (n--) *a++ = *b++; } + else { a += n; b += n; while (n--) *--a = *--b; } + return d; +} + +/* -------------------------------------------------------------------------- + maths, without libm + -------------------------------------------------------------------------- */ +#define PI_ 3.14159265359f +#define TAU_ 6.28318530718f + +static float fsin_(float x) +{ + float y, z; + x -= TAU_ * (float)(int)(x / TAU_); + if (x > PI_) x -= TAU_; + if (x < -PI_) x += TAU_; + /* Fold before evaluating. The former seventh-order polynomial evaluated + at +/-pi jumped by 0.1504 every wrap, including inside audio voices. */ + if (x > PI_ * 0.5f) x = PI_ - x; + if (x < -PI_ * 0.5f) x = -PI_ - x; + z = x * x; + y = x * (1.0f + z * (-0.166666667f + z * (0.00833333333f + z * + (-0.000198412698f + z * (2.75573192e-6f - z * 2.50521084e-8f))))); + return y; +} +static float fcos_(float x) { return fsin_(x + PI_ * 0.5f); } +static float fsqrt_(float x) +{ + return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(x))); +} + +/* The synth and camera use this folded polynomial. The isolated audio harness + must select its D3D backend to match this implementation, not the old LUT. */ +#define lsin fsin_ + +#include "song_data.h" /* the baked note tables */ +#include "synth.h" /* and the synth that reads them */ +#include "shaders.h" /* fxc /Fh output, built by buildgfx.bat */ +#include "shots.h" /* the edit */ +#include "cats_mesh.h" /* the cat, 24 baked frames of a run cycle */ +#include "tardis_mesh.h" /* a single-frame, distant blue-box callback */ +#include "c64_font.h" /* the boot screen, the author's own words */ + +/* -------------------------------------------------------------------------- + sizes + -------------------------------------------------------------------------- */ +#define VH 360 /* virtual height, fixed. width follows aspect */ +#define VW_MIN 480 /* 4:3 */ +#define VW_MAX 1280 /* 32:9 */ + +/* One row of the song, in seconds, derived from the synth's own constants so + there is a single source of truth. Better still, the clock below does not + use it at all in the normal case: the row index comes from the number of + samples the sound card has actually played. */ +#define SEC_PER_ROW ((float)SPR / (float)SRATE) + +static int g_vw = 640, g_vh = VH; +static int g_ow = 1280, g_oh = 720; + +/* -------------------------------------------------------------------------- + d3d + -------------------------------------------------------------------------- */ +static ID3D11Device *g_dev; +static ID3D11DeviceContext *g_ctx; +static IDXGISwapChain1 *g_sc; +static ID3D11RenderTargetView *g_backRTV; + +/* Two scene targets: a dissolve renders the outgoing and the incoming shot + in the same frame and the post pass mixes them. */ +static ID3D11Texture2D *g_sceneTex[2]; +static ID3D11RenderTargetView *g_sceneRTV[2]; +static ID3D11ShaderResourceView *g_sceneSRV[2]; + +static ID3D11Texture2D *g_depthTex; +static ID3D11DepthStencilView *g_depthDSV; +static ID3D11DepthStencilState *g_dsSdf; /* the background always wins */ +static ID3D11DepthStencilState *g_dsMesh; /* meshes test, reversed Z */ + +static ID3D11ShaderResourceView *g_catPos; /* over g_rcatV, no copy */ +static ID3D11ShaderResourceView *g_catIdx; /* over g_rcatI */ +static ID3D11ShaderResourceView *g_tarPos; /* over g_tarV */ +static ID3D11ShaderResourceView *g_tarIdx; /* over g_tarI */ +static ID3D11ShaderResourceView *g_tarColor; +static ID3D11ShaderResourceView *g_spinPos, *g_spinIdx, *g_spinColor; +static ID3D11RasterizerState *g_rsRigid; /* imported meshes, two-sided */ +static ID3D11VertexShader *g_vsMesh; +static ID3D11PixelShader *g_psMesh; +static ID3D11Buffer *g_cbMesh; +static ID3D11Buffer *g_cbFont; /* uploaded once, never again */ + +/* Bloom: two half resolution targets, ping-ponged through a bright pass and + two blur passes. Half res and one level only - at 180 lines a second level + stopped being a glow and started being fog. */ +static ID3D11Texture2D *g_bloomTex[2]; +static ID3D11RenderTargetView *g_bloomRTV[2]; +static ID3D11ShaderResourceView *g_bloomSRV[2]; +static ID3D11PixelShader *g_psBloom; +static ID3D11Buffer *g_cbBloom; +static int g_bw, g_bh; + +typedef struct { float step[4], param[4]; } BloomCB; + +typedef struct { float model[4], anim[4], misc[4]; } MeshCB; + +static ID3D11VertexShader *g_vsFull; +static ID3D11PixelShader *g_psScene[SC_COUNT]; +static ID3D11PixelShader *g_psPost; +static ID3D11Buffer *g_cbScene; +static ID3D11Buffer *g_cbPost; +static ID3D11SamplerState *g_sampPoint; +static ID3D11SamplerState *g_sampLinear; + +/* Unbinding takes a real array of nulls. Passing NULL for the array is not + how you clear a slot, and the scene texture stays bound as an input while + we try to render into it - which D3D resolves by giving you a black frame + and a warning nobody sees in a build with no debug layer. */ +static ID3D11ShaderResourceView *const g_nullSRV[3] = { 0, 0, 0 }; +static ID3D11RenderTargetView *const g_nullRTV[1] = { 0 }; + +/* gSync.xyz are the three bands, gSync.w the master peak; gVoice carries + the four per-instrument envelopes a scene might want to single out. */ +typedef struct { + float t[4], cam[4], dir[4], tune[4]; + float sync[4], voice[4], caption[4]; +} SceneCB; +typedef struct { + float res[4], crt[4], grade[4], fx[4]; + float ramp[4][4]; /* the section palette, already crossfaded */ + float tone[4]; /* x hue kept, y quantiser steps */ +} PostCB; + +/* -------------------------------------------------------------------------- + state + -------------------------------------------------------------------------- */ +static HWND g_hwnd; +static int g_full = 0; +static int g_paused = 0; +static int g_running = 1; +static int g_sizing = 0; +static int g_warp = 0; +static WINDOWPLACEMENT g_place = { sizeof(WINDOWPLACEMENT) }; +static LONG_PTR g_style = 0; +static double g_row = 0.0; /* the clock, in song rows */ +static int g_wasPaused = 0; +static __int64 g_lastPlayed = -1; /* to notice the device going away */ +static int g_audioStall = 0; +static LARGE_INTEGER g_qpcFreq, g_qpcLast; + +static void fatal(const WCHAR *msg) +{ + MessageBoxW(0, msg, L"CRTkafa", MB_OK | MB_ICONERROR); + ExitProcess(1); +} + +/* -------------------------------------------------------------------------- + device + -------------------------------------------------------------------------- + d3d11.dll is resolved at runtime on purpose. As a normal import, a machine + without it would never reach our code at all: the loader would put up "The + code execution cannot proceed because d3d11.dll was not found" and quit. + Loading it by hand is the only way our own message ever appears. dxgi.dll + needs no such care - we reach the factory through the device's own + adapter, so we never call a dxgi export directly. + -------------------------------------------------------------------------- */ +typedef HRESULT (WINAPI *PFN_D3D11CreateDevice_)( + IDXGIAdapter *, D3D_DRIVER_TYPE, HMODULE, UINT, + const D3D_FEATURE_LEVEL *, UINT, UINT, + ID3D11Device **, D3D_FEATURE_LEVEL *, ID3D11DeviceContext **); + +static int createDevice(void) +{ + static const D3D_FEATURE_LEVEL want[] = { + D3D_FEATURE_LEVEL_11_0 + }; + PFN_D3D11CreateDevice_ create; + D3D_FEATURE_LEVEL got = (D3D_FEATURE_LEVEL)0; + HMODULE lib; + HRESULT hr; + UINT flags = D3D11_CREATE_DEVICE_SINGLETHREADED; + + lib = LoadLibraryA("d3d11.dll"); + if (!lib) + fatal(L"This demo needs Direct3D 11.\n\n" + L"d3d11.dll was not found. This build requires Windows 10 or " + L"newer. Repair Windows system components and update your graphics driver."); + + create = (PFN_D3D11CreateDevice_)GetProcAddress(lib, "D3D11CreateDevice"); + if (!create) + fatal(L"This demo needs Direct3D 11.\n\n" + L"d3d11.dll is present but does not export D3D11CreateDevice."); + + hr = create(0, D3D_DRIVER_TYPE_HARDWARE, 0, flags, + want, 1, D3D11_SDK_VERSION, &g_dev, &got, &g_ctx); + + if (hr < 0) { + /* No usable GPU. WARP is the software rasteriser that ships with + Windows: feature level 11_0, and slow enough that the virtual + resolution is halved to keep it moving. Seeing the demo badly + beats not seeing it. */ + hr = create(0, D3D_DRIVER_TYPE_WARP, 0, flags, + want, 1, D3D11_SDK_VERSION, &g_dev, &got, &g_ctx); + if (hr < 0) + fatal(L"No Direct3D 11 capable device was found.\n\n" + L"Updating the graphics driver usually fixes this. The " + L"software fallback could not be created either."); + g_warp = 1; + } + if (got < D3D_FEATURE_LEVEL_11_0) + fatal(L"This device is below Direct3D feature level 11.0 and cannot " + L"run the demo."); + return 1; +} + +/* -------------------------------------------------------------------------- + swap chain + -------------------------------------------------------------------------- */ +static int createSwapChain(HWND hwnd) +{ + IDXGIDevice1 *dxdev = 0; + IDXGIAdapter *adap = 0; + IDXGIFactory2 *fac = 0; + DXGI_SWAP_CHAIN_DESC1 sd; + RECT rc; + HRESULT hr; + + GetClientRect(hwnd, &rc); + g_ow = rc.right - rc.left; if (g_ow < 16) g_ow = 16; + g_oh = rc.bottom - rc.top; if (g_oh < 16) g_oh = 16; + + if (ID3D11Device_QueryInterface(g_dev, &IID_IDXGIDevice1, (void **)&dxdev) < 0) + return 0; + if (IDXGIDevice1_GetAdapter(dxdev, &adap) < 0) return 0; + if (IDXGIAdapter_GetParent(adap, &IID_IDXGIFactory2, (void **)&fac) < 0) + return 0; + + memset(&sd, 0, sizeof(sd)); + sd.Width = (UINT)g_ow; + sd.Height = (UINT)g_oh; + sd.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + sd.SampleDesc.Count = 1; + sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + sd.BufferCount = 2; + sd.Scaling = DXGI_SCALING_STRETCH; + /* Flip model. The combined D3D11CreateDeviceAndSwapChain call can only + produce the old blt model, where DWM copies the backbuffer every frame + - extra latency, extra bandwidth, and borderless fullscreen never gets + independent flip. That is why the device and the chain are separate. */ + sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; + sd.AlphaMode = DXGI_ALPHA_MODE_IGNORE; + + hr = IDXGIFactory2_CreateSwapChainForHwnd(fac, (IUnknown *)g_dev, hwnd, + &sd, 0, 0, &g_sc); + + /* DXGI installs its own hook on our window and will try to handle + Alt+Enter itself, fighting the F key and flipping display modes behind + our back. This tells it to keep its hands off. */ + IDXGIFactory2_MakeWindowAssociation(fac, hwnd, + DXGI_MWA_NO_ALT_ENTER | DXGI_MWA_NO_WINDOW_CHANGES); + + IDXGIFactory2_Release(fac); + IDXGIAdapter_Release(adap); + IDXGIDevice1_Release(dxdev); + return hr >= 0; +} + +static void releaseBackbuffer(void) +{ + if (g_backRTV) { ID3D11RenderTargetView_Release(g_backRTV); g_backRTV = 0; } +} + +static int createBackbuffer(void) +{ + ID3D11Texture2D *tex = 0; + HRESULT hr; + if (IDXGISwapChain1_GetBuffer(g_sc, 0, &IID_ID3D11Texture2D, (void **)&tex) < 0) + return 0; + hr = ID3D11Device_CreateRenderTargetView(g_dev, (ID3D11Resource *)tex, 0, + &g_backRTV); + /* GetBuffer hands us a reference we own. Holding it is what makes the + next ResizeBuffers fail with DXGI_ERROR_INVALID_CALL, and that failure + is the classic "resizing the window breaks everything" bug. */ + ID3D11Texture2D_Release(tex); + return hr >= 0; +} + +/* -------------------------------------------------------------------------- + the low resolution scene targets + -------------------------------------------------------------------------- + Fixed height, width from the aspect ratio: the camera widens on an + ultrawide instead of the picture being letterboxed. The chunky pixels are + not a compromise, they are the look - ordered dither and scanlines only + read as themselves when a virtual pixel is several real ones. + -------------------------------------------------------------------------- */ +static void releaseSceneTargets(void) +{ + int i; + for (i = 0; i < 2; i++) { + if (g_sceneSRV[i]) { ID3D11ShaderResourceView_Release(g_sceneSRV[i]); g_sceneSRV[i] = 0; } + if (g_sceneRTV[i]) { ID3D11RenderTargetView_Release(g_sceneRTV[i]); g_sceneRTV[i] = 0; } + if (g_sceneTex[i]) { ID3D11Texture2D_Release(g_sceneTex[i]); g_sceneTex[i] = 0; } + } + for (i = 0; i < 2; i++) { + if (g_bloomSRV[i]) { ID3D11ShaderResourceView_Release(g_bloomSRV[i]); g_bloomSRV[i] = 0; } + if (g_bloomRTV[i]) { ID3D11RenderTargetView_Release(g_bloomRTV[i]); g_bloomRTV[i] = 0; } + if (g_bloomTex[i]) { ID3D11Texture2D_Release(g_bloomTex[i]); g_bloomTex[i] = 0; } + } + if (g_depthDSV) { ID3D11DepthStencilView_Release(g_depthDSV); g_depthDSV = 0; } + if (g_depthTex) { ID3D11Texture2D_Release(g_depthTex); g_depthTex = 0; } +} + +static int createSceneTargets(int ow, int oh) +{ + D3D11_TEXTURE2D_DESC td; + int w, i, lo; + + g_vh = g_warp ? VH / 2 : VH; + lo = g_warp ? VW_MIN / 2 : VW_MIN; + w = (int)(((float)g_vh * (float)ow) / (float)oh + 0.5f); + if (w < lo) w = lo; + if (w > VW_MAX) w = VW_MAX; + g_vw = w & ~1; + + releaseSceneTargets(); + + memset(&td, 0, sizeof(td)); + td.Width = (UINT)g_vw; + td.Height = (UINT)g_vh; + td.MipLevels = 1; + td.ArraySize = 1; + td.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; /* the scene is HDR */ + td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; + td.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; + + for (i = 0; i < 2; i++) { + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &g_sceneTex[i]) < 0) return 0; + if (ID3D11Device_CreateRenderTargetView(g_dev, + (ID3D11Resource *)g_sceneTex[i], 0, &g_sceneRTV[i]) < 0) return 0; + if (ID3D11Device_CreateShaderResourceView(g_dev, + (ID3D11Resource *)g_sceneTex[i], 0, &g_sceneSRV[i]) < 0) return 0; + } + + /* Depth, so a mesh and the distance field behind it can occlude each + other. It follows the aspect-driven resize like everything else. */ + memset(&td, 0, sizeof(td)); + td.Width = (UINT)g_vw; + td.Height = (UINT)g_vh; + td.MipLevels = 1; + td.ArraySize = 1; + td.Format = DXGI_FORMAT_D32_FLOAT; + td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; + td.BindFlags = D3D11_BIND_DEPTH_STENCIL; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &g_depthTex) < 0) return 0; + if (ID3D11Device_CreateDepthStencilView(g_dev, (ID3D11Resource *)g_depthTex, + 0, &g_depthDSV) < 0) return 0; + + g_bw = g_vw / 2; g_bh = g_vh / 2; + memset(&td, 0, sizeof(td)); + td.Width = (UINT)g_bw; + td.Height = (UINT)g_bh; + td.MipLevels = 1; + td.ArraySize = 1; + td.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; + td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; + td.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; + for (i = 0; i < 2; i++) { + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &g_bloomTex[i]) < 0) return 0; + if (ID3D11Device_CreateRenderTargetView(g_dev, + (ID3D11Resource *)g_bloomTex[i], 0, &g_bloomRTV[i]) < 0) return 0; + if (ID3D11Device_CreateShaderResourceView(g_dev, + (ID3D11Resource *)g_bloomTex[i], 0, &g_bloomSRV[i]) < 0) return 0; + } + return 1; +} + +/* -------------------------------------------------------------------------- + pipeline objects + -------------------------------------------------------------------------- */ +/* An immutable typed buffer laid straight over a const array in a header. + pSysMem points at the array itself, so there is no copy and no allocation - + which is the only reason a program with no C runtime can have meshes at + all. The view holds a reference, so the buffer is released immediately. */ +static ID3D11ShaderResourceView *makeBuf(const void *data, unsigned bytes, + DXGI_FORMAT fmt, unsigned elems) +{ + D3D11_BUFFER_DESC bd; + D3D11_SUBRESOURCE_DATA sd; + D3D11_SHADER_RESOURCE_VIEW_DESC vd; + ID3D11Buffer *buf = 0; + ID3D11ShaderResourceView *srv = 0; + + memset(&bd, 0, sizeof(bd)); + bd.ByteWidth = bytes; + bd.Usage = D3D11_USAGE_IMMUTABLE; + bd.BindFlags = D3D11_BIND_SHADER_RESOURCE; + memset(&sd, 0, sizeof(sd)); + sd.pSysMem = data; + if (ID3D11Device_CreateBuffer(g_dev, &bd, &sd, &buf) < 0) return 0; + + memset(&vd, 0, sizeof(vd)); + vd.Format = fmt; + vd.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; + vd.Buffer.FirstElement = 0; + vd.Buffer.NumElements = elems; + ID3D11Device_CreateShaderResourceView(g_dev, (ID3D11Resource *)buf, &vd, &srv); + ID3D11Buffer_Release(buf); + return srv; +} + +static int createPipeline(void) +{ + D3D11_BUFFER_DESC bd; + D3D11_SAMPLER_DESC sd; + + if (ID3D11Device_CreateVertexShader(g_dev, g_vsFullscreen, + sizeof(g_vsFullscreen), 0, &g_vsFull) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psEyeBlob, + sizeof(g_psEyeBlob), 0, &g_psScene[SC_EYE]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psSceneBlob, + sizeof(g_psSceneBlob), 0, &g_psScene[SC_CATHEDRAL]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psLogoBlob, + sizeof(g_psLogoBlob), 0, &g_psScene[SC_LOGO]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psReactorBlob, + sizeof(g_psReactorBlob), 0, &g_psScene[SC_REACTOR]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psOrbitalBlob, + sizeof(g_psOrbitalBlob), 0, &g_psScene[SC_ORBITAL]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psVoidBlob, + sizeof(g_psVoidBlob), 0, &g_psScene[SC_VOID]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psHorizonBlob, + sizeof(g_psHorizonBlob), 0, &g_psScene[SC_HORIZON]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psKaleidoBlob, + sizeof(g_psKaleidoBlob), 0, &g_psScene[SC_KALEIDO]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psFramesBlob, + sizeof(g_psFramesBlob), 0, &g_psScene[SC_FRAMES]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psPlinthBlob, + sizeof(g_psPlinthBlob), 0, &g_psScene[SC_PLINTH]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psDriveBlob, + sizeof(g_psDriveBlob), 0, &g_psScene[SC_DRIVE]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psCorridorBlob, + sizeof(g_psCorridorBlob), 0, &g_psScene[SC_CORRIDOR]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psPowerOffBlob, + sizeof(g_psPowerOffBlob), 0, &g_psScene[SC_POWEROFF]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psBurstBlob, + sizeof(g_psBurstBlob), 0, &g_psScene[SC_BURST]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psBloomBlob, + sizeof(g_psBloomBlob), 0, &g_psBloom) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psC64Blob, + sizeof(g_psC64Blob), 0, &g_psScene[SC_C64]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psSignBlob, + sizeof(g_psSignBlob), 0, &g_psScene[SC_SIGNOFF]) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psPostBlob, + sizeof(g_psPostBlob), 0, &g_psPost) < 0) return 0; + + memset(&bd, 0, sizeof(bd)); + bd.ByteWidth = sizeof(SceneCB); + bd.Usage = D3D11_USAGE_DYNAMIC; + bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + if (ID3D11Device_CreateBuffer(g_dev, &bd, 0, &g_cbScene) < 0) return 0; + bd.ByteWidth = sizeof(PostCB); + if (ID3D11Device_CreateBuffer(g_dev, &bd, 0, &g_cbPost) < 0) return 0; + + memset(&sd, 0, sizeof(sd)); + sd.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; + sd.AddressU = sd.AddressV = sd.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; + sd.ComparisonFunc = D3D11_COMPARISON_NEVER; + sd.MaxLOD = D3D11_FLOAT32_MAX; + if (ID3D11Device_CreateSamplerState(g_dev, &sd, &g_sampPoint) < 0) return 0; + sd.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + if (ID3D11Device_CreateSamplerState(g_dev, &sd, &g_sampLinear) < 0) return 0; + + /* ---- the mesh path ---------------------------------------------- */ + if (ID3D11Device_CreateVertexShader(g_dev, g_vsMeshBlob, + sizeof(g_vsMeshBlob), 0, &g_vsMesh) < 0) return 0; + if (ID3D11Device_CreatePixelShader(g_dev, g_psMeshBlob, + sizeof(g_psMeshBlob), 0, &g_psMesh) < 0) return 0; + + bd.ByteWidth = sizeof(MeshCB); + if (ID3D11Device_CreateBuffer(g_dev, &bd, 0, &g_cbMesh) < 0) return 0; + bd.ByteWidth = sizeof(BloomCB); + if (ID3D11Device_CreateBuffer(g_dev, &bd, 0, &g_cbBloom) < 0) return 0; + + /* The font and the boot text: immutable, bound at b1, and uploaded once. + It has no business in b0, which is rewritten every frame for every + shot. */ + { + D3D11_BUFFER_DESC fd; + D3D11_SUBRESOURCE_DATA fs; + static unsigned int fontBlob[24 * 4 + 40 * 4]; + int k; + for (k = 0; k < 24 * 4; k++) + fontBlob[k] = (k < (int)(sizeof(g_c64Font) / sizeof(g_c64Font[0]))) + ? g_c64Font[k] : 0u; + for (k = 0; k < 40 * 4; k++) + fontBlob[24 * 4 + k] = + (k < (int)(sizeof(g_c64Text) / sizeof(g_c64Text[0]))) + ? g_c64Text[k] : 0u; + memset(&fd, 0, sizeof(fd)); + fd.ByteWidth = sizeof(fontBlob); + fd.Usage = D3D11_USAGE_IMMUTABLE; + fd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + memset(&fs, 0, sizeof(fs)); + fs.pSysMem = fontBlob; + if (ID3D11Device_CreateBuffer(g_dev, &fd, &fs, &g_cbFont) < 0) return 0; + } + + /* 396 vertices x 3 shorts x 24 frames, straight out of the header. */ + g_catPos = makeBuf(g_rcatV, (unsigned)sizeof(g_rcatV), + DXGI_FORMAT_R16_SINT, RCAT_NV * 3 * RCAT_NF); + g_catIdx = makeBuf(g_rcatI, (unsigned)sizeof(g_rcatI), + DXGI_FORMAT_R16_UINT, RCAT_NT * 3); + g_tarPos = makeBuf(g_tarV, (unsigned)sizeof(g_tarV), + DXGI_FORMAT_R16_SINT, TAR_NV * 3); + g_tarIdx = makeBuf(g_tarI, (unsigned)sizeof(g_tarI), + DXGI_FORMAT_R16_UINT, TAR_NT * 3); + /* The spinning mesh is named OCAT (Oiiaioooooiai) in cats_mesh.h. */ + g_spinPos = makeBuf(g_ocatV, (unsigned)sizeof(g_ocatV), + DXGI_FORMAT_R16_SINT, OCAT_NV * 3); + g_spinIdx = makeBuf(g_ocatI, (unsigned)sizeof(g_ocatI), + DXGI_FORMAT_R16_UINT, OCAT_NT * 3); + { + static unsigned char tarRGBA[TAR_NT * 4], spinRGBA[OCAT_NT * 4]; + int i, c; + for (i = 0; i < TAR_NT; i++) { + for (c = 0; c < 3; c++) + tarRGBA[i * 4 + c] = g_tarPal[g_tarM[i] * 3 + c]; + tarRGBA[i * 4 + 3] = 255; + } + for (i = 0; i < OCAT_NT; i++) { + for (c = 0; c < 3; c++) + spinRGBA[i * 4 + c] = g_ocatC[i * 3 + c]; + spinRGBA[i * 4 + 3] = 255; + } + g_tarColor = makeBuf(tarRGBA, (unsigned)sizeof(tarRGBA), + DXGI_FORMAT_R8G8B8A8_UNORM, TAR_NT); + g_spinColor = makeBuf(spinRGBA, (unsigned)sizeof(spinRGBA), + DXGI_FORMAT_R8G8B8A8_UNORM, OCAT_NT); + } + if (!g_catPos || !g_catIdx || !g_tarPos || !g_tarIdx || !g_tarColor || + !g_spinPos || !g_spinIdx || !g_spinColor) return 0; + { + D3D11_RASTERIZER_DESC rd; + memset(&rd, 0, sizeof(rd)); + rd.FillMode = D3D11_FILL_SOLID; + rd.CullMode = D3D11_CULL_NONE; + rd.DepthClipEnable = TRUE; + if (ID3D11Device_CreateRasterizerState(g_dev, &rd, &g_rsRigid) < 0) + return 0; + } + + /* The one way this fails silently: the fullscreen distance-field pass + must be ALWAYS. It is the background and always wins. With GREATER, a + sky pixel writes 0 against a cleared 0, the test fails, and the sky + disappears. */ + { + D3D11_DEPTH_STENCIL_DESC dd; + memset(&dd, 0, sizeof(dd)); + dd.DepthEnable = TRUE; + dd.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + dd.DepthFunc = D3D11_COMPARISON_ALWAYS; + if (ID3D11Device_CreateDepthStencilState(g_dev, &dd, &g_dsSdf) < 0) return 0; + dd.DepthFunc = D3D11_COMPARISON_GREATER; /* reversed Z: near is big */ + if (ID3D11Device_CreateDepthStencilState(g_dev, &dd, &g_dsMesh) < 0) return 0; + } + return 1; +} + + +static void upload(ID3D11Buffer *cb, const void *src, int bytes) +{ + D3D11_MAPPED_SUBRESOURCE m; + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)cb, 0, + D3D11_MAP_WRITE_DISCARD, 0, &m) < 0) return; + memcpy(m.pData, src, (size_t)bytes); + ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)cb, 0); +} + +static void setViewport(int w, int h) +{ + D3D11_VIEWPORT vp; + vp.TopLeftX = 0.0f; vp.TopLeftY = 0.0f; + vp.Width = (float)w; vp.Height = (float)h; + vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; + ID3D11DeviceContext_RSSetViewports(g_ctx, 1, &vp); +} + +/* -------------------------------------------------------------------------- + what the music is doing + -------------------------------------------------------------------------- + Read at the same sample index that decides which row is on screen, so the + two cannot disagree - and so the demo never has to know what the driver's + latency is. + + Draining the whole span since the last read is not an optimisation. A + 60 fps frame advances 2.87 grains, so reading one grain would skip two of + every three and lose any transient that landed in them. Peak-combining is + idempotent, so re-reading a grain above 172 fps is harmless. + -------------------------------------------------------------------------- */ +static void syncRead(__int64 played, SyncFrame *o) +{ + __int64 k = played >> 8, f = g_syncR; + memset(o, 0, sizeof(*o)); + if (f > k) f = k; /* paused, or rewound */ + if (k - f > SYNC_FRAMES - 1) f = k - (SYNC_FRAMES - 1); + for (; f <= k; f++) { + const SyncFrame *sf = &g_sync[f & SYNC_MASK]; + #define PK(m) if (sf->m > o->m) o->m = sf->m; + PK(kick) PK(snare) PK(hat) PK(gtr) PK(solo) PK(org) PK(bass) PK(level) + #undef PK + } + g_syncR = k + 1; +} + +/* A one-pole that needs no exp and is unconditionally stable for any dt, + which matters because dt spikes hard on an alt-tab. */ +static float follow(float cur, float tgt, float dt, float atkT, float relT) +{ + float T = (tgt > cur) ? atkT : relT; + return cur + (tgt - cur) * (dt / (T + dt)); +} + +/* Three bands, and each drives a different SCALE of thing: low moves big + slow shapes, mid moves mid-sized ones, high moves fine texture. That is + what makes the image share the music's frequency structure rather than + merely reacting to it. + + The low band's 420 ms release is the whole argument against the beat + throb this replaces. Against a 454 ms beat it is still falling when the + next kick lands, so it can only ever be a slowly breathing offset - it is + physically incapable of strobing. */ +/* Flash cuts scaled to taste, or to a viewer who needs them gone. */ +static float g_flashScale = 1.0f; + +static float g_bLow = 0.0f, g_bMid = 0.0f, g_bHigh = 0.0f; +static SyncFrame g_syncNow; + +static void syncFollow(const SyncFrame *sf, float dt) +{ + float lo = sf->kick > sf->bass ? sf->kick : sf->bass; + float mi = sf->gtr > sf->solo ? sf->gtr : sf->solo; + float hi = sf->hat > sf->snare ? sf->hat : sf->snare; + if (sf->org > mi) mi = sf->org; + g_bLow = follow(g_bLow, lo, dt, 0.008f, 0.420f); + g_bMid = follow(g_bMid, mi, dt, 0.020f, 0.180f); + g_bHigh = follow(g_bHigh, hi, dt, 0.002f, 0.070f); +} + +/* -------------------------------------------------------------------------- + the edit + -------------------------------------------------------------------------- */ +static int findShot(double row) +{ + int i, best = 0; + for (i = 0; i < NSHOTS; i++) { + if ((double)g_shots[i].start <= row) best = i; + else break; + } + return best; +} + +typedef struct { float eye[3], dir[3], roll, fov; } Cam; + +/* What the camera is doing, evaluated from the shot alone. Nothing here + accumulates across shots, and that is exactly why a cut is possible: the + next shot's camera owes the previous one nothing. */ +static void evalCam(const Shot *s, float lt, Cam *c) +{ + /* The shot's axis: from the camera towards what it is looking at. Every + behaviour is expressed against this, so aiming a shot is one idea + instead of two - an earlier version let `at` mean a direction for some + behaviours and a point for others, and two shots ended up staring at + the sky because of it. */ + float ax = s->at[0] - s->eye[0]; + float ay = s->at[1] - s->eye[1]; + float az = s->at[2] - s->eye[2]; + float n = fsqrt_(ax * ax + ay * ay + az * az); + float fx, fy, fz; + + if (n > 1e-5f) { ax /= n; ay /= n; az /= n; } + else { ax = 0.0f; ay = 0.0f; az = 1.0f; } + fx = ax; fy = ay; fz = az; + + c->eye[0] = s->eye[0]; c->eye[1] = s->eye[1]; c->eye[2] = s->eye[2]; + c->fov = s->fov; c->roll = s->roll; + + switch (s->cam) { + case CAM_HOLD: + break; /* genuinely still. no drift at all */ + + case CAM_DRIFT: + c->eye[0] += fsin_(lt * 0.31f) * 0.22f; + c->eye[1] += fsin_(lt * 0.24f) * 0.14f; + c->eye[2] += fsin_(lt * 0.19f) * 0.30f; + break; + + case CAM_PUSH: + c->eye[0] += ax * s->speed * lt; + c->eye[1] += ay * s->speed * lt; + c->eye[2] += az * s->speed * lt; + break; + + case CAM_PULL: + c->eye[0] -= ax * s->speed * lt; + c->eye[1] -= ay * s->speed * lt; + c->eye[2] -= az * s->speed * lt; + /* backing away keeps the subject framed, so re-aim at it */ + fx = s->at[0] - c->eye[0]; + fy = s->at[1] - c->eye[1]; + fz = s->at[2] - c->eye[2]; + break; + + case CAM_TRACK: { /* sideways, across the look axis */ + float rx = az, rz = -ax; + float m = fsqrt_(rx * rx + rz * rz); + if (m > 1e-5f) { rx /= m; rz /= m; } + c->eye[0] += rx * s->speed * lt; + c->eye[2] += rz * s->speed * lt; + fx = s->at[0] - c->eye[0]; /* the subject stays in frame */ + fy = s->at[1] - c->eye[1]; + fz = s->at[2] - c->eye[2]; + break; + } + + case CAM_CRANE: + c->eye[1] += s->speed * lt; + fx = s->at[0] - c->eye[0]; + fy = s->at[1] - c->eye[1]; + fz = s->at[2] - c->eye[2]; + break; + + case CAM_ORBIT: { + /* eye is the starting offset from the target: rotate it and keep + looking at the middle. No atan2 needed. */ + float ox = s->eye[0] - s->at[0], oz = s->eye[2] - s->at[2]; + float a = s->speed * lt, ca = fcos_(a), sa = fsin_(a); + c->eye[0] = s->at[0] + ox * ca - oz * sa; + c->eye[2] = s->at[2] + ox * sa + oz * ca; + fx = s->at[0] - c->eye[0]; + fy = s->at[1] - c->eye[1]; + fz = s->at[2] - c->eye[2]; + break; + } + + case CAM_WHIP: { + /* The look sweeps hard away from the shot axis and eases out, so it + can hand off into a cut instead of just stopping. The sweep is + relative to where the shot was aimed, never absolute. */ + /* CLAMPED, and it has to be. u is lt*speed with nothing bounding + it, and e = 1-(1-u)^2 peaks at 1 when u reaches 1 and then comes + back DOWN - at u=2 it is zero again and past that it goes negative. + So an unclamped whip sweeps out, swings back through where it + started, and then over-rotates the other way. An ease that is only + an ease over its own domain has to be told where its domain ends. */ + float u = lt * s->speed; + if (u > 1.0f) u = 1.0f; + float e = 1.0f - (1.0f - u) * (1.0f - u); + /* 0.35 radians, not 1.9. This is geometry, not taste: the only + scene the demo whips is the tunnel, whose bore radius is 1.65, and + a look angle of theta off the bore meets the wall at z = 1.65/tan + theta - 4.5 units at 20 degrees, 1.65 at 45, behind the camera by + 109. At 1.9 radians (108.9 degrees) there is a tunnel in frame for + the first 9% of the shot and a wall for the rest, at any speed, + clamped or not. Clamping u fixed a genuine bug - past u=1 the ease + came back down and reversed - but it did not fix this frame, and a + brighter picture is not the same thing as a picture with a tunnel + in it. 0.35 is about 20 degrees, which is the largest sweep that + keeps the bore in shot. The violence lives in the roll below, which + reads at any amplitude. */ + float a = e * 0.35f, ca = fcos_(a), sa = fsin_(a); + fx = ax * ca - az * sa; + fz = ax * sa + az * ca; + fy = ay; + c->roll += e * 0.22f; + break; + } + } + + c->dir[0] = fx; c->dir[1] = fy; c->dir[2] = fz; +} + +/* Mesh actors are drawn AFTER the distance field, into the same + depth buffer, so the two occlude each other properly. + + The animation frame is fractional and comes from the song row, not from + lt - that is what puts a footfall on a beat. At 3 frames per row a full + 24-frame cycle takes 8 rows, which is two beats, which is a lope; the + forward travel in from/to has to agree with it or the feet skate, and the + cat is 1.718 units long so a cycle should cover about 3.44 units of that. +*/ +static void drawActor(const Shot *s, double row, float lt) +{ + MeshCB mc; + ID3D11ShaderResourceView *srv[2]; + ID3D11ShaderResourceView *color = 0; + unsigned nv = RCAT_NV, nt = RCAT_NT, nf = RCAT_NF; + float qs = RCAT_QS; + int rigid; + float u, frame; + + if (s->actor == ACT_NONE) return; + + rigid = s->actor == ACT_TARDIS || s->actor == ACT_SPIN_CAT; + srv[0] = g_catPos; srv[1] = g_catIdx; + if (s->actor == ACT_TARDIS) { + nv = TAR_NV; nt = TAR_NT; nf = 1; qs = TAR_QS; + srv[0] = g_tarPos; srv[1] = g_tarIdx; color = g_tarColor; + } else if (s->actor == ACT_SPIN_CAT) { + nv = OCAT_NV; nt = OCAT_NT; nf = 1; qs = OCAT_QS; + srv[0] = g_spinPos; srv[1] = g_spinIdx; color = g_spinColor; + } + + u = (s->actRate > 0.0f || s->actor == ACT_SPIN_CAT) + ? (float)(row - (double)s->start) : 0.0f; + frame = (s->actor == ACT_CAT_STILL) ? 2.0f + : u * s->actRate; + /* the shot's own progress, for the from -> to travel */ + { + float span = (float)(SONG_ROWS - s->start); + float t01; + int i; + (void)lt; + /* End at this shot's next cut, including the one-bar run at 77. + Look up by start row so dissolve draws of the previous shot and + diagnostic copies of a Shot use the same duration. */ + for (i = 0; i < NSHOTS; i++) { + if (g_shots[i].start > s->start) { + span = (float)(g_shots[i].start - s->start); + break; + } + } + if (span < 1.0f) span = 1.0f; + t01 = (float)(row - (double)s->start) / span; + if (t01 > 1.0f) t01 = 1.0f; + if (t01 < 0.0f) t01 = 0.0f; + memset(&mc, 0, sizeof(mc)); + for (i = 0; i < 3; i++) + mc.model[i] = s->from[i] + (s->to[i] - s->from[i]) * t01; + mc.model[3] = s->actScale > 0.0f ? s->actScale : 1.0f; + } + mc.anim[0] = rigid ? 0.0f : frame; + mc.anim[1] = (float)nf; + mc.anim[2] = qs; + /* Spin rate is radians per song row; zero holds the original pose. */ + mc.anim[3] = (s->actor == ACT_SPIN_CAT) ? frame : 0.0f; + mc.misc[0] = (float)nv; + mc.misc[1] = 0.0f; /* wireframe */ + mc.misc[2] = 0.0f; /* shatter */ + /* The organ lifts it, but the BASE has to stand on its own: the rule in + this file is base + k*s and never base*s, so muting the synth must + leave every frame deliberate. At a base of 0.10 the cat was a black + shape on a dark floor and simply vanished. */ + mc.misc[3] = rigid + ? 0.0f + : (0.42f + g_syncNow.org * 0.30f); + upload(g_cbMesh, &mc, sizeof(mc)); + + ID3D11DeviceContext_VSSetShaderResources(g_ctx, 0, 2, srv); + ID3D11DeviceContext_PSSetShaderResources(g_ctx, 2, 1, &color); + if (rigid) ID3D11DeviceContext_RSSetState(g_ctx, g_rsRigid); + ID3D11DeviceContext_VSSetShader(g_ctx, g_vsMesh, 0, 0); + ID3D11DeviceContext_PSSetShader(g_ctx, g_psMesh, 0, 0); + ID3D11DeviceContext_VSSetConstantBuffers(g_ctx, 0, 1, &g_cbScene); + ID3D11DeviceContext_VSSetConstantBuffers(g_ctx, 1, 1, &g_cbMesh); + ID3D11DeviceContext_PSSetConstantBuffers(g_ctx, 0, 1, &g_cbScene); + ID3D11DeviceContext_PSSetConstantBuffers(g_ctx, 1, 1, &g_cbMesh); + ID3D11DeviceContext_OMSetDepthStencilState(g_ctx, g_dsMesh, 0); + ID3D11DeviceContext_Draw(g_ctx, nt * 3, 0); + + { /* leave nothing bound: the scene targets become inputs next */ + ID3D11ShaderResourceView *const nul[2] = { 0, 0 }; + ID3D11DeviceContext_VSSetShaderResources(g_ctx, 0, 2, nul); + ID3D11DeviceContext_PSSetShaderResources(g_ctx, 2, 1, nul); + if (rigid) ID3D11DeviceContext_RSSetState(g_ctx, 0); + ID3D11DeviceContext_VSSetShader(g_ctx, g_vsFull, 0, 0); + } +} + +/* `row` is the absolute song row being rendered, and it is a parameter + rather than the g_row global on purpose. The live and video paths both + call renderFrame(g_row, ...) so the two agree there - but the OFFLINE self + test renders arbitrary rows and never touches g_row, so anything reading + the global was drawing a different moment than the post pass was, and the + diagnostic was not testing what ships. The actor animation had that split + already; the matte would have inherited it. */ +static void drawScene(const Shot *s, double row, float lt, float matte, + ID3D11RenderTargetView *rtv) +{ + SceneCB sc; + Cam c; + + evalCam(s, lt, &c); + + memset(&sc, 0, sizeof(sc)); + sc.t[0] = lt; + sc.t[1] = 0.0f; + sc.t[2] = 1.0f; + sc.t[3] = (float)g_vw / (float)g_vh; + sc.cam[0] = c.eye[0]; sc.cam[1] = c.eye[1]; sc.cam[2] = c.eye[2]; + sc.cam[3] = c.fov; + sc.dir[0] = c.dir[0]; sc.dir[1] = c.dir[1]; sc.dir[2] = c.dir[2]; + sc.dir[3] = c.roll; + sc.tune[0] = s->tune[0]; sc.tune[1] = s->tune[1]; + sc.tune[2] = s->tune[2]; sc.tune[3] = s->tune[3]; + /* The sign-off is the one thing in the demo that has to be legible, and + it lives in the bottom-left corner - which is exactly where the matte + eats the picture. So it gets told how far up the visible bottom edge + currently is and places itself above that, rather than at a fixed + height that happens to work for one matte setting. Type inside a + title-safe area, which is the same thing every broadcast designer has + done since the fifties. + + Handed in rather than looked up here, and that is not tidiness: the + row this function receives is the STUTTERED one, and the post pass + draws the bar from the true row. Two matteAt() calls could disagree + the moment a sign-off shot ever got a stutter. One value, computed + once, cannot. */ + if (s->scene == SC_SIGNOFF) sc.tune[3] = matte; + sc.sync[0] = g_bLow; sc.sync[1] = g_bMid; sc.sync[2] = g_bHigh; + sc.sync[3] = g_syncNow.level; + sc.voice[0] = g_syncNow.gtr; sc.voice[1] = g_syncNow.solo; + sc.voice[2] = g_syncNow.org; sc.voice[3] = g_syncNow.hat; + // Each inscription gets one appearance, on a monitor inside the scene. + if (s->scene == SC_PLINTH) { + int label = s->start == BAR(14) ? 1 : + s->start == BAR(51) ? 2 : s->start == BAR(85) ? 3 : 0; + if (label) { + float v = lt < .35f ? lt/.35f : lt < 2.7f ? 1.0f : (3.15f-lt)/.45f; + if (v < 0) v = 0; if (v > 1) v = 1; + sc.caption[0] = (float)label; sc.caption[1] = v; + } + } + upload(g_cbScene, &sc, sizeof(sc)); + + ID3D11DeviceContext_PSSetShaderResources(g_ctx, 0, 2, g_nullSRV); + ID3D11DeviceContext_ClearDepthStencilView(g_ctx, g_depthDSV, + D3D11_CLEAR_DEPTH, 0.0f, 0); + ID3D11DeviceContext_OMSetRenderTargets(g_ctx, 1, &rtv, g_depthDSV); + ID3D11DeviceContext_OMSetDepthStencilState(g_ctx, g_dsSdf, 0); + setViewport(g_vw, g_vh); + ID3D11DeviceContext_IASetPrimitiveTopology(g_ctx, + D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ID3D11DeviceContext_IASetInputLayout(g_ctx, 0); + ID3D11DeviceContext_VSSetShader(g_ctx, g_vsFull, 0, 0); + ID3D11DeviceContext_PSSetShader(g_ctx, g_psScene[s->scene], 0, 0); + ID3D11DeviceContext_PSSetConstantBuffers(g_ctx, 0, 1, &g_cbScene); + if (s->scene == SC_C64) + ID3D11DeviceContext_PSSetConstantBuffers(g_ctx, 1, 1, &g_cbFont); + ID3D11DeviceContext_Draw(g_ctx, 3, 0); + + drawActor(s, row, lt); +} + +#ifdef OFFLINE +static const char *grName(int g) +{ + switch (g) { + case GR_COLD: return "cold "; + case GR_WARM: return "warm "; + case GR_NEON: return "neon "; + case GR_BLEACH: return "bleach "; + case GR_PHOSPHOR: return "phosphor"; + } + return "?"; +} +#endif + +/* Which palette a shot is cut in: its scene's own look if it has one, and + otherwise whatever the music is doing at the row the shot starts. Keyed on + the START row and not on the playhead, so a grade change never lands in the + middle of a held shot - the schedule and the edit are both written against + the same bar lines, so in practice they always agree anyway, and this + makes that guaranteed rather than lucky. */ +static const Grade *gradeFor(const Shot *s) +{ + unsigned char g = g_sceneGrade[s->scene]; + if (g == GR_FOLLOW) { + int i; + g = g_gradeKeys[0].grade; + for (i = 0; i < GRADE_KEYS; i++) + if (g_gradeKeys[i].row <= s->start) g = g_gradeKeys[i].grade; + } + return &g_grades[g < GR_COUNT ? g : 0]; +} + +static void renderFrame(double row, ID3D11RenderTargetView *dest, + int ow, int oh) +{ + const Shot *s; + PostCB pc; + ID3D11SamplerState *samps[2]; + int si = findShot(row); + double srow = row; + float matte = matteAt(row); + float lt, flash = 0.0f, fade = 1.0f, mix = 0.0f, whip = 0.0f; + + s = &g_shots[si]; + lt = (float)(row - (double)s->start) * SEC_PER_ROW; + + /* The stutter, if this shot has one. Everything downstream of here reads + the quantised row, so the scene, the camera and the actor judder as one + picture - while `row`, which the edit and the matte are addressed in, + is untouched and the cut still lands exactly where it was written. */ + if (s->stutter > 0.0f) { + double q = (double)s->stutter; + double dr = row - (double)s->start; + dr = q * (double)(__int64)(dr / q); /* dr >= 0, so this is floor */ + srow = (double)s->start + dr; + lt = (float)dr * SEC_PER_ROW; + } + + /* The transition INTO this shot. It is a property of the shot, so a hard + cut is simply a shot whose transRows is zero - which is most of them, + and should be. */ + if (s->transRows > 0) { + double p = (row - (double)s->start) / (double)s->transRows; + if (p >= 0.0 && p < 1.0) { + float u = (float)p; + switch (s->trans) { + /* LINEAR, not squared, and that is a photosensitivity decision + rather than a taste one. + + Measured on the rendered video: a squared decay from full white + over one row is about 16% of full scale per frame, and the + guideline counts every transition over 10% affecting more than + a quarter of the screen. So one flash was being counted as six + events, and the wind-up at 2:21 - three flash cuts inside seven + bars - came out at seven transitions in a second against a + limit of three. + + A linear ramp over the shot's transition window spreads the + same energy at a constant rate: at the two rows the flash cuts + now use, that is 6.6% per frame. One up-transition on the cut, + where it belongs, and nothing on the way down. */ + case TR_FLASH: flash = (1.0f - u) * 0.90f * g_flashScale; break; + case TR_BLACK: fade = u * u; break; + case TR_DISSOLVE: mix = 1.0f - u; break; + case TR_WHIPIN: whip = 1.0f - u; break; + default: break; + } + } + } + + drawScene(s, srow, lt, matte, g_sceneRTV[0]); + + if (mix > 0.002f && si > 0) { + const Shot *prev = &g_shots[si - 1]; + drawScene(prev, row, (float)(row - (double)prev->start) * SEC_PER_ROW, + matte, g_sceneRTV[1]); + } + + /* Unbind the render targets before the scene textures become inputs. */ + ID3D11DeviceContext_OMSetRenderTargets(g_ctx, 1, g_nullRTV, 0); + + /* ---- bloom: bright pass down, then blur across, then blur down ---- */ + { + BloomCB bc; + ID3D11ShaderResourceView *one[1]; + int pass; + + ID3D11DeviceContext_PSSetShader(g_ctx, g_psBloom, 0, 0); + ID3D11DeviceContext_PSSetConstantBuffers(g_ctx, 0, 1, &g_cbBloom); + ID3D11DeviceContext_PSSetSamplers(g_ctx, 1, 1, &g_sampLinear); + + /* Downsample first, then both blurs on their native half-res grid. + Targets 0 -> 1 -> 0; the composite must sample target 0. */ + for (pass = 0; pass < 3; pass++) { + int dst = (pass == 0) ? 0 : (pass & 1); + int srcW = (pass == 0) ? g_vw : g_bw; + int srcH = (pass == 0) ? g_vh : g_bh; + + memset(&bc, 0, sizeof(bc)); + bc.step[0] = 1.0f / (float)srcW; + bc.step[1] = 1.0f / (float)srcH; + bc.param[0] = 0.62f; /* threshold */ + bc.param[1] = 0.35f; /* knee */ + bc.param[3] = (float)pass; + upload(g_cbBloom, &bc, sizeof(bc)); + + one[0] = (pass == 0) ? g_sceneSRV[0] : g_bloomSRV[1 - dst]; + ID3D11DeviceContext_PSSetShaderResources(g_ctx, 0, 1, g_nullSRV); + ID3D11DeviceContext_OMSetRenderTargets(g_ctx, 1, &g_bloomRTV[dst], 0); + setViewport(g_bw, g_bh); + ID3D11DeviceContext_PSSetShaderResources(g_ctx, 0, 1, one); + ID3D11DeviceContext_Draw(g_ctx, 3, 0); + ID3D11DeviceContext_OMSetRenderTargets(g_ctx, 1, g_nullRTV, 0); + } + ID3D11DeviceContext_PSSetShaderResources(g_ctx, 0, 1, g_nullSRV); + } + + memset(&pc, 0, sizeof(pc)); + pc.res[0] = (float)g_vw; pc.res[1] = (float)g_vh; + pc.res[2] = (float)ow; pc.res[3] = (float)oh; + pc.crt[0] = 0.22f; /* scanline depth */ + pc.crt[1] = 0.30f; /* aperture grille depth */ + pc.crt[2] = 0.35f; /* keep edges and small inscriptions legible */ + pc.crt[3] = 0.12f; /* preserve foreground parallax at the edges */ + /* THE TEXTURAL LAYER. One to three percent, on the transient band only. + This is meant to be felt and never seen: if a viewer can point at the + screen and say "that was the snare", it is too much. Done here rather + than in eleven shaders so there is one place to turn it down. */ + pc.crt[2] += g_bHigh * 0.18f; /* aberration, 0.2 px */ + pc.crt[0] += g_bLow * 0.015f; /* scanline depth */ + /* The palette. Crossfaded through a dissolve so the two shots are graded + into each other as well as mixed - a hard palette switch underneath a + crossfade reads as a glitch rather than as a transition. The dither and + the exposure fold into the two knobs that already existed, so the music + still breathes on top of whatever the section's look is. */ + { + const Grade *ga = gradeFor(s), *gb = ga; + float k = 0.0f, dith, expo; + int r2, c2; + if (mix > 0.002f && si > 0) { gb = gradeFor(&g_shots[si - 1]); k = mix; } + for (r2 = 0; r2 < 4; r2++) + for (c2 = 0; c2 < 4; c2++) + pc.ramp[r2][c2] = ga->ramp[r2][c2] * (1.0f - k) + + gb->ramp[r2][c2] * k; + pc.tone[0] = ga->hueKeep * (1.0f - k) + gb->hueKeep * k; + pc.tone[1] = ga->steps * (1.0f - k) + gb->steps * k; + dith = ga->dither * (1.0f - k) + gb->dither * k; + expo = ga->exposure * (1.0f - k) + gb->exposure * k; + pc.grade[0] = dith + g_bHigh * 0.05f; /* dither amount */ + pc.grade[1] = expo + g_bLow * 0.030f; /* exposure breathes */ + } + pc.grade[2] = mix; + /* Leave through the 3D world, not a flat power-off card. */ + if (row > (double)SONG_ROWS - 16.0) { + float end = (float)(((double)SONG_ROWS - row) / 16.0); + if (end < 0.0f) end = 0.0f; + fade *= end * end * (3.0f - 2.0f * end); + } + pc.grade[3] = fade; + pc.fx[0] = flash; + pc.fx[1] = whip; + /* How much of the glow comes back. The organ and the solo push it a + little: the brightest passages of the music are the ones where the + picture should feel like it is running hot. */ + pc.fx[2] = 0.55f + g_bMid * 0.20f; + pc.fx[3] = matte; + upload(g_cbPost, &pc, sizeof(pc)); + + ID3D11DeviceContext_OMSetRenderTargets(g_ctx, 1, &dest, 0); + setViewport(ow, oh); + ID3D11DeviceContext_PSSetShader(g_ctx, g_psPost, 0, 0); + ID3D11DeviceContext_PSSetConstantBuffers(g_ctx, 0, 1, &g_cbPost); + { + ID3D11ShaderResourceView *three[3]; + three[0] = g_sceneSRV[0]; three[1] = g_sceneSRV[1]; + /* Last pass writes target 0: threshold -> V -> H. */ + three[2] = g_bloomSRV[0]; + ID3D11DeviceContext_PSSetShaderResources(g_ctx, 0, 3, three); + } + samps[0] = g_sampPoint; samps[1] = g_sampLinear; + ID3D11DeviceContext_PSSetSamplers(g_ctx, 0, 2, samps); + ID3D11DeviceContext_Draw(g_ctx, 3, 0); + + /* All THREE, not two. t2 held a view of a bloom target, and the next + frame's blur pass binds that same texture as a render target - which is + the exact SRV/RTV collision every other unbind in this file exists to + avoid. D3D11 resolves it by silently dropping the SRV, so it costs a + frame of bloom rather than crashing, which is why it survived. */ + ID3D11DeviceContext_PSSetShaderResources(g_ctx, 0, 3, g_nullSRV); +} + +/* -------------------------------------------------------------------------- + window + -------------------------------------------------------------------------- */ +static void resizeSwapChain(int w, int h) +{ + if (!g_sc || w < 1 || h < 1) return; + ID3D11DeviceContext_OMSetRenderTargets(g_ctx, 0, 0, 0); + releaseBackbuffer(); /* every reference, or this fails */ + if (IDXGISwapChain1_ResizeBuffers(g_sc, 0, (UINT)w, (UINT)h, + DXGI_FORMAT_UNKNOWN, 0) < 0) { + /* The old buffers survive a failed resize; restore their view. */ + if (createBackbuffer()) return; + audioCleanup(); + fatal(L"The back buffer could not be restored after resizing."); + } + g_ow = w; g_oh = h; + if (!createBackbuffer() || !createSceneTargets(g_ow, g_oh)) { + audioCleanup(); + fatal(L"The render targets could not be created after resizing."); + } +} + +static void updatePause(void) +{ + int paused = g_paused || g_sizing; + if (paused == g_wasPaused) return; + if (g_audioOk) { + MMRESULT result = paused ? waveOutPause(g_wo) : waveOutRestart(g_wo); + if (result != MMSYSERR_NOERROR) audioCleanup(); + } + g_wasPaused = paused; + g_audioStall = 0; + QueryPerformanceCounter(&g_qpcLast); +} + +static void setFullscreen(int on) +{ + MONITORINFO mi = { sizeof(MONITORINFO) }; + HMONITOR mon; + + if (on == g_full) return; + g_full = on; + + if (on) { + g_style = GetWindowLongPtrW(g_hwnd, GWL_STYLE); + GetWindowPlacement(g_hwnd, &g_place); + /* The monitor the window is ON, not the primary one. Getting this + wrong is why so many demos jump to the wrong screen. */ + mon = MonitorFromWindow(g_hwnd, MONITOR_DEFAULTTONEAREST); + GetMonitorInfoW(mon, &mi); + SetWindowLongPtrW(g_hwnd, GWL_STYLE, + g_style & ~(LONG_PTR)WS_OVERLAPPEDWINDOW); + SetWindowPos(g_hwnd, HWND_TOP, + mi.rcMonitor.left, mi.rcMonitor.top, + mi.rcMonitor.right - mi.rcMonitor.left, + mi.rcMonitor.bottom - mi.rcMonitor.top, + SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + ShowCursor(FALSE); + } else { + SetWindowLongPtrW(g_hwnd, GWL_STYLE, g_style); + SetWindowPlacement(g_hwnd, &g_place); + SetWindowPos(g_hwnd, 0, 0, 0, 0, 0, + SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | + SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + ShowCursor(TRUE); + } +} + +static LRESULT CALLBACK wndProc(HWND h, UINT m, WPARAM w, LPARAM l) +{ + switch (m) { + case WM_CLOSE: + case WM_DESTROY: + g_running = 0; + PostQuitMessage(0); + return 0; + + case WM_KEYDOWN: + if (w == VK_ESCAPE) { g_running = 0; PostQuitMessage(0); return 0; } + if ((w == 'F' || w == 'G') && (l & ((LPARAM)1 << 30))) return 0; + if (w == 'F') { setFullscreen(!g_full); return 0; } + /* Flash cuts off, half, full. The demo is legible without them - they + punctuate the edit rather than carrying it - so this costs a viewer + who needs it nothing but the punctuation. */ + if (w == 'G') { + g_flashScale = (g_flashScale > 0.75f) ? 0.35f + : (g_flashScale > 0.10f) ? 0.0f : 1.0f; + return 0; + } + return 0; + + case WM_SYSCOMMAND: + /* No screensaver and no display blanking in the middle of a demo. */ + if ((w & 0xFFF0) == SC_SCREENSAVE || (w & 0xFFF0) == SC_MONITORPOWER) + return 0; + break; + + case WM_ACTIVATE: + /* Focus loss stops the whole thing, picture and sound, and it + resumes on the frame it left. */ + g_paused = (LOWORD(w) == WA_INACTIVE); + updatePause(); + return 0; + + case WM_GETMINMAXINFO: + ((MINMAXINFO *)l)->ptMinTrackSize.x = 480; + ((MINMAXINFO *)l)->ptMinTrackSize.y = 320; + return 0; + + case WM_ENTERSIZEMOVE: + /* Dragging the border runs a modal loop that owns the message pump. + We stop resizing the chain per WM_SIZE while it is up and do it + once at the end. */ + g_sizing = 1; + updatePause(); + return 0; + + case WM_EXITSIZEMOVE: { + RECT rc; + GetClientRect(h, &rc); + resizeSwapChain(rc.right - rc.left, rc.bottom - rc.top); + g_sizing = 0; + updatePause(); + return 0; + } + + case WM_SIZE: + if (w == SIZE_MINIMIZED) return 0; /* 0x0, never resize to it */ + if (!g_sizing) resizeSwapChain(LOWORD(l), HIWORD(l)); + return 0; + + case WM_DPICHANGED: { + /* Use the rectangle Windows suggests, or the window is the wrong + size on the new monitor. */ + RECT *rc = (RECT *)l; + SetWindowPos(h, 0, rc->left, rc->top, + rc->right - rc->left, rc->bottom - rc->top, + SWP_NOZORDER | SWP_NOACTIVATE); + return 0; + } + } + return DefWindowProcW(h, m, w, l); +} + +/* -------------------------------------------------------------------------- + entry + -------------------------------------------------------------------------- */ +#ifdef OFFLINE +static void offlineMain(void); +#endif +#ifdef VIDEO +static void videoMain(void); +#endif + +void entry(void) +{ + WNDCLASSEXW wc; + MSG msg; + + /* Denormals off. The reverb tails in the synth run into them, and a + denormal stall in the audio callback is a click. */ + _mm_setcsr(_mm_getcsr() | 0x8040); + + /* DPI first, before any window exists. Without it DWM bitmap-stretches + the window on a scaled display and every pixel of the CRT pass is + blurred by something we do not control. */ + { + HMODULE u32 = GetModuleHandleA("user32.dll"); + typedef BOOL (WINAPI *PFN_SPDAC)(HANDLE); + PFN_SPDAC set = (PFN_SPDAC)GetProcAddress(u32, + "SetProcessDpiAwarenessContext"); + if (set) set((HANDLE)-4); /* PER_MONITOR_AWARE_V2 */ + else { + typedef BOOL (WINAPI *PFN_SPDA)(void); + PFN_SPDA old = (PFN_SPDA)GetProcAddress(u32, "SetProcessDPIAware"); + if (old) old(); + } + } + + if (!createDevice()) ExitProcess(1); + +#ifdef OFFLINE + offlineMain(); /* never returns: no window, no sound */ +#endif +#ifdef VIDEO + videoMain(); /* never returns: frames to stdout */ +#endif + + memset(&wc, 0, sizeof(wc)); + wc.cbSize = sizeof(wc); + wc.lpfnWndProc = wndProc; + wc.hInstance = GetModuleHandleW(0); + wc.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW); + wc.hbrBackground = 0; /* we paint every pixel every frame; a brush here + would only add an erase flicker, and it was the + last thing that needed gdi32 */ + wc.lpszClassName = L"CRTkafa"; + RegisterClassExW(&wc); + + /* Created hidden on purpose - see the pre-warm below. */ + g_hwnd = CreateWindowExW(0, L"CRTkafa", L"CRTkafa", + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, + 0, 0, wc.hInstance, 0); + if (!g_hwnd) fatal(L"The window could not be created."); + + if (!createSwapChain(g_hwnd)) fatal(L"The swap chain could not be created."); + if (!createBackbuffer()) fatal(L"The back buffer could not be created."); + if (!createPipeline()) fatal(L"The shaders could not be created."); + if (!createSceneTargets(g_ow, g_oh)) + fatal(L"The render targets could not be created."); + + /* Warm every shader before the audience sees anything. fxc precompiles + to DXBC but the driver still JITs to native ISA on the FIRST DRAW with + a given shader, not at CreatePixelShader - so without this the match + cut at bar 9 and the hard cut into white at bar 18, the two most + carefully placed moments in the demo, each eat a first-draw hitch. + One frame per scene, into the scene target, thrown away. */ + { + int sc; + Shot warm; + memset(&warm, 0, sizeof(warm)); + warm.at[2] = 1.0f; warm.fov = 0.62f; + for (sc = 0; sc < SC_COUNT; sc++) { + warm.scene = (unsigned char)sc; + drawScene(&warm, 0.0, 0.0f, 0.0f, g_sceneRTV[0]); + } + /* and once through the post pass with a dissolve live, which is the + only path that binds the second scene target */ + renderFrame(0.0, g_backRTV, g_ow, g_oh); + } + + setFullscreen(1); + /* Only now does the window appear. Creating it visible at 1280x720 and + resizing afterwards showed the audience a window full of stale desktop, + then a jump to fullscreen, then finally frame zero. */ + ShowWindow(g_hwnd, SW_SHOW); + SetForegroundWindow(g_hwnd); + SetFocus(g_hwnd); + + audioInit(); /* from here on the song, not the wall clock, is time */ + g_wasPaused = 0; + updatePause(); + + QueryPerformanceFrequency(&g_qpcFreq); + QueryPerformanceCounter(&g_qpcLast); + + while (g_running) { + LARGE_INTEGER now; + double dt; + + while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)) { + if (msg.message == WM_QUIT) { g_running = 0; break; } + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + if (!g_running) break; + + updatePause(); + if (g_paused || g_sizing) { + /* Stopped means stopped, picture and sound both, and it resumes + on the frame it left. */ + WaitMessage(); + QueryPerformanceCounter(&g_qpcLast); + continue; + } + + QueryPerformanceCounter(&now); + dt = (double)(now.QuadPart - g_qpcLast.QuadPart) / + (double)g_qpcFreq.QuadPart; + g_qpcLast = now; + if (dt < 0.0) dt = 0.0; + if (dt > 0.25) dt = 0.25; + + audioPump(); + + /* THE CLOCK. Every cut in the demo is placed on a row of the song, so + the row index has to come from the music itself - the number of + samples the card has actually played - and not from a wall clock + that would slowly slide out of time with it. The QPC path below is + only for a machine with no working sound device. */ + { + __int64 played = audioPlayedSamples(); + /* Losing the device mid-run - bluetooth dropping, an interface + unplugged, the audio re-routed - usually does NOT return an + error: waveOutGetPosition just keeps reporting the last + position. The clock then stops and the picture freezes while + the process is perfectly healthy. So watch for a position that + has stopped advancing and fall back to the wall clock, seeded + from the last good row. A demo that keeps playing silently is + recoverable; one that freezes on screen is not. */ + if (played >= 0 && played == g_lastPlayed) { + if (++g_audioStall > 30) played = -1; /* half a second */ + } else { + g_audioStall = 0; + g_lastPlayed = played; + } + if (played >= 0) { + g_row = (double)played / (double)SPR; + syncRead(played, &g_syncNow); + } else { + audioCleanup(); + memset(&g_syncNow, 0, sizeof(g_syncNow)); + g_row += dt / (double)SEC_PER_ROW; + } + } + /* Live playback ends once the card has played the complete song. + Offline timeline functions retain their explicit loop semantics. */ + if (g_row >= (double)SONG_ROWS) { + const float black[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; + ID3D11DeviceContext_ClearRenderTargetView(g_ctx, g_backRTV, black); + IDXGISwapChain1_Present(g_sc, 1, 0); + break; + } + syncFollow(&g_syncNow, (float)dt); + + renderFrame(g_row, g_backRTV, g_ow, g_oh); + IDXGISwapChain1_Present(g_sc, 1, 0); + } + + audioCleanup(); + ExitProcess(0); +} + +/* -------------------------------------------------------------------------- + offline verification + -------------------------------------------------------------------------- + No window, no swap chain, no sound: render straight into an offscreen + target, copy it back and write bitmaps. This is how the renderer is + checked without ever putting anything on screen. + -------------------------------------------------------------------------- */ +#ifdef OFFLINE +/* Sized for the OUTPUT width, not the virtual one. It held VW_MAX*3 and the + first bitmap written at 1920 wide ran past the end into the next object + in BSS. */ +#define BMP_MAXW 3840 +static unsigned char g_bmpRow[BMP_MAXW * 3]; + +static void dumpBmp(const char *name, const unsigned char *bgra, int w, int h, + int pitch) +{ + HANDLE f = CreateFileA(name, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); + unsigned char hdr[54]; + DWORD wr; + int x, y, pad = (4 - ((w * 3) & 3)) & 3; + unsigned int img = (unsigned int)(w * 3 + pad) * (unsigned int)h; + + if (f == INVALID_HANDLE_VALUE) return; + if (w > BMP_MAXW) { CloseHandle(f); return; } + + memset(hdr, 0, sizeof(hdr)); + hdr[0] = 'B'; hdr[1] = 'M'; + *(unsigned int *)(hdr + 2) = img + 54; + *(unsigned int *)(hdr + 10) = 54; + *(unsigned int *)(hdr + 14) = 40; + *(int *)(hdr + 18) = w; + *(int *)(hdr + 22) = h; + *(unsigned short *)(hdr + 26) = 1; + *(unsigned short *)(hdr + 28) = 24; + *(unsigned int *)(hdr + 34) = img; + WriteFile(f, hdr, 54, &wr, 0); + + for (y = h - 1; y >= 0; y--) { + const unsigned char *src = bgra + (size_t)y * (size_t)pitch; + for (x = 0; x < w; x++) { + g_bmpRow[x * 3 + 0] = src[x * 4 + 0]; + g_bmpRow[x * 3 + 1] = src[x * 4 + 1]; + g_bmpRow[x * 3 + 2] = src[x * 4 + 2]; + } + WriteFile(f, g_bmpRow, (DWORD)(w * 3), &wr, 0); + if (pad) WriteFile(f, g_bmpRow, (DWORD)pad, &wr, 0); + } + CloseHandle(f); +} + +static char g_log[16384]; +static int g_logN = 0; +static void logs(const char *s) { while (*s && g_logN < 16000) g_log[g_logN++] = *s++; } +static void logi(int v) +{ + char t[16]; int n = 0; + if (v < 0) { logs("-"); v = -v; } + if (!v) { logs("0"); return; } + while (v) { t[n++] = (char)('0' + v % 10); v /= 10; } + while (n && g_logN < 16000) g_log[g_logN++] = t[--n]; +} +static void logf2(float v) /* two decimals, no CRT */ +{ + int w = (int)v, f = (int)((v - (float)w) * 100.0f + 0.5f); + if (f < 0) f = -f; + logi(w); logs("."); if (f < 10) logs("0"); logi(f); +} + +static const char *camName(int k) +{ + switch (k) { + case CAM_HOLD: return "hold "; + case CAM_PUSH: return "push "; + case CAM_PULL: return "pull "; + case CAM_TRACK: return "track"; + case CAM_CRANE: return "crane"; + case CAM_ORBIT: return "orbit"; + case CAM_WHIP: return "whip "; + default: return "drift"; + } +} +static const char *trName(int k) +{ + switch (k) { + case TR_CUT: return "cut"; + case TR_FLASH: return "flash"; + case TR_BLACK: return "black"; + case TR_DISSOLVE: return "dissolve"; + case TR_MATCH: return "match"; + default: return "whip-in"; + } +} +static const char *scName(int k) +{ + switch (k) { + case SC_EYE: return "eye "; + case SC_CATHEDRAL: return "cathedral"; + case SC_LOGO: return "logo 3D "; + case SC_REACTOR: return "reactor "; + case SC_ORBITAL: return "orbital "; + case SC_VOID: return "void "; + case SC_HORIZON: return "horizon "; + case SC_KALEIDO: return "kaleido "; + case SC_FRAMES: return "frames "; + case SC_PLINTH: return "plinth "; + case SC_DRIVE: return "drive "; + case SC_CORRIDOR: return "corridor "; + case SC_POWEROFF: return "power-off"; + case SC_C64: return "c64 boot "; + case SC_SIGNOFF: return "sign-off "; + default: return "burst "; + } +} + +static void offlineMain(void) +{ + static const int RES[][2] = { + { 1920, 1080 }, { 2560, 1080 }, { 3440, 1440 }, { 1366, 768 }, + { 1280, 1024 }, { 3840, 2160 }, { 5120, 1440 }, { 1024, 768 }, + { 800, 600 }, { 640, 480 } + }; + ID3D11Texture2D *out = 0, *stage = 0; + ID3D11RenderTargetView *outRTV = 0; + D3D11_TEXTURE2D_DESC td; + D3D11_MAPPED_SUBRESOURCE m; + int i, fails = 0; + HANDLE f; + DWORD wr; + + if (!createPipeline()) { logs("pipeline FAILED\r\n"); fails++; } + + logs("CRTkafa D3D11 self test\r\n\r\n device: "); + logs(g_warp ? "WARP (software)" : "hardware"); + logs("\r\n\r\n"); + + /* ---- every resolution renders ------------------------------------ */ + for (i = 0; i < (int)(sizeof(RES) / sizeof(RES[0])); i++) { + int ow = RES[i][0], oh = RES[i][1], lit = 0, px; + + if (!createSceneTargets(ow, oh)) { fails++; continue; } + + memset(&td, 0, sizeof(td)); + td.Width = (UINT)ow; td.Height = (UINT)oh; + td.MipLevels = 1; td.ArraySize = 1; + td.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; + td.BindFlags = D3D11_BIND_RENDER_TARGET; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &out) < 0) { fails++; continue; } + if (ID3D11Device_CreateRenderTargetView(g_dev, (ID3D11Resource *)out, 0, + &outRTV) < 0) { fails++; continue; } + td.Usage = D3D11_USAGE_STAGING; + td.BindFlags = 0; + td.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &stage) < 0) { fails++; continue; } + + renderFrame((double)BAR(8), outRTV, ow, oh); + ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)stage, + (ID3D11Resource *)out); + + logs(" "); logi(ow); logs("x"); logi(oh); + logs(" -> scene "); logi(g_vw); logs("x"); logi(g_vh); logs(" "); + + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)stage, 0, + D3D11_MAP_READ, 0, &m) >= 0) { + const unsigned char *p = (const unsigned char *)m.pData; + int x, y; + for (y = 0; y < oh; y += 7) + for (x = 0; x < ow; x += 7) { + const unsigned char *q = p + (size_t)y * m.RowPitch + x * 4; + if (q[0] + q[1] + q[2] > 24) lit++; + } + px = (ow / 7) * (oh / 7); + if (lit * 100 < px * 3) { logs("DARK "); fails++; } + else logs("ok "); + logi(lit * 100 / (px ? px : 1)); logs("% lit"); + ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)stage, 0); + } else { logs("MAP FAILED"); fails++; } + logs("\r\n"); + + /* ---- one frame per shot, so the whole edit can be looked at --- */ + if (i == 0) { + int sh; + logs("\r\n the edit: "); logi(NSHOTS); logs(" shots\r\n"); + for (sh = 0; sh < NSHOTS; sh++) { + const Shot *s = &g_shots[sh]; + double end = (sh + 1 < NSHOTS) ? (double)g_shots[sh + 1].start + : (double)SONG_ROWS; + double mid = (double)s->start + (end - (double)s->start) * 0.55; + char nm[16]; + D3D11_MAPPED_SUBRESOURCE mm; + + if (sh == 0) { + static const float seconds[] = { + 1.0f, 2.4f, 4.8f, 6.5f, + BAR(14)*SEC_PER_ROW+1.5f, + BAR(51)*SEC_PER_ROW+1.5f, + BAR(85)*SEC_PER_ROW+1.5f + }; + static const char *names[] = { + "intro0.bmp", "intro1.bmp", "intro2.bmp", "intro3.bmp", + "caption0.bmp", "caption1.bmp", "caption2.bmp" + }; + int shotSample; + for (shotSample = 0; shotSample < (int)(sizeof(seconds)/sizeof(seconds[0])); shotSample++) { + renderFrame((double)seconds[shotSample] / SEC_PER_ROW, outRTV, ow, oh); + ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)stage, + (ID3D11Resource *)out); + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)stage, 0, + D3D11_MAP_READ, 0, &mm) >= 0) { + dumpBmp(names[shotSample], (const unsigned char *)mm.pData, + ow, oh, (int)mm.RowPitch); + ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)stage, 0); + } else fails++; + } + } + renderFrame(mid, outRTV, ow, oh); + ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)stage, + (ID3D11Resource *)out); + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)stage, 0, + D3D11_MAP_READ, 0, &mm) >= 0) { + nm[0]='s'; nm[1]='h'; nm[2]='t'; + nm[3]=(char)('0' + sh / 10); nm[4]=(char)('0' + sh % 10); + nm[5]='.'; nm[6]='b'; nm[7]='m'; nm[8]='p'; nm[9]=0; + dumpBmp(nm, (const unsigned char *)mm.pData, ow, oh, + (int)mm.RowPitch); + + /* TITLE SAFE. The sign-off is the author's domain and + email address - crt.fyi and hi@crt.fyi - + it sits in the bottom-left corner, and the outro closes + a letterbox over exactly that corner - so this is the + one piece of type in the demo that a change to the matte + schedule can silently cut in half, three seconds from + the end where nobody is watching during development. + Measured rather than asserted: find the bar, find the + lowest stroke, and require daylight between them. */ + if (s->scene == SC_SIGNOFF) { + const unsigned char *pp = + (const unsigned char *)mm.pData; + int y2, x2, bar = oh, lowest = -1, lim = ow / 3; + for (y2 = oh - 1; y2 >= oh / 2; y2--) { + int alive = 0; + for (x2 = 0; x2 < ow && !alive; x2 += 11) { + const unsigned char *q = pp + + (size_t)y2 * mm.RowPitch + x2 * 4; + if (q[0] + q[1] + q[2] > 6) alive = 1; + } + if (alive) { bar = y2 + 1; break; } + } + /* well above the dither floor, which peaks near 150 */ + for (y2 = 0; y2 < oh; y2++) { + int hits2 = 0; + for (x2 = 0; x2 < lim; x2++) { + const unsigned char *q = pp + + (size_t)y2 * mm.RowPitch + x2 * 4; + if (q[0] + q[1] + q[2] > 320) hits2++; + } + if (hits2 >= 2) lowest = y2; + } + logs("\r\n title safe:\r\n picture ends at line "); + logi(bar * g_vh / oh); + logs(", the sign-off ends at "); + logi(lowest < 0 ? -1 : lowest * g_vh / oh); + logs("\r\n"); + if (lowest < 0) { + logs(" the sign-off is not drawn at all\r\n"); + fails++; + } else if (bar - lowest < 4 * oh / g_vh) { + logs(" it runs into the matte\r\n"); fails++; + } else { + logs(" clears the matte by "); + logi((bar - lowest) * g_vh / oh); + logs(" lines, ok\r\n"); + } + } + ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)stage, 0); + } else fails++; + + logs(" "); if (sh < 10) logs(" "); logi(sh); + logs(" bar "); logi(s->start / ROWS_PER_BAR + 1); + logs("."); logi((s->start % ROWS_PER_BAR) / ROWS_PER_BEAT + 1); + logs(" "); logf2((float)(end - (double)s->start) * SEC_PER_ROW); + logs("s "); logs(camName(s->cam)); + logs(" "); logs(scName(s->scene)); + logs(" <- "); logs(trName(s->trans)); + + /* Does this cut land on anything the music is doing? The + edit and the song are two separate tables, and the only + thing that stops them drifting apart is checking. */ + { + int o = s->start / SNG_ROWS; + int r = s->start % SNG_ROWS; + int gt = 0, so = 0, dr = 0, k; + if (o >= 0 && o < SNG_ORDERS) { + for (k = 0; k < SNG_ROWS; k++) { + int q = o * SNG_ROWS + k; + if (g_sqGtr[q] >= 0) gt++; + if (g_sqSolo[q] >= 0) so++; + if (g_sqDrum[q] != 0) dr++; + } + logs(" [song ord "); logi(o); + logs(" row "); logi(r); + logs(": gtr "); logi(gt); + logs(" solo "); logi(so); + logs(" drum "); logi(dr); + /* a cut on a bar line is worth flagging - it is the + only place a hard cut reads as intentional */ + logs(r % ROWS_PER_BAR == 0 ? " ON THE BAR]" : " offbeat]"); + } + } + logs("\r\n"); + } + logs("\r\n"); + } + + + /* THE STUTTER, proved rather than assumed. A quantised clock is exactly + the kind of feature that can be wired up wrong and still look plausible + in motion, so this pins it down: two different moments inside the same + quantisation window must render the SAME picture, and a moment in the + next window must render a different one. Both halves matter - without + the second, a stutter that froze the shot completely would pass. + + Sampled clear of the shot's own transition, because a dissolve or a + whip is driven by the true row and would differ between the two frames + no matter how well the stutter works. */ + if (i == 0) { + int sh5, bad = 0, tested = 0; + logs("\r\n stutter:\r\n"); + for (sh5 = 0; sh5 < NSHOTS; sh5++) { + const Shot *st = &g_shots[sh5]; + double base, q; + unsigned a1 = 0, a2 = 0, a3 = 0, a4 = 0; + int k; + if (st->stutter <= 0.0f) continue; + tested++; + q = (double)st->stutter; + base = (double)st->start + (double)st->transRows + 0.001; + /* onto the start of a whole window, so the three samples below + land where they are meant to */ + base = (double)st->start + + q * (double)(__int64)((base - (double)st->start) / q + 1.0); + + /* 0.2 and 0.8 of a window are the same held frame; 1.2 is the + next one. 4.2 is far enough away that ANY moving scene must + have changed by then - it is what separates "the stutter is + broken" from "this shot was not moving in the first place", + which need opposite fixes. */ + for (k = 0; k < 4; k++) { + double at = base + q * (k == 0 ? 0.2 : (k == 1 ? 0.8 : + (k == 2 ? 1.2 : 4.2))); + D3D11_MAPPED_SUBRESOURCE ms; + unsigned h = 2166136261u; + renderFrame(at, outRTV, ow, oh); + ID3D11DeviceContext_CopyResource(g_ctx, + (ID3D11Resource *)stage, (ID3D11Resource *)out); + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)stage, 0, + D3D11_MAP_READ, 0, &ms) >= 0) { + int x3, y3; + for (y3 = 0; y3 < oh; y3 += 3) + for (x3 = 0; x3 < ow; x3 += 3) { + const unsigned char *qq = + (const unsigned char *)ms.pData + + (size_t)y3 * ms.RowPitch + x3 * 4; + h = (h ^ qq[0]) * 16777619u; + h = (h ^ qq[1]) * 16777619u; + h = (h ^ qq[2]) * 16777619u; + } + ID3D11DeviceContext_Unmap(g_ctx, + (ID3D11Resource *)stage, 0); + } else bad++; + if (k == 0) a1 = h; else if (k == 1) a2 = h; + else if (k == 2) a3 = h; else a4 = h; + } + + logs(" shot "); logi(sh5); + logs(" bar "); logi(st->start / ROWS_PER_BAR + 1); + logs(", every "); logi((int)(st->stutter * 1000.0f + 0.5f)); + logs(" thousandths of a row: "); + if (a1 != a2) { logs("HELD FRAMES DIFFER"); bad++; } + else if (a2 == a3 && a3 == a4) { + logs("the shot is static - the stutter is wasted here"); bad++; + } else if (a2 == a3) { + logs("the scene moves slower than the stutter"); bad++; + } else logs("holds, then advances"); + logs("\r\n"); + } + if (!tested) { logs(" no shot in the edit stutters\r\n"); } + fails += bad; + } + + ID3D11Texture2D_Release(stage); stage = 0; + ID3D11RenderTargetView_Release(outRTV); outRTV = 0; + ID3D11Texture2D_Release(out); out = 0; + } + + /* ---- what a frame costs ------------------------------------------ */ + { + LARGE_INTEGER f0, t0, t1; + D3D11_MAPPED_SUBRESOURCE mb; + const int N = 120; + int b, ow = 1920, oh = 1080; + + createSceneTargets(ow, oh); + memset(&td, 0, sizeof(td)); + td.Width = (UINT)ow; td.Height = (UINT)oh; + td.MipLevels = 1; td.ArraySize = 1; + td.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; + td.BindFlags = D3D11_BIND_RENDER_TARGET; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &out) >= 0 && + ID3D11Device_CreateRenderTargetView(g_dev, (ID3D11Resource *)out, + 0, &outRTV) >= 0) { + td.Usage = D3D11_USAGE_STAGING; + td.BindFlags = 0; + td.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &stage) >= 0) { + renderFrame((double)BAR(8), outRTV, ow, oh); /* warm up */ + ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)stage, + (ID3D11Resource *)out); + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)stage, 0, + D3D11_MAP_READ, 0, &mb) >= 0) + ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)stage, 0); + + /* PER SHOT, not averaged. A mean over the whole edit hides + the frame that actually decides whether this drops on + someone else's machine - and the expensive frames are + exactly the ones that matter, because a dissolve renders + TWO scenes and the demo has dissolves between heavy ones. + Each shot is rendered in a batch so one readback amortises + over the batch instead of the sync dominating it. */ + { + const int BATCH = 24; + double worstUs = 0.0, sumUs = 0.0; + int worst = 0, sh2; + QueryPerformanceFrequency(&f0); + for (sh2 = 0; sh2 < NSHOTS; sh2++) { + double mid = (double)g_shots[sh2].start + 4.0; + double us; + QueryPerformanceCounter(&t0); + for (b = 0; b < BATCH; b++) + renderFrame(mid, outRTV, ow, oh); + ID3D11DeviceContext_CopyResource(g_ctx, + (ID3D11Resource *)stage, (ID3D11Resource *)out); + if (ID3D11DeviceContext_Map(g_ctx, + (ID3D11Resource *)stage, 0, + D3D11_MAP_READ, 0, &mb) >= 0) + ID3D11DeviceContext_Unmap(g_ctx, + (ID3D11Resource *)stage, 0); + QueryPerformanceCounter(&t1); + us = (double)(t1.QuadPart - t0.QuadPart) * 1000000.0 / + (double)f0.QuadPart / (double)BATCH; + sumUs += us; + if (us > worstUs) { worstUs = us; worst = sh2; } + } + logs(" 1920x1080 frame cost: mean "); + logi((int)(sumUs / (double)NSHOTS + 0.5)); + logs(" us WORST "); + logi((int)(worstUs + 0.5)); + logs(" us (shot "); logi(worst); + logs(", "); logs(scName(g_shots[worst].scene)); + logs(", "); logs(trName(g_shots[worst].trans)); + logs(")\r\n the worst frame is "); + logi((int)(worstUs * 100.0 / 16667.0 + 0.5)); + logs("% of a 60 fps budget\r\n"); + if (worstUs > 16667.0 * 0.5) { + logs(" OVER HALF THE BUDGET on one frame\r\n"); + fails++; + } + } + ID3D11Texture2D_Release(stage); stage = 0; + } + ID3D11RenderTargetView_Release(outRTV); outRTV = 0; + ID3D11Texture2D_Release(out); out = 0; + } + } + + /* The clock. Everything on screen is placed by row, and the row index in + the shipping build comes from waveOutGetPosition, so confirm the two + ends of that conversion agree. */ + { + static const int PROBE[] = { 0, 5000, 80000, 960000, 7679999 }; + int q; + logs("\r\n clock: "); + logi(SPR); logs(" samples per row at "); logi(SRATE); + logs(" Hz, song is "); logi(SONG_ROWS); logs(" rows\r\n"); + for (q = 0; q < (int)(sizeof(PROBE) / sizeof(PROBE[0])); q++) { + int smp = PROBE[q]; + int row = smp / SPR; + int sht = findShot((double)row); + logs(" sample "); logi(smp); + logs(" -> row "); logi(row); + logs(" (bar "); logi(row / ROWS_PER_BAR + 1); + logs(") -> shot "); logi(sht); + logs(" "); logs(scName(g_shots[sht].scene)); + logs("\r\n"); + } + if (SONG_ROWS * SPR != SNG_ORDERS * SNG_ROWS * SPR) { + logs(" MISMATCH: the edit and the song disagree on length\r\n"); + fails++; + } + } + + /* ---- prove the sync ring lines up --------------------------------- + This is the one thing in the demo that can be silently wrong. If the + ring is read at the wrong sample index the picture answers the music + early or late by a tenth of a second, which looks like nothing in a + still and like a badly dubbed film in motion - and no test that only + asks "did a value arrive" would catch it. + + So: walk forward through the song, and at rows where the arrangement + SAYS a kick is playing, read the ring back at that row's sample index + and check the channel is actually lit. The two sides come from + different places - one from the baked note table, one from the synth's + own envelopes - so agreement means the indexing is right. + + The walk matters. The ring holds 743 ms on purpose: it is a window, + not a recording. Rendering the whole song first and then probing the + start reads a ring that has wrapped four hundred times, which is what + the first version of this test did and why it reported everything + misaligned. */ + { + static short scratch[BUFFRAMES * 2]; + SyncFrame sf; + __int64 gen = 0; + int probe, lit = 0, tested = 0, mism = 0; + + logs("\r\n sync ring:\r\n"); + for (probe = 0; probe < SNG_ORDERS; probe += 6) { + int k, kicks = 0, solos = 0, firstKick = -1; + __int64 smp, want; + + for (k = 0; k < SNG_ROWS; k++) { + int q = probe * SNG_ROWS + k; + if (g_sqDrum[q] == 1 || g_sqDrum[q] == 6 || g_sqDrum[q] == 7) { + kicks++; + if (firstKick < 0) firstKick = k; + } + if (g_sqSolo[q] >= 0) solos++; + } + smp = ((__int64)probe * SNG_ROWS + (firstKick < 0 ? 0 : firstKick)) * SPR; + want = smp + SPR * 2; + while (gen < want) { + renderAudio(scratch, BUFFRAMES); + gen += BUFFRAMES; + } + g_syncR = smp >> 8; + syncRead(smp + 512, &sf); + tested++; + + logs(" order "); if (probe < 10) logs(" "); logi(probe); + logs(": table says kick x"); logi(kicks); + logs(" solo x"); logi(solos); + logs(" ring reads kick "); logi((int)(sf.kick * 100.0f)); + logs(" solo "); logi((int)(sf.solo * 100.0f)); + if (kicks > 0 && sf.kick < 0.02f) { logs(" MISSED"); mism++; } + else if (kicks > 0) { lit++; } + logs("\r\n"); + } + logs(" "); logi(lit); logs(" of "); logi(tested - 1); + logs(" patterns with a kick answered on the right sample"); + if (mism) { logs(", "); logi(mism); logs(" MISALIGNED"); fails += mism; } + logs("\r\n"); + } + + /* ---- does the mesh path draw anything at all ----------------------- + The cat is a black silhouette in a dark corridor, so "I cannot see it" + and "it is not being drawn" look identical in a still. This asks the + narrow question instead: bind the mesh, draw it alone into a cleared + target with no scene behind it, and count the pixels that changed. */ + { + MeshCB mc; + ID3D11ShaderResourceView *srv[2]; + D3D11_MAPPED_SUBRESOURCE mm; + SceneCB sc; + int ow = 640, oh = 360, hits = 0; + + logs("\r\n mesh path:\r\n objects: vs "); + logs(g_vsMesh ? "ok" : "NULL"); + logs(" ps "); logs(g_psMesh ? "ok" : "NULL"); + logs(" pos "); logs(g_catPos ? "ok" : "NULL"); + logs(" idx "); logs(g_catIdx ? "ok" : "NULL"); + logs(" cb "); logs(g_cbMesh ? "ok" : "NULL"); + logs("\r\n"); + + createSceneTargets(ow, oh); + memset(&td, 0, sizeof(td)); + td.Width = (UINT)ow; td.Height = (UINT)oh; + td.MipLevels = 1; td.ArraySize = 1; + td.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; + td.BindFlags = D3D11_BIND_RENDER_TARGET; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &out) >= 0 && + ID3D11Device_CreateRenderTargetView(g_dev, (ID3D11Resource *)out, + 0, &outRTV) >= 0) { + td.Usage = D3D11_USAGE_STAGING; + td.BindFlags = 0; + td.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &stage) >= 0) { + float clr[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; + + /* a camera four units back, looking at the origin */ + memset(&sc, 0, sizeof(sc)); + sc.t[2] = 1.0f; + sc.t[3] = (float)ow / (float)oh; + sc.cam[0] = 0.0f; sc.cam[1] = 0.0f; sc.cam[2] = -4.0f; + sc.cam[3] = 0.62f; + sc.dir[2] = 1.0f; + upload(g_cbScene, &sc, sizeof(sc)); + + memset(&mc, 0, sizeof(mc)); + mc.model[3] = 1.0f; /* at the origin, scale 1 */ + mc.anim[1] = (float)RCAT_NF; + mc.anim[2] = RCAT_QS; + mc.misc[0] = (float)RCAT_NV; + mc.misc[3] = 1.0f; /* fully lit, not a silhouette */ + upload(g_cbMesh, &mc, sizeof(mc)); + + ID3D11DeviceContext_ClearRenderTargetView(g_ctx, outRTV, clr); + ID3D11DeviceContext_ClearDepthStencilView(g_ctx, g_depthDSV, + D3D11_CLEAR_DEPTH, 0.0f, 0); + ID3D11DeviceContext_OMSetRenderTargets(g_ctx, 1, &outRTV, g_depthDSV); + ID3D11DeviceContext_OMSetDepthStencilState(g_ctx, g_dsMesh, 0); + setViewport(ow, oh); + ID3D11DeviceContext_IASetPrimitiveTopology(g_ctx, + D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ID3D11DeviceContext_IASetInputLayout(g_ctx, 0); + srv[0] = g_catPos; srv[1] = g_catIdx; + ID3D11DeviceContext_VSSetShaderResources(g_ctx, 0, 2, srv); + ID3D11DeviceContext_VSSetShader(g_ctx, g_vsMesh, 0, 0); + ID3D11DeviceContext_PSSetShader(g_ctx, g_psMesh, 0, 0); + ID3D11DeviceContext_VSSetConstantBuffers(g_ctx, 0, 1, &g_cbScene); + ID3D11DeviceContext_VSSetConstantBuffers(g_ctx, 1, 1, &g_cbMesh); + ID3D11DeviceContext_PSSetConstantBuffers(g_ctx, 0, 1, &g_cbScene); + ID3D11DeviceContext_PSSetConstantBuffers(g_ctx, 1, 1, &g_cbMesh); + ID3D11DeviceContext_Draw(g_ctx, RCAT_NT * 3, 0); + + ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)stage, + (ID3D11Resource *)out); + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)stage, 0, + D3D11_MAP_READ, 0, &mm) >= 0) { + int x, y; + for (y = 0; y < oh; y++) + for (x = 0; x < ow; x++) { + const unsigned char *q = + (const unsigned char *)mm.pData + + (size_t)y * mm.RowPitch + x * 4; + if (q[0] + q[1] + q[2] > 8) hits++; + } + dumpBmp("catonly.bmp", (const unsigned char *)mm.pData, + ow, oh, (int)mm.RowPitch); + ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)stage, 0); + } + ID3D11Texture2D_Release(stage); stage = 0; + } + ID3D11RenderTargetView_Release(outRTV); outRTV = 0; + ID3D11Texture2D_Release(out); out = 0; + } + logs(" the cat alone on black: "); logi(hits); + logs(" lit pixels of "); logi(ow * oh); + if (!hits) { logs(" NOTHING DREW"); fails++; } + logs("\r\n"); + + /* the vertex data itself, so a bad SRV can be told from a bad shader */ + { + int lo = 32767, hi = -32768, k; + for (k = 0; k < RCAT_NV * 3; k++) { + if (g_rcatV[k] < lo) lo = g_rcatV[k]; + if (g_rcatV[k] > hi) hi = g_rcatV[k]; + } + logs(" frame 0 vertex range "); logi(lo); logs(" .. "); logi(hi); + logs(" x quant = "); logi((int)(hi * RCAT_QS * 1000.0f)); + logs(" thousandths of a unit\r\n"); + logs(" first index "); logi((int)g_rcatI[0]); + logs(" last index "); logi((int)g_rcatI[RCAT_NT * 3 - 1]); + logs(" of "); logi(RCAT_NV); logs(" vertices\r\n"); + } + } + + /* ---- photosensitivity -------------------------------------------- + A flash is a luminance transition over about a tenth of full scale + affecting more than a quarter of the screen; more than three of them + in any one second is the guideline threshold. This demo has flash cuts + and a strobing wind-up, so this is not a hypothetical. + + It has already caught one real problem. The flash used to decay as + (1-u) squared over a single row, which is 16% of scale per frame - so + ONE flash registered as six transitions, and the three flash cuts in + the wind-up came out at seven in a second. The decay is linear over + two rows now, at 6.6% per frame: one transition on the cut, where it + belongs, and nothing on the way down. + + Measured on a 64x36 reduction at 60 fps, which is what the eye + integrates rather than what any single pixel does. */ + { + static unsigned char lumA[64 * 36], lumB[64 * 36]; + unsigned char *cur = lumA, *prv = lumB; + int f, worst = 0, worstAt = 0, total = 0; + int fps = 60, nfr = (int)((__int64)SONG_ROWS * SPR * 60 / SRATE); + int ow = 640, oh = 360; + + createSceneTargets(ow, oh); + memset(&td, 0, sizeof(td)); + td.Width = (UINT)ow; td.Height = (UINT)oh; + td.MipLevels = 1; td.ArraySize = 1; + td.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; + td.BindFlags = D3D11_BIND_RENDER_TARGET; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &out) >= 0 && + ID3D11Device_CreateRenderTargetView(g_dev, (ID3D11Resource *)out, + 0, &outRTV) >= 0) { + td.Usage = D3D11_USAGE_STAGING; + td.BindFlags = 0; + td.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &stage) >= 0) { + int lastSec = -1, inSec = 0; + for (f = 0; f < nfr; f++) { + D3D11_MAPPED_SUBRESOURCE mm; + double row = (double)f * (double)SRATE / + ((double)fps * (double)SPR); + int x, y, up = 0, dn = 0, sec2; + + renderFrame(row, outRTV, ow, oh); + ID3D11DeviceContext_CopyResource(g_ctx, + (ID3D11Resource *)stage, (ID3D11Resource *)out); + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)stage, + 0, D3D11_MAP_READ, 0, &mm) < 0) continue; + for (y = 0; y < 36; y++) + for (x = 0; x < 64; x++) { + const unsigned char *q = + (const unsigned char *)mm.pData + + (size_t)(y * oh / 36) * mm.RowPitch + + (size_t)(x * ow / 64) * 4; + cur[y * 64 + x] = (unsigned char) + ((q[2] * 77 + q[1] * 150 + q[0] * 29) >> 8); + } + ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)stage, 0); + + if (f > 0) { + for (x = 0; x < 64 * 36; x++) { + int dlt = (int)cur[x] - (int)prv[x]; + if (dlt > 26) up++; + else if (dlt < -26) dn++; + } + if ((up > dn ? up : dn) * 4 > 64 * 36) { + total++; + inSec++; + } + } + sec2 = f / fps; + if (sec2 != lastSec) { + if (inSec > worst) { worst = inSec; worstAt = lastSec; } + inSec = 0; lastSec = sec2; + } + { unsigned char *t2 = prv; prv = cur; cur = t2; } + } + ID3D11Texture2D_Release(stage); stage = 0; + } + ID3D11RenderTargetView_Release(outRTV); outRTV = 0; + ID3D11Texture2D_Release(out); out = 0; + } + + logs("\r\n photosensitivity:\r\n "); + logi(total); logs(" qualifying transitions in the whole demo, worst "); + logi(worst); logs(" in one second (at "); logi(worstAt); + logs(" s)\r\n "); + if (worst > 3) { + logs("OVER the guideline of 3 per second"); fails++; + } else { + logs("within the guideline of 3 per second"); + } + logs("\r\n"); + } + + /* The shot table is hand written, and a table nobody checks is a table + with a typo in it. These are the mistakes that produce a black frame + or a camera at infinity rather than a compile error. */ + { + int sh3, bad = 0, logos = 0; + logs("\r\n shot table:\r\n"); + for (sh3 = 0; sh3 < NSHOTS; sh3++) { + const Shot *a = &g_shots[sh3]; + const Shot *b = (sh3 + 1 < NSHOTS) ? &g_shots[sh3 + 1] : 0; + if (a->scene == SC_LOGO) logos++; + if (a->scene == SC_EYE || a->scene == SC_FRAMES || + a->scene == SC_SIGNOFF || + a->scene == SC_POWEROFF) { + logs(" flat scene in 3D-only edit\r\n"); bad++; + } + if (a->scene >= SC_COUNT) { + logs(" shot "); logi(sh3); logs(": scene out of range\r\n"); + bad++; + } + if (b && b->start <= a->start) { + logs(" shot "); logi(sh3); + logs(": starts at or after the next one\r\n"); bad++; + } + if (b && a->transRows > (b->start - a->start)) { + logs(" shot "); logi(sh3); + logs(": its transition outlasts the shot\r\n"); bad++; + } + if (a->stutter < 0.0f || a->stutter > 4.0f) { + logs(" shot "); logi(sh3); + logs(": stutter out of range\r\n"); bad++; + } + if (a->fov <= 0.0f || a->fov > 2.0f) { + logs(" shot "); logi(sh3); logs(": fov out of range\r\n"); + bad++; + } + { /* a look-at equal to the eye leaves normalize() with a zero + vector, and every pixel of that shot becomes NaN */ + float dx = a->at[0] - a->eye[0]; + float dy = a->at[1] - a->eye[1]; + float dz = a->at[2] - a->eye[2]; + if (dx * dx + dy * dy + dz * dz < 1e-6f) { + logs(" shot "); logi(sh3); + logs(": looks at its own eye position\r\n"); bad++; + } + } + } + if (!bad) logs(" ok, "); else logs(" "); + if (logos != 1 || g_shots[1].scene != SC_LOGO || + g_shots[1].start != BAR(3)) { + logs(" 3D logo must occur exactly once, immediately after the opening tube\r\n"); bad++; + } + logi(NSHOTS); logs(" shots, "); logi(bad); logs(" problems\r\n"); + fails += bad; + } + + /* The grade and the matte are schedules, and a schedule nobody checks + drifts away from the arrangement it was written against. These are the + mistakes that do not produce a compile error: a palette authored and + never reached, a key out of order so a whole section takes the wrong + look, or a matte that does not land back where it started and so puts + a jump cut nobody wrote at the loop point. */ + { + int i2, bad = 0, used = 0; + logs("\r\n grade schedule:\r\n"); + + for (i2 = 1; i2 < GRADE_KEYS; i2++) + if (g_gradeKeys[i2].row <= g_gradeKeys[i2 - 1].row) { + logs(" key "); logi(i2); logs(": out of order\r\n"); bad++; + } + for (i2 = 0; i2 < GRADE_KEYS; i2++) + if (g_gradeKeys[i2].grade >= GR_COUNT || + g_gradeKeys[i2].row < 0 || g_gradeKeys[i2].row >= SONG_ROWS) { + logs(" key "); logi(i2); logs(": out of range\r\n"); bad++; + } + + /* Which palettes the finished edit actually reaches, and for how + long. A grade that no shot resolves to is dead weight in a demo + that counts its bytes. */ + for (i2 = 0; i2 < GR_COUNT; i2++) { + int sh4, shots = 0, rows = 0; + for (sh4 = 0; sh4 < NSHOTS; sh4++) { + if (gradeFor(&g_shots[sh4]) != &g_grades[i2]) continue; + shots++; + rows += ((sh4 + 1 < NSHOTS) ? g_shots[sh4 + 1].start + : SONG_ROWS) - g_shots[sh4].start; + } + logs(" "); logs(grName(i2)); + logs(": "); logi(shots); logs(" shots, "); + logi(rows * SPR / SRATE); logs(" s\r\n"); + if (shots) used++; + else { logs(" never reached\r\n"); bad++; } + } + logs(" "); logi(used); logs(" of "); logi(GR_COUNT); + logs(" palettes reached, "); logi(bad); logs(" problems\r\n"); + fails += bad; + } + + { + int i2, bad = 0; + float d = matteAt((double)(SONG_ROWS - 1) + 0.999) - matteAt(0.0); + if (d < 0.0f) d = -d; + + logs("\r\n matte:\r\n"); + for (i2 = 0; i2 < MATTE_KEYS; i2++) { + logs(" bar "); logi(g_mattes[i2].row / ROWS_PER_BAR + 1); + logs(": "); logi((int)(g_mattes[i2].bars * 1000.0f + 0.5f)); + logs(" thousandths over "); logi(g_mattes[i2].rows); + logs(" rows\r\n"); + if (g_mattes[i2].bars < 0.0f || g_mattes[i2].bars > 0.25f) { + logs(" out of range\r\n"); bad++; + } + if (i2 && g_mattes[i2].row <= g_mattes[i2 - 1].row) { + logs(" out of order\r\n"); bad++; + } + if (i2 && g_mattes[i2 - 1].row + g_mattes[i2 - 1].rows + > g_mattes[i2].row) { + logs(" its move outlasts the gap to the next key\r\n"); + bad++; + } + } + /* The demo loops. If the outro does not close the bars back to + exactly where the cold open opens them, the wrap is a jump nobody + wrote - and it lands on the first frame, where it is most visible. */ + logs(" loop wrap differs by "); + logi((int)(d * 1000.0f + 0.5f)); logs(" thousandths\r\n"); + if (d > 0.002f) { logs(" the matte jumps at the loop\r\n"); bad++; } + if (!bad) logs(" ok\r\n"); + fails += bad; + } + + logs("\r\nfailures: "); logi(fails); logs("\r\n"); + f = CreateFileA("gputest.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); + if (f != INVALID_HANDLE_VALUE) { + WriteFile(f, g_log, (DWORD)g_logN, &wr, 0); + CloseHandle(f); + } + ExitProcess(fails ? 1 : 0); +} +#endif /* OFFLINE */ + +/* -------------------------------------------------------------------------- + video export + -------------------------------------------------------------------------- + The whole demo to stdout as raw BGRA, faster than real time, so a two and + a half minute capture never has to be watched to be made. Piped into + ffmpeg by makevideo.bat. + + The sync layer is what makes this exact rather than approximate. There is + no sound card here, so instead of asking the driver what has played, the + renderer GENERATES audio up to each frame's sample position and reads the + ring at that same index - which is the identical arithmetic the shipping + build does, with waveOutGetPosition swapped for a counter. The capture is + therefore frame-for-frame what the demo looks like, not a re-timing of it. + + Set CRTK_WAVONLY to emit only the soundtrack and exit. + -------------------------------------------------------------------------- */ +#ifdef VIDEO +#define VIDW 1280 +#define VIDH 720 +#define VIDFPS 60 + +static short g_vidPcm[BUFFRAMES * 2]; + +static void writeAll(HANDLE file, const void *data, DWORD bytes) +{ + const unsigned char *p = (const unsigned char *)data; + while (bytes) { + DWORD written = 0; + if (!WriteFile(file, p, bytes, &written, 0) || !written) ExitProcess(2); + p += written; + bytes -= written; + } +} + +static void wavOut(void) +{ + HANDLE f = CreateFileA("dump.wav", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); + __int64 total = (__int64)SONG_ROWS * SPR; + __int64 done = 0; + unsigned char h[44]; + unsigned int bytes = (unsigned int)(total * 4); + if (f == INVALID_HANDLE_VALUE) ExitProcess(2); + memset(h, 0, sizeof(h)); + h[0]='R'; h[1]='I'; h[2]='F'; h[3]='F'; + *(unsigned int *)(h + 4) = bytes + 36; + h[8]='W'; h[9]='A'; h[10]='V'; h[11]='E'; + h[12]='f'; h[13]='m'; h[14]='t'; h[15]=' '; + *(unsigned int *)(h + 16) = 16; + *(unsigned short *)(h + 20) = 1; + *(unsigned short *)(h + 22) = 2; + *(unsigned int *)(h + 24) = SRATE; + *(unsigned int *)(h + 28) = SRATE * 4; + *(unsigned short *)(h + 32) = 4; + *(unsigned short *)(h + 34) = 16; + h[36]='d'; h[37]='a'; h[38]='t'; h[39]='a'; + *(unsigned int *)(h + 40) = bytes; + writeAll(f, h, 44); + + while (done < total) { + int n = BUFFRAMES; + if (done + n > total) n = (int)(total - done); + renderAudio(g_vidPcm, n); + writeAll(f, g_vidPcm, (DWORD)(n * 4)); + done += n; + } + CloseHandle(f); +} + +static void videoMain(void) +{ + ID3D11Texture2D *out = 0, *stage = 0; + ID3D11RenderTargetView *rtv = 0; + D3D11_TEXTURE2D_DESC td; + HANDLE so; + __int64 frames = ((__int64)SONG_ROWS * SPR * VIDFPS + SRATE - 1) / SRATE; + __int64 fr; + char env[8]; + + if (GetEnvironmentVariableA("CRTK_WAVONLY", env, sizeof(env)) > 0) { + wavOut(); + ExitProcess(0); + } + + if (!createPipeline()) ExitProcess(1); + if (!createSceneTargets(VIDW, VIDH)) ExitProcess(1); + + memset(&td, 0, sizeof(td)); + td.Width = VIDW; td.Height = VIDH; + td.MipLevels = 1; td.ArraySize = 1; + td.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + td.SampleDesc.Count = 1; + td.Usage = D3D11_USAGE_DEFAULT; + td.BindFlags = D3D11_BIND_RENDER_TARGET; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &out) < 0) ExitProcess(1); + if (ID3D11Device_CreateRenderTargetView(g_dev, (ID3D11Resource *)out, 0, + &rtv) < 0) ExitProcess(1); + td.Usage = D3D11_USAGE_STAGING; + td.BindFlags = 0; + td.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + if (ID3D11Device_CreateTexture2D(g_dev, &td, 0, &stage) < 0) ExitProcess(1); + + so = GetStdHandle(STD_OUTPUT_HANDLE); + + for (fr = 0; fr < frames; fr++) { + D3D11_MAPPED_SUBRESOURCE m; + __int64 smp = fr * SRATE / VIDFPS; + + /* Generate the music up to this frame, then read the ring at the + same index - the identical arithmetic the shipping build does. */ + while (g_genSamples < smp) renderAudio(g_vidPcm, BUFFRAMES); + syncRead(smp, &g_syncNow); + syncFollow(&g_syncNow, 1.0f / (float)VIDFPS); + + g_row = (double)smp / (double)SPR; + renderFrame(g_row, rtv, VIDW, VIDH); + + ID3D11DeviceContext_CopyResource(g_ctx, (ID3D11Resource *)stage, + (ID3D11Resource *)out); + if (ID3D11DeviceContext_Map(g_ctx, (ID3D11Resource *)stage, 0, + D3D11_MAP_READ, 0, &m) >= 0) { + int y; + /* The staging pitch is whatever the driver chose, so write row by + row rather than assuming it equals the width. */ + for (y = 0; y < VIDH; y++) + writeAll(so, (const unsigned char *)m.pData + + (size_t)y * m.RowPitch, VIDW * 4); + ID3D11DeviceContext_Unmap(g_ctx, (ID3D11Resource *)stage, 0); + } else ExitProcess(3); + } + ExitProcess(0); +} +#endif /* VIDEO */ diff --git a/makevideo_gpu.bat b/makevideo_gpu.bat new file mode 100644 index 0000000..7118567 --- /dev/null +++ b/makevideo_gpu.bat @@ -0,0 +1,7 @@ +@echo off +setlocal +cd /d "%~dp0" +rem Always rebuild: an existing video renderer may embed stale shaders. +call buildgfx.bat video || exit /b 1 +python tools\export_video.py +exit /b %errorlevel% diff --git a/shots.h b/shots.h new file mode 100644 index 0000000..622c551 --- /dev/null +++ b/shots.h @@ -0,0 +1,526 @@ +/* ========================================================================== + T H E E D I T + -------------------------------------------------------------------------- + The old demo had one continuous camera integral and decorated it. That is + why 85% of it was a single shot: there was nowhere for a cut to live. + + Here a shot is data. Once that is true, jump cuts, match cuts, holds, + reverses, whip pans and aspect changes are all just fields in a row, and + the edit becomes something you write rather than something that falls out + of a sine wave. + + Time is addressed in ROWS - one sixteenth note - never in seconds, so no + cut can land off the music: + + row = 5000 samples @ 44100 = 0.113378 s + beat = 4 rows = 0.453515 s + bar = 16 rows = 1.814059 s + pattern = 32 rows = 3.628118 s + song = 96 bars = 1536 rows = 174.15 s (132.30 BPM) + + `eye` is where the camera is. `at` is ALWAYS a point in the world that it + looks at - never a direction. Motion behaviours take their axis from + (at - eye), so aiming a shot is one idea rather than two. + + FIXED 48-ORDER MUSIC MAP (32 rows/order): + orders 0-1 rows 0-63 intro: Commodore tube, then CRTKAFA (7.256 s) + orders 2-7 rows 64-255 synthwave + orders 8-11 rows 256-383 single metal verse + orders 12-15 rows 384-511 single chorus + orders 16-39 rows 512-1279 evolving solo (87.075 s) + orders 40-45 rows 1280-1471 arrival + orders 46-47 rows 1472-1535 outro + + All runtime scenes are spatial. Legacy shader IDs remain stable; LOGO is + appended for the parent's shader registration. Its shader owns the actual + flypast and tune interpretation; this edit supplies one 32-row slot. + No flash transitions, stutter or matte pumping. Two isolated one-beat + bursts join phrases; other shots last two to four bars except the final + three-beat solo pickup. Reactor camera distances stay outside radius six. + ========================================================================== */ + +#define ROWS_PER_BEAT 4 +#define ROWS_PER_BAR 16 +#define SONG_ROWS 1536 + +/* BAR(n) is one-based, the way a musician counts. */ +#define BAR(n) (((n) - 1) * ROWS_PER_BAR) +#define BEAT(n, b) (BAR(n) + ((b) - 1) * ROWS_PER_BEAT) +#define ROWAT(n, b, r) (BEAT(n, b) + (r)) + +/* ---- which pixel shader draws this shot -------------------------------- */ +enum { + SC_EYE = 0, /* macro cat eye */ + SC_CATHEDRAL, /* the organ hall */ + SC_VOID, /* monolith in the white void */ + SC_HORIZON, /* synthwave: grid, banded sun, ridge, slabs */ + SC_KALEIDO, /* kaleidoscopic IFS tunnel - the acid trip. */ + /* travels along its axis only: a crane or a track */ + /* walks the camera out of the tunnel and the frame */ + /* goes black. */ + SC_FRAMES, /* flat 2D design frames: rules, blocks, big type */ + SC_PLINTH, /* vaporwave plinth room lit by a wall of CRTs */ + SC_DRIVE, /* the road at night, from inside the car */ + SC_CORRIDOR, /* the liminal corridor that never arrives */ + SC_POWEROFF, /* the tube collapsing to a line, then a point */ + SC_BURST, /* streaks past the camera at the fastest passage */ + SC_C64, /* the original boot text inside a spatial CRT */ + SC_SIGNOFF, /* crt.fyi and hi@crt.fyi, small, once */ + SC_LOGO, /* one spatial CRTKAFA flypast, intro only */ + SC_REACTOR, /* finite porous core, broken rings and shards */ + SC_ORBITAL, /* gas giant, eclipse and luminous filaments */ + SC_COUNT +}; + +/* ---- what the camera does ---------------------------------------------- */ +enum { + CAM_HOLD = 0, /* completely still. the demo has never had one of these */ + CAM_PUSH, /* forward, towards `at` */ + CAM_PULL, /* backwards, away from it - a reveal */ + CAM_TRACK, /* lateral dolly across the look axis */ + CAM_CRANE, /* vertical */ + CAM_ORBIT, /* around `at` */ + CAM_WHIP, /* the look sweeps fast across the shot */ + CAM_DRIFT /* held, but breathing */ +}; + +/* ---- what, if anything, is IN the shot --------------------------------- */ +enum { + ACT_NONE = 0, + ACT_CAT_RUN, /* the black cat, running. 24 baked frames */ + ACT_CAT_STILL, /* the same mesh, held on one frame */ + ACT_CAT_SWARM, /* several of them, instanced across the frame */ + ACT_TARDIS, /* single-frame blue-box callback */ + ACT_SPIN_CAT /* one short spinning meme cut */ +}; + +/* ========================================================================== + T H E G R A D E + -------------------------------------------------------------------------- + The CRT pass quantises luminance into a four-stop ramp, so the ramp is + what the picture is built out of: every pixel is re-coloured through it, + and then the scene's own hue is mixed back on top by hueKeep - which is + why the stops are felt everywhere without flattening the scenes to four + literal colours. Changing them is still a far stronger instrument than + multiplying the output by a tint, and it makes the step count the single biggest lever in the + renderer: at seven steps the picture is a woodcut, at twelve it is nearly + continuous. Both were hardcoded until now, which is why 174 seconds of + demo all looked like the same afternoon. + + A grade belongs to the SECTION, not to the shot. The music is what changes + colour; the camera just happens to be pointing somewhere while it does. + So this is a schedule in rows, exactly like the arrangement it follows, + and not a field in the shot table. + ========================================================================== */ +enum { GR_COLD = 0, GR_WARM, GR_NEON, GR_BLEACH, GR_PHOSPHOR, GR_COUNT }; + +typedef struct { + float ramp[4][4]; /* the four stops. this is the whole palette */ + /* How much of the SOURCE hue is allowed back on top of the ramp. Read it + backwards from how it sounds: 1.0 means the scene's own colour wins and + the palette is barely felt, 0.0 means the frame is made of nothing but + the four stops. So the grades that exist to be noticed sit low. */ + float hueKeep; + float steps; /* quantiser levels */ + float dither; + float exposure; +} Grade; + +static const Grade g_grades[GR_COUNT] = { + /* COLD - the metal, the breakdown, the corridors. A blue black, neutral + midtones, a slightly warm white. This is the look the whole demo had. */ + { { {0.030f,0.034f,0.048f,0.0f}, {0.150f,0.180f,0.235f,0.0f}, + {0.520f,0.545f,0.585f,0.0f}, {0.980f,0.965f,0.940f,0.0f} }, + 0.62f, 10.0f, 0.85f, 1.00f }, + + /* WARM - the turn, the chorus, and the arrival: everywhere the music is + at its biggest. Amber shadows, and the ramp opens up a step. */ + { { {0.042f,0.031f,0.030f,0.0f}, {0.205f,0.150f,0.120f,0.0f}, + {0.600f,0.530f,0.430f,0.0f}, {1.000f,0.960f,0.880f,0.0f} }, + 0.50f, 11.0f, 0.82f, 1.05f }, + + /* NEON - the synthwave half and the solo. A magenta-leaning shadow and a + cyan white is most of the genre in two numbers. Measured at hueKeep + 0.78 this came out plain red: the horizon and the drive are both lit + by an orange sun, and at that setting their own hue simply overrode + the palette. Half is where the magenta survives contact with them. */ + { { {0.048f,0.026f,0.062f,0.0f}, {0.210f,0.115f,0.250f,0.0f}, + {0.510f,0.470f,0.690f,0.0f}, {0.890f,0.980f,1.000f,0.0f} }, + 0.50f, 12.0f, 0.90f, 1.06f }, + + /* BLEACH - the void, and the design frames. Almost no shadow at all and + few enough steps that the dither stops being texture and becomes the + subject. Two bars of this is why the solo lands. */ + { { {0.125f,0.128f,0.138f,0.0f}, {0.430f,0.435f,0.450f,0.0f}, + {0.765f,0.770f,0.780f,0.0f}, {1.000f,1.000f,0.995f,0.0f} }, + 0.35f, 7.0f, 1.00f, 1.00f }, + + /* PHOSPHOR - the eye, the C64 screen, the power-off, the sign-off. One + hue and eight steps: it should read as a tube, not as a render. */ + { { {0.018f,0.030f,0.022f,0.0f}, {0.085f,0.225f,0.115f,0.0f}, + {0.350f,0.645f,0.395f,0.0f}, {0.870f,1.000f,0.895f,0.0f} }, + 0.30f, 8.0f, 0.95f, 1.02f }, +}; + +/* Section keys follow the fixed 48-order score, including solo chapters. */ +typedef struct { short row; unsigned char grade; } GradeKey; + +static const GradeKey g_gradeKeys[] = { + { BAR(1), GR_COLD }, /* orders 0-1: logo */ + { BAR(5), GR_NEON }, /* orders 2-7: synthwave */ + { BAR(17), GR_COLD }, /* orders 8-11: single verse */ + { BAR(25), GR_WARM }, /* orders 12-15: single chorus */ + { BAR(33), GR_NEON }, /* solo I: space, clean lead */ + { BAR(45), GR_COLD }, /* solo II: angular, low architecture */ + { BAR(57), GR_WARM }, /* solo III: expansion and ascent */ + { BAR(66), GR_PHOSPHOR }, /* three-bar green iris climax */ + { BAR(69), GR_NEON }, /* solo IV: broad release */ + { BAR(81), GR_WARM }, /* orders 40-45: arrival */ + { BAR(93), GR_COLD }, /* orders 46-47: withdrawal */ +}; +#define GRADE_KEYS ((int)(sizeof(g_gradeKeys) / sizeof(g_gradeKeys[0]))) + +/* Some scenes carry their own look wherever they appear. A green cat eye in + the middle of the synthwave section is a callback rather than a mistake, + and a C64 screen graded warm would simply be wrong. GR_FOLLOW means "take + the section's grade", which is what most scenes do. */ +#define GR_FOLLOW 0xFF +static const unsigned char g_sceneGrade[SC_COUNT] = { + GR_PHOSPHOR, /* SC_EYE */ + GR_FOLLOW, /* SC_CATHEDRAL */ + GR_BLEACH, /* SC_VOID */ + GR_FOLLOW, /* SC_HORIZON */ + GR_FOLLOW, /* SC_KALEIDO */ + GR_BLEACH, /* SC_FRAMES */ + GR_FOLLOW, /* SC_PLINTH */ + GR_FOLLOW, /* SC_DRIVE */ + GR_FOLLOW, /* SC_CORRIDOR */ + GR_PHOSPHOR, /* SC_POWEROFF */ + GR_FOLLOW, /* SC_BURST */ + GR_PHOSPHOR, /* SC_C64 */ + GR_PHOSPHOR, /* SC_SIGNOFF */ + GR_FOLLOW, /* SC_LOGO */ + GR_FOLLOW, /* SC_REACTOR */ + GR_FOLLOW /* SC_ORBITAL */ +}; + +/* ========================================================================== + T H E M A T T E + -------------------------------------------------------------------------- + Full frame throughout. The ending withdraws through 3D space; the parent + renderer supplies the final darkness fade, without an aspect-ratio cue. + ========================================================================== */ +typedef struct { + short row; /* when it starts moving */ + short rows; /* how long it takes. 0 is instant */ + float bars; /* fraction of the height hidden at each edge. + 0.128 turns 16:9 into 2.39:1, 0.056 into 2.00:1 */ +} MatteKey; + +static const MatteKey g_mattes[] = { + { BAR(1), 0, 0.000f }, +}; +#define MATTE_KEYS ((int)(sizeof(g_mattes) / sizeof(g_mattes[0]))) + +static float matteAt(double row) +{ + int i, k = 0; + float from, u; + for (i = 0; i < MATTE_KEYS; i++) + if ((double)g_mattes[i].row <= row) k = i; + + if (g_mattes[k].rows <= 0) return g_mattes[k].bars; + + u = (float)((row - (double)g_mattes[k].row) / (double)g_mattes[k].rows); + if (u >= 1.0f) return g_mattes[k].bars; + if (u < 0.0f) u = 0.0f; + + /* The one before it is where we are coming from - and for the first key + that is the LAST one, because the demo loops. The outro closes the + bars to exactly where the cold open starts them, so the wrap is + continuous rather than a snap. */ + from = (k > 0) ? g_mattes[k - 1].bars : g_mattes[MATTE_KEYS - 1].bars; + u = u * u * (3.0f - 2.0f * u); /* ease, so it reads as a move */ + return from + (g_mattes[k].bars - from) * u; +} + +/* ---- how we get INTO this shot ----------------------------------------- */ +enum { + TR_CUT = 0, /* hard. the default, and it should be */ + TR_FLASH, /* white blown through the cut */ + TR_BLACK, /* up from black */ + TR_DISSOLVE, /* both shots rendered and crossfaded */ + TR_MATCH, /* a hard cut placed so a shape survives it */ + TR_WHIPIN /* smeared in - pairs with CAM_WHIP on the shot before */ +}; + +typedef struct { + short start; /* first row of the shot */ + unsigned char scene; + unsigned char cam; + unsigned char trans; /* transition INTO this shot */ + unsigned char transRows;/* how long that transition lasts, in rows */ + float eye[3]; /* where the camera is */ + float at[3]; /* the point it looks at */ + float speed; /* units per second, or radians per second */ + float fov; /* tan(vfov/2) */ + float roll; + float tune[4]; /* handed to the scene shader untouched */ + + /* An actor is a mesh standing in the scene, and it is addressed in the + same language as everything else here: where it starts, where it ends, + and how fast its cycle runs in FRAMES PER ROW. Tying the animation to + the song's row clock rather than to a wall clock is the whole point - + the old demo stepped the cat against wall-clock seconds and it never + once landed on the beat. */ + unsigned char actor; + float from[3]; /* world position at the start of the shot */ + float to[3]; /* and at the end of it */ + float actScale; + float actRate; /* animation frames per row. 3.0 is a lope */ + + /* STUTTER. The clock the picture reads, quantised to this many rows - + 0 is smooth, 1.0 holds each frame for a full sixteenth, 2.0 for an + eighth note. The edit clock underneath it keeps running, so the cut + still lands where it was written; only what is inside the frame + judders. + + It has to be COARSER than the motion it quantises or it does nothing + at all, which is a thing you cannot see by reading the table - the + self test measures it per shot. + + Nothing here is decoded video, so there is nothing to datamosh and no + frame ring worth carrying: freezing a procedural picture means holding + the time you hand it. Which is also why the actor freezes with the + scene rather than sliding through a held frame - they read the same + clock, so they stop together or not at all. */ + float stutter; +} Shot; + +/* -------------------------------------------------------------------------- + The 48-order spatial edit. Start rows determine actual shot durations. + Cat actors retain four appearances; their from/to paths span the next cut. + Kaleido moves are strictly axial. Plinth paths stay in the centre aisle. + Source shaders, C64 prose and music are frozen by this edit. + -------------------------------------------------------------------------- */ +static const Shot g_shots[] = { + +/* INTRO: the Commodore tube gets the first bar: short, physical, phosphor-lit + and already inside the 3D world. It is the threshold, not a waiting room. */ +{ BAR(1), SC_C64, CAM_PUSH, TR_CUT, 0, + {0.0f, 0.0f, -7.0f}, {0.0f, 0.0f, 0.0f}, 0.35f, 0.58f, 0.0f, + {1.0f, 0.72f, 0.16f, 0.0f} }, + +/* The spatial CRTKAFA flypast occupies intro bars 3-4. + The mark appears once and is then left behind for the rest of the run. */ +{ BAR(3), SC_LOGO, CAM_HOLD, TR_CUT, 0, + {0.0f, 0.0f, -12.0f}, {0.0f, 0.0f, 0.0f}, 0.0f, 0.48f, 0.0f, + {0.0f, 0.0f, 0.0f, 0.0f} }, + +/* SYNTHWAVE: first a distant TARDIS callback, almost swallowed by the void. + It is there early, but never becomes a mascot or a repeated prop. */ +{ BAR(5), SC_BURST, CAM_HOLD, TR_CUT, 0, + {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 10.0f}, 0.0f, 0.6f, 0.0f, + {30.0f, 0.10f, 0.12f, 0.0f}, + ACT_TARDIS, {-2.4f, 0.2f, 8.0f}, {2.9f, 0.6f, 4.4f}, 2.1f, 0.0f }, + +/* Immediate tunnel acceleration after the one-bar TARDIS crossing. */ +{ BAR(6), SC_KALEIDO, CAM_PUSH, TR_CUT, 0, + {0.15f, -0.1f, 0.0f}, {0.15f, -0.1f, 10.0f}, 7.0f, 0.74f, 0.08f, + {0.15f, 1.45f, 0.25f, 1.0f} }, + +/* First running cat, near enough to read during the initial 15 seconds. */ +{ BAR(8), SC_CORRIDOR, CAM_TRACK, TR_CUT, 0, + {-0.8f, 0.9f, 0.0f}, {0.0f, 0.8f, 4.0f}, 0.3f, 0.58f, 0.0f, + {0.24f, 0.68f, 0.3f, 0.0f}, + ACT_CAT_RUN, {-1.2f, 0.60f, 3.4f}, {1.3f, 0.60f, 3.4f}, 1.35f, 2.5f }, + +/* Rise over the ridge; small distant sun and deeper haze. */ +{ BAR(10), SC_HORIZON, CAM_CRANE, TR_CUT, 0, + {5.0f, 2.1f, -14.0f}, {0.0f, 4.0f, 40.0f}, 0.45f, 0.58f, 0.0f, + {58.0f, 0.14f, 0.86f, 0.45f} }, + +{ BAR(12), SC_REACTOR, CAM_ORBIT, TR_CUT, 0, + {-5.0f, 2.0f, -10.0f}, {0.0f, 0.0f, 0.0f}, 0.22f, 0.45f, 0.0f, + {0.0f, 1.0f, 0.15f, 0.3f} }, + +/* Waveform gallery, high transverse move, mirror floor. */ +{ BAR(14), SC_PLINTH, CAM_TRACK, TR_CUT, 0, + {-2.0f, 6.0f, 8.0f}, {0.0f, 7.2f, 30.0f}, 0.3f, 0.58f, 0.0f, + {1.1f, 0.6f, 0.2f, 0.65f} }, + +/* VERSE: low left arcade, rising sightline into the organ rank. */ +{ BAR(17), SC_CATHEDRAL, CAM_PUSH, TR_CUT, 0, + {-6.0f, -3.0f, -22.0f}, {-3.0f, 8.0f, 26.0f}, 2.0f, 0.64f, 0.0f, + {0.15f, 0.4f, 0.0f, 0.0f} }, + +/* Cat 1: +X jamb crossing, 32-row run, fixed z matches the mesh facing. */ +{ BAR(19), SC_CORRIDOR, CAM_TRACK, TR_CUT, 0, + {-1.0f, 1.6f, 0.0f}, {1.0f, 1.5f, 8.0f}, 0.18f, 0.62f, 0.0f, + {0.26f, 0.66f, 0.3f, 0.0f}, + ACT_CAT_RUN, {-0.65f, 0.56f, 5.0f}, {0.65f, 0.56f, 5.0f}, 1.25f, 3.0f }, + +/* Night drive: close asphalt and distant lamps supply layered parallax. */ +{ BAR(21), SC_DRIVE, CAM_HOLD, TR_CUT, 0, + {0.0f, 1.3f, 0.0f}, {0.0f, 1.3f, 20.0f}, 0.0f, 0.70f, 0.0f, + {22.0f, 0.25f, 0.05f, 0.65f} }, + +/* Cat 2: +X floor reflection crossing over 32 rows. */ +{ BAR(23), SC_CORRIDOR, CAM_TRACK, TR_CUT, 0, + {0.5f, 0.55f, 0.0f}, {0.0f, -0.35f, 6.0f}, -0.25f, 0.66f, 0.0f, + {0.22f, 0.7f, 0.5f, 0.0f}, + ACT_CAT_RUN, {-0.65f, 0.63f, 4.2f}, {0.65f, 0.63f, 4.2f}, 1.4f, 3.0f }, + +/* CHORUS: crane up through nave, broad organ-and-vault composition. */ +{ BAR(25), SC_CATHEDRAL, CAM_CRANE, TR_CUT, 0, + {3.0f, -2.0f, -16.0f}, {0.0f, 10.0f, 34.0f}, 0.65f, 0.78f, 0.0f, + {0.55f, 0.35f, -0.1f, 0.2f} }, + +/* Wide low sun, level travel keeps camera above the grid. */ +{ BAR(27), SC_HORIZON, CAM_PUSH, TR_CUT, 0, + {-5.0f, 3.0f, 0.0f}, {-5.0f, 3.0f, 40.0f}, 5.0f, 0.76f, 0.0f, + {44.0f, 0.52f, 0.62f, 0.28f} }, + +/* A luminous filament sculpture turns independently of the lateral camera. */ +{ BAR(29), SC_ORBITAL, CAM_TRACK, TR_CUT, 0, + {0.0f, 1.0f, -11.0f}, {0.0f, 0.0f, 0.0f}, 0.30f, 0.445f, 0.0f, + {2.0f, 1.0f, 1.0f, 0.15f} }, + +/* Four-sector entry, axial movement keeps the camera inside the bore. */ +{ BAR(31), SC_KALEIDO, CAM_PUSH, TR_CUT, 0, + {0.2f, -0.16f, 0.0f}, {0.2f, -0.16f, 6.0f}, 4.0f, 0.72f, 0.0f, + {0.1f, 1.35f, 0.2f, 0.0f} }, + +/* One beat only, restrained connective accent into the solo. */ +{ BEAT(32,4), SC_BURST, CAM_DRIFT, TR_CUT, 0, + {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 6.0f}, 0.0f, 0.64f, 0.0f, + {70.0f, 0.3f, 0.55f, 0.12f} }, + +/* SOLO I: delayed fracture, orbit uncovers suspended slab depth. */ +{ BAR(33), SC_VOID, CAM_ORBIT, TR_CUT, 0, + {-6.0f, 3.0f, -22.0f}, {0.0f, 4.8f, 0.0f}, 0.055f, 0.48f, 0.0f, + {0.3f, 0.6f, 0.0f, 1.4f} }, + +/* Compact reactor: close low orbit, radius 12.25, tighter portrait lens. */ +{ BAR(36), SC_REACTOR, CAM_ORBIT, TR_CUT, 0, + {-5.0f, -2.0f, -11.0f}, {0.0f, 0.0f, 0.0f}, 0.12f, 0.38f, 0.05f, + {0.0f, 0.7f, 0.15f, 0.2f} }, + +/* Cat 3: 48-row crane, y ends at 2.30; run uses actual shot span. */ +{ BAR(39), SC_CORRIDOR, CAM_CRANE, TR_CUT, 0, + {0.0f, 1.65f, 0.0f}, {0.0f, 2.5f, 8.0f}, 0.12f, 0.65f, 0.0f, + {0.24f, 0.68f, 0.2f, 0.0f}, + ACT_CAT_RUN, {-0.7f, 0.61f, 5.8f}, {0.7f, 0.61f, 5.8f}, 1.35f, 3.0f }, + +/* Faster night drive as the solo answers. */ +{ BAR(42), SC_DRIVE, CAM_HOLD, TR_CUT, 0, + {0.0f, 1.3f, 0.0f}, {0.0f, 1.3f, 20.0f}, 0.0f, 0.76f, 0.0f, + {42.0f, 0.80f, 0.45f, 0.9f} }, + +/* SOLO II: orbit the eclipse and its intersecting accretion sheets. */ +{ BAR(45), SC_ORBITAL, CAM_ORBIT, TR_CUT, 0, + {0.0f, 2.9f, -13.0f}, {0.0f, 0.0f, 0.0f}, 0.09f, 0.425f, 0.0f, + {1.0f, 0.8f, 1.0f, -0.18f} }, + +/* Six-sector reverse passage; axis remains fixed, no sideways wall impact. */ +{ BAR(48), SC_KALEIDO, CAM_PULL, TR_CUT, 0, + {-0.2f, 0.2f, 0.0f}, {-0.2f, 0.2f, 6.0f}, 4.0f, 0.64f, 0.0f, + {0.35f, 1.5f, 0.4f, 1.0f} }, + +/* Close moving monitor wall; 'signal found' appears once inside one CRT. */ +{ BAR(51), SC_PLINTH, CAM_TRACK, TR_CUT, 0, + {3.0f, 7.0f, 9.0f}, {0.0f, 7.2f, 30.0f}, -0.4f, 0.58f, 0.0f, + {1.0f, 0.4f, 0.2f, 0.15f} }, + +/* Expanded reactor: eye-level lateral reveal, distance 12.33..12.47. */ +{ BAR(54), SC_REACTOR, CAM_TRACK, TR_CUT, 0, + {6.0f, 4.0f, -10.0f}, {0.0f, 0.0f, 0.0f}, -0.35f, 0.42f, 0.0f, + {1.0f, 1.0f, 0.4f, 0.55f} }, + +/* SOLO III: organ ascent, eye ends below vault spring. */ +{ BAR(57), SC_CATHEDRAL, CAM_CRANE, TR_CUT, 0, + {-3.0f, -2.0f, -20.0f}, {0.0f, 11.0f, 36.0f}, 0.9f, 0.7f, 0.0f, + {0.35f, 0.4f, -0.2f, 0.25f} }, + +/* Accelerating debris passage with restrained warm highlights. */ +{ BAR(60), SC_BURST, CAM_DRIFT, TR_CUT, 0, + {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 10.0f}, 0.0f, 0.78f, -0.15f, + {105.0f, 0.48f, 0.2f, 0.1f} }, + +/* The sole gas-giant flyby: a tilted ring plane and a lit atmosphere. */ +{ BAR(63), SC_ORBITAL, CAM_TRACK, TR_CUT, 0, + {0.0f, 3.8f, -12.0f}, {0.0f, 0.0f, 0.0f}, 0.35f, 0.404f, 0.0f, + {0.0f, 1.0f, 1.0f, 0.22f} }, + +/* Ten-sector open iris hall; forward travel, wider architecture. */ +{ BAR(66), SC_KALEIDO, CAM_PUSH, TR_CUT, 0, + {0.32f, -0.25f, 0.0f}, {0.32f, -0.25f, 6.0f}, 6.0f, 0.8f, 0.0f, + {0.65f, 1.85f, 0.7f, 2.0f} }, + +/* SOLO IV: close collapse study, gentle withdrawal 12.05 -> 12.92 over 64 rows. + Narrow lens keeps the shrinking core substantial through the whole phrase. */ +{ BAR(69), SC_REACTOR, CAM_PULL, TR_CUT, 0, + {-3.0f, 2.0f, -11.5f}, {0.0f, 0.0f, 0.0f}, 0.12f, 0.35f, 0.0f, + {3.0f, 1.5f, 0.95f, 1.0f} }, + +/* Fast axial tunnel passage during the dense solo run. */ +{ BAR(73), SC_KALEIDO, CAM_PUSH, TR_CUT, 0, + {0.1f, 0.1f, 0.0f}, {0.1f, 0.1f, 10.0f}, 10.0f, 0.76f, 0.25f, + {0.35f, 1.7f, 0.45f, 0.0f} }, + +/* Lateral +X run over 32 rows, camera tracks beside the cat. */ +{ BAR(77), SC_CORRIDOR, CAM_TRACK, TR_CUT, 0, + {-0.65f, 1.35f, 0.0f}, {0.15f, 1.15f, 6.0f}, 0.12f, 0.7f, 0.0f, + {0.2f, 0.75f, 0.6f, 0.0f}, + ACT_CAT_RUN, {-0.7f, 0.6f, 4.8f}, {0.7f, 0.6f, 4.8f}, 1.1f, 3.0f }, + +/* One-bar spinning meme cat, then a short skyline pickup into arrival. */ +{ BAR(79), SC_BURST, CAM_HOLD, TR_CUT, 0, + {0.0f, 0.1f, -4.0f}, {0.0f, 0.1f, 0.0f}, 0.0f, 0.62f, 0.0f, + {40.0f, 0.15f, 0.2f, 0.0f}, + ACT_SPIN_CAT, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, 1.5f, 0.55f }, + +{ BAR(80), SC_HORIZON, CAM_CRANE, TR_CUT, 0, + {-6.0f, 2.5f, -14.0f}, {0.0f, 4.0f, 40.0f}, 0.55f, 0.72f, 0.0f, + {12.0f, 0.36f, 0.44f, 0.6f} }, + +/* One beat into arrival, no white-flash transition. */ +{ BEAT(80,4), SC_BURST, CAM_DRIFT, TR_CUT, 0, + {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 6.0f}, 0.0f, 0.64f, 0.0f, + {84.0f, 0.35f, 0.5f, 0.1f} }, + +/* ARRIVAL: central nave opens, full rank and stained glass. */ +{ BAR(81), SC_CATHEDRAL, CAM_PUSH, TR_CUT, 0, + {0.0f, -2.0f, -30.0f}, {0.0f, 8.0f, 50.0f}, 1.6f, 0.76f, 0.0f, + {0.25f, 0.4f, -0.2f, 0.25f} }, + +{ BAR(83), SC_ORBITAL, CAM_PULL, TR_CUT, 0, + {4.0f, -1.5f, -10.0f}, {0.0f, 0.0f, 0.0f}, 0.55f, 0.64f, -0.12f, + {1.0f, 1.5f, 0.9f, 0.65f} }, + +/* Two-bar monitor-wall inscription: 'stay strange'. */ +{ BAR(85), SC_PLINTH, CAM_CRANE, TR_CUT, 0, + {0.0f, 6.5f, 5.0f}, {0.0f, 7.2f, 30.0f}, 0.18f, 0.60f, 0.0f, + {1.1f, 0.6f, 0.15f, 0.7f} }, + +{ BAR(87), SC_VOID, CAM_PULL, TR_CUT, 0, + {7.0f, 5.0f, -16.0f}, {0.0f, 4.0f, 0.0f}, 0.4f, 0.62f, 0.0f, + {-0.4f, 0.4f, 0.8f, 0.0f} }, + +/* Broad sunset release with sustained lateral parallax. */ +{ BAR(89), SC_HORIZON, CAM_TRACK, TR_CUT, 0, + {-5.0f, 3.0f, -10.0f}, {0.0f, 4.0f, 40.0f}, 0.8f, 0.82f, 0.0f, + {6.0f, 0.6f, 0.72f, 0.34f} }, + +/* OUTRO: retreat from the filament sculpture. */ +{ BAR(93), SC_ORBITAL, CAM_PULL, TR_CUT, 0, + {3.0f, 2.0f, -12.0f}, {0.0f, 0.0f, 0.0f}, 1.0f, 0.52f, 0.0f, + {2.0f, 0.45f, 0.7f, -0.40f} }, + +/* Last two bars: withdraw through the dark nave; parent applies endfade. */ +{ BAR(95), SC_CATHEDRAL, CAM_PULL, TR_CUT, 0, + {0.0f, -1.0f, -26.0f}, {0.0f, 5.0f, 46.0f}, 3.0f, 0.62f, 0.0f, + {0.3f, -0.7f, 0.35f, -0.7f} }, +}; + +#define NSHOTS ((int)(sizeof(g_shots) / sizeof(g_shots[0]))) diff --git a/song_data.h b/song_data.h new file mode 100644 index 0000000..6813b4a --- /dev/null +++ b/song_data.h @@ -0,0 +1,763 @@ +/* Through-composed form: 48 orders / 96 bars / 174.149659864 seconds. + Notes use MIDI-12; X=-1 means no trigger. No audio mastering validation. + Drum API: 6 = kick + crash; 7 = kick + snare (synthwave backbeats). + INTRO orders 0- 1; section ID 0; proposed trim -14 dB + SYNTHWAVE orders 2- 7; section ID 1; proposed trim -14 dB + METAL VERSE orders 8-11; section ID 4; proposed trim -8 dB + CHORUS orders 12-15; section ID 5; proposed trim -6 dB + SOLO orders 16-39; section ID 8; proposed trim -10 dB + ARRIVAL orders 40-45; section ID 9; proposed trim -3 dB + OUTRO orders 46-47; section ID 10; proposed trim -14 dB + SOLO spacious lead orders 16-19; proposed trim -14 dB + SOLO melodic guitar orders 20-24; proposed trim -11 dB + SOLO driving shredding orders 25-30; proposed trim -9 dB + SOLO organ counterpoint orders 31-35; proposed trim -12 dB + SOLO climactic burst orders 36-39; proposed trim -6 dB + Outro final order trim -22 dB. Generated by tools/arrange_v11.py. */ +#define SNG_ORDERS 48 +#define SNG_ROWS 32 + +static const signed char g_sqLead[] = { + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 57,-1,-1,-1,65,-1,64,-1,64,-1,-1,-1,60,-1,-1,-1, + 62,-1,64,-1,67,-1,-1,-1,65,-1,64,-1,-1,-1,-1,-1, + 60,-1,64,-1,69,-1,-1,67,-1,-1,64,-1,60,-1,-1,-1, + 57,-1,60,-1,62,-1,-1,-1,64,-1,64,-1,-1,-1,-1,-1, + 65,-1,-1,69,-1,-1,72,-1,69,-1,-1,67,-1,-1,64,-1, + 60,-1,-1,-1,62,-1,-1,-1,65,-1,64,-1,-1,-1,-1,-1, + 62,-1,65,-1,-1,-1,64,-1,-1,-1,60,-1,59,-1,-1,-1, + 57,-1,55,-1,59,-1,-1,-1,64,-1,64,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +}; +static const signed char g_sqSolo[] = { + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 45,-1,-1,-1,-1,-1,53,-1,-1,-1,52,-1,52,-1,-1,-1, + -1,-1,48,-1,-1,-1,50,-1,-1,-1,-1,47,-1,-1,-1,-1, + -1,60,-1,-1,-1,64,-1,-1,-1,67,-1,-1,65,-1,-1,-1, + -1,62,-1,-1,-1,60,-1,-1,57,-1,-1,-1,59,-1,-1,-1, + 65,-1,-1,64,-1,-1,-1,60,-1,-1,57,-1,-1,-1,-1,-1, + 60,-1,-1,62,-1,-1,67,-1,-1,64,-1,-1,62,-1,-1,-1, + 62,-1,-1,-1,64,-1,-1,67,-1,-1,69,-1,69,-1,-1,-1, + 65,-1,-1,-1,64,-1,-1,-1,60,-1,-1,59,-1,64,-1,-1, + 57,-1,60,-1,64,-1,69,-1,69,-1,69,-1,-1,67,65,64, + -1,-1,60,-1,62,-1,65,-1,-1,64,-1,64,-1,-1,-1,-1, + 62,-1,65,-1,69,-1,72,71,69,-1,65,-1,64,-1,60,-1, + 57,-1,60,-1,64,-1,64,-1,64,-1,-1,62,-1,59,-1,-1, + 65,-1,65,-1,-1,69,72,74,72,-1,69,-1,67,-1,64,-1, + 60,-1,62,-1,65,-1,69,67,64,-1,64,-1,-1,60,-1,-1, + 60,64,67,-1,69,-1,72,-1,69,-1,64,-1,60,-1,57,-1, + 60,64,67,-1,65,-1,62,-1,60,-1,57,-1,57,-1,59,-1, + 62,64,65,69,72,-1,74,-1,72,-1,69,-1,65,-1,62,-1, + 60,64,67,69,67,-1,64,-1,60,-1,59,-1,57,-1,57,-1, + 57,59,60,62,64,65,67,69,67,65,64,62,60,59,57,55, + 57,60,64,69,72,69,64,60,57,59,60,64,69,67,64,57, + 62,65,64,67,65,69,67,72,69,65,62,60,57,60,64,67, + 60,64,67,69,64,67,69,72,67,69,72,74,72,69,64,-1, + 69,64,69,65,69,64,69,62,69,64,69,60,69,64,69,59, + 69,64,69,67,69,64,69,65,69,64,69,64,69,67,69,71, + 60,67,72,76,72,67,60,64,65,72,77,72,65,62,57,62, + 69,72,69,67,65,64,62,60,59,57,55,59,60,64,67,-1, + 62,60,57,55,57,60,62,65,67,62,65,67,69,65,67,69, + 72,67,69,72,74,72,69,65,64,60,59,60,64,67,69,-1, + 65,57,67,59,69,60,72,64,74,65,72,64,69,60,67,59, + 64,65,67,69,72,74,77,74,72,69,67,64,60,57,-1,-1, + -1,-1,69,-1,72,-1,76,-1,-1,-1,-1,72,-1,-1,74,-1, + -1,-1,77,-1,-1,-1,74,-1,-1,-1,-1,72,-1,-1,-1,-1, + -1,79,-1,-1,76,-1,76,-1,-1,-1,74,-1,-1,-1,71,-1, + -1,-1,69,-1,-1,-1,72,-1,-1,76,-1,-1,72,-1,-1,-1, + -1,-1,74,-1,-1,77,-1,-1,81,-1,-1,77,-1,-1,76,-1, + -1,-1,72,-1,-1,69,-1,-1,72,-1,-1,76,-1,79,-1,-1, + -1,77,-1,76,-1,72,-1,-1,-1,74,-1,-1,77,-1,-1,74, + -1,-1,71,-1,-1,69,-1,-1,72,-1,76,-1,79,-1,76,-1, + 74,-1,74,-1,-1,77,-1,79,-1,77,-1,74,-1,-1,72,-1, + -1,69,-1,-1,72,-1,76,-1,79,-1,76,-1,74,-1,71,-1, + 57,64,69,72,69,64,60,65,62,65,69,74,72,69,65,62, + 65,69,72,77,74,72,69,65,67,69,72,74,77,74,72,69, + 72,69,65,60,65,69,72,77,74,72,69,65,62,65,69,74, + 69,67,64,60,64,67,69,76,72,69,67,64,60,64,67,69, + 77,69,76,69,74,67,72,65,69,72,74,77,81,77,74,72, + 69,65,67,69,72,74,77,81,77,74,72,69,67,64,60,59, + 62,65,69,74,72,69,65,62,65,69,72,77,74,69,64,64, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,64,67,69,72, + 83,-1,83,-1,83,-1,-1,-1,-1,-1,79,-1,-1,-1,78,-1, + -1,-1,74,-1,-1,-1,-1,-1,71,-1,71,-1,-1,-1,-1,-1, + 67,-1,71,-1,74,-1,79,-1,79,-1,-1,-1,76,-1,74,-1, + 71,-1,-1,-1,69,-1,66,-1,-1,-1,62,-1,62,-1,-1,-1, + 64,-1,-1,67,-1,-1,71,-1,76,-1,74,-1,71,-1,-1,69, + -1,-1,67,-1,-1,64,-1,-1,66,-1,69,-1,71,-1,71,-1, + 62,-1,66,-1,69,-1,74,-1,71,-1,69,-1,67,-1,71,-1, + 74,-1,79,-1,76,-1,74,-1,71,-1,67,-1,66,-1,66,-1, + 67,71,74,76,79,-1,76,-1,74,-1,71,-1,69,-1,66,-1, + 64,-1,67,-1,71,-1,69,-1,66,-1,62,-1,61,-1,59,-1, + 64,-1,67,-1,66,-1,62,-1,61,-1,66,-1,71,-1,71,-1, + -1,-1,66,-1,-1,-1,62,-1,-1,-1,59,-1,59,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +}; +static const signed char g_sqHarm[] = { + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 76,-1,-1,-1,-1,-1,-1,-1,77,-1,-1,-1,-1,-1,-1,-1, + 72,-1,-1,-1,-1,-1,-1,-1,76,-1,-1,-1,-1,-1,-1,-1, + 81,-1,-1,-1,-1,-1,-1,-1,72,-1,-1,-1,-1,-1,-1,-1, + 72,-1,-1,-1,-1,-1,-1,-1,81,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +}; +static const signed char g_sqBass[] = { + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 21,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 24,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1, + 21,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 24,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 21,-1,21,21,-1,21,21,-1,21,-1,21,21,21,-1,21,21, + 31,-1,31,-1,-1,31,-1,-1,28,-1,28,28,28,-1,28,-1, + 26,26,26,-1,26,26,26,-1,29,-1,-1,29,29,-1,29,-1, + 21,-1,21,21,21,21,21,-1,28,-1,-1,28,28,-1,28,28, + 29,-1,29,-1,29,29,-1,-1,31,-1,31,31,31,-1,31,-1, + 26,-1,26,-1,26,26,26,-1,28,28,-1,28,28,-1,28,28, + 24,24,24,-1,24,24,24,-1,31,-1,31,31,31,-1,31,-1, + 28,-1,28,-1,28,28,28,-1,21,-1,21,21,21,21,21,-1, + 21,-1,-1,-1,21,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 24,-1,-1,-1,24,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 21,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 24,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 21,-1,-1,-1,21,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 24,-1,-1,-1,24,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 24,-1,-1,-1,24,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 21,-1,-1,-1,21,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1, + 21,-1,21,-1,-1,21,-1,-1,31,-1,-1,31,-1,-1,31,-1, + 29,-1,29,-1,-1,29,-1,-1,28,-1,-1,28,-1,-1,28,-1, + 26,-1,26,-1,-1,26,-1,-1,21,-1,-1,21,-1,-1,21,-1, + 24,-1,24,-1,-1,24,-1,-1,28,-1,-1,28,-1,-1,28,-1, + 29,-1,29,-1,-1,29,-1,-1,26,-1,-1,26,-1,-1,26,-1, + 31,-1,31,-1,-1,31,-1,-1,28,-1,-1,28,-1,-1,28,-1, + 24,-1,24,-1,-1,24,-1,-1,29,-1,-1,29,-1,-1,29,-1, + 21,-1,21,-1,-1,21,-1,-1,31,-1,-1,31,-1,-1,31,-1, + 26,-1,26,-1,-1,26,-1,-1,31,-1,-1,31,-1,-1,31,-1, + 24,-1,24,-1,-1,24,-1,-1,28,-1,-1,28,-1,-1,28,-1, + 29,-1,29,-1,-1,29,-1,-1,31,-1,-1,31,-1,-1,31,-1, + 28,-1,28,-1,-1,28,-1,-1,21,-1,-1,21,-1,-1,21,-1, + 21,-1,-1,-1,21,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,21,-1,-1,-1,21,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,24,-1,-1,-1,24,-1,-1,-1, + 29,-1,-1,-1,29,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1, + 24,-1,-1,-1,24,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,29,-1,-1,-1,29,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 21,-1,21,-1,-1,21,-1,-1,26,-1,-1,26,-1,-1,26,-1, + 29,-1,29,-1,-1,29,-1,-1,31,-1,-1,31,-1,-1,31,-1, + 24,-1,24,-1,-1,24,-1,-1,29,-1,-1,29,-1,-1,29,-1, + 26,-1,26,-1,-1,26,-1,-1,28,-1,-1,28,-1,-1,28,-1, + 29,-1,29,-1,-1,29,-1,-1,31,-1,-1,31,-1,-1,31,-1, + 21,-1,21,-1,-1,21,-1,-1,28,-1,-1,28,-1,-1,28,-1, + 26,-1,26,-1,-1,26,-1,-1,29,-1,-1,29,-1,-1,29,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,28,-1, + 23,-1,-1,-1,23,-1,-1,-1,23,-1,-1,-1,23,-1,-1,-1, + 23,-1,-1,-1,23,-1,-1,-1,23,-1,-1,-1,23,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1,26,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 33,-1,-1,-1,33,-1,-1,-1,33,-1,-1,-1,33,-1,-1,-1, + 26,-1,-1,-1,26,-1,-1,-1,31,-1,-1,-1,31,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,30,-1,-1,-1,30,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,33,-1,-1,-1,33,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,30,-1,-1,-1,30,-1,-1,-1, + 28,-1,-1,-1,28,-1,-1,-1,30,-1,-1,-1,30,-1,-1,-1, + 23,-1,-1,-1,23,-1,-1,-1,23,-1,-1,-1,23,-1,-1,-1, + 31,-1,-1,-1,31,-1,-1,-1,28,-1,-1,-1,28,-1,-1,-1, + 23,-1,-1,-1,23,-1,-1,-1,23,-1,-1,-1,23,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +}; +static const signed char g_sqGtr[] = { + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 21,-1,21,28,-1,-1,21,-1,33,-1,28,-1,21,-1,-1,21, + 38,-1,43,-1,-1,38,-1,-1,28,-1,28,-1,28,-1,28,-1, + 26,33,-1,-1,26,-1,38,-1,36,-1,-1,29,29,-1,36,-1, + 21,-1,-1,33,28,-1,21,-1,28,-1,-1,28,40,-1,-1,28, + 29,-1,29,-1,41,36,-1,-1,31,-1,38,-1,43,-1,31,-1, + 33,-1,26,-1,38,-1,33,-1,28,28,-1,-1,28,-1,28,40, + 24,31,36,-1,31,-1,24,-1,31,-1,38,-1,31,-1,43,-1, + 28,-1,28,-1,28,-1,40,-1,28,-1,21,-1,21,28,21,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +}; +static const unsigned char g_sqHold[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; +static const unsigned char g_sqDrum[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 6,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,3,0, + 1,0,3,0,7,0,3,0,1,0,3,0,7,0,2,4, + 6,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,2,4, + 6,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,2,4, + 1,0,3,0,3,0,3,0,1,0,3,0,3,0,3,0, + 1,0,3,0,3,0,3,0,1,0,3,0,3,0,3,0, + 1,0,3,0,3,0,3,0,1,0,3,0,3,0,3,0, + 1,0,3,0,3,0,3,0,1,0,3,0,3,0,3,0, + 1,0,3,0,3,0,3,0,1,0,3,0,3,0,3,0, + 1,0,3,0,3,0,3,0,1,0,3,0,3,0,3,0, + 1,0,3,0,3,0,3,0,1,0,3,0,3,0,3,0, + 1,0,3,0,3,0,3,0,1,0,3,0,3,0,3,0, + 6,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,1,2,2, + 6,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,1,2,2, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,2,0,2,4, + 6,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,1,2,2, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,3,0, + 6,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,1,3,1,2,0,1,0,1,0,1,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,3,0, + 1,0,3,0,2,0,1,0,1,0,3,0,2,0,2,4, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 1,0,0,0,0,0,3,0,2,0,0,0,0,0,3,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; +static const signed char g_sqRoot[] = { + 9,9,4,4,5,5,7,4,9,9,5,5,0,0,7,7, + 2,2,9,9,5,5,4,4,0,0,2,2,7,7,4,4, + 9,9,7,4,2,5,9,4,5,7,2,4,0,7,4,9, + 9,5,0,7,2,9,5,4,5,0,7,2,2,5,4,4, + 9,9,5,5,0,0,2,2,5,5,7,7,2,2,4,4, + 9,5,0,7,2,9,4,4,5,0,2,7,0,7,9,5, + 2,5,4,9,9,7,5,4,2,9,0,4,5,2,7,4, + 0,5,9,7,2,7,0,4,5,7,4,9,9,0,2,5, + 7,5,2,0,2,9,5,0,5,2,0,7,2,5,7,4, + 9,2,5,7,0,5,2,4,5,7,9,4,2,5,4,4, + 11,11,11,11,7,7,2,2,4,4,9,9,2,7,4,6, + 7,9,4,6,4,6,11,11,7,4,11,11,11,11,11,11, +}; +static const unsigned char g_sqType[] = { + 0,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1, + 0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0, + 0,0,1,0,0,1,0,0,1,1,0,0,1,1,0,0, + 0,1,1,1,0,0,1,0,1,1,1,0,0,1,0,0, + 0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0, + 0,1,1,1,0,0,0,0,1,1,0,1,1,1,0,1, + 0,1,0,0,0,1,1,0,0,0,1,0,1,0,1,0, + 1,1,0,1,0,1,1,0,1,1,0,0,0,1,0,1, + 1,1,0,1,0,0,1,1,1,0,1,1,0,1,1,0, + 0,0,1,1,1,1,0,0,1,1,0,0,0,1,0,0, + 0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0, + 1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0, +}; +static const signed char g_sqArpOct[] = { + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +}; +static const unsigned char g_sqOrgMode[] = { + 2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2, + 2,2,2,2,0,0,0,0,0,0,0,0,0,0,2,2, +}; +static const unsigned char g_sqRiser[] = { + 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0, +}; +static const short g_sqGain[] = { + 204,204,204,204,204,204,204,204,408,408,408,408,513,513,513,513, + 204,204,204,204,289,289,289,289,289,363,363,363,363,363,363,257, + 257,257,257,257,513,513,513,513,725,725,725,725,725,725,204,81, +}; +static const unsigned char g_sqSyn[] = { + 0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; +static const unsigned char g_sqSect[] = { + 0,0,1,1,1,1,1,1,4,4,4,4,5,5,5,5, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,9,9,9,9,9,9,10,10, +}; diff --git a/synth.h b/synth.h new file mode 100644 index 0000000..4949fdf --- /dev/null +++ b/synth.h @@ -0,0 +1,1055 @@ +/* ========================================================================== + S Y N T H - realtime chip and metal synth, shared by both builds + -------------------------------------------------------------------------- + Lifted verbatim out of crtkafa.c so the D3D11 engine and the original + software build can play the same music from the same source. It owns the + song clock: audioPlayedSamples() is the number of samples the sound card + has actually played, and everything on screen is timed from that rather + than from a wall clock, so the picture cannot drift away from the music. + + Needs, from the translation unit that includes it: + - windows.h and mmsystem.h + - song_data.h (the baked note tables) + - memset / memcpy + ========================================================================== */ +#ifndef CRTK_SYNTH_H +#define CRTK_SYNTH_H + +/* The software build defines these before it includes us; the D3D11 build + does not, so carry our own. */ +#ifndef TAU +#define TAU 6.28318530718f +#endif +#ifndef PI +#define PI 3.14159265359f +#endif + +/* ========================================================================== + M U S I C - realtime 4 channel chip synth + ========================================================================== */ +#define SRATE 44100 +#define NBUF 4 +#define BUFFRAMES 2048 +#define SPR 5000 /* samples per row -> ~132 BPM 16ths */ +#define TPR 6 /* ticks per row */ +/* SPR/TPR is 833.33 and truncating it threw away two samples every row: the + song ran 70 ms fast over its length, and every row was very slightly short, + which is what a tight part sounds like when it feels loose. Worse now that + the picture takes its clock from the sample counter - the synth advanced a + row every 4998 samples while the visuals counted 5000, so the two drifted + apart by 0.6 of a row by the end. + + Deriving each tick's length from the row boundary makes the six of them sum + to SPR exactly: 833, 833, 834, 833, 833, 834. */ +#define SPT (SPR / TPR) /* the nominal length, for buffer sizing */ +#define TICKLEN(t) ((((t) + 1) * SPR) / TPR - ((t) * SPR) / TPR) +#define ROWS 32 +#define ORDERS 48 + +static HWAVEOUT g_wo; +static WAVEHDR g_whdr[NBUF]; +static short g_wbuf[NBUF][BUFFRAMES * 2]; +static int g_audioOk = 0; +static __int64 g_genSamples = 0; + +/* note numbers: octave*12 + pitchclass, C-0 == 0 */ +#define nC2 24 +#define nD2 26 +#define nE2 28 +#define nF2 29 +#define nG2 31 +#define nA2 33 +#define nB2 35 +#define nC3 36 +#define nD3 38 +#define nE3 40 +#define nF3 41 +#define nG3 43 +#define nA3 45 +#define nA4 57 +#define nB4 59 +#define nC5 60 +#define nD5 62 +#define nE5 64 +#define nF5 65 +#define nG5 67 +#define nGs5 68 +#define nA5 69 +#define nB5 71 +#define nC6 72 +#define nD6 74 +#define nE6 76 +#define nF4 53 +#define nG4 55 +#define RST (-1) + +static const float g_baseFreq[12] = { + 16.35160f, 17.32391f, 18.35405f, 19.44544f, 20.60172f, 21.82676f, + 23.12465f, 24.49971f, 25.95654f, 27.50000f, 29.13524f, 30.86771f +}; +static float noteFreq(int n) +{ + int oct = n / 12, pc = n % 12; + float f = g_baseFreq[pc]; + while (oct-- > 0) f *= 2.0f; + return f; +} + +/* ---- song --------------------------------------------------------------- + The arrangement lives in song_data.h, fully resolved by tools/export_song.py. + Nothing here decides anything musical - it only reads notes and plays them. + -------------------------------------------------------------------------- */ +#undef ORDERS +#define ORDERS SNG_ORDERS + +/* saturates properly at +-1, unlike a bare Pade tanh */ +/* x^(-2/3), for the compressor's 3:1 gain law. There is no C runtime here, + so this is the classic reciprocal-cube-root bit seed plus two Newton + steps, squared. Worst case 0.38 percent (0.03 dB) over the range the + compressor actually uses. */ +static float invCbrt2(float x) +{ + union { float f; unsigned int i; } u; + float c; + u.f = x; + u.i = 0x548c2b4bu - u.i / 3u; + c = u.f; + c = c * (1.3333333f - 0.33333333f * x * c * c * c); + c = c * (1.3333333f - 0.33333333f * x * c * c * c); + return c * c; +} + +static float softClip(float x) +{ + if (x < -3.0f) return -1.0f; + if (x > 3.0f) return 1.0f; + return x * (27.0f + x * x) / (27.0f + 9.0f * x * x); +} + +typedef struct { + float phase, inc, amp, decay, sustain, duty, vib, lp; + float env; /* the VCA, 0..1, separate from the decay envelope */ + float atk, rel; /* per sample, so 1/(seconds * SRATE) */ + int gate; /* 1 while the key is down */ + int active; +} Voice; + +static Voice g_vLead, g_vArp, g_vBass; + +/* two takes of a power chord, panned hard apart */ +static struct { + float p1, p2, p3, p4, q1, q2; + float inc, amp, decay, lp, hp, lp2, hp2; + int on; +} g_gtr; + +/* the solo, and its diatonic third */ +static struct { float p1, p2, inc, amp, decay, lp, vib, level; int on; } g_sol, g_hsol; + +/* church organ: drawbars, three notes, each partial with a detuned twin */ +#define ORG_BARS 6 +static const float g_orgMul[ORG_BARS] = { 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f }; +static const float g_orgAmp[ORG_BARS] = { 0.62f, 1.00f, 0.40f, 0.58f, 0.24f, 0.30f }; +/* Two banks, crossfaded. Resetting all 36 oscillator phases to zero at full + level - which is what this did - put every partial in phase at once: the + accumulator peaked at 3 x 3.14 against a steady-state RMS of 1.23, a + +17.7 dB transient on every single chord change. Letting the phases + free-run instead is not the answer either, because the detuned twins are + only 0.22% apart and drift into cancellation over a held chord. + + So: on a chord change the new bank is seeded from the OUTGOING bank's + phases, which makes the waveform continuous across the switch, and the two + are crossfaded. The twins still start together relative to each other, so + the cancellation cannot accumulate. */ +static struct { + float ph[2][3][ORG_BARS][2], inc[2][3][ORG_BARS][2]; + float amp, tgt, xf; /* xf runs 0 -> 1, old bank -> new bank */ + int note[3], mode, bars, bank; +} g_org; + +/* 45 ms. One and a half cycles of the lowest partial in play (16 foot at + A2 = 55 Hz, an 18 ms period): shorter than one cycle and the crossfade is + itself a click, longer than about 80 ms and the chord change smears into + the next beat, which at 132 BPM is only 455 ms away. */ +#define ORG_XF_RATE (1.0f / (0.045f * (float)SRATE)) + +static float g_kickAmp, g_kickPhase, g_kickFreq, g_kickClick; +static float g_kickLast, g_kickTail, g_kickAtk; +static float g_snrAmp, g_snrPhase, g_snrHp; +static float g_hatAmp, g_hatHp; +/* A closed hat must not replace the crash's still-audible decay. */ +static float g_crashAmp, g_crashHp; +static unsigned int g_noise = 0x2545f491u; + +#define ECHOLEN (SPR * 2) /* exactly two rows: the echo lands on the grid */ +static float g_echoL[ECHOLEN], g_echoR[ECHOLEN]; +static int g_echoPos = 0; + +/* comb bank reverb */ +/* A hall, built the Schroeder way: a pre-delay, four damped combs, then two + allpasses to smear the combs' resonances into something continuous. + + What was here before was four BARE combs - no damping, no diffusion, no + pre-delay - which measured an RT60 of 0.82 to 1.69 s that was identical at + 80 Hz and at 8 kHz, with mode spacings of 35.7, 24.4, 16.6 and 13.0 Hz. + That is not a room. That is four tuned pipes, and those four resonances + were audible as a hollow ring under everything sustained. */ +#define RVN 4 +static const int g_rvLen[2][RVN] = { {1237,1811,2657,3391}, {1289,1867,2713,3457} }; +static const float g_rvFb[RVN] = { 0.79f, 0.77f, 0.75f, 0.73f }; +static float g_rvBuf[2][RVN][3457]; +static int g_rvPos[2][RVN]; + +/* One lowpass inside each comb's feedback path. This single coefficient is + the whole difference between a room and a pipe: the top of the tail has to + die faster than the bottom, because air and stone absorb high frequencies. + 0.26 is a ~2.0 kHz one-pole - dark stone, which is what a cathedral is. */ +#define RV_DAMP 0.26f +static float g_rvDamp[2][RVN]; + +/* Schroeder allpasses. Flat magnitude, scrambled phase: they turn four + discrete resonances into a continuous tail without colouring it. Prime + lengths, and different on each side, which is what makes the tail stereo + without anything being panned. */ +#define APN 2 +static const int g_apLen[2][APN] = { {225, 341}, {241, 359} }; +static float g_apBuf[2][APN][359]; +static int g_apPos[2][APN]; +#define AP_G 0.5f + +/* 34 ms between the dry signal and the reverb input. Without it the room + starts on top of the transient and every hit sounds distant; with it the + attack is dry and defined and the hall arrives behind it. Of everything in + this block, this is the number that does the most. */ +#define RV_PREDELAY 1500 +static float g_rvPre[2][RV_PREDELAY]; +static int g_rvPrePos; + +static float g_vol = 1.0f; +static float g_fadeIn = 0.0f; +static float g_duck = 1.0f; +static float g_compEnv = 0.0f, g_drmEnv = 0.0f; +static float g_riserLp = 0.0f; +static int g_rsOrder = 0, g_rsRow = 0, g_rsTick = 0; /* pre-advance clock */ +static float g_tapeL = 0.0f, g_tapeR = 0.0f; +static float g_masterDcL = 0.0f, g_masterDcR = 0.0f; + +static int g_playOrder = 0, g_playRow = 0, g_playTick = 0; +static int g_sampToTick = 0; +static int g_leadRest = 0, g_bassRest = 0; /* rows since this lane last played */ +static float g_secGain = 1.0f; /* the section intensity budget */ + +/* -------------------------------------------------------------------------- + T H E S Y N C R I N G + -------------------------------------------------------------------------- + What the music is doing right now, so the picture can answer it. + + We synthesise the music ourselves, so there is no need for an FFT or an + envelope follower on the output: the synth already knows exactly when the + kick fired and what the guitar's envelope is. The only hard part is + LATENCY. Audio is generated four buffers ahead of playback - 46 to 186 ms, + up to eleven frames - so reading g_kickAmp straight from the render loop + would fire the picture visibly early. + + The fix is to write a ring INDEXED BY ABSOLUTE SAMPLE and read it at the + same sample index the edit clock already uses. waveOutGetPosition is + already the authority for which row is on screen; asking it once per frame + for both the row and the sync read makes picture, edit and music agree by + construction, and the demo never has to know what the driver's latency is. + + Two details make it exact rather than approximate. The writer PEAK-HOLDS + over each grain instead of point sampling, because a kick's click decays + with a 5 ms time constant and point sampling every 256 samples would drop + it entirely on most frames. And the reader drains the whole span since its + last read, because 60 fps advances 2.87 grains and sampling one would skip + two of every three. + + audioPump runs on the main thread (CALLBACK_NULL), so the ring is written + earlier in the same frame than it is read: no thread, no atomics, no tear. */ +#define SYNC_GRAIN 256 /* 5.8 ms */ +#define SYNC_FRAMES 128 /* 743 ms of history: 4x the worst lead */ +#define SYNC_MASK (SYNC_FRAMES - 1) + +typedef struct { /* eight channels, all 0..1 */ + float kick, snare, hat; + float gtr, solo, org; + float bass, level; +} SyncFrame; + +static SyncFrame g_sync[SYNC_FRAMES]; +static SyncFrame g_syncAcc; +static int g_syncFill = 0; +static __int64 g_syncW = 0; /* grains generated */ +static __int64 g_syncR = 0; /* next grain to consume */ + +/* Offline probe: the loudest the lead and bass VCAs reach inside each pattern. + A lane with no notes in a pattern should read zero here; before the gate + existed it read whatever the last note had decayed to, for seconds. */ +static float g_probeLead[SNG_ORDERS], g_probeBass[SNG_ORDERS]; +static float g_probeVoice[6][SNG_ORDERS]; /* pad arp fm gsnr pump chorus */ + +static float noiseSample(void) +{ + g_noise ^= g_noise << 13; g_noise ^= g_noise >> 17; g_noise ^= g_noise << 5; + return (float)(int)(g_noise >> 9) * (1.0f / 4194304.0f) - 1.0f; +} + +/* A note now has an end. Before this there was no note-off at all: `active` + was set once and never cleared, and the last note of a phrase rang on + through every pattern that had no note on its lane - several seconds of + drone under music that had moved on. */ +static void trigVoice(Voice *v, int note, float decay, float sustain, float duty) +{ + v->inc = noteFreq(note) / (float)SRATE; + v->amp = 1.0f; v->decay = decay; v->sustain = sustain; + v->duty = duty; v->vib = 0.0f; v->active = 1; + v->gate = 1; + if (v->env < 0.0f) v->env = 0.0f; +} + +/* The release is LINEAR on purpose. At 70 ms that is a clean stop with no + click, where an exponential release of the same audible length takes about + three times as long to actually reach zero - which is the drone coming + back in through the door it was thrown out of. */ +static void voiceEnv(Voice *v) +{ + if (v->gate) { + v->env += v->atk; + if (v->env > 1.0f) v->env = 1.0f; + } else { + v->env -= v->rel; + if (v->env <= 0.0f) { v->env = 0.0f; v->active = 0; } + } +} + +static void gtrTrig(int note, int hold) +{ + float f = noteFreq(note) / (float)SRATE; + g_gtr.inc = f; + g_gtr.amp = 1.0f; + g_gtr.decay = hold ? 0.999975f : 0.99984f; + g_gtr.on = 1; +} + +static void solTrig(int which, int note) +{ + float f = noteFreq(note) / (float)SRATE; + if (which) { g_hsol.inc = f; g_hsol.amp = 1.0f; g_hsol.decay = 0.99985f; g_hsol.on = 1; } + else { g_sol.inc = f; g_sol.amp = 1.0f; g_sol.decay = 0.99985f; g_sol.on = 1; } +} + +static void kickTrig(void) +{ + /* A sixteenth-note retrigger finds the old kick still at 55% amplitude. + Blend from its outgoing sample with a 1 ms time constant, preserving + the grid and the noise attack without abruptly discarding the tail. */ + g_kickTail = g_kickLast; + g_kickAtk = 0.0f; + g_kickAmp = 1.0f; g_kickFreq = 165.0f; + g_kickPhase = 0.0f; g_kickClick = 1.0f; +} + +static void orgSet(int mode, int root, int type) +{ + static const int minorOff[3] = { 0, 3, 7 }; + static const int majorOff[3] = { 0, 4, 7 }; + static const int pedalOff[3] = { 0, 7, 12 }; + int i, b, base, n0, n1, n2; + const int *off; + if (!mode) { g_org.tgt = 0.0f; g_org.mode = 0; return; } + base = 24 + (mode == 2 ? 12 : 0) + root; + off = (mode == 1) ? pedalOff : (type ? majorOff : minorOff); + n0 = base + off[0]; n1 = base + off[1]; n2 = base + off[2]; + g_org.tgt = (mode == 1) ? 1.0f : 1.35f; + g_org.bars = (mode == 1) ? 3 : ORG_BARS; + if (g_org.mode == mode && g_org.note[0] == n0 && + g_org.note[1] == n1 && g_org.note[2] == n2) return; + g_org.mode = mode; + g_org.note[0] = n0; g_org.note[1] = n1; g_org.note[2] = n2; + g_org.bank ^= 1; + g_org.xf = 0.0f; + for (i = 0; i < 3; i++) { + float f = noteFreq(g_org.note[i]) / (float)SRATE; + for (b = 0; b < ORG_BARS; b++) { + g_org.inc[g_org.bank][i][b][0] = f * g_orgMul[b]; + g_org.inc[g_org.bank][i][b][1] = f * g_orgMul[b] * 1.0022f; + /* Both twins start together on every chord, exactly as the + reference render does. The twin is only 0.22 percent sharp, so + its beat period is many seconds: starting it a third of a cycle + late left the 16 foot and 8 foot drawbars near antiphase for as + long as a chord was held, and that cancelled the pedal. Letting + them free-run instead just drifts into the same cancellation. */ + /* seeded from the outgoing bank, so the waveform does not step */ + g_org.ph[g_org.bank][i][b][0] = g_org.ph[g_org.bank ^ 1][i][b][0]; + g_org.ph[g_org.bank][i][b][1] = g_org.ph[g_org.bank ^ 1][i][b][1]; + } + } +} + +/* These two are leaf helpers with no dependencies, and they sit here + rather than further down because every voice below needs them. */ +static float pulseWave(Voice *v) +{ + float s = (v->phase < v->duty) ? 1.0f : -1.0f; + v->phase += v->inc; + if (v->phase >= 1.0f) v->phase -= 1.0f; + return s; +} + +static float sawAt(float *ph, float inc) +{ + float s = *ph * 2.0f - 1.0f; + *ph += inc; + if (*ph >= 1.0f) *ph -= 1.0f; + return s; +} + +#include "voices.h" /* pad, gated snare, sidechain, FM, chorus, arp */ + +/* Which patterns the synthwave voices play in. This was a hardcoded range + while the arrangement was still v10; it is a lane in the baked data now, + like every other musical decision in this program. */ +static int isSynthwave(int o) { return g_sqSyn[o] != 0; } + +/* The quiet sections, where a bell has room: the cold open, the void, and + the outro. Section indices come from the same bake. */ +static int isQuiet(int o) +{ + int k = g_sqSect[o]; + return k == 0 || k == 7 || k == 10; +} + +static void songTick(void) +{ + static const int minorOff[3] = { 0, 3, 7 }; + static const int majorOff[3] = { 0, 4, 7 }; + int o = g_playOrder, r = g_playRow, k = o * SNG_ROWS + r; + int ci = r >> 3; + + g_rsOrder = o; g_rsRow = r; g_rsTick = g_playTick; + if (r == 0 && g_playTick == 0) g_riserLp = 0.0f; + int root = g_sqRoot[o * 4 + ci], typ = g_sqType[o * 4 + ci]; + + if (g_playTick == 0) { + int n, drum; + /* The note-off comes from the arrangement without needing a new + table: a lane that has been resting for long enough releases. Two + rows is an eighth note at 132 BPM, the shortest rest that should + actually sound like one; the bass gets four, because a bass line + wants to ring through sixteenth-note gaps. */ + n = g_sqLead[k]; + if (n >= 0) { trigVoice(&g_vLead, n, 0.999975f, 0.55f, 0.5f); g_leadRest = 0; } + else if (++g_leadRest >= 2) g_vLead.gate = 0; + + /* Two basses, with a brief release tail at the handoff. The pumping bass + REPLACES the plain one through the synthwave stretch rather than + sitting on top of it - two voices in the same octave holding the + same note is not a thicker bass, it is a comb filter. The kick keys + the duck either way. */ + n = g_sqBass[k]; + if (isSynthwave(o)) { + g_vBass.gate = 0; + if (n >= 0) pumpBassNote(n); + else if (++g_bassRest >= 4) pumpBassRelease(); + if (n >= 0) g_bassRest = 0; + } else { + pumpBassRelease(); + if (n >= 0) { trigVoice(&g_vBass, n, 0.99992f, 0.45f, 0.5f); g_bassRest = 0; } + else if (++g_bassRest >= 4) g_vBass.gate = 0; + } + n = g_sqGtr[k]; if (n >= 0) gtrTrig(n, g_sqHold[k]); + n = g_sqSolo[k]; + if (n >= 0) { + solTrig(0, n); + if (g_sqHarm[k] >= 0) solTrig(1, g_sqHarm[k]); + } + drum = g_sqDrum[k]; + /* Combined downbeats retain their kick and sidechain trigger. */ + if (drum == 6 || drum == 7) { + kickTrig(); + pumpBassKick(); + drum = (drum == 6) ? 5 : 2; + } + switch (drum) { + case 1: kickTrig(); + pumpBassKick(); /* the sidechain is keyed off the kick + itself, not off a level detector */ + break; + case 2: g_snrAmp = 0.85f; g_snrPhase = 0.0f; + gsnrTrig(0.85f); /* and the gate off the snare */ + break; + case 3: g_hatAmp = 0.30f; break; + case 4: g_hatAmp = 0.55f; break; + case 5: g_crashAmp = 0.95f; break; + default: break; + } + orgSet(g_sqOrgMode[o], root, typ); + + /* The pad holds the chord, so it is set from the same root and type + the organ gets, and only in the synthwave stretch. */ + padSet(isSynthwave(o), root, typ); + chorusSet(isSynthwave(o) ? 0.85f : 0.0f); + + /* A bell on the first beat of a pattern where nothing else is + carrying a line. Sparse is the whole character of the voice: it is + an accent, and one every three and a half seconds is already a lot. */ + if (r == 0 && isQuiet(o) && (o & 1) == 0) + bellTrig(48 + root, 0.55f); + } + + /* The polysynth arpeggio runs on the same grid as the chip arp but an + eighth apart, and only in the synthwave stretch. It is a different + voice, not a louder version of the old one: detuned saws through a + filter that gets plucked on every note. */ + if (isSynthwave(o) && g_playTick == 0 && (r & 1) == 0) { + static const int aOff[4] = { 0, 3, 7, 12 }; + static const int aMaj[4] = { 0, 4, 7, 12 }; + int st = (r >> 1) & 3; + arpTrig(36 + root + (typ ? aMaj[st] : aOff[st]), (r & 7) == 0 ? 1.0f : 0.62f); + /* No hard-coded section offset: C's negative remainder pushed the + relocated synthwave sweep below its intended range. */ + arpSetSweep(0.35f + 0.55f * (float)(o & 3) * 0.25f); + } + + if (g_sqArpOct[o] >= 0) { + int step = (g_playTick + r * TPR) % 3; + int off = typ ? majorOff[step] : minorOff[step]; + int note = g_sqArpOct[o] + root + off; + /* An Amiga arpeggio changes pitch every tick; it does not restrike. + Retriggering here was firing about 53 note attacks a second, which + is why the chiptune voice swamped everything else. */ + if (g_playTick == 0) trigVoice(&g_vArp, note, 0.99975f, 0.0f, 0.35f); + else g_vArp.inc = noteFreq(note) / (float)SRATE; + } + + if (++g_playTick >= TPR) { + g_playTick = 0; + if (++g_playRow >= SNG_ROWS) { + g_playRow = 0; + if (++g_playOrder >= SNG_ORDERS) g_playOrder = 0; + } + } +} + + + +/* Attack and release, once. 1.5 ms on the lead is short enough to still read + as a chip pulse and long enough not to click. The bass is slower on both + counts: a bass transient that fast just sounds like a click sitting on top + of the kick. The arp's own decay ends its notes, so its gate stays open. */ +static int g_synthReady = 0; +static void synthInit(void) +{ + g_vLead.atk = 1.0f / (0.0015f * (float)SRATE); + g_vLead.rel = 1.0f / (0.070f * (float)SRATE); + g_vBass.atk = 1.0f / (0.0040f * (float)SRATE); + g_vBass.rel = 1.0f / (0.110f * (float)SRATE); + g_vArp.atk = 1.0f / (0.0010f * (float)SRATE); + g_vArp.rel = 1.0f / (0.040f * (float)SRATE); + g_vArp.gate = 1; + g_synthReady = 1; +} + +/* Absolute sample positions: the last exported sample is exactly silent. */ +static float songEndGain(__int64 sample) +{ + const __int64 total = (__int64)SNG_ORDERS * SNG_ROWS * SPR; + const int fade = SRATE * 9 / 5; /* 1.8 seconds */ + float t; + if (sample >= total - 1) return 0.0f; + if (sample <= total - fade) return 1.0f; + t = (float)(total - 1 - sample) / (float)(fade - 1); + return t * t * (3.0f - 2.0f * t); +} + +static void renderAudio(short *out, int frames) +{ + int i; + if (!g_synthReady) synthInit(); + while (frames > 0) { + int n; + /* g_rsTick is the tick songTick() just fired, captured before it + advanced its counters, so this is that tick's own length. */ + if (g_sampToTick <= 0) { songTick(); g_sampToTick = TICKLEN(g_rsTick); } + n = frames < g_sampToTick ? frames : g_sampToTick; + g_sampToTick -= n; + frames -= n; + + for (i = 0; i < n; i++) { + float l = 0.0f, r = 0.0f, sendL = 0.0f, sendR = 0.0f; + float dl = 0.0f, dr = 0.0f, rvL = 0.0f, rvR = 0.0f, s; + float busy = 0.0f; + + /* --- lead ------------------------------------------------- */ + if (g_vLead.active) { + s = pulseWave(&g_vLead); /* no vibrato: the reference lead + is a plain pulse, and the wobble + was what made it dominate */ + g_vLead.amp = (g_vLead.amp - g_vLead.sustain) * g_vLead.decay + g_vLead.sustain; + voiceEnv(&g_vLead); + if (g_vLead.amp * g_vLead.env > 0.12f) busy = 0.55f; + /* Only the second half of the pattern. A note that holds + across the bar line and then releases is correct, so + measuring from row 0 would count that as a drone. By row 16 + the two-row rest threshold and the 70 ms release are both + long finished. */ + if (g_rsRow >= 16 && g_vLead.env > g_probeLead[g_rsOrder]) + g_probeLead[g_rsOrder] = g_vLead.env; + s *= g_vLead.amp * g_vLead.env * 0.20f; + l += s * 0.62f; r += s * 0.38f; + sendL += s * 0.45f; sendR += s * 0.30f; + } + /* --- solo and its third ----------------------------------- */ + if (g_sol.on && g_sol.amp > 0.0004f) { + float w, sat, vib; + g_sol.vib += 5.4f / (float)SRATE; + vib = 1.0f + lsin(g_sol.vib * TAU) * 0.010f; + w = (sawAt(&g_sol.p1, g_sol.inc) + + sawAt(&g_sol.p2, g_sol.inc * 1.004f)) * 0.5f; + sat = softClip(w * vib * 9.0f); /* drive, not pitch */ + g_sol.lp += (sat - g_sol.lp) * 0.66f; + g_sol.amp *= g_sol.decay; + g_sol.level += (g_sol.amp - g_sol.level) * 0.0227f; + if (g_sol.amp > 0.12f) busy = 0.72f; + s = g_sol.lp * g_sol.level * 0.30f; + l += s * 1.06f; r += s * 0.94f; + sendL += s * 0.34f; sendR += s * 0.26f; + } + if (g_hsol.on && g_hsol.amp > 0.0004f) { + float w = (sawAt(&g_hsol.p1, g_hsol.inc) + + sawAt(&g_hsol.p2, g_hsol.inc * 1.004f)) * 0.5f; + float sat = softClip(w * 8.0f); + g_hsol.lp += (sat - g_hsol.lp) * 0.60f; + g_hsol.amp *= g_hsol.decay; + g_hsol.level += (g_hsol.amp - g_hsol.level) * 0.0227f; + s = g_hsol.lp * g_hsol.level * 0.20f; + l += s * 0.80f; r += s * 1.20f; + } + g_duck += ((1.0f - busy) - g_duck) * 0.000517f; + + /* --- arp -------------------------------------------------- */ + if (g_vArp.active) { + s = pulseWave(&g_vArp); + g_vArp.lp += (s - g_vArp.lp) * 0.34f; + g_vArp.amp *= g_vArp.decay; + s = g_vArp.lp * g_vArp.amp * 0.10f * g_duck; + l += s * 0.34f; r += s * 0.66f; + sendL += s * 0.30f; sendR += s * 0.45f; + } + /* --- bass ------------------------------------------------- */ + if (g_vBass.active) { + float raw; + g_vBass.phase += g_vBass.inc; + if (g_vBass.phase >= 1.0f) g_vBass.phase -= 1.0f; + raw = g_vBass.phase * 2.0f - 1.0f; + raw = raw * 0.65f + ((g_vBass.phase < 0.5f) ? 0.35f : -0.35f); + g_vBass.lp += (raw - g_vBass.lp) * 0.22f; + g_vBass.amp = (g_vBass.amp - g_vBass.sustain) * g_vBass.decay + g_vBass.sustain; + voiceEnv(&g_vBass); + if (g_rsRow >= 16 && g_vBass.env > g_probeBass[g_rsOrder]) + g_probeBass[g_rsOrder] = g_vBass.env; + s = g_vBass.lp * g_vBass.amp * g_vBass.env * 0.30f; + l += s; r += s; + } + /* --- guitar, two takes ------------------------------------ */ + if (g_gtr.on && g_gtr.amp > 0.0004f) { + float a, b, sa, sb, w3, w4; + /* Both takes are power chords. The second one used to be the + octave pair alone, so the hard panned double track had + different harmonic content from the take beside it. */ + w3 = sawAt(&g_gtr.p3, g_gtr.inc * 1.4983f); + w4 = sawAt(&g_gtr.p4, g_gtr.inc * 1.5065f); + a = (sawAt(&g_gtr.p1, g_gtr.inc) + + sawAt(&g_gtr.p2, g_gtr.inc * 1.0055f)) * 0.5f + + (w3 + w4) * 0.30f; + b = (sawAt(&g_gtr.q1, g_gtr.inc * 0.9962f) + + sawAt(&g_gtr.q2, g_gtr.inc * 1.0091f)) * 0.5f + + (w3 + w4) * 0.30f; + sa = softClip(a * 7.5f); + sb = softClip(b * 7.5f); + g_gtr.lp += (sa - g_gtr.lp) * 0.52f; + g_gtr.lp2 += (sb - g_gtr.lp2) * 0.49f; + g_gtr.hp += (g_gtr.lp - g_gtr.hp) * 0.019f; + g_gtr.hp2 += (g_gtr.lp2 - g_gtr.hp2) * 0.019f; + g_gtr.amp *= g_gtr.decay; + { + float ga = (g_gtr.lp - g_gtr.hp) * g_gtr.amp * 0.34f; + float gb = (g_gtr.lp2 - g_gtr.hp2) * g_gtr.amp * 0.34f; + l += ga * 1.00f + gb * 0.16f; + r += ga * 0.16f + gb * 1.00f; + } + } + /* --- organ ------------------------------------------------ */ + g_org.amp += (g_org.tgt - g_org.amp) * 0.000209f; + if (g_org.xf < 1.0f) { + g_org.xf += ORG_XF_RATE; + if (g_org.xf > 1.0f) g_org.xf = 1.0f; + } + if (g_org.amp > 0.001f) { + float lowN = 0.0f, upN = 0.0f; /* the live bank */ + float lowO = 0.0f, upO = 0.0f; /* the outgoing one */ + int ni, b, bk = g_org.bank; + + for (ni = 0; ni < 3; ni++) + for (b = 0; b < g_org.bars; b++) { + float *p0 = &g_org.ph[bk][ni][b][0]; + float *p1 = &g_org.ph[bk][ni][b][1]; + float v = (lsin(*p0 * TAU) + lsin(*p1 * TAU)) + * (g_orgAmp[b] * 0.5f); + if (b < 2) lowN += v; else upN += v; + *p0 += g_org.inc[bk][ni][b][0]; if (*p0 >= 1.0f) *p0 -= 1.0f; + *p1 += g_org.inc[bk][ni][b][1]; if (*p1 >= 1.0f) *p1 -= 1.0f; + } + + /* The outgoing bank only costs anything during the 45 ms of a + chord change, which is a tenth of a percent of the demo. */ + if (g_org.xf < 1.0f) { + int ob = bk ^ 1; + for (ni = 0; ni < 3; ni++) + for (b = 0; b < g_org.bars; b++) { + float *p0 = &g_org.ph[ob][ni][b][0]; + float *p1 = &g_org.ph[ob][ni][b][1]; + float v = (lsin(*p0 * TAU) + lsin(*p1 * TAU)) + * (g_orgAmp[b] * 0.5f); + if (b < 2) lowO += v; else upO += v; + *p0 += g_org.inc[ob][ni][b][0]; if (*p0 >= 1.0f) *p0 -= 1.0f; + *p1 += g_org.inc[ob][ni][b][1]; if (*p1 >= 1.0f) *p1 -= 1.0f; + } + } + { + float g = g_org.amp * (g_org.mode == 1 ? 0.030f : 0.042f) + * (0.45f + 0.55f * g_duck); + float xf = g_org.xf; + float lo = (lowN * xf + lowO * (1.0f - xf)) * g; + float up = (upN * xf + upO * (1.0f - xf)) * g; + s = lo + up; + /* The 16 and 8 foot drawbars are the pedal, and they sit dead + centre. A level difference on a 55 Hz partial buys no + audible width and spends real limiter headroom on nothing, + so only 5 1/3 foot and above are allowed off centre. */ + l += lo + up * 0.92f; + r += lo + up * 1.08f; + } + sendL += s * 0.26f; sendR += s * 0.26f; + rvL += s * 0.85f; rvR += s * 0.85f; + } + /* --- drums onto their own bus ----------------------------- */ + if (g_kickAmp > 0.0005f) { + g_kickPhase += g_kickFreq / (float)SRATE; + if (g_kickPhase >= 1.0f) g_kickPhase -= 1.0f; + s = lsin(g_kickPhase * TAU) * g_kickAmp * 0.55f; + if (g_kickClick > 0.002f) { + s += noiseSample() * g_kickClick * 0.22f; + g_kickClick *= 0.9955f; + } + g_kickFreq = 46.0f + (g_kickFreq - 46.0f) * 0.99955f; + g_kickAmp *= 0.99988f; + g_kickAtk += (1.0f - g_kickAtk) * 0.0227f; + s = g_kickTail * (1.0f - g_kickAtk) + s * g_kickAtk; + g_kickLast = s; + dl += s; dr += s; + } else g_kickLast = 0.0f; + if (g_snrAmp > 0.0005f) { + float nz = noiseSample(); + g_snrHp += (nz - g_snrHp) * 0.45f; + g_snrPhase += 187.0f / (float)SRATE; + if (g_snrPhase >= 1.0f) g_snrPhase -= 1.0f; + s = ((nz - g_snrHp * 0.6f) * 0.75f + lsin(g_snrPhase * TAU) * 0.35f) + * g_snrAmp * 0.30f; + g_snrAmp *= 0.99977f; + dl += s * 1.05f; dr += s * 0.95f; + /* No discrete two-row snare echo over the written fills. */ + rvL += s * 0.30f; rvR += s * 0.30f; + } + if (g_hatAmp > 0.0005f) { + float nz = noiseSample(); + g_hatHp += (nz - g_hatHp) * 0.70f; + s = (nz - g_hatHp) * g_hatAmp * 0.28f; + g_hatAmp *= 0.9988f; + dl += s * 0.85f; dr += s * 1.15f; + rvL += s * 0.5f; rvR += s * 0.5f; + } + + if (g_crashAmp > 0.0005f) { + float nz = noiseSample(); + g_crashHp += (nz - g_crashHp) * 0.70f; + s = (nz - g_crashHp) * g_crashAmp * 0.28f; + g_crashAmp *= 0.99975f; + dl += s * 0.85f; dr += s * 1.15f; + rvL += s * 0.5f; rvR += s * 0.5f; + } + + /* --- parallel drum bus ------------------------------------ */ + { + float pk = (dl > 0.0f ? dl : -dl); + float pr = (dr > 0.0f ? dr : -dr); + float m = pk > pr ? pk : pr; + float g; + g_drmEnv = (m > g_drmEnv) ? m : g_drmEnv + (m - g_drmEnv) * 0.00082f; + g = (g_drmEnv > 0.045f) ? 0.045f / g_drmEnv : 1.0f; + l += dl * (1.0f + g * 3.2f * 0.34f); + r += dr * (1.0f + g * 3.2f * 0.34f); + } + + /* --- riser into the next section -------------------------- */ + if (g_sqRiser[g_rsOrder]) { + /* Filtered noise only. This used to carry a sine sweeping + 200 -> 2600 Hz whose t^4 swell put its peak exactly on the + section change: an audible whistle every 3.6 seconds. + The counters are the ones captured before songTick advanced + them, so t no longer snaps back a tick early at the bar. */ + float t = ((float)g_rsRow * (float)TPR + (float)g_rsTick) + / (float)(SNG_ROWS * TPR - 1); + float amt = t * t * t * t; + float nz = noiseSample(); + g_riserLp += (nz - g_riserLp) * (0.02f + 0.55f * t * t); + s = (nz - g_riserLp) * 0.62f * amt * 0.62f; + l += s; r += s; + } + + /* --- the synthwave voices --------------------------------- */ + /* Probed one at a time: the peak each voice adds, per pattern. + A voice that is wired up but silent looks exactly like a voice + that is working, right up until you measure it. */ + { + float b0 = l, b1 = r, d; + int pv = g_rsOrder; + #define VPROBE(idx, call) b0 = l; b1 = r; call; d = (l - b0); if (d < 0.0f) d = -d; { float e = (r - b1); if (e < 0.0f) e = -e; if (e > d) d = e; } if (d > g_probeVoice[idx][pv]) g_probeVoice[idx][pv] = d; + VPROBE(0, padRender(&l, &r)) + VPROBE(1, arpRender(&l, &r)) + VPROBE(2, fmRender(&l, &r)) + VPROBE(3, gsnrRender(&l, &r)) + VPROBE(4, renderPumpBass(&l, &r)) + /* The chorus is a SEND. It was wired up and rendering silence + because nothing ever fed it - the pad already computes the + send level it wants, so use that. */ + chorusFeed(g_padSendL + g_padSendR); + VPROBE(5, chorusRender(&l, &r)) + #undef VPROBE + } + + /* --- echo, locked to two rows ----------------------------- */ + { + float eL = g_echoL[g_echoPos], eR = g_echoR[g_echoPos]; + l += eR * 0.42f; + r += eL * 0.42f; + g_echoL[g_echoPos] = sendL + eL * 0.28f; + g_echoR[g_echoPos] = sendR + eR * 0.28f; + if (++g_echoPos >= ECHOLEN) g_echoPos = 0; + } + /* --- reverb ----------------------------------------------- */ + { + int t; + float accL = 0.0f, accR = 0.0f, inL, inR; + + /* pre-delay */ + inL = g_rvPre[0][g_rvPrePos]; + inR = g_rvPre[1][g_rvPrePos]; + g_rvPre[0][g_rvPrePos] = rvL; + g_rvPre[1][g_rvPrePos] = rvR; + if (++g_rvPrePos >= RV_PREDELAY) g_rvPrePos = 0; + + /* four damped combs in parallel */ + for (t = 0; t < RVN; t++) { + float *bl = &g_rvBuf[0][t][0], *br = &g_rvBuf[1][t][0]; + int pl = g_rvPos[0][t], pr2 = g_rvPos[1][t]; + float yl = bl[pl], yr = br[pr2]; + g_rvDamp[0][t] += (yl - g_rvDamp[0][t]) * RV_DAMP; + g_rvDamp[1][t] += (yr - g_rvDamp[1][t]) * RV_DAMP; + bl[pl] = inL + g_rvDamp[0][t] * g_rvFb[t]; + br[pr2] = inR + g_rvDamp[1][t] * g_rvFb[t]; + accL += yl; accR += yr; + if (++g_rvPos[0][t] >= g_rvLen[0][t]) g_rvPos[0][t] = 0; + if (++g_rvPos[1][t] >= g_rvLen[1][t]) g_rvPos[1][t] = 0; + } + accL *= 1.0f / (float)RVN; + accR *= 1.0f / (float)RVN; + + /* two allpasses in series, per channel */ + for (t = 0; t < APN; t++) { + float *al = &g_apBuf[0][t][0], *ar = &g_apBuf[1][t][0]; + int pl = g_apPos[0][t], pr2 = g_apPos[1][t]; + float dl = al[pl], dr = ar[pr2]; + al[pl] = accL + dl * AP_G; + ar[pr2] = accR + dr * AP_G; + accL = dl - accL * AP_G; + accR = dr - accR * AP_G; + if (++g_apPos[0][t] >= g_apLen[0][t]) g_apPos[0][t] = 0; + if (++g_apPos[1][t] >= g_apLen[1][t]) g_apPos[1][t] = 0; + } + + l += accL * 0.30f; + r += accR * 0.30f; + } + + /* THE INTENSITY BUDGET. Each section is allowed a loudness, and + it is applied here - before the bus compressor, so the + compressor and the limiter cannot simply undo it, which is + exactly what happened when the sections differed only in how + many voices were playing. This is the mechanism that lets the + climax be the climax without adding a single layer to it: the + chorus actually has more voices than the arrival does. */ + { + float want = (float)g_sqGain[g_rsOrder] * (1.0f / 1024.0f); + g_secGain += (want - g_secGain) * 0.00006f; /* ~0.4 s glide */ + l *= g_secGain; r *= g_secGain; + } + + /* The reference fades before the bus compressor, so the intro + never engages it and the makeup lifts the quiet organ back up. + Fading after the limiter instead made the first 3 s far too + quiet. */ + g_fadeIn += 1.0f / (float)(SRATE * 3); + if (g_fadeIn > 1.0f) g_fadeIn = 1.0f; + l *= g_fadeIn * g_fadeIn; + r *= g_fadeIn * g_fadeIn; + + /* --- bus compressor, tape, limiter ------------------------ */ + { + float pk = (l > 0.0f ? l : -l), pr = (r > 0.0f ? r : -r); + float m = pk > pr ? pk : pr, g = 1.0f; + /* A real 3:1 knee. The old approximation floored at 0.33, so + the loudest transients got far less reduction than the + reference; the attack was also instantaneous. */ + g_compEnv += (m - g_compEnv) * (m > g_compEnv ? 0.0017f : 0.00007f); + if (g_compEnv > 0.30f) g = invCbrt2(g_compEnv * (1.0f / 0.30f)); + l *= g; r *= g; + } + { + /* tape then limiter, in that order, matching the reference + render in tools/music_render.py */ + float fv = g_vol; + float pa = l * 2.6f, pb = r * 2.6f; + float ta = softClip(pa * 0.9f) + 0.04f * softClip(pa * pa * 0.5f); + float tb = softClip(pb * 0.9f) + 0.04f * softClip(pb * pb * 0.5f); + float sa = 0.72f * ta + 0.28f * g_tapeL; + float sb = 0.72f * tb + 0.28f * g_tapeR; + g_tapeL = ta; g_tapeR = tb; + sa = softClip(sa * 1.15f) * 0.94f; /* the reference drive, exactly */ + sb = softClip(sb * 1.15f) * 0.94f; + /* Tape's even-order term introduces positive DC. Remove it + after the nonlinear stages and before the end fade. + 10 Hz one-pole: <0.21 dB loss at the 46 Hz kick floor. */ + g_masterDcL += (sa - g_masterDcL) * 0.001424f; + g_masterDcR += (sb - g_masterDcR) * 0.001424f; + sa -= g_masterDcL; sb -= g_masterDcR; + /* g_genSamples advances after each n-sample chunk; i is the + current sample within it, not within the caller's buffer. */ + { + float endGain = songEndGain(g_genSamples + i); + sa *= endGain; + sb *= endGain; + } + *out++ = (short)(sa * fv * 32700.0f); + *out++ = (short)(sb * fv * 32700.0f); + + /* The sync grain. Here at the bottom of the sample loop + because the master peak only exists at this point and every + instrument's envelope is still live. + + Hat and crash share the existing visual sync channel; + peak-hold either envelope while their audio tails remain + independent. */ + { + float pa = sa > 0.0f ? sa : -sa; + float pb = sb > 0.0f ? sb : -sb; + #define SPK(f, v) { float t_ = (v); \ + if (t_ > g_syncAcc.f) g_syncAcc.f = t_; } + SPK(kick, g_kickAmp) + SPK(snare, g_snrAmp * (1.0f / 0.85f)) + SPK(hat, (g_hatAmp > g_crashAmp ? g_hatAmp : g_crashAmp) * (1.0f / 0.95f)) + SPK(gtr, g_gtr.on ? g_gtr.amp : 0.0f) + SPK(solo, g_sol.on ? g_sol.amp : 0.0f) + SPK(org, g_org.amp * (1.0f / 1.35f)) + SPK(bass, g_vBass.amp * g_vBass.env) + SPK(level, pa > pb ? pa : pb) + #undef SPK + if (++g_syncFill >= SYNC_GRAIN) { + g_sync[g_syncW & SYNC_MASK] = g_syncAcc; + g_syncW++; + g_syncFill = 0; + memset(&g_syncAcc, 0, sizeof(g_syncAcc)); + } + } + } + } + g_genSamples += n; + } +} + +static int g_audioNext = 0; + +static void audioCleanup(void) +{ + int i, pending = 0; + g_audioOk = 0; + if (!g_wo) return; + /* Keep ownership if a driver refuses cleanup; a later call can retry. */ + if (waveOutReset(g_wo) != MMSYSERR_NOERROR) return; + for (i = 0; i < NBUF; i++) { + if ((g_whdr[i].dwFlags & WHDR_PREPARED) && + waveOutUnprepareHeader(g_wo, &g_whdr[i], sizeof(WAVEHDR)) + != MMSYSERR_NOERROR) pending = 1; + } + if (!pending && waveOutClose(g_wo) == MMSYSERR_NOERROR) g_wo = 0; +} + +static void audioInit(void) +{ + WAVEFORMATEX wf; + int i; + HWAVEOUT opened = 0; + audioCleanup(); + if (g_wo) return; + g_audioNext = 0; + memset(&wf, 0, sizeof(wf)); + wf.wFormatTag = WAVE_FORMAT_PCM; + wf.nChannels = 2; + wf.nSamplesPerSec = SRATE; + wf.wBitsPerSample = 16; + wf.nBlockAlign = 4; + wf.nAvgBytesPerSec = SRATE * 4; + if (waveOutOpen(&opened, WAVE_MAPPER, &wf, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR) + return; + g_wo = opened; + /* Start only after the complete initial queue has been submitted. */ + if (waveOutPause(g_wo) != MMSYSERR_NOERROR) { audioCleanup(); return; } + for (i = 0; i < NBUF; i++) { + memset(&g_whdr[i], 0, sizeof(WAVEHDR)); + g_whdr[i].lpData = (LPSTR)g_wbuf[i]; + g_whdr[i].dwBufferLength = BUFFRAMES * 4; + if (waveOutPrepareHeader(g_wo, &g_whdr[i], sizeof(WAVEHDR)) + != MMSYSERR_NOERROR) { audioCleanup(); return; } + renderAudio(g_wbuf[i], BUFFRAMES); + if (waveOutWrite(g_wo, &g_whdr[i], sizeof(WAVEHDR)) + != MMSYSERR_NOERROR) { audioCleanup(); return; } + } + if (waveOutRestart(g_wo) != MMSYSERR_NOERROR) { audioCleanup(); return; } + g_audioOk = 1; +} + +static void audioPump(void) +{ + int count; + if (!g_audioOk) return; + for (count = 0; count < NBUF; count++) { + int i = g_audioNext; + if (!(g_whdr[i].dwFlags & WHDR_DONE)) break; + renderAudio(g_wbuf[i], BUFFRAMES); + if (waveOutWrite(g_wo, &g_whdr[i], sizeof(WAVEHDR)) + != MMSYSERR_NOERROR) { audioCleanup(); return; } + g_audioNext = (i + 1) % NBUF; + } +} + +static __int64 audioPlayedSamples(void) +{ + MMTIME mt; + if (!g_audioOk) return -1; + mt.wType = TIME_SAMPLES; + if (waveOutGetPosition(g_wo, &mt, sizeof(mt)) != MMSYSERR_NOERROR) return -1; + if (mt.wType != TIME_SAMPLES) return -1; + return (__int64)mt.u.sample; +} + +#endif /* CRTK_SYNTH_H */ diff --git a/tardis_mesh.h b/tardis_mesh.h new file mode 100644 index 0000000..1653f85 --- /dev/null +++ b/tardis_mesh.h @@ -0,0 +1,505 @@ +/* -------------------------------------------------------------------------- + baked mesh: "TARDIS" by Andrewsha, CC-BY-4.0 + https://sketchfab.com/3d-models/tardis-576b10bd7fb64af987c4f59e3ba475cd + ~100k triangles decimated offline by vertex clustering to the table below; + nothing is parsed at runtime. + -------------------------------------------------------------------------- */ +#define TAR_NV 454 +#define TAR_NT 912 +#define TAR_QS (1.0f / 16384.0f) +static const short g_tarV[] = { + -7088,-12342,7421,-6977,-11788,6523,-6812,9146,4679,-494,6249,-6263, + -355,9168,-6718,-7026,-11867,-259,-6351,-10343,-378,-371,9166,6702, + -6402,-10343,-4867,-6402,-5910,-4867,-5011,9088,6705,-6323,-5910,4614, + -6323,-1555,4614,4826,9103,-6711,-6323,-10343,4614,-6787,9122,-297, + -5029,9089,-6723,-6160,3826,1574,-6184,6249,-1995,-6171,3841,-1995, + 6530,9084,1537,-6153,-5910,-3388,-6153,-1555,-3388,-6402,-1555,-4867, + -6663,9029,-1995,-6836,9152,-4890,-6153,-10343,-3388,-6346,6249,-411, + -6351,3789,-378,-6402,6249,-4867,-6402,3789,-4867,-6171,3841,-3388, + -6375,-566,-309,-6429,-589,-4914,-6122,-654,-3388,-6369,-1555,-320, + -6351,-5910,-378,-6174,6249,1561,-6465,3020,-4975,-7017,-11836,-4943, + -6153,-5910,-1995,-6153,-1555,-1995,-6122,-654,-1995,-6384,3020,-295, + -6679,9054,1525,-6323,6249,4614,-6084,3020,1404,-6392,3020,4819, + -6326,3789,4621,-6153,-5910,1545,-6153,-1555,1545,-6153,-10343,1545, + -7020,-11943,1523,-6133,-640,1504,-6350,-606,4694,-6904,-11813,4692, + 7339,-12314,7421,7339,-12282,6248,6145,-10343,4638,6491,-11527,4716, + 6457,-11527,-206,6175,-10343,-378,6624,9170,-278,6212,-1156,-479, + 6175,-1555,-378,6227,6249,-4868,6227,3789,-4868,6170,3789,-411, + 6170,6249,-411,6484,9021,-1995,5995,3841,-1995,6254,-589,-4914, + 6227,-1555,-4868,6227,-5910,-4868,6175,-5910,-378,6227,-10343,-4868, + 6110,1960,-474,5977,-10343,1544,6453,-11563,1561,5977,-5910,1544, + 6142,-5884,4653,6145,-1555,4638,5977,-1555,1544,5988,3819,1554, + 5860,2369,1319,6004,6249,1572,6134,3799,4591,6126,6249,4577, + 6642,9143,4665,5901,-566,1561,6000,-345,4551,1488,-5910,-6077, + 1488,-1555,-6077,1488,-10343,-6077,1444,-638,-6057,-3467,-5910,-6071, + -4947,-5910,-6320,-4947,-1555,-6320,-3467,-1555,-6071,-3467,-10343,-6071, + -4947,-10343,-6320,-2077,9019,-6578,6388,-11809,-6938,7339,-12314,-6846, + -494,-10343,-6263,-494,-5910,-6263,-494,-1555,-6263,-378,3020,-6301, + -494,3789,-6263,-5056,3020,-6382,-4947,3789,-6320,-4947,6249,-6320, + -2077,3841,-6088,-442,-585,-6280,-328,-11833,-6907,-5038,-11788,-6820, + -4994,-589,-6347,-3467,-654,-6040,-3467,3841,-6088,1560,6249,-6098, + 1539,9029,-6583,4787,6253,-6343,1341,3020,-6009,1509,3807,-6083, + 4880,3020,-6382,4772,3789,-6320,4772,-5910,-6320,4772,-1555,-6320, + 4772,-10343,-6320,3291,9019,-6578,1491,-11933,-6932,4819,-589,-6347, + 4848,-11836,-6935,3291,3841,-6088,3291,-5910,6060,4772,-5910,6309, + 4772,-1555,6309,3291,-1555,6060,3291,-10343,6060,4772,-10343,6309, + 4802,-581,6319,3291,-601,6010,-336,-11513,6540,-493,-10343,6253, + 1494,-11560,6489,1488,-10343,6066,1488,-5910,6066,-493,-5910,6253, + 1488,-1555,6066,-493,-1555,6253,1481,-599,6029,-442,-585,6269, + 4837,3020,6332,4772,3789,6309,1560,6249,6087,1527,3824,6077, + 1539,9029,6572,4772,6249,6309,4826,9103,6700,3291,3841,6077, + 4880,-11502,6576,-378,3020,6290,1470,3020,5974,-493,3789,6253, + -3472,-1555,6060,-4950,-1555,6309,-4950,-5910,6309,-3472,-5910,6060, + -4950,-10343,6309,-3472,-10343,6060,-5086,3020,6422,-4980,3789,6353, + -4980,6249,6353,-3470,9019,6567,-493,6249,6253,-3471,3841,6078, + -5012,-581,6361,-5066,-11520,6537,-3472,-654,6029,-2077,6249,6091, + -2077,9019,6567,-3473,6252,6088,-2079,3841,6077,-3471,-11542,6526, + 3287,9031,6585,3291,6249,6091,3291,6249,-6102,-3467,9019,-6578, + -3460,-11846,-6856,-3467,6249,-6101,-2077,6249,-6101,-6184,6249,-3388, + -7037,-11902,-3388,-6663,9029,-3388,-6029,3020,-3388,-6663,9029,3378, + -6153,3850,3378,-6184,6249,3378,5995,3841,3378,6535,9074,3386, + 6484,9021,-3388,5995,3841,-3388,5977,-5910,-3388,5977,-10343,-3388, + 6659,9148,-4926,5977,-1555,-3388,5946,-654,-3388,6008,6249,3378, + 6008,6249,-1995,6008,6249,-3388,6443,-11542,-3388,6493,-11502,-4975, + -6664,10947,-6541,-4957,10907,-6948,-4959,10908,6936,-3638,10389,7092, + 7037,10352,-232,7008,10411,3455,6992,10461,1702,-7176,10530,1408, + 4288,10603,-7046,3359,10375,-7118,6472,10948,-6544,-3171,10344,-7111, + 1342,10500,7089,-200,10352,-7134,-7190,10375,-1979,-7214,10350,6, + -7198,10320,2865,-6788,-10343,-6706,-7080,-12035,-6839,-6788,-10343,6695, + -6665,9294,6595,-6699,10952,6482,6613,6249,6695,6613,3789,6695, + 6556,9376,6545,6612,-10343,-6706,6613,-10343,6695,6530,-11487,6612, + 6856,10887,-4896,6166,-12314,7421,7339,-12314,-4898,5917,2677,4502, + 6289,3020,-4975,6508,10953,6483,6476,9321,-6621,6876,10872,4589, + -6678,9388,-6614,-7173,10423,-3613,7024,10459,-1648,-7042,10907,4618, + -7044,10861,-4840,-1975,10366,7095,5792,3020,3377,6613,3020,6695, + 6613,-566,6695,7339,-12314,-1995,7339,-12337,-266,-7020,-11925,-2001, + 7339,-12314,1470,7339,-12282,4621,6443,-11542,-1995,4802,-12314,7421, + 3291,-11902,-6954,1488,-12364,7421,-316,-12305,7421,-2077,-11950,-6919, + -3467,-12314,7421,-4977,-12393,7422,-2077,-12393,7421,-2080,-11571,6469, + 3298,-12350,7421,3297,-11556,6499,6613,-1555,6695,6613,-5910,6695, + 6488,6257,-6795,6612,3789,-6706,6612,-566,-6706,6612,-1555,-6706, + 6612,-5910,-6706,5856,2150,-3039,-6908,-11870,3378,6443,-11542,3378, + 7339,-12282,3378,-6029,3020,3378,-6153,-5910,3378,-6153,-1555,3378, + -6122,-654,3378,-6153,-10343,3378,5977,-1555,3378,5885,-566,3378, + 5977,-10343,3378,5977,-5910,3378,5856,2213,-2211,5977,-10343,-1995, + 5977,-5910,-1995,5977,-1555,-1995,5946,-654,-1995,-6029,3020,-1995, + -6153,-10343,-1995,7339,-12314,-3388,6612,3020,-6706,3291,-654,-6040, + 3291,3020,-5947,3291,-1555,-6071,3291,-10343,-6071,3291,-5910,-6071, + -2077,-654,-6040,-2077,3020,-5947,-3467,3020,-5947,-6788,-5910,-6706, + -6788,-1555,-6706,-6788,-566,-6706,-6788,6249,-6706,-6788,3789,-6706, + -6788,3020,-6706,-2077,-10343,-6071,-2077,-1555,-6071,-2077,-5910,-6071, + -2081,-10343,6060,-2081,-654,6029,-2081,-1555,6060,-2081,-5910,6060, + 3291,3020,5936,-3472,3020,5936,-2081,3020,5936,-6588,3764,6803, + -6557,6249,6811,-6623,3020,6794,-6664,-552,6784,-6788,-1555,6695, + -6788,-5910,6695,1749,10423,-7101,-1866,10399,-7130,7025,10316,-3040, + 4661,10857,6911,2850,10309,7114,27,10353,7117,5792,12280,1394, + 4566,12265,5891,5751,12313,-1681,5714,12343,-77,6158,11979,-6251, + 5683,12424,-4976,-3469,11979,-6251,-1881,11979,-6251,2957,11979,-6251, + 5722,12337,3161,-159,12343,5796,-1764,12313,5833,-3251,12313,5833, + -349,11979,-6251,-6334,11979,-3390,-6334,11979,-5091,5792,12280,4679, + -6334,11979,4823,5731,12380,5813,-5173,11979,-6251,5751,12313,-3169, + -6334,11979,3318,1368,11979,-6251,4653,11979,-6251,-6334,11979,1368, + -6334,11979,-1798,1460,12265,5891,-6333,11979,6240,-6334,11979,-6251, + -6334,11979,-266,2961,12355,5782,-5166,12401,5758,-5723,12480,-3059, + -5723,12480,3140,4500,12480,-5641,2962,12480,-5641,-3138,12480,-5641, + -5723,12480,-1623,-5723,12480,1412,-5723,12551,4583,-5723,12480,-5, + -1705,12480,-5641,-5413,12498,-5175,-88,12480,-5641,1528,12480,-5641, + -128,13687,897,-121,13579,-950,-1032,13541,-950,821,13669,-20, + -1032,13576,-4,821,13579,889,798,13558,-950,-1032,13541,876, + -141,13973,-37,4802,-12950,-4897,4802,-12950,-1995,4802,-12950,-295, + 4802,-12950,1519,4802,-12950,4621,3291,-12950,4621,3291,-12950,-295, + 3291,-12950,1519,3291,-12950,-1995,3291,-12950,-4897,-377,-12950,-4897, + 1527,-12950,-4897,-377,-12950,-1995,1528,-12950,-1995,1528,-12950,-295, + -377,-12950,-295,1528,-12950,1519,-377,-12950,1519,-377,-12950,4621, + 1528,-12950,4621,-2077,-12950,4621,-2077,-12950,-295,-2077,-12950,1520, + -2077,-12950,-1995,-2077,-12950,-4897,-4977,-12950,-4897,-4977,-12950,-1995, + -4977,-12950,-295,-4977,-12950,1520,-4977,-12950,4621,-4977,-12950,3378, + -2077,-12950,3378,-377,-12950,3378,1528,-12950,3378,3292,-12950,3378, + 4802,-12950,3378,4802,-12950,-3388,3291,-12950,-3388,-377,-12950,-3388, + 1528,-12950,-3388,-2077,-12950,-3388,-4977,-12950,-3388,-3467,-12950,4621, + -3467,-12950,-295,-3467,-12950,1520,-3467,-12950,-1995,-3467,-12950,-4897, + -3467,-12950,3378,-3467,-12950,-3388,5881,1253,1060,6109,811,-476, + -134,15806,-52,812,15640,-166,675,15646,757,-88,15646,-1035, + -1117,15646,-5,-248,15640,895,-88,16384,-5,5857,1011,-3067, + 5857,1081,-2191,5874,184,4477, +}; +static const signed char g_tarN[] = { + -13,89,89,-99,-79,3,-126,-18,-2,-2,-13,-126, + 0,-29,-124,-112,-59,-3,-126,13,0,1,-28,124, + -124,14,23,-125,0,24,-4,-20,125,-125,0,-24, + -125,0,-24,6,-15,-126,-124,17,-20,-124,-28,2, + 3,-15,-126,-127,-3,8,-126,-14,-5,-127,-3,-9, + 123,-31,2,-127,0,11,-127,1,7,-125,0,23, + -123,-29,-6,-126,-16,-1,-125,18,13,-126,-11,-3, + -127,1,3,-126,-6,16,-125,-1,25,-126,-6,12, + -127,1,-2,-125,-1,24,-125,2,20,-127,0,1, + -127,0,0,-126,-10,6,-124,1,27,-111,-61,8, + -127,0,-8,-126,0,-11,-127,3,-7,-127,-2,-2, + -124,-28,3,-126,-8,-14,-126,-2,13,-125,1,-21, + -126,0,-17,-127,0,7,-127,0,9,-126,13,5, + -122,-36,4,-127,2,8,-125,1,-21,-86,-88,30, + 51,103,54,-75,-102,14,124,7,-24,111,-61,-6, + 114,-55,2,127,8,4,124,-26,0,127,3,0, + 127,0,-3,126,-9,12,125,2,22,127,-1,0, + 127,-11,0,124,-29,-3,127,-4,-10,125,-1,24, + 125,0,24,125,0,24,127,0,0,125,3,24, + 127,-2,0,127,9,5,115,-53,0,127,0,7, + 125,0,-24,125,2,-23,127,1,9,127,-4,7, + 126,-3,12,126,-14,5,124,-6,-25,126,-9,-15, + 126,-16,5,127,4,7,123,4,-31,6,0,-127, + 6,0,-127,6,22,-125,9,2,-127,11,0,-127, + 24,0,-125,24,0,-125,9,0,-127,12,15,-126, + 20,10,-125,-5,-28,-124,31,-115,-45,88,90,-19, + 0,12,-126,0,0,-127,0,0,-127,-1,-1,-127, + 1,-1,-127,27,0,-124,23,1,-125,21,-9,-125, + -8,-6,-127,-1,1,-127,2,-56,-114,5,-59,-113, + 25,-1,-125,18,2,-126,12,-3,-126,6,-12,-126, + 5,-29,-124,-18,-7,-125,10,0,-127,5,-4,-127, + -28,-1,-124,-26,-1,-124,-24,0,-125,-24,0,-125, + -22,11,-125,-5,-26,-124,-2,-52,-116,-26,0,-124, + 24,-71,-102,-14,-4,-126,-9,0,127,-24,0,125, + -24,0,125,-7,1,127,-11,10,126,-21,8,125, + -27,1,124,-6,3,127,-2,-77,101,3,10,127, + 2,-53,116,5,7,127,6,0,127,0,0,127, + 8,1,127,-3,0,127,9,2,127,5,0,127, + -27,-1,124,-22,0,125,7,-10,126,5,-4,127, + 3,-27,124,-21,-10,125,-1,-8,127,-11,-5,126, + -2,-77,101,-5,0,127,11,0,127,1,-1,127, + 13,1,126,25,-2,125,24,0,125,11,0,127, + 24,3,125,7,9,126,33,-2,123,31,1,123, + 20,-6,125,8,-29,123,-2,-13,126,14,-3,126, + 31,-1,123,-6,-68,107,11,3,126,-6,-10,126, + -5,-28,124,9,-14,126,-8,-6,127,0,-62,111, + -4,-26,124,-8,-11,126,-8,-10,-126,8,-29,-123, + -5,-52,-116,8,-14,-126,-6,-10,-126,-126,-12,9, + -115,-55,3,-124,-26,2,-126,0,14,-123,-33,-5, + -127,-5,-4,-126,-14,-7,127,-8,-5,124,-26,-2, + 123,-30,9,127,-3,9,127,0,11,126,9,8, + 125,-21,-5,126,1,13,126,1,19,127,-10,-4, + 126,-10,-7,126,-14,8,116,-53,1,111,-61,-5, + -108,22,-63,-14,18,-125,-12,39,120,1,29,124, + 123,32,0,124,30,1,124,29,0,-126,15,1, + 11,20,-125,1,13,-126,56,22,-112,-1,17,-126, + 2,26,124,0,18,-126,-126,15,-2,-126,17,1, + -126,17,0,-64,11,-109,-50,-71,-93,-117,3,50, + -75,-6,102,-76,38,94,102,-2,75,82,-5,96, + 116,-3,53,88,12,-91,67,3,108,76,-21,99, + 123,29,-9,9,-98,-80,41,-86,84,124,-2,-27, + 124,1,26,95,31,78,96,-8,-83,122,34,8, + -103,-4,-74,-126,15,-3,124,28,-1,-125,18,11, + -125,20,-10,0,29,124,127,-3,-2,122,1,37, + 80,2,98,95,84,-6,-74,-55,87,-114,-56,1, + -94,71,-48,104,73,-3,114,-56,-3,53,-86,77, + -2,-55,-114,-111,60,-17,122,27,22,-4,-56,-114, + 21,-41,-118,79,-82,57,-49,80,86,-1,-66,109, + -31,-110,-55,-3,-62,111,118,0,48,98,0,80, + 90,2,-89,90,3,-90,90,-1,-90,90,0,-90, + 90,0,-90,126,0,14,-115,-54,2,116,-52,-3, + -76,-101,-8,-127,0,-8,-127,0,-7,-127,0,-6, + -126,2,-14,-125,18,-8,127,2,-4,127,6,-7, + 126,12,-8,127,0,-7,126,-1,-12,126,10,-10, + 127,0,-8,127,0,-7,127,4,-8,-126,-2,-15, + -126,18,-6,-110,-51,-39,90,-1,-90,-6,3,-127, + -15,-2,-126,-13,0,-126,-6,18,-126,-9,0,-127, + -8,3,-127,-16,-1,-126,13,-1,-126,-90,0,-89, + -113,0,-57,-63,-1,-110,-94,-3,-85,-62,1,-111, + -113,0,-58,-6,17,-126,-10,1,-127,-8,0,-127, + -9,9,126,-14,2,126,-7,0,127,-8,0,127, + -16,-2,126,21,-3,125,-11,0,127,-115,2,53, + -97,-6,82,-67,1,108,-114,0,56,-67,0,108, + -98,0,80,1,15,-126,-1,15,-126,123,32,0, + 11,25,124,2,27,124,-2,32,123,67,108,0, + 0,113,58,66,109,0,69,107,0,70,92,-51, + 52,116,-4,-3,80,-99,0,73,-104,-1,75,-103, + 63,110,-2,0,107,69,0,107,68,3,110,64, + 0,73,-104,-102,76,2,-95,85,-6,56,114,3, + -94,85,1,27,116,44,3,85,-94,67,108,1, + -106,70,2,0,73,-104,5,72,-105,-101,77,-1, + -104,73,0,-1,109,65,-88,85,33,-47,89,-78, + -107,69,0,1,108,67,-10,117,47,-37,121,-2, + -45,119,-2,8,123,-30,0,120,-40,-4,121,-37, + -40,121,0,-37,122,0,-28,123,9,-43,120,0, + 0,121,-40,-18,124,-21,0,121,-40,0,121,-40, + 1,123,32,-1,124,-28,-15,125,-14,32,123,0, + -29,124,-2,17,125,17,12,125,-19,-18,125,15, + -5,127,-2,49,-112,-34,54,-115,0,52,-116,0, + 51,-116,0,34,-117,35,0,-116,51,0,-127,0, + 0,-127,0,0,-127,0,0,-122,-36,2,-122,-37, + -1,-122,-36,0,-127,0,0,-127,0,0,-127,0, + 0,-127,0,0,-127,0,0,-127,0,0,-116,53, + 0,-116,51,0,-115,53,0,-127,0,0,-127,0, + 0,-127,0,-2,-122,-35,-22,-123,-25,-34,-122,0, + -36,-122,1,-35,-122,-1,-34,-112,49,-39,-121,2, + 0,-127,0,0,-127,0,0,-127,0,0,-127,0, + 49,-117,0,51,-116,0,0,-127,0,0,-127,0, + 0,-127,0,0,-127,0,-37,-121,-1,0,-116,51, + 0,-127,0,0,-127,0,0,-127,0,-1,-121,-40, + 0,-127,0,0,-127,0,0,0,0,0,0,0, + -12,-126,-12,-22,-125,-3,-12,-126,-12,0,0,0, + 0,0,0,-3,-125,-22,0,0,0,127,0,0, + 127,0,0,0,0,0, +}; +static const unsigned short g_tarI[] = { + 21,22,23,21,23,9,26,21,9,26,9,8, + 41,35,32,41,32,42,6,5,52,6,52,51, + 6,51,49,6,49,36,36,49,50,36,50,35, + 35,50,53,35,53,32,43,46,17,43,17,28, + 15,27,37,15,37,44,77,78,60,77,60,61, + 79,77,61,79,61,74,76,67,83,76,83,84, + 89,82,64,89,64,63,82,79,74,82,74,64, + 95,96,97,95,97,98,99,100,96,99,96,95, + 98,97,116,98,116,117,93,130,114,93,114,104, + 91,93,104,91,104,105,92,91,105,92,105,106, + 94,92,106,94,106,113,119,123,108,119,108,3, + 134,135,136,134,136,137,138,139,135,138,135,134, + 137,136,140,137,140,141,144,145,143,144,143,142, + 143,145,146,143,146,147,147,146,148,147,148,149, + 149,148,150,149,150,151,161,162,155,161,155,163, + 164,165,166,164,166,167,167,166,168,167,168,169, + 178,176,165,178,165,164,169,168,177,169,177,183, + 99,188,115,99,115,100,192,26,8,192,8,39, + 194,31,30,194,30,38,202,203,75,202,75,73, + 205,202,73,205,73,72,206,205,72,206,72,71, + 203,210,211,203,211,75,229,230,39,229,39,8, + 237,75,211,237,211,102,238,239,59,238,59,58, + 237,102,132,237,132,128,231,1,177,231,177,168, + 57,239,56,241,56,239,85,68,62,85,62,20, + 255,256,90,255,90,243,103,102,211,103,211,242, + 258,60,78,258,78,260,257,262,60,257,60,258, + 0,269,177,0,177,1,270,266,142,270,142,271, + 266,265,144,266,144,142,272,263,160,272,160,273, + 234,235,86,234,86,87,256,274,81,256,81,90, + 274,275,80,274,80,81,275,238,58,275,58,80, + 276,277,125,276,125,121,276,65,66,276,66,277, + 278,279,127,278,127,131,278,71,72,278,72,279, + 279,280,126,279,126,127,279,72,73,279,73,280, + 280,237,128,280,128,126,280,73,75,280,75,237, + 281,244,66,281,66,201,235,255,243,235,243,86, + 284,283,59,284,59,261,285,47,48,285,48,196, + 287,12,54,287,54,288,289,14,11,289,11,286, + 286,11,12,286,12,287,81,290,291,81,291,90, + 14,289,282,14,282,55,80,58,292,80,292,293, + 81,80,293,81,293,290,58,59,283,58,283,292, + 86,243,254,86,254,198,260,78,283,260,283,284, + 46,285,196,46,196,17,50,287,288,50,288,53, + 51,289,286,51,286,49,49,286,287,49,287,50, + 290,82,89,290,89,291,289,51,52,289,52,282, + 293,292,77,293,77,79,290,293,79,290,79,82, + 292,283,78,292,78,77,198,254,84,198,84,83, + 76,294,70,76,70,67,61,60,262,61,262,295, + 63,64,297,63,297,298,64,74,296,64,296,297, + 74,61,295,74,295,296,43,28,19,43,19,299, + 5,6,300,5,300,259,6,36,40,6,40,300, + 36,35,41,36,41,40,22,41,42,22,42,34, + 301,210,262,301,262,257,294,281,201,294,201,70, + 295,262,210,295,210,203,298,297,205,298,205,206, + 297,296,202,297,202,205,296,295,203,296,203,202, + 299,19,31,299,31,194,259,300,26,259,26,192, + 300,40,21,300,21,26,40,41,22,40,22,21, + 23,22,34,23,34,33,242,211,210,242,210,301, + 67,68,85,67,85,83,302,244,71,302,71,278, + 302,278,131,302,131,124,277,302,124,277,124,125, + 277,66,244,277,244,302,298,206,281,298,281,294, + 63,298,294,63,294,76,63,76,84,63,84,89, + 246,204,65,246,65,276,246,276,121,246,121,13, + 123,122,107,123,107,108,122,94,113,122,113,107, + 131,303,304,131,304,124,206,71,244,206,244,281, + 124,304,133,124,133,125,139,138,273,139,273,160, + 128,132,264,128,264,306,131,127,305,131,305,303, + 128,306,307,128,307,126,126,307,305,126,305,127, + 146,134,137,146,137,148,145,138,134,145,134,146, + 148,137,141,148,141,150,265,272,273,265,273,144, + 307,91,92,307,92,305,306,93,91,306,91,307, + 303,305,92,303,92,94,306,264,130,306,130,93, + 138,145,144,138,144,273,304,122,123,304,123,133, + 303,94,122,303,122,304,309,107,113,309,113,308, + 118,310,109,118,109,110,311,229,8,311,8,9, + 312,311,9,312,9,23,313,312,23,313,23,33, + 314,315,30,314,30,29,315,316,38,315,38,30, + 316,313,33,316,33,38,108,107,309,108,309,112, + 104,114,267,104,267,317,143,320,271,143,271,142, + 151,321,322,151,322,149,147,323,320,147,320,143, + 149,322,323,149,323,147,106,318,308,106,308,113, + 104,317,319,104,319,105,105,319,318,105,318,106, + 268,270,271,268,271,183,310,309,308,310,308,117, + 112,309,310,112,310,118,317,267,188,317,188,99, + 320,169,183,320,183,271,321,178,164,321,164,322, + 323,167,169,323,169,320,322,164,167,322,167,323, + 318,98,117,318,117,308,317,99,95,317,95,319, + 319,95,98,319,98,318,269,268,183,269,183,177, + 109,310,117,109,117,116,120,119,3,120,3,4, + 261,59,239,261,239,57,14,55,1,14,1,231, + 155,162,324,155,324,159,159,324,152,159,152,153, + 151,150,162,151,162,161,182,175,325,182,325,326, + 163,182,326,163,326,161,175,171,170,175,170,325, + 53,288,285,53,285,46,288,54,47,288,47,285, + 32,53,46,32,46,43,42,299,194,42,194,34, + 32,43,299,32,299,42,162,150,141,162,141,324, + 324,141,140,324,140,152,326,325,178,326,178,321, + 161,326,321,161,321,151,325,170,176,325,176,178, + 34,194,38,34,38,33,45,48,327,45,327,328, + 328,327,171,328,171,172,48,47,329,48,329,327, + 327,329,170,327,170,171,47,54,330,47,330,329, + 329,330,176,329,176,170,54,12,331,54,331,330, + 330,331,165,330,165,176,12,11,332,12,332,331, + 331,332,166,331,166,165,11,14,231,11,231,332, + 332,231,168,332,168,166,156,7,174,156,174,154, + 163,155,154,163,154,174,236,234,87,236,87,88, + 232,328,172,232,172,10,2,45,328,2,328,232, + 37,27,28,37,28,17,248,314,29,248,29,25, + 158,157,234,158,234,236,153,152,255,153,255,235, + 152,140,256,152,256,255,135,139,238,135,238,275, + 136,135,275,136,275,274,140,136,274,140,274,256, + 263,241,239,263,239,160,139,160,239,139,239,238, + 157,153,235,157,235,234,16,111,314,16,314,248, + 109,116,313,109,313,316,110,109,316,110,316,315, + 111,110,315,111,315,314,116,97,312,116,312,313, + 97,96,311,97,311,312,96,100,229,96,229,311, + 100,115,230,100,230,229,216,250,341,216,341,342, + 240,222,343,240,343,344,334,223,345,334,345,346, + 217,218,339,217,339,348,253,338,349,253,349,350, + 215,253,350,215,350,351,252,249,353,252,353,354, + 247,217,348,247,348,355,336,245,357,336,357,340, + 250,335,359,250,359,341,221,333,361,221,361,347, + 222,220,362,222,362,343,249,226,364,249,364,353, + 251,233,366,251,366,356,212,252,354,212,354,367, + 226,227,368,226,368,364,245,247,355,245,355,357, + 224,337,369,224,369,365,338,224,365,338,365,349, + 333,225,352,333,352,361,225,334,346,225,346,352, + 214,215,351,214,351,370,218,216,342,218,342,339, + 228,251,356,228,356,360,220,221,347,220,347,362, + 219,228,360,219,360,363,233,214,370,233,370,366, + 335,240,344,335,344,359,337,336,340,337,340,369, + 223,213,358,223,358,345,213,212,367,213,367,358, + 227,219,363,227,363,368,360,356,372,362,347,374, + 363,360,372,363,372,377,363,377,379,345,358,381, + 345,381,375,358,367,381,361,382,383,368,363,379, + 346,345,375,346,375,380,354,353,371,354,371,381, + 356,378,372,347,361,383,347,383,374,343,362,373, + 343,373,344,353,364,376,353,376,371,356,366,370, + 356,370,378,367,354,381,364,368,379,364,379,376, + 362,374,373,361,352,382,352,346,380,352,380,382, + 375,381,385,349,365,384,381,386,385,383,382,385, + 355,348,387,340,389,384,342,341,387,344,390,387, + 380,375,385,348,339,387,350,349,384,351,350,384, + 339,342,387,381,371,388,372,378,388,340,357,389, + 341,359,387,374,383,385,344,373,390,371,376,388, + 378,370,391,381,388,386,376,379,388,357,355,389, + 369,340,384,355,387,389,373,374,385,365,369,384, + 382,380,385,370,351,384,373,385,390,377,372,388, + 370,384,391,378,391,388,359,344,387,379,377,388, + 387,390,385,387,385,392,389,387,392,389,392,384, + 391,384,392,391,392,388,388,392,385,388,385,386, + 62,68,208,62,208,69,69,208,209,69,209,200, + 200,209,65,200,65,204,201,209,208,201,208,70, + 67,70,208,67,208,68,199,207,85,199,85,20, + 87,86,198,87,198,207,83,85,207,83,207,198, + 133,186,121,133,121,125,186,133,123,186,123,119, + 129,186,119,129,119,120,121,186,129,121,129,13, + 112,190,3,112,3,108,3,190,101,3,101,4, + 101,190,189,101,189,187,111,16,187,111,187,189, + 118,110,111,118,111,189,118,189,190,118,190,112, + 65,209,201,65,201,66,29,30,31,29,31,191, + 193,191,18,193,18,24,24,18,27,24,27,15, + 19,18,191,19,191,31,27,18,19,27,19,28, + 44,37,197,44,197,195,196,197,37,196,37,17, + 48,45,197,48,197,196,2,195,197,2,197,45, + 175,181,172,175,172,171,172,181,173,172,173,10, + 180,173,181,180,181,179,175,182,179,175,179,181, + 182,163,174,182,174,179,174,7,180,174,180,179, + 156,154,185,156,185,184,155,159,185,155,185,154, + 153,157,185,153,185,159,157,158,184,157,184,185, + 25,29,191,25,191,193,199,88,87,199,87,207, + 243,90,291,243,291,254,254,291,89,254,89,84, + 88,247,245,88,245,236,25,252,212,25,212,248, + 158,236,245,158,245,336,213,16,248,213,248,212, + 102,103,242,102,242,211,262,257,258,262,258,60, + 60,258,260,60,260,78,273,160,263,273,263,272, + 399,395,396,399,396,400,401,394,395,401,395,399, + 264,132,393,264,393,402,114,130,404,114,404,403, + 405,406,407,405,407,408,408,407,409,408,409,410, + 142,144,265,142,265,266,271,142,266,271,266,270, + 414,408,410,414,410,415,416,405,408,416,408,414, + 267,114,403,267,403,417,1,177,269,1,269,0, + 5,420,421,5,421,52,259,419,420,259,420,5, + 230,115,418,230,418,39,282,423,422,282,422,55, + 424,425,411,424,411,413,425,426,412,425,412,411, + 427,428,397,427,397,398,283,284,261,283,261,59, + 52,421,423,52,423,282,415,410,425,415,425,424, + 410,409,426,410,426,425,400,396,428,400,428,427, + 78,260,284,78,284,283,210,301,257,210,257,262, + 430,429,394,430,394,401,431,432,406,431,406,405, + 433,431,405,433,405,416,192,434,419,192,419,259, + 211,242,301,211,301,210,402,393,429,402,429,430, + 403,404,432,403,432,431,417,403,431,417,431,433, + 39,418,434,39,434,192,144,273,272,144,272,265, + 407,399,400,407,400,409,406,401,399,406,399,407, + 130,264,402,130,402,404,426,427,398,426,398,412, + 409,400,427,409,427,426,432,430,401,432,401,406, + 404,402,430,404,430,432,183,271,270,183,270,268, + 436,414,415,436,415,437,438,416,414,438,414,436, + 188,267,417,188,417,439,440,424,413,440,413,435, + 437,415,424,437,424,440,441,433,416,441,416,438, + 439,417,433,439,433,441,177,183,268,177,268,269, + 420,436,437,420,437,421,419,438,436,419,436,420, + 115,188,439,115,439,418,423,440,435,423,435,422, + 421,437,440,421,440,423,434,441,438,434,438,419, + 418,439,441,418,441,434,422,435,183,422,183,177, + 435,413,271,435,271,183,412,398,273,412,273,144, + 55,422,177,55,177,1,413,411,142,413,142,271, + 411,412,144,411,144,142,398,397,160,398,160,273, + 59,261,57,59,57,239,397,59,239,397,239,160, + 429,210,262,429,262,394,396,78,283,396,283,428, + 428,283,59,428,59,397,395,60,78,395,78,396, + 394,262,60,394,60,395,132,102,211,132,211,393, + 160,239,241,160,241,263,393,211,210,393,210,429, + 222,246,13,222,13,220,220,13,129,220,129,221, + 221,129,120,221,120,333,333,120,4,333,4,225, + 225,4,101,225,101,334,334,101,187,334,187,223, + 223,187,16,223,16,213,246,222,240,246,240,204, + 199,217,247,199,247,88,204,240,335,204,335,200, + 200,335,250,200,250,69,69,250,216,69,216,62, + 62,216,218,62,218,20,20,218,217,20,217,199, + 232,10,214,232,214,233,10,173,215,10,215,214, + 173,180,253,173,253,215,184,158,336,184,336,337, + 156,184,337,156,337,224,7,156,224,7,224,338, + 180,7,338,180,338,253,232,233,251,232,251,2, + 193,249,252,193,252,25,24,226,249,24,249,193, + 2,251,228,2,228,195,15,227,226,15,226,24, + 195,228,219,195,219,44,44,219,227,44,227,15, + 444,445,446,444,446,449,294,452,451,281,294,451, +}; +static const unsigned char g_tarM[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,1, +}; +static const unsigned char g_tarPal[] = { + 26,54,108,214,224,236,255,238,190,26,26,32,198,168,62, +}; diff --git a/tools/arrange_v11.py b/tools/arrange_v11.py new file mode 100644 index 0000000..eee09f1 --- /dev/null +++ b/tools/arrange_v11.py @@ -0,0 +1,186 @@ +"""Bake 48 through-composed orders, without rendering or playback. + +5000 samples/row at 44100 Hz: 174.149659864 seconds, unchanged. +Section gains are compositional estimates, NOT calibrated mastering results. +Run python tools/arrange_v11.py to regenerate song_data.h. +""" +from pathlib import Path +import motif as MO + +HERE = Path(__file__).resolve().parent +ROWS, ORDERS, SPR, SRATE = 32, 48, 5000, 44100 +X = -1 +SECTIONS = [ + ("INTRO", 0, 1, -14), ("SYNTHWAVE", 2, 7, -14), + ("METAL VERSE", 8, 11, -8), ("CHORUS", 12, 15, -6), + ("SOLO", 16, 39, -10), ("ARRIVAL", 40, 45, -3), ("OUTRO", 46, 47, -14), +] +# Preserve synth.h's quiet-section ABI (IDs 0 and 10), not list indices. +SECTION_IDS = (0, 1, 4, 5, 8, 9, 10) +SOLO_PHASES = [ + ("spacious lead", 16, 19, -14), ("melodic guitar", 20, 24, -11), + ("driving shredding", 25, 30, -9), ("organ counterpoint", 31, 35, -12), + ("climactic burst", 36, 39, -6), +] +KEYCHANGE_ORDER, KEYCHANGE = 40, 2 + +# Four eight-row harmonic slots per order, in A natural minor degrees. +# Riffs use this global scale, not a new minor scale at each chord root. +PROGRESSION = [ + (0,0,4,4), (5,5,6,4), + (0,0,5,5), (2,2,6,6), (3,3,0,0), (5,5,4,4), (2,2,3,3), (6,6,4,4), + (0,0,6,4), (3,5,0,4), (5,6,3,4), (2,6,4,0), + (0,5,2,6), (3,0,5,4), (5,2,6,3), (3,5,4,4), + (0,0,5,5), (2,2,3,3), (5,5,6,6), (3,3,4,4), + (0,5,2,6), (3,0,4,4), (5,2,3,6), (2,6,0,5), (3,5,4,0), + (0,6,5,4), (3,0,2,4), (5,3,6,4), (2,5,0,6), (3,6,2,4), (5,6,4,0), + (0,2,3,5), (6,5,3,2), (3,0,5,2), (5,3,2,6), (3,5,6,4), + (0,3,5,6), (2,5,3,4), (5,6,0,4), (3,5,4,4), + (0,0,0,0), (5,5,2,2), (3,3,6,6), (2,5,3,4), (5,6,3,4), (3,4,0,0), + (5,3,0,0), (0,0,0,0), +] + +def section_of(o): + return next(i for i, (_, a, b, _) in enumerate(SECTIONS) if a <= o <= b) + +def kit(kind): + out = [0] * ROWS + if kind == "syn": + for r in range(0, ROWS, 4): out[r] = 1 + for r in (4,12,20,28): out[r] = 7 # kick + snare + for r in range(2, ROWS, 4): out[r] = 3 + elif kind == "ride": + for r in range(0, ROWS, 2): out[r] = 1 if r % 8 == 0 else 3 + elif kind == "half": + for r in (0,16): out[r] = 1 + for r in (8,24): out[r] = 2 + for r in (6,14,22,30): out[r] = 3 + elif kind in ("metal", "drive"): + for r in range(ROWS): out[r] = 3 if r % 2 == 0 else 0 + for r in (0,6,8,16,22,24): out[r] = 1 + if kind == "drive": + for r in (1,3,10,17,19,26): out[r] = 1 + for r in (4,12,20,28): out[r] = 2 + return out + +def order_spec(o): + # Organ mode 2 and riff guitar never coincide. The melodic guitar is + # g_sqSolo's existing guitar voice; no new voice or runtime API is needed. + d = dict(owner="organ", kit=None, bass=0, org=2, syn=0, riff=False) + if 2 <= o <= 7: + d.update(owner="synth arp", kit="syn", bass=1, org=0, syn=1) + elif 8 <= o <= 11: + d.update(owner="riff", kit="metal", bass=2, org=0, riff=True) + elif 12 <= o <= 15: + d.update(owner="lead", kit="drive", bass=1, org=0) + elif 16 <= o <= 19: + d.update(owner="solo", kit="ride", bass=1, org=0) + elif 20 <= o <= 24: + d.update(owner="solo", kit="metal", bass=1, org=0) + elif 25 <= o <= 30: + d.update(owner="solo", kit="drive", bass=2, org=0) + elif 31 <= o <= 35: + # Organ owns the midrange; sparse solo answers above the voicings. + d.update(owner="organ", kit="half", bass=1) + elif 36 <= o <= 39: + d.update(owner="solo", kit="drive", bass=2, org=0) + elif 40 <= o <= 45: + d.update(owner="solo", kit="metal" if o in (40,45) else "drive", bass=1, org=0) + elif o == 46: + d.update(kit="half", bass=1) + return d + +lead, solo, harm, bass, gtr = ([[X]*ROWS for _ in range(ORDERS)] for _ in range(5)) +hold, drum = ([[0]*ROWS for _ in range(ORDERS)] for _ in range(2)) +root, ctyp = ([[0]*4 for _ in range(ORDERS)] for _ in range(2)) +arpo = [X]*ORDERS +orgm, rise, trim, syn, sect = ([0]*ORDERS for _ in range(5)) +SOLO = {o: MO.extended_solo(o - 16) for o in range(16,40)} + +for o in range(ORDERS): + d, si = order_spec(o), section_of(o) + sect[o], trim[o] = SECTION_IDS[si], SECTIONS[si][3] + for _, a, b, t in SOLO_PHASES: + if a <= o <= b: trim[o] = t + orgm[o], syn[o] = d["org"], d["syn"] + shift = KEYCHANGE if o >= KEYCHANGE_ORDER else 0 + for ci, degree in enumerate(PROGRESSION[o]): + root[o][ci] = MO.deg(degree, 9 + shift) % 12 + ctyp[o][ci] = int(degree in (2,5,6)) + drum[o] = kit(d["kit"]) + if o in (2,8,12,20,25,36,40): drum[o][0] = 6 # kick + crash + if o in (7,11,15,24,30,35,38,45): + drum[o][28:32] = [2,0,2,4] if o % 2 else [2,1,2,2] + if d["syn"]: drum[o][28] = 7 # fill preserves four-on-the-floor + if 12 <= o <= 15: lead[o] = MO.lead_chorus(o - 12) + if o in SOLO: solo[o] = list(SOLO[o]) + if 40 <= o <= 45: + solo[o] = [n + shift if n >= 0 else X for n in MO.arrival_phrase(o - 40)] + # Upper thirds punctuate two burst phrases, not a second continuous line. + if o in (37,38): + for r in (0,8,16,24): + n = solo[o][r] + if n >= 0: harm[o][r] = MO.scale_step(n, 2) + if d["bass"]: + rhythm = (0,4,8,12,16,20,24,28) if d["bass"] == 1 else (0,2,5,8,11,14,16,18,21,24,27,30) + for r in rhythm: + bass[o][r] = MO.deg(PROGRESSION[o][r // 8], 21 + shift) + if d["riff"]: + for r, degree in enumerate(MO.verse_riff(o - 8)): + if degree is not None: + gtr[o][r] = MO.deg(PROGRESSION[o][r // 8] + degree, 21) + # The voice adds a fixed perfect fifth. B itself is in Am, + # but B5 adds F#: return to this chord's root (E here). + scale = {(9 + shift + n) % 12 for n in MO.MIN_IV} + if (gtr[o][r] + 7) % 12 not in scale: + gtr[o][r] = MO.deg(PROGRESSION[o][r // 8], 21 + shift) + bass[o][r] = MO.deg(PROGRESSION[o][r // 8], 21) + if o == 39: + # Twelve rows without triggers, then a four-note pickup. Envelopes + # decay rather than hold: this does not promise digital silence. + for lane, rest in ((solo,X),(bass,X),(drum,0)): + lane[o][16:28] = [rest]*12 + if o == 47: trim[o] = -22 + +for o in (7,15,35,39): rise[o] = 1 +gain = [round(1024 * 10 ** (t / 20)) for t in trim] +TABLES = { + "g_sqLead": ("signed char", lead), "g_sqSolo": ("signed char", solo), + "g_sqHarm": ("signed char", harm), "g_sqBass": ("signed char", bass), + "g_sqGtr": ("signed char", gtr), "g_sqHold": ("unsigned char", hold), + "g_sqDrum": ("unsigned char", drum), "g_sqRoot": ("signed char", root), + "g_sqType": ("unsigned char", ctyp), "g_sqArpOct": ("signed char", arpo), + "g_sqOrgMode": ("unsigned char", orgm), "g_sqRiser": ("unsigned char", rise), + "g_sqGain": ("short", gain), "g_sqSyn": ("unsigned char", syn), + "g_sqSect": ("unsigned char", sect), +} + +def flatten(rows): + return [v for row in rows for v in (row if isinstance(row,list) else [row])] + +def header(): + out = "/* Through-composed form: 48 orders / 96 bars / 174.149659864 seconds.\n" + out += " Notes use MIDI-12; X=-1 means no trigger. No audio mastering validation.\n" + out += " Drum API: 6 = kick + crash; 7 = kick + snare (synthwave backbeats).\n" + for i, (name,a,b,t) in enumerate(SECTIONS): + out += f" {name:12s} orders {a:2d}-{b:2d}; section ID {SECTION_IDS[i]}; proposed trim {t:+d} dB\n" + for name,a,b,t in SOLO_PHASES: + out += f" SOLO {name:18s} orders {a}-{b}; proposed trim {t:+d} dB\n" + out += " Outro final order trim -22 dB. Generated by tools/arrange_v11.py. */\n" + out += f"#define SNG_ORDERS {ORDERS}\n#define SNG_ROWS {ROWS}\n\n" + for name,(ctype,rows) in TABLES.items(): + values = flatten(rows) + out += f"static const {ctype} {name}[] = {{\n" + for i in range(0,len(values),16): + out += " " + ",".join(map(str,values[i:i+16])) + ",\n" + out += "};\n" + return out + +if __name__ == "__main__": + (HERE.parent / "song_data.h").write_text(header(), encoding="utf-8") + print(f"song_data.h: {ORDERS*ROWS} rows, {ORDERS*ROWS*SPR} samples, {ORDERS*ROWS*SPR/SRATE:.9f}s") + for name,a,b,_ in SECTIONS: + print(f"{name:12s} {a:02d}-{b:02d} {(b-a+1)*ROWS*SPR/SRATE:.3f}s") + for name,a,b,_ in SOLO_PHASES: + counts = [sum(n>=0 for n in solo[o]) for o in range(a,b+1)] + print(f"SOLO {name}: {len(counts)} phrases; attacks {counts}") diff --git a/tools/audio_audit.py b/tools/audio_audit.py new file mode 100644 index 0000000..553c062 --- /dev/null +++ b/tools/audio_audit.py @@ -0,0 +1,78 @@ +"""Read-only PCM audit; emits JSON, never renders, plays, or rewrites audio. + +Usage: python tools/audio_audit.py dump.wav [other.wav ...] +Requires numpy. Optional ffmpeg supplies EBU R128/true-peak measurements. +Row-boundary steps are diagnostics, not proof of audible clicks: chip waves +and intended drum transients also have large first differences. +""" +import argparse +import hashlib +import json +import re +import shutil +import subprocess +import wave +from pathlib import Path + +import numpy as np + + +def db(value): + return float(20 * np.log10(max(float(value), 1e-12))) + + +def audit(path): + path = Path(path).resolve() + with wave.open(str(path), "rb") as wav: + rate, channels, width, frames = (wav.getframerate(), wav.getnchannels(), + wav.getsampwidth(), wav.getnframes()) + if width != 2 or channels != 2 or rate != 44100: + raise ValueError("Expected 44100 Hz stereo 16-bit PCM") + pcm = np.frombuffer(wav.readframes(frames), dtype=" 0) else None, + final_100ms_rms_dbfs=db(np.sqrt(np.mean(x[-4410:]**2))), + row_step_quantiles_fs=np.quantile(row_steps, [.5, .95, 1]).tolist() + if len(row_steps) else [], + all_step_quantiles_fs=np.quantile(step, [.5, .95, 1]).tolist() + if len(step) else []) + result["worst_row_steps"] = [dict(order=int(boundaries[i]//160000), + row=int(boundaries[i]//5000 % 32), step_fs=float(row_steps[i])) + for i in np.argsort(row_steps)[-12:][::-1]] + result["orders"] = [dict(order=i//160000, + rms_dbfs=db(np.sqrt(np.mean(x[i:i+160000]**2))), + peak_dbfs=db(np.max(np.abs(x[i:i+160000])))) + for i in range(0,len(x),160000)] + if shutil.which("ffmpeg"): + proc = subprocess.run(["ffmpeg", "-hide_banner", "-nostats", "-nostdin", + "-i", str(path), "-af", "ebur128=peak=true:framelog=verbose", + "-f", "null", "-"], capture_output=True, text=True, check=True) + summary = proc.stderr.rsplit("Summary:", 1)[-1] + for key, pattern in (("integrated_lufs", r"I:\s+(-?[\d.]+) LUFS"), + ("lra_lu", r"LRA:\s+([\d.]+) LU"), + ("true_peak_dbtp", r"Peak:\s+(-?[\d.]+) dBFS")): + match = re.search(pattern, summary) + if match: + result[key] = float(match[1]) + return result + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("wav", nargs="+", type=Path) + args = parser.parse_args() + print(json.dumps([audit(path) for path in args.wav], indent=2, allow_nan=False)) diff --git a/tools/export_song.py b/tools/export_song.py new file mode 100644 index 0000000..c52e6ca --- /dev/null +++ b/tools/export_song.py @@ -0,0 +1,32 @@ +"""Export the current through-composed score without rendering or playback. + +This compatibility entry point shares arrange_v11.header(); it must never +silently restore the obsolete v10 score or omit the section/gain/synth lanes. +Use --check for a read-only comparison, or --output to export elsewhere. +""" +import argparse +from pathlib import Path + +from arrange_v11 import header + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--output", type=Path, + default=Path(__file__).resolve().parent.parent / "song_data.h") + parser.add_argument("--check", action="store_true") + args = parser.parse_args(argv) + expected = header() + if args.check: + if not args.output.exists() or args.output.read_text(encoding="utf-8") != expected: + print(f"{args.output}: missing or stale; export the current arrangement") + return 1 + print(f"{args.output}: current arrangement verified") + return 0 + args.output.write_text(expected, encoding="utf-8") + print(f"{args.output}: current arrangement exported") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/export_video.py b/tools/export_video.py new file mode 100644 index 0000000..aa8c072 --- /dev/null +++ b/tools/export_video.py @@ -0,0 +1,82 @@ +"""Silent offline export; check BOTH pipeline processes before publishing the MP4.""" +import json +import hashlib +import math +import os +from pathlib import Path +import subprocess +import wave +from datetime import datetime + +ROOT = Path(__file__).resolve().parents[1] + + +def main(): + exe = ROOT / "gfxvideo.exe" + inputs = [ROOT / name for name in ("gfx.c", "synth.h", "voices.h", "shots.h", + "song_data.h", "cats_mesh.h", "tardis_mesh.h", "c64_font.h", "shaders.h")] + inputs += list((ROOT / "fx").glob("*.hlsl")) + list((ROOT / "fx/out").glob("*.h")) + inputs += list((ROOT / "fx").glob("*.hlsli")) + # Each target rewrites identical FXC headers. A later release build does + # not make the video executable stale; authored shader/include files do. + authored = [p for p in inputs if p.name != "shaders.h" and p.parent.name != "out"] + if any(p.stat().st_mtime_ns > exe.stat().st_mtime_ns for p in authored): + raise RuntimeError("Stale video renderer: run makevideo_gpu.bat to rebuild first") + def fingerprint(): + return {str(p.relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() + for p in inputs + [exe]} + provenance = fingerprint() + subprocess.run([str(exe)], cwd=ROOT, + env={**os.environ, "CRTK_WAVONLY": "1"}, check=True) + with wave.open(str(ROOT / "dump.wav"), "rb") as wav: + assert (wav.getnchannels(), wav.getsampwidth(), wav.getframerate()) == (2, 2, 44100) + duration = wav.getnframes() / wav.getframerate() + expected = math.ceil(duration * 60) + pending = ROOT / "CRTkafa.pending.mp4" + env = dict(os.environ) + env.pop("CRTK_WAVONLY", None) + render = subprocess.Popen([str(exe)], cwd=ROOT, env=env, stdout=subprocess.PIPE) + try: + encode = subprocess.Popen([ + "ffmpeg", "-nostdin", "-y", "-hide_banner", "-loglevel", "warning", "-stats", + "-f", "rawvideo", "-pixel_format", "bgra", "-video_size", "1280x720", + "-framerate", "60", "-i", "pipe:0", "-i", str(ROOT / "dump.wav"), + "-c:v", "libx264", "-preset", "medium", "-crf", "19", + "-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "256k", + "-movflags", "+faststart", str(pending)], stdin=render.stdout, cwd=ROOT) + render.stdout.close() + encoder_status = encode.wait() + renderer_status = render.wait() + if encoder_status or renderer_status: + raise RuntimeError(f"Export failed: renderer={renderer_status}, encoder={encoder_status}") + finally: + if render.poll() is None: + render.terminate() + render.wait() + info = json.loads(subprocess.check_output([ + "ffprobe", "-v", "error", "-show_streams", "-show_format", "-of", "json", str(pending)])) + video = next(s for s in info["streams"] if s["codec_type"] == "video") + audio = next(s for s in info["streams"] if s["codec_type"] == "audio") + if int(video["nb_frames"]) != expected: + raise RuntimeError(f"Incomplete video: expected {expected}, got {video['nb_frames']}") + if abs(float(audio["duration"]) - duration) > 0.05: + raise RuntimeError("Audio duration differs from source WAV") + if fingerprint() != provenance: + raise RuntimeError("Sources or renderer changed during export; not publishing") + published = ROOT / "CRTkafa.mp4" + try: + pending.replace(published) + except PermissionError: + # A player may hold the previous delivery open on Windows. Do not + # close it or destroy that file; publish the verified render separately. + published = ROOT / ("CRTkafa-cracktro-1-" + datetime.now().strftime("%Y%m%d-%H%M%S") + ".mp4") + pending.replace(published) + provenance[published.name] = hashlib.sha256(published.read_bytes()).hexdigest() + provenance["dump.wav"] = hashlib.sha256((ROOT / "dump.wav").read_bytes()).hexdigest() + (ROOT / "export-provenance.json").write_text(json.dumps(provenance, indent=2) + "\n") + print(f"Verified export: {expected} frames, {duration:.3f} s, 1280x720, 60 fps", flush=True) + print(f"Video: {published}", flush=True) + + +if __name__ == "__main__": + main() diff --git a/tools/motif.py b/tools/motif.py new file mode 100644 index 0000000..32a58bd --- /dev/null +++ b/tools/motif.py @@ -0,0 +1,133 @@ +"""Written phrases for the 48-order form, in MIDI-minus-12 note numbers. + +The call A-F-E remains in the chorus. The solo also develops the natural +minor run and pedal contour of music_render.py V7_SOLO (used by v8), with +new intervallic runs, lyrical lines, displaced accents and upper responses. +No register folding or clipping: pitches are chosen in the score itself. +Each phrase is exactly one 32-row / two-bar order, not a looping cell. +""" +ROWS, X, A4 = 32, -1, 57 +MIN_IV = (0,2,3,5,7,8,10) + +def deg(d, base=A4): + octave, index = divmod(d,7) + return base + 12*octave + MIN_IV[index] + +def scale_step(note, steps): + octave, pc = divmod(note-A4,12) + return deg(octave*7 + MIN_IV.index(pc) + steps) + +def lay(pitches, rhythm=None, base=A4): + """Place explicit attacks; X is no trigger, not sustain or note-off. + + Repeated pitches in lyrical passages deliberately refresh the existing + decaying solo envelope. No claim of continuous legato is made. + """ + if rhythm is None: rhythm = range(len(pitches)) + assert len(pitches) == len(rhythm), "pitch/rhythm length mismatch" + assert len(set(rhythm)) == len(rhythm), "two attacks in one lane/row" + out = [X]*ROWS + for p,r in zip(pitches,rhythm): + assert 0 <= r < ROWS + if p is not None: + out[r] = deg(p,base) + assert 0 <= out[r] <= 127, "signed-char note overflow" + return out + +def lead_chorus(i): + # Four distinct statements in the sole chorus; no octave folding. + phrases = ( + ((0,5,4,4,2,3,4,6,5,4), (0,4,6,8,12,16,18,20,24,26)), + ((2,4,7,6,4,2,0,2,3,4,4), (0,2,4,7,10,12,16,18,20,24,26)), + ((5,7,9,7,6,4,2,3,5,4), (0,3,6,8,11,14,16,20,24,26)), + ((3,5,4,2,1,0,-1,1,4,4), (0,2,6,10,12,16,18,20,24,26)), + ) + pitches, rhythm = phrases[i] + return lay(pitches,rhythm,base=A4) + +# Each tuple is (scale degrees, attack rows, base). Dense lines are written +# in full, including rests, so there is no repeated motif-call generator. +SOLO_SCORE = ( + # 16-19: spacious pickup, displaced answer, longer arcs entering the band. + ((0,5,4,4,2,3,1), (0,6,10,12,18,22,27), 45), + ((2,4,6,5,3,2,0,1), (1,5,9,12,17,21,24,28), 57), + ((5,4,2,0,2,3,6,4,3), (0,3,7,10,16,19,22,25,28), 57), + ((3,4,6,7,7,5,4,2,1,4), (0,4,7,10,12,16,20,24,27,29), 57), + # 20-24: lyrical electric guitar; repeated tones refresh long gestures, + # with short sixteenth flourishes embedded between them. + ((0,2,4,7,7,7,6,5,4,2,3,5,4,4), + (0,2,4,6,8,10,13,14,15,18,20,22,25,27), 57), + ((3,5,7,9,8,7,5,4,2,0,2,4,4,4,3,1), + (0,2,4,6,7,8,10,12,14,16,18,20,22,24,27,29), 57), + ((5,5,7,9,10,9,7,6,4,2,3,5,7,6,4,4,2), + (0,2,5,6,7,8,10,12,14,16,18,20,22,23,24,26,29), 57), + ((2,4,6,7,9,7,4,2,0,2,4,6,5,3,2,0,0,1), + (0,1,2,4,6,8,10,12,14,16,17,18,20,22,24,26,28,30), 57), + ((3,4,5,7,9,10,9,7,5,3,2,4,6,7,6,4,2,1,0,0), + (0,1,2,3,4,6,8,10,12,14,16,17,18,19,20,22,24,26,28,30), 57), + # 25: v8 natural-minor ascent/descent + broken arpeggio contour retained. + ((0,1,2,3,4,5,6,7, 6,5,4,3,2,1,0,-1, + 0,2,4,7,9,7,4,2, 0,1,2,4,7,6,4,0), None, 57), + # 26: contrary thirds, a falling sixth, then upward displaced groups. + ((3,5,4,6,5,7,6,9, 7,5,3,2,0,2,4,6, + 2,4,6,7,4,6,7,9, 6,7,9,10,9,7,4,None), None, 57), + # 27: v8's pedal-point gesture, natural seventh against the shared scale. + ((7,4,7,5,7,4,7,3, 7,4,7,2,7,4,7,1, + 7,4,7,6,7,4,7,5, 7,4,7,4,7,6,7,8), None, 57), + # 28: cross-string arpeggios and a long cascading answer. + ((2,6,9,11,9,6,2,4, 5,9,12,9,5,3,0,3, + 7,9,7,6,5,4,3,2, 1,0,-1,1,2,4,6,None), None, 57), + # 29: low-to-high sixteenth sequence with changing group lengths. + ((3,2,0,-1,0,2,3,5, 6,3,5,6,7,5,6,7, + 9,6,7,9,10,9,7,5, 4,2,1,2,4,6,7,None), None, 57), + # 30: descending sixths become a rising scalar exit; two-row breath. + ((5,0,6,1,7,2,9,4, 10,5,9,4,7,2,6,1, + 4,5,6,7,9,10,12,10, 9,7,6,4,2,0,None,None), None, 57), + # 31-35: upper answers over moving organ chords; guitar riff is absent. + ((0,2,4,2,3,5,3,2), (2,4,6,11,14,18,22,27), 69), + ((6,4,4,3,1,0,2,4,2), (1,4,6,10,14,18,22,25,28), 69), + ((3,5,7,5,4,2,0,2,4,6), (2,5,8,11,14,18,21,24,27,29), 69), + ((5,4,2,3,5,3,1,0,2,4,6,4), (1,3,5,9,12,15,18,21,24,26,28,30), 69), + ((3,3,5,6,5,3,2,0,2,4,6,4,3,1), (0,2,5,7,9,11,14,17,20,22,24,26,28,30), 69), + # 36-38: continuous sixteenths, large interval shapes and a higher peak. + ((0,4,7,9,7,4,2,5, 3,5,7,10,9,7,5,3, + 5,7,9,12,10,9,7,5, 6,7,9,10,12,10,9,7), None, 57), + ((9,7,5,2,5,7,9,12, 10,9,7,5,3,5,7,10, + 7,6,4,2,4,6,7,11, 9,7,6,4,2,4,6,7), None, 57), + ((12,7,11,7,10,6,9,5, 7,9,10,12,14,12,10,9, + 7,5,6,7,9,10,12,14, 12,10,9,7,6,4,2,1), None, 57), + # 39: dense first bar, twelve trigger-free rows, four-note rising pickup. + ((3,5,7,10,9,7,5,3,5,7,9,12,10,7,4,4,4,6,7,9), + tuple(range(16)) + (28,29,30,31), 57), +) + +def extended_solo(i): + pitches, rhythm, base = SOLO_SCORE[i] + return lay(pitches, rhythm, base) + +def arrival_phrase(i): + # Six new phrases after the +2 shift, not a repeated chorus. A high + # tonic is refreshed, answered, expanded, and finally brought home. + phrases = ( + ((14,14,14,12,11,9,7,7), (0,2,4,10,14,18,24,26)), + ((5,7,9,12,12,10,9,7,6,4,2,2), (0,2,4,6,8,12,14,16,20,22,26,28)), + ((3,5,7,10,9,7,6,5,3,4,6,7,7), (0,3,6,8,10,12,15,18,21,24,26,28,30)), + ((2,4,6,9,7,6,5,7,9,12,10,9,7,5,4,4), tuple(range(0,32,2))), + ((5,7,9,10,12,10,9,7,6,4,3,5,7,6,4,2,1,0), + (0,1,2,3,4,6,8,10,12,14,16,18,20,22,24,26,28,30)), + ((3,5,4,2,1,4,7,7,4,2,0,0), (0,2,4,6,8,10,12,14,18,22,26,28)), + ) + return lay(*phrases[i]) + +def verse_riff(i): + # Four individually paced two-bar riffs. Chord-relative root/fifth/octave + # degrees preserve both key consonance and the guitar's power fifth. + phrases = ( + ((0,0,4,0,7,4,0,0,4,7,4,0,4,0,0), (0,2,3,6,8,10,12,15,16,18,21,24,26,28,30)), + ((0,4,0,7,4,0,0,4,0,7,4,0,0,4,7,0), (0,1,4,6,8,11,12,14,16,19,20,22,24,27,28,31)), + ((0,0,7,4,0,4,7,0,4,0,7,4,0,0,4,0,7), (0,2,4,5,8,10,12,14,16,18,20,22,24,25,28,30,31)), + ((0,4,7,4,0,0,4,0,7,4,0,4,7,4,0,0,4,0), (0,1,2,4,6,8,10,12,14,16,18,20,22,24,26,28,29,30)), + ) + out = [None]*ROWS + for p,r in zip(*phrases[i]): out[r] = p + return out diff --git a/tools/test_bloom.py b/tools/test_bloom.py new file mode 100644 index 0000000..2afdf0e --- /dev/null +++ b/tools/test_bloom.py @@ -0,0 +1,41 @@ +"""Regression checks for native-grid separable bloom kernels (no third-party deps).""" +import math +import unittest + + +def kernel(center, offsets, weights): + result = {0: center} + for offset, weight in zip(offsets, weights): + lo = math.floor(offset) + fraction = offset - lo + for sign in (-1, 1): + for index, amount in ((lo, 1 - fraction), (lo + 1, fraction)): + at = sign * index + result[at] = result.get(at, 0) + amount * weight + return result + + +class BloomTests(unittest.TestCase): + def test_native_grid_kernel(self): + vertical = kernel(.2363835, [1.3730788, 3.2295113], [.3171515, .0646567]) + horizontal = kernel(.1513151, [1.4415567, 3.3767077, 5.3550101, 7.3868203], + [.2503180, .1212263, .0394885, .0133097]) + for profile in (vertical, horizontal): + self.assertAlmostEqual(sum(profile.values()), 1, places=6) + self.assertAlmostEqual(sum(i * w for i, w in profile.items()), 0, places=6) + for i in range(max(profile)): + self.assertGreater(profile[i], profile[i + 1]) + self.assertEqual(profile[i], profile[-i]) + + def test_source_row_parity(self): + # Two adjacent source rows map to the same 2:1 downsample pixel; + # both undergo the same knee and carry precisely half its row energy. + vertical = kernel(.2363835, [1.3730788, 3.2295113], [.3171515, .0646567]) + for row in range(12, 20, 2): + even = {row // 2 + k: w * .5 for k, w in vertical.items()} + odd = {(row + 1) // 2 + k: w * .5 for k, w in vertical.items()} + self.assertEqual(even, odd) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/test_music_form.py b/tools/test_music_form.py new file mode 100644 index 0000000..df7e681 --- /dev/null +++ b/tools/test_music_form.py @@ -0,0 +1,163 @@ +"""Score/API regression checks; no build, rendering, or audio playback. + +Run: python -m unittest discover -s tools -p test_music_form.py -v +""" +import re +import unittest +import tempfile +from unittest.mock import patch +from pathlib import Path +import arrange_v11 as A +import motif as M +import export_song as E + + +class MusicFormTests(unittest.TestCase): + def test_exporter_preserves_current_score_and_check_is_read_only(self): + with tempfile.TemporaryDirectory(prefix="crtk-score-test-") as tmp: + output = Path(tmp) / "score.h" + self.assertEqual(E.main(["--output", str(output)]), 0) + self.assertEqual(output.read_text(encoding="utf-8"), A.header()) + with patch.object(Path, "write_text", side_effect=AssertionError("check wrote a file")): + self.assertEqual(E.main(["--check", "--output", str(output)]), 0) + self.assertEqual(E.main(["--check", "--output", str(Path(tmp)/"missing.h")]), 1) + output.write_text("obsolete v10 score", encoding="utf-8") + with patch.object(Path, "write_text", side_effect=AssertionError("check wrote a file")): + self.assertEqual(E.main(["--check", "--output", str(output)]), 1) + + def test_exact_form_and_clock(self): + self.assertEqual([(n,a,b) for n,a,b,_ in A.SECTIONS], [ + ("INTRO",0,1), ("SYNTHWAVE",2,7), ("METAL VERSE",8,11), + ("CHORUS",12,15), ("SOLO",16,39), ("ARRIVAL",40,45), ("OUTRO",46,47)]) + self.assertEqual([o for _,a,b,_ in A.SECTIONS for o in range(a,b+1)], list(range(48))) + source = (A.HERE.parent / "synth.h").read_text() + for name,value in (("SPR",5000),("SRATE",44100),("TPR",6),("ROWS",32),("ORDERS",48)): + self.assertRegex(source, rf"#define\s+{name}\s+{value}\b") + self.assertEqual(sum((t+1)*5000//6 - t*5000//6 for t in range(6)),5000) + self.assertEqual(A.ORDERS*A.ROWS*A.SPR,7680000) + self.assertAlmostEqual(A.ORDERS*A.ROWS*A.SPR/A.SRATE,174.149659864,places=8) + self.assertEqual(A.sect[:2], [0,0]) + self.assertEqual(A.sect[46:], [10,10]) + self.assertTrue(all(s not in (0,7,10) for s in A.sect[2:46])) + + def test_no_duplicate_musical_orders(self): + # Exclude drums, metadata, gain and risers: percussion or a new label + # must not disguise a repeated arrangement. Include voiced harmony. + signatures = {} + for o in range(48): + sig = tuple(tuple(lane[o]) for lane in + (A.lead,A.solo,A.harm,A.bass,A.gtr,A.hold,A.root,A.ctyp)) + sig += (A.arpo[o], A.orgm[o], A.syn[o]) + self.assertNotIn(sig,signatures, f"duplicate orders {signatures.get(sig)} and {o}") + signatures[sig] = o + for lane,orders in ((A.solo,range(16,40)),(A.solo,range(40,46)),(A.lead,range(12,16))): + phrases = [tuple(lane[o]) for o in orders] + self.assertEqual(len(phrases),len(set(phrases))) + + def test_dense_runs_and_lyrical_contrast(self): + counts = [sum(n>=0 for n in A.solo[o]) for o in range(16,40)] + self.assertGreaterEqual(sum(n>=30 for n in counts),9) + self.assertGreaterEqual(len(set(counts)),10) + rhythms = {tuple(r for r,n in enumerate(A.solo[o]) if n>=0) for o in range(16,40)} + self.assertGreaterEqual(len(rhythms),14) + for o in (*range(25,31),36,37,38): + longest = current = 0 + for n in A.solo[o]: + current = current+1 if n>=0 else 0 + longest = max(longest,current) + self.assertGreaterEqual(longest,28) + self.assertLess(max(counts[:4]),min(counts[4:9])) + # Lyrical figures explicitly refresh selected notes; rests aren't ties. + for o in range(20,25): + pitches = [n for n in A.solo[o] if n>=0] + self.assertTrue(any(a==b for a,b in zip(pitches,pitches[1:]))) + self.assertEqual(A.solo[39][16:28],[-1]*12) + self.assertTrue(all(n>=0 for n in A.solo[39][28:32])) + + def test_v8_contour_preserved_without_register_clamp(self): + expected = [57,59,60,62,64,65,67,69,67,65,64,62,60,59,57,55, + 57,60,64,69,72,69,64,60,57,59,60,64,69,67,64,57] + self.assertEqual(A.solo[25],expected) + self.assertEqual(max(A.solo[38]),81) + self.assertEqual(A.solo[40][0],83) + self.assertEqual(M.deg(0),57) # A4 is MIDI 69 minus 12. + + def test_pitch_scale_and_bass_riff_consonance(self): + for o in range(48): + key = 9 + (2 if o>=40 else 0) + scale = {(key+n)%12 for n in M.MIN_IV} + for lane in (A.lead,A.solo,A.harm,A.bass,A.gtr): + self.assertEqual(len(lane[o]),32) + for n in lane[o]: + self.assertTrue(n==-1 or 0<=n<=127) + if n>=0: self.assertIn(n%12,scale) + for ci,rt in enumerate(A.root[o]): + chord = {(rt+n)%12 for n in (0,4 if A.ctyp[o][ci] else 3,7)} + self.assertTrue(chord <= scale) + for r in range(32): + rt = A.root[o][r//8] + if A.bass[o][r]>=0: self.assertEqual(A.bass[o][r]%12,rt) + if A.gtr[o][r]>=0: + self.assertIn((A.gtr[o][r]-rt)%12,(0,7)) + self.assertIn(A.gtr[o][r]%12,scale) + self.assertIn((A.gtr[o][r]+7)%12,scale) + self.assertGreaterEqual(A.gtr[o][r],21) + for o,r in ((8,26),(9,27),(10,28),(11,16),(11,20)): + self.assertEqual(A.gtr[o][r]%12,4) # E power chord, not B + F# + + def test_drum_codes_preserve_kicks(self): + kick_codes, snare_codes, crash_codes = {1,6,7}, {2,7}, {5,6} + crashes = [] + for o in range(48): + self.assertEqual(len(A.drum[o]),32) + for r,code in enumerate(A.drum[o]): + self.assertIn(code,range(8)) + if code in crash_codes: + crashes.append((o,r)) + self.assertIn(code,kick_codes) + if code==7: + self.assertIn(o,range(2,8)) + self.assertIn(r,(4,12,20,28)) + if 2<=o<=7: + for r in range(0,32,4): + self.assertIn(A.drum[o][r],kick_codes) + for r in (4,12,20,28): + self.assertIn(A.drum[o][r],snare_codes) + self.assertEqual(crashes,[(o,0) for o in (2,8,12,20,25,36,40)]) + + def test_one_foreground_owner(self): + for o in range(48): + active = [any(n>=0 for n in lane[o]) for lane in (A.lead,A.solo,A.gtr)] + self.assertLessEqual(sum(active),1) + self.assertEqual(A.arpo[o],-1) # no extra chip arp over synth arp + if A.orgm[o]==2 and active[1]: + self.assertIn(o,range(31,36)) + self.assertGreaterEqual(min(n for n in A.solo[o] if n>=0),69) + self.assertFalse(active[0] or active[2]) + if A.syn[o]: + self.assertIn(o,range(2,8)) + self.assertFalse(any(active)) + self.assertEqual(A.orgm[o],0) + + def test_generated_header_matches_runtime_api(self): + text = (A.HERE.parent / "song_data.h").read_text(encoding="utf-8") + self.assertEqual(text,A.header()) + parsed = {} + for typ,name,body in re.findall(r"static const (signed char|unsigned char|short) (g_sq\w+)\[\] = \{(.*?)\};",text,re.S): + parsed[name] = (typ,list(map(int,re.findall(r"-?\d+",body)))) + expected_names = {"g_sqLead","g_sqSolo","g_sqHarm","g_sqBass","g_sqGtr","g_sqHold", + "g_sqDrum","g_sqRoot","g_sqType","g_sqArpOct","g_sqOrgMode", + "g_sqRiser","g_sqGain","g_sqSyn","g_sqSect"} + self.assertEqual(set(parsed),expected_names) + source = (A.HERE.parent / "synth.h").read_text() + self.assertEqual(set(re.findall(r"\bg_sq\w+",source)),expected_names) + for name,(typ,values) in parsed.items(): + self.assertEqual((typ,values),(A.TABLES[name][0],A.flatten(A.TABLES[name][1]))) + expected_size = 1536 if name in {"g_sqLead","g_sqSolo","g_sqHarm","g_sqBass","g_sqGtr","g_sqHold","g_sqDrum"} else 192 if name in {"g_sqRoot","g_sqType"} else 48 + self.assertEqual(len(values),expected_size) + lo,hi = (-128,127) if typ=="signed char" else (0,255) if typ=="unsigned char" else (-32768,32767) + self.assertTrue(all(lo<=v<=hi for v in values)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/verify_delivery.py b/tools/verify_delivery.py new file mode 100644 index 0000000..4ff6466 --- /dev/null +++ b/tools/verify_delivery.py @@ -0,0 +1,69 @@ +"""Verify final offline artifacts and record exact source/artifact hashes.""" +from array import array +import hashlib +import json +from pathlib import Path +import subprocess +import sys +import wave + +ROOT = Path(__file__).resolve().parents[1] + + +def sha(path): + return hashlib.sha256(path.read_bytes()).hexdigest() + + +def main(): + provenance = json.loads((ROOT / "export-provenance.json").read_text()) + for name, digest in provenance.items(): + assert sha(ROOT / name) == digest, f"Export no longer matches: {name}" + videos = [ROOT / name for name in provenance if name.lower().endswith(".mp4")] + assert len(videos) == 1, "Expected exactly one published video in provenance" + video_path = videos[0] + test = (ROOT / "gputest.txt").read_text() + if "failures: 0" not in test: + raise RuntimeError("Offline renderer tests have not passed") + with wave.open(str(ROOT / "dump.wav"), "rb") as wav: + frames, rate = wav.getnframes(), wav.getframerate() + assert (wav.getnchannels(), wav.getsampwidth(), rate) == (2, 2, 44100) + pcm = array("h", wav.readframes(frames)) + if sys.byteorder != "little": + pcm.byteswap() + peak = max(abs(min(pcm)), abs(max(pcm))) + assert 0 < peak < 32767, f"Invalid/clipped PCM peak {peak}" + assert max(map(abs, pcm[-882:])) <= 8, "Final 10 ms must approach silence" + assert max(map(abs, pcm[:882])) <= 8, "Opening 10 ms must approach silence" + info = json.loads(subprocess.check_output([ + "ffprobe", "-v", "error", "-show_streams", "-of", "json", str(video_path)])) + video = next(s for s in info["streams"] if s["codec_type"] == "video") + audio = next(s for s in info["streams"] if s["codec_type"] == "audio") + assert int(video["nb_frames"]) == (frames * 60 + rate - 1) // rate + assert (video["width"], video["height"], video["r_frame_rate"]) == (1280, 720, "60/1") + assert abs(float(audio["duration"]) - frames / rate) < .05 + subprocess.run(["ffmpeg", "-nostdin", "-v", "error", "-xerror", "-i", + str(video_path), "-f", "null", "-"], check=True) + sources = [ROOT / "gfx.c", ROOT / "synth.h", ROOT / "voices.h", ROOT / "song_data.h", + ROOT / "shots.h", ROOT / "cats_mesh.h", ROOT / "tardis_mesh.h", ROOT / "c64_font.h", ROOT / "shaders.h"] + sources += sorted((ROOT / "fx").glob("*.hlsl")) + sorted((ROOT / "fx/out").glob("*.h")) + sources += sorted((ROOT / "fx").glob("*.hlsli")) + artifacts = [ROOT / name for name in ("CRTkafa.exe", "gfxvideo.exe", "gfxoffline.exe")] + artifacts += [video_path, ROOT / "dump.wav"] + # Generated headers are rewritten for every build, so freshness is checked + # against authored source, not the last build's identical generated files. + authored = [p for p in sources if p.name != "shaders.h" and p.parent.name != "out"] + newest = max(p.stat().st_mtime_ns for p in authored) + for p in artifacts[:3]: + assert p.stat().st_mtime_ns >= newest, f"Stale executable: {p.name}" + assert (ROOT / "gputest.txt").stat().st_mtime_ns >= (ROOT / "gfxoffline.exe").stat().st_mtime_ns + record = {"duration_seconds": frames / rate, "video_frames": int(video["nb_frames"]), + "pcm_peak": peak, "offline_failures": 0, "full_video_decode": "passed", + "interactive_gui_tested": False, + "sources": {str(p.relative_to(ROOT)): sha(p) for p in sources}, + "artifacts": {p.name: {"bytes": p.stat().st_size, "sha256": sha(p)} for p in artifacts}} + (ROOT / "delivery.json").write_text(json.dumps(record, indent=2) + "\n") + print(json.dumps({k: v for k, v in record.items() if k not in ("sources", "artifacts")}, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/voices.h b/voices.h new file mode 100644 index 0000000..60b06f9 --- /dev/null +++ b/voices.h @@ -0,0 +1,1830 @@ +/* ========================================================================== + T H E S Y N T H W A V E V O I C E S + -------------------------------------------------------------------------- + Six voices the synth did not have. Their absence, not the mix, is why the + demo had no synthwave in it: there was no pad, no gated snare, no + sidechain, no FM bell, no chorus and no polysynth arpeggio anywhere in the + code. Each was written against the spectral allocation table and then + reviewed against it, which is where the level and timing errors came out. + ========================================================================== */ + +/* ---------------------------------------------------------------- pad */ +/* ========================================================================== + P A D - seven voice unison saw/pulse, resonant SVF, stereo spread + -------------------------------------------------------------------------- + Owns 220 Hz .. 1.6 kHz. Two pole highpass underneath, the SVF is the only + lowpass. Panned wide and symmetrically: the even unison voices go left, the + odd ones go right and lag by a few samples. Ducks against lead and solo, + and gets out of the rhythm guitar's way when the guitar is playing. + ========================================================================== */ + +#define PAD_UNI 7 /* oscillators per note */ +#define PAD_NOTES 3 /* notes of the triad held at once */ +/* Derived, not hand counted: these are the divisors that set each group's + level, so if PAD_UNI is ever changed they have to follow it. Written out + as 12 and 9 they went silently out of step with the loop bounds and one + side came out mis-scaled. */ +#define PAD_NA (PAD_NOTES * ((PAD_UNI + 1) / 2)) /* even u: 0 2 4 6 */ +#define PAD_NB (PAD_NOTES * (PAD_UNI / 2)) /* odd u: 1 3 5 */ +#define PAD_DLY 31 /* samples the odd group lags the even group */ +#define PAD_BASE 48 /* C4 in this note numbering: 261.6 Hz */ + +/* +-6 cents, 12 cents from the flattest voice to the sharpest. Wider than + this and the seven voices stop reading as one note; narrower and the + slowest beat (2 cents at 260 Hz is 0.3 Hz) is too slow to hear inside the + 3.6 s a pattern lasts. These are 2^(c/1200) written out, because there is + no powf here and this is the only place that needs one. */ +static const float g_padDet[PAD_UNI] = { + 0.9965403f, 0.9976921f, 0.9988454f, 1.0000000f, + 1.0011559f, 1.0023132f, 1.0034718f +}; + +static struct { + float ph[PAD_NOTES][PAD_UNI]; /* free running: never reset on a chord */ + float inc[PAD_NOTES][PAD_UNI]; + float env, fenv, fdk, tgt; /* amplitude and filter contours */ + float lfoC, lfoP; /* cutoff and pulse width modulators */ + float hp1a, hp2a, hp1b, hp2b; /* the two highpass poles, per side */ + float lpA, bpA, lpB, bpB; /* the two state variable filters */ + float gtrA; /* smoothed "the guitar is playing" */ + int note[PAD_NOTES]; + int on, seeded; +} g_pad; + +static float g_padDlyBuf[PAD_DLY]; +static int g_padDlyPos = 0; + +/* Written by padRender every sample so the integrator can feed the existing + reverb and echo busses without changing padRender's signature. */ +static float g_padSendL = 0.0f, g_padSendR = 0.0f; + +/* --- envelopes. One pole coefficients: a = 1/(tau * SRATE). --------------- + 0.41 s attack is the point where the pad stops sounding plucked and starts + sounding like it was already there; 1.3 s release lets a chord bleed under + the next one, which is the whole reason a pad is here. */ +#define PAD_ATK 0.000055f /* amp attack, tau 0.41 s */ +#define PAD_REL 0.000189f /* amp release, tau 0.12 s; clear the metal handoff */ +#define PAD_FATK 0.000105f /* filter opens in tau 0.22 s, ahead of the amp */ + +/* The two constants that shape the per-chord filter swell. Both of them MUST + be faster than the rate at which padSet resets fdk, which is the chord + rate: g_sqRoot is indexed o*4 + (row>>3), so the chord turns over every + 8 rows = 40000 samples = 0.907 s. At the 1.9 s and 2.4 s these used to be, + fdk was reset long before it had decayed, fenv never moved off 0.95, and + the "contour" was a constant that parked the cutoff at 1307..1335 Hz - + inside the lead's register, with none of the movement the design is for. + At 0.50 s and 0.35 s the swell actually happens: 905..1086 Hz per chord, + 700..1291 Hz once the LFO is added. Anything slower than about 0.6 s here + and the contour quietly stops existing again. */ +#define PAD_FDEC 0.0000454f /* filter closes in tau 0.50 s */ +#define PAD_FDK 0.9999352f /* sag from wide open to sustain, tau 0.35 s */ +#define PAD_FSUS 0.42f /* how far open a held chord settles */ + +/* Cutoff in Hz: floor + envelope + LFO. 235 sits just above the highpass so + a closed pad still has a body instead of disappearing, and so the resonant + peak never lands below the highpass where it would only be cancelled. + 235 + 1150 + 205 = 1590, i.e. fully open is exactly the top of the band + this voice is allowed to own. Raising PAD_FCE is how the pad starts eating + the lead. */ +#define PAD_FC0 235.0f +#define PAD_FCE 1150.0f +#define PAD_FCL 205.0f +/* The floor is PAD_FC0 itself, not something under it. At 150 the LFO could + pull the cutoff to below the 220 Hz highpass corner whenever fenv was near + zero - during a release, and for the first instants of a note - which is + precisely the case the paragraph above says cannot happen. */ +#define PAD_FCMIN 235.0f +#define PAD_FCMAX 1600.0f + +/* q is 1/Q. 0.45 is Q = 2.2: an audible peak that follows the sweep without + whistling. Below about 0.3 the filter starts to ring on its own and the + pad turns into a siren every time the LFO comes round. */ +#define PAD_Q 0.45f +#define PAD_FSKEW 1.035f /* right side's cutoff, 60 cents off the left */ + +/* Saw carries the body, pulse the hollow. The pulse width is modulated in + opposite directions on the two sides, which decorrelates them for free: + duty d and duty 1-d have the same magnitude spectrum. */ +#define PAD_SAW 0.62f +#define PAD_PUL 0.38f +#define PAD_PWM 0.18f /* duty swings 0.32 .. 0.68 */ + +/* Deliberately not integer multiples of the 3.63 s pattern, and not of each + other, so neither modulator ever lines up with the bar and starts to read + as rhythm. */ +#define PAD_LFOC_INC (0.137f / (float)SRATE) +#define PAD_LFOP_INC (0.081f / (float)SRATE) + +/* Two cascaded one pole highpasses are -3 dB at 1.5538 * fc, not at fc, so + the poles go at 220/1.5538 = 141.6 Hz and 400/1.5538 = 257.4 Hz to put the + corner of the PAIR where the allocation table says. Coefficient is + 1 - exp(-TAU*fc/SRATE). Moving these to 220 and 400 directly is the + obvious looking "fix" and it guts the bottom octave of the pad. */ +#define PAD_HP_LO 0.01997f /* pair corner 220 Hz */ +#define PAD_HP_HI 0.03601f /* pair corner 400 Hz, guitar playing */ + +/* g_duck falls to 1-0.72 = 0.28 under the solo, so 1/0.72 renormalises it to + a clean 0..1 before the pad's own 0.42 depth is applied. */ +#define PAD_DUCK 0.42f +#define PAD_DUCKN 1.3889f +#define PAD_GDROP 0.6452f /* -9 dB when the rhythm guitar is in */ +#define PAD_GATK 0.0012f /* get out of the way in ~25 ms */ +#define PAD_GREL 0.00012f /* come back over ~190 ms, so it cannot pump + between two chords of the same riff */ + +/* Twenty one detuned oscillators mostly cancel: after the normalisation by + voice count the bank sits near 0.07 RMS and only reaches 0.37 on the peaks + where the unison happens to line up, so this has to be much larger than + the 0.2 .. 0.34 the single oscillator voices use. At 0.55 the pad measures + 0.037 RMS and 0.20 peak, under the guitar and just under the lead. Sane + range is 0.35 .. 0.7; past that the 15 dB crest of the unison starts + triggering the bus compressor on its own. */ +/* Measured: at 0.55 the pad peaked at 0.052 against a mix peaking near + 0.30, which is 15 dB down - present in the file and absent from the record. + A pad is a bed; it has to be the thing the section sits on. 1.9 puts its + peak at about 0.18, under the guitar and over everything else. */ +#define PAD_LEVEL 1.90f /* the one knob worth tuning by ear */ + +/* Group B has three voices per note against group A's four. Dividing each + group by its own voice count does NOT equalise them: twenty one detuned + oscillators are mutually incoherent, so a group's level after dividing by + N falls as 1/sqrt(N), and the smaller group therefore comes out + sqrt(12/9) = 1.155x - a measured 1.18 dB - LOUDER. Since A is panned hard + left and B hard right, that tips the whole pad to the right and breaks the + mirror counterweight the allocation table asks for. This trim is + sqrt(PAD_NB/PAD_NA), written out because there is no sqrtf here; it folds + into the existing normalising multiply, so it is free. */ +#define PAD_BTRIM 0.8660254f +#define PAD_PNEAR 0.925f /* pan +-0.85, linear: (1 +- 0.85) / 2 */ +#define PAD_PFAR 0.075f +#define PAD_SEND 0.30f + +/* Call every row (cheap: it returns immediately unless the chord changed). + on = 0 releases. root/type are the same g_sqRoot / g_sqType the organ + uses, so the pad is always in the song's harmony by construction. */ +static void padSet(int on, int root, int type) +{ + static const int minorOff[3] = { 0, 3, 7 }; + static const int majorOff[3] = { 0, 4, 7 }; + const int *off = type ? majorOff : minorOff; + int n0, n1, n2, i, u; + + if (!on) { g_pad.tgt = 0.0f; g_pad.on = 0; return; } + /* Every note is folded into the one octave above PAD_BASE, so the chord + is whatever inversion of itself lies in 261 .. 494 Hz. Adding root + straight on instead lets the pad leap an octave between two chords of + the same progression, and drags the low ones under the highpass. The + bass owns the actual root, so the pad having the third at the bottom + is a feature. */ + n0 = PAD_BASE + ((root + off[0]) % 12); + n1 = PAD_BASE + ((root + off[1]) % 12); + n2 = PAD_BASE + ((root + off[2]) % 12); + g_pad.tgt = 1.0f; + /* Same chord as last row: leave everything running. Re-seeding the filter + contour once a row would make the pad breathe at 8 Hz. */ + if (g_pad.on && g_pad.note[0] == n0 && + g_pad.note[1] == n1 && g_pad.note[2] == n2) return; + g_pad.on = 1; + g_pad.note[0] = n0; g_pad.note[1] = n1; g_pad.note[2] = n2; + for (i = 0; i < PAD_NOTES; i++) { + float f = noteFreq(g_pad.note[i]) / (float)SRATE; + for (u = 0; u < PAD_UNI; u++) g_pad.inc[i][u] = f * g_padDet[u]; + } + /* Only ever once. Twenty one oscillators starting from phase zero hit the + filter as one large step; after that they are left free running, + because an analog pad does not restrike its oscillators and because a + phase reset under a 0.4 s attack would still show up as a thump. */ + if (!g_pad.seeded) { + for (i = 0; i < PAD_NOTES; i++) + for (u = 0; u < PAD_UNI; u++) + g_pad.ph[i][u] = (float)((i * 7 + u * 5) & 15) * 0.0625f; + g_pad.seeded = 1; + } + g_pad.fdk = 1.0f; /* every chord change reopens the filter */ +} + +static void padRender(float *l, float *r) +{ + float sA = 0.0f, sB = 0.0f; /* phase sums, even and odd unison */ + float qA = 0.0f, qB = 0.0f; /* pulse sums, same split */ + float dutyA, dutyB, cut, f, hpc, gain, mA, mB, bDel; + int i, u; + + g_padSendL = 0.0f; g_padSendR = 0.0f; + /* Once the release has run out there is nothing to do, and skipping is + worth it: the oscillator bank is almost all of this voice's cost. + Everything frozen here - phases, both highpasses, both SVFs - is + frozen consistently, so resuming does not step. */ + if (g_pad.tgt <= 0.0f && g_pad.env < 0.0002f) return; + + /* --- envelopes and the guitar's hold-off ------------------------ */ + g_pad.env += (g_pad.tgt - g_pad.env) + * (g_pad.tgt > g_pad.env ? PAD_ATK : PAD_REL); + g_pad.fdk *= PAD_FDK; + { + float ft = g_pad.tgt * (PAD_FSUS + (1.0f - PAD_FSUS) * g_pad.fdk); + g_pad.fenv += (ft - g_pad.fenv) + * (ft > g_pad.fenv ? PAD_FATK : PAD_FDEC); + } + { + float gt = (g_gtr.on && g_gtr.amp > 0.10f) ? 1.0f : 0.0f; + g_pad.gtrA += (gt - g_pad.gtrA) + * (gt > g_pad.gtrA ? PAD_GATK : PAD_GREL); + } + + g_pad.lfoC += PAD_LFOC_INC; if (g_pad.lfoC >= 1.0f) g_pad.lfoC -= 1.0f; + g_pad.lfoP += PAD_LFOP_INC; if (g_pad.lfoP >= 1.0f) g_pad.lfoP -= 1.0f; + + /* --- the oscillator bank ---------------------------------------- */ + dutyA = 0.5f + PAD_PWM * lsin(g_pad.lfoP * TAU); + dutyB = 1.0f - dutyA; + for (i = 0; i < PAD_NOTES; i++) { + for (u = 0; u < PAD_UNI; u += 2) { + float p = g_pad.ph[i][u] + g_pad.inc[i][u]; + if (p >= 1.0f) p -= 1.0f; + g_pad.ph[i][u] = p; + sA += p; /* saw, converted below */ + qA += (p < dutyA) ? 1.0f : -1.0f; + } + for (u = 1; u < PAD_UNI; u += 2) { + float p = g_pad.ph[i][u] + g_pad.inc[i][u]; + if (p >= 1.0f) p -= 1.0f; + g_pad.ph[i][u] = p; + sB += p; + qB += (p < dutyB) ? 1.0f : -1.0f; + } + } + /* The saw is 2*phase-1 per voice, so the sum of N of them is 2*sum - N. + Doing it once per group instead of once per oscillator saves nineteen + multiplies a sample for nothing but arithmetic. PAD_BTRIM is on the B + side only, and it is what makes the two halves the same loudness - + see the constant. */ + mA = ((sA + sA - (float)PAD_NA) * PAD_SAW + qA * PAD_PUL) + * (1.0f / (float)PAD_NA); + mB = ((sB + sB - (float)PAD_NB) * PAD_SAW + qB * PAD_PUL) + * (PAD_BTRIM / (float)PAD_NB); + + /* --- two pole highpass, before the resonance --------------------- */ + /* Ahead of the SVF on purpose. A pulse of duty d carries 2d-1 of DC, so + the PWM would otherwise pour a slowly wobbling offset straight into a + resonant filter, and the resonance would have low frequency rubbish to + amplify. Both filters are linear, so nothing else about the order + matters. */ + hpc = PAD_HP_LO + (PAD_HP_HI - PAD_HP_LO) * g_pad.gtrA; + g_pad.hp1a += (mA - g_pad.hp1a) * hpc; mA -= g_pad.hp1a; + g_pad.hp2a += (mA - g_pad.hp2a) * hpc; mA -= g_pad.hp2a; + g_pad.hp1b += (mB - g_pad.hp1b) * hpc; mB -= g_pad.hp1b; + g_pad.hp2b += (mB - g_pad.hp2b) * hpc; mB -= g_pad.hp2b; + + /* --- resonant state variable filter, one per side ---------------- */ + cut = PAD_FC0 + PAD_FCE * g_pad.fenv + PAD_FCL * lsin(g_pad.lfoC * TAU); + if (cut < PAD_FCMIN) cut = PAD_FCMIN; + if (cut > PAD_FCMAX) cut = PAD_FCMAX; + /* f = 2*sin(pi*fc/SRATE), but the argument never exceeds 0.114 rad here, + so the straight line is within 0.25 percent - under 4 cents of cutoff - + and costs one multiply. */ + f = cut * (TAU / (float)SRATE); + { + float hp = mA - g_pad.lpA - PAD_Q * g_pad.bpA; + g_pad.bpA += f * hp; + g_pad.lpA += f * g_pad.bpA; + mA = g_pad.lpA; + } + { + float fb = f * PAD_FSKEW; + float hp = mB - g_pad.lpB - PAD_Q * g_pad.bpB; + g_pad.bpB += fb * hp; + g_pad.lpB += fb * g_pad.bpB; + mB = g_pad.lpB; + } + + /* --- level: master, envelope, duck, guitar ---------------------- */ + { + float busy = (1.0f - g_duck) * PAD_DUCKN; + if (busy > 1.0f) busy = 1.0f; + if (busy < 0.0f) busy = 0.0f; + gain = PAD_LEVEL * g_pad.env * (1.0f - PAD_DUCK * busy) + * (1.0f - PAD_GDROP * g_pad.gtrA); + } + mA *= gain; mB *= gain; + + /* --- stereo: the odd group lags, then the two mirror each other -- */ + /* 31 samples is 0.70 ms, inside the precedence window, so it widens + rather than echoes. The two groups are different oscillators, not a + copy, so a mono fold down blurs instead of combing. Read before write, + so the buffer length IS the delay. */ + bDel = g_padDlyBuf[g_padDlyPos]; + g_padDlyBuf[g_padDlyPos] = mB; + if (++g_padDlyPos >= PAD_DLY) g_padDlyPos = 0; + + *l += mA * PAD_PNEAR + bDel * PAD_PFAR; + *r += mA * PAD_PFAR + bDel * PAD_PNEAR; + g_padSendL = mA * PAD_SEND; + g_padSendR = bDel * PAD_SEND; +} + +/* ---------------------------------------------------------------- gatesnare */ +/* ========================================================================== + Gated reverb snare tail - the 1980s sound. + + Four parallel combs and two short allpasses per side. The two sides share + no delay length, so this is a genuine stereo pair and not one tail panned + twice. The room is deliberately long-decaying: the GATE, not the room, + decides when the tail ends. A room that decays to nothing inside the hold + sounds like an ordinary tail with a pointless gate hanging off it. + ========================================================================== */ +#define GS_NC 4 +#define GS_NA 2 +#define GS_CMAX 1867 /* longest comb; sizes the comb buffers */ +#define GS_AMAX 419 /* longest allpass */ + +/* 16.8 to 42.3 ms - a live drum room, not a hall. All eight are prime. Inside + one side each length is about 1.31 times the last, which is far from any + small integer ratio, so the comb modes never stack up into a pitch. Across + the sides no length is near a simple ratio of any other, and that is the + whole reason the pair sounds wide instead of centred. */ +static const int g_gsCLen[2][GS_NC] = { { 743, 977, 1279, 1669 }, + { 829, 1091, 1427, 1867 } }; +/* fb = 10^(-3L/(T60*SRATE)) with T60 = 3 s, so all eight combs decay at the + same rate. Equal decay times matter: with unequal ones the short combs drop + out first and leave the long ones exposed, and the tail turns metallic + exactly where the gate is supposed to be holding it flat. Three seconds is + long on purpose - over the 170 ms hold the room falls only 3.4 dB on its + own, and the excitation is still feeding for 200 ms, which is what "stays + loud and then stops dead" actually requires. */ +static const float g_gsCFb[2][GS_NC] = { { 0.962f, 0.950f, 0.935f, 0.917f }, + { 0.958f, 0.945f, 0.928f, 0.907f } }; +/* 5.5 to 9.5 ms, again prime and different per side. Four combs on their own + flutter; these smear the flutter without being long enough to read as a + separate slap. */ +static const int g_gsALen[2][GS_NA] = { { 241, 349 }, { 281, 419 } }; + +static float g_gsC[2][GS_NC][GS_CMAX]; /* 58 KB, BSS */ +static float g_gsA[2][GS_NA][GS_AMAX]; /* 6.5 KB, BSS */ + +#define GS_HOLD 7500 /* samples wide open: 170 ms, 1.5 rows at SPR 5000 */ +#define GS_CLOSE 0.00189f /* 1/529: a linear ramp that reaches exactly 0 in 12 ms */ +#define GS_BDEC 0.99922f /* excitation burst, -60 dB in 200 ms */ +#define GS_TONEINC (335.0f / (float)SRATE) +#define GS_HPA 0.03086f /* one pole at 220 Hz, applied twice */ +#define GS_LPA 0.6312f /* one pole at 7 kHz */ +#define GS_DAMP 0.85f /* in-loop trim, one pole near 13 kHz per pass */ +#define GS_APG 0.5f /* allpass coefficient; must be < 1 for a stable pole */ +#define GS_DRIVE 0.82f /* 0.25 (mean of four combs) x 3.3 into the saturator */ +#define GS_LEVEL 0.18f /* the only level knob, and a hard per-channel ceiling */ + +static struct { + float damp[2][GS_NC]; /* one-pole state inside each comb loop */ + float lp[2], hp1[2], hp2[2]; /* input band limiting, per channel */ + float burst, tone; /* excitation envelope, shell-tone phase */ + float gate; /* 1.0 open, ramps to exactly 0.0 */ + float ext; /* optional dry-snare feed, see notes */ + int cp[2][GS_NC], ap[2][GS_NA]; + int hold; /* samples left before the gate starts closing */ + int on; +} g_gsnr; + +/* Call this from songTick() wherever the snare fires, alongside the dry + snare's own trigger. vel is 0..1 and scales only the excitation, never the + gate: the gate is keyed, so a soft hit gets a quieter room for exactly the + same 170 ms. That is the point of a key gate over a level detector - the + tail length does not wander with how hard the drum was hit. */ +static void gsnrTrig(float vel) +{ + g_gsnr.burst = vel; + g_gsnr.tone = 0.0f; /* reset so every hit has the same attack */ + g_gsnr.gate = 1.0f; + g_gsnr.hold = GS_HOLD; + g_gsnr.on = 1; +} + +/* Runs every sample. Adds only into *l and *r; it never touches the echo or + the main reverb sends, because feeding a reverb into another reverb blurs + the one edge this voice exists to produce. */ +static void gsnrRender(float *l, float *r) +{ + float x[2], acc[2], e, tone, v; + int c, t; + + /* Idle between hits: the buffers were wiped when the gate shut, so there + is nothing to run and nothing to denormalise. This voice is only alive + about a fifth of the time. */ + if (!g_gsnr.on) { g_gsnr.ext = 0.0f; return; } + + /* Excitation. The two sides get independent noise draws - two microphones + in a real room never hear the same noise realisation, and independent + noise is what makes the pair sound enormous. They share the shell tone + in phase, which gives the image a pitched anchor so the two halves still + read as one drum. 335 Hz is roughly the first shell mode above the dry + snare's 187 Hz body, and it is above the 220 Hz corner, so unlike the + body it actually survives the highpass. Without it the room is hiss. + The exact value is not free: at 330 Hz the tone sits 0.7 Hz from the + 14th mode of the 1867-sample comb, whose -3 dB width is 0.70 Hz, so it + would ring at 0.71 of that comb's full resonant gain in the RIGHT + channel only and pull the anchor off centre. 335 Hz is 6.2 widths from + the nearest mode of any of the eight combs (0.16 of peak), which is the + best available inside the 300-360 Hz window. Move it and re-check + against 44100/L and its multiples for all eight lengths. + tone stays in [0,1) so tone*TAU stays inside lsin's one-turn range. */ + e = g_gsnr.burst; + tone = lsin(g_gsnr.tone * TAU) * 0.25f * e; + g_gsnr.tone += GS_TONEINC; + if (g_gsnr.tone >= 1.0f) g_gsnr.tone -= 1.0f; + x[0] = noiseSample() * 0.85f * e + tone + g_gsnr.ext; + x[1] = noiseSample() * 0.85f * e + tone + g_gsnr.ext; + g_gsnr.ext = 0.0f; + /* The send outlasts the hold on purpose. If it died first the room would + already be coasting when the gate shut and the shut would sound like the + end of a decay rather than a cut. */ + g_gsnr.burst = e * GS_BDEC; + + /* Band limiting on the way in, so nothing outside 220 Hz - 7 kHz ever gets + into the feedback loops in the first place. The highpass is two cascaded + poles, not one: this is the only hard panned wideband element in the mix + and a single pole still leaves about -7 dB at 110 Hz, which would put + stereo content under the 140 Hz mono line. Two poles give -11 dB at + 140 Hz and -21 dB at 70 Hz. */ + for (c = 0; c < 2; c++) { + g_gsnr.lp[c] += (x[c] - g_gsnr.lp[c]) * GS_LPA; + v = g_gsnr.lp[c]; + g_gsnr.hp1[c] += (v - g_gsnr.hp1[c]) * GS_HPA; v -= g_gsnr.hp1[c]; + g_gsnr.hp2[c] += (v - g_gsnr.hp2[c]) * GS_HPA; v -= g_gsnr.hp2[c]; + x[c] = v; + } + + for (c = 0; c < 2; c++) { + float a = 0.0f; + for (t = 0; t < GS_NC; t++) { + float *b = &g_gsC[c][t][0]; + int p = g_gsnr.cp[c][t]; + float d = b[p]; + /* Read first, then write: the comb output carries the delayed + signal only. The dry snare is already centre in the mix and must + not leak back out here, hard panned. The damping pole is only a + gentle trim - it barely bites in one pass, but the tail goes + round seven or eight times inside the hold and compounded that is + enough to stop the last of the tail being pure top end. */ + g_gsnr.damp[c][t] += (d - g_gsnr.damp[c][t]) * GS_DAMP; + b[p] = x[c] + g_gsnr.damp[c][t] * g_gsCFb[c][t]; + a += d; + if (++p >= g_gsCLen[c][t]) p = 0; + g_gsnr.cp[c][t] = p; + } + for (t = 0; t < GS_NA; t++) { + float *b = &g_gsA[c][t][0]; + int p = g_gsnr.ap[c][t]; + float d = b[p]; + /* Schroeder allpass, and the three lines below have to be exactly + this: the value fed back into the line is w, and the output is + d - g*w, NOT d - g*input. Subtracting g times the input instead + gives ((1+g*g)z^-N - g)/(1 - g z^-N), which is a comb with 2 dB + of ripple and 1.17 to 1.5 of gain - two of them in series then + throw up to 2.25x into the saturator, so the diffusers colour + the tail and quietly wreck the GS_DRIVE gain staging. With the + form below the magnitude is flat and only the phase is smeared, + which is the entire point of putting them here. */ + float w = a + d * GS_APG; + b[p] = w; + a = d - w * GS_APG; + if (++p >= g_gsALen[c][t]) p = 0; + g_gsnr.ap[c][t] = p; + } + acc[c] = a; + } + + /* Saturate, then gate, in that order. The 1980s tail was a room squashed + flat by a compressor before it hit the gate, and softClip standing in for + that compressor does two useful things: it flattens the plateau further, + and because it asymptotes at 1.0 this voice can never put more than + GS_LEVEL into a channel however hot the room gets. Gating after the + saturator keeps the closing ramp linear; gating first would let the + saturator partly undo it and soften the shut. Hard +-1.00: each side + gets only its own room, no cross-feed. */ + *l += softClip(acc[0] * GS_DRIVE) * g_gsnr.gate * GS_LEVEL; + *r += softClip(acc[1] * GS_DRIVE) * g_gsnr.gate * GS_LEVEL; + + if (g_gsnr.hold > 0) { + g_gsnr.hold--; + } else { + /* Linear, not exponential. An exponential never reaches zero and leaves + a whisper of room under the next bar; linear lands on exactly 0.0, + which is both the sound we want and what lets us wipe the buffers. + The wipe is the "stops dead": without it the frozen room, still only + a few dB down when the gate shut, would replay as a ghost behind the + next hit. It also guarantees the loops start every hit from silence + so nothing can accumulate over a long song. */ + g_gsnr.gate -= GS_CLOSE; + if (g_gsnr.gate <= 0.0f) { + g_gsnr.gate = 0.0f; + g_gsnr.on = 0; + /* Clear per line rather than the whole array: only the first + g_gsCLen entries of each buffer are ever read, so this is 39 KB + of stores instead of 66 KB in one sample slot. It is still the + most expensive sample this voice ever runs, which is fine with a + block-based renderer and worth knowing about if you ever go to + single-sample callbacks. */ + for (c = 0; c < 2; c++) { + for (t = 0; t < GS_NC; t++) + memset(g_gsC[c][t], 0, g_gsCLen[c][t] * sizeof(float)); + for (t = 0; t < GS_NA; t++) + memset(g_gsA[c][t], 0, g_gsALen[c][t] * sizeof(float)); + } + memset(g_gsnr.damp, 0, sizeof(g_gsnr.damp)); + memset(g_gsnr.lp, 0, sizeof(g_gsnr.lp)); + memset(g_gsnr.hp1, 0, sizeof(g_gsnr.hp1)); + memset(g_gsnr.hp2, 0, sizeof(g_gsnr.hp2)); + } + } +} + +/* ---------------------------------------------------------------- duckbass */ +/* ========================================================================= + state - file-scope, goes next to the other voice statics in synth.h, + after g_gtr / g_sol / g_org and before g_kickAmp. + ========================================================================= */ + +/* ---- sidechained pumping bass ------------------------------------------- + Owns 60-140 Hz and is mono, so it is the one thing holding the bottom of + the mix together. The duck is keyed off the kick trigger rather than off a + level detector, which is how the compressor is really patched in this + genre: the gain starts moving on the same sample the kick does instead of + waiting for a detector to charge, and it ducks by exactly the same amount + whether the kick lands under a loud bar or a quiet one. + + Nothing here touches g_duck. That one is the lead-priority duck, it moves + over seconds, and it is fed by which melodic voice is busy; this one moves + over milliseconds and is fed by the drum part. They multiply cleanly + because they are answering different questions. + -------------------------------------------------------------------------- */ + +/* Duck depth. 0.35 is about -9 dB, which is deep enough that the kick has the + band to itself and shallow enough that the bass is still audible under it. + Below about 0.25 this stops being a compressor and becomes a gate: you hear + the bass switch off rather than lean out. Above 0.5 the pump disappears. */ +#define PB_FLOOR 0.35f + +/* One-pole coefficient for the gain itself, tau = 1 ms, so it covers 95 + percent of the drop in 3 ms (measured: 130 samples, 2.95 ms). Faster than + about 2 ms and the gain step itself becomes a click; slower than about 4 ms + and the kick transient is already past before the bass gets out of its way, + which reads as the bass being late rather than as the kick being big. */ +#define PB_ATK 0.0227f + +/* Recovery ramp: 1 / (0.150 s * SRATE). 150 ms sits in the middle of the + usable window. At this tempo a row is 116 ms and an eighth is 227 ms, so + the pump completes between eighth-note kicks and is still climbing when + the kicks are on sixteenths. Much under 90 ms and the bass is back at full + while the kick body is still ringing and they fight; much over 200 ms and + the bass never reaches full level, so the part just sounds quiet rather + than pumping. */ +#define PB_REL_INC 0.00015117f + +/* How far the recovery curve is pulled from a smoothstep toward r*r. This is + the entire audible character of the pump. At 0 it is a plain smoothstep; + at 1 it is a pure square law, a long flat floor and then a sudden swell, + which is the obvious cheesy version. 0.30 holds the floor for the first + ~30 ms, does most of the climb through the middle, and eases into unity. + Measured gain after a kick: 0.35 at 0 ms, 0.38 at 24 ms, 0.48 at 49 ms, + 0.62 at 74 ms, 0.76 at 99 ms, 0.90 at 124 ms, 1.00 at 151 ms. */ +#define PB_HOLD 0.30f + +/* Two cascaded one-poles at 190 Hz. One pole was not enough to own a band: + at 350 Hz it is only 6 dB down. Cascaded, and measured on the real output, + the 250-350 region sits 18 dB under the 60-140 region, comfortably past the + 3 dB the allocation table asks for. Raising this coefficient walks the + corner up and fills 250-350 back in, which is the guitar's air. */ +#define PB_LP_A 0.02707f + +/* 38 Hz highpass, TAU * 38 / SRATE. Costs 1.3 dB at C2 and 0.6 dB at G2, and + it is insurance rather than repair - see the comment at the filter. */ +#define PB_HP_A 0.005414f + +/* Drive into the saturator, and the trim after it. softClip(1.35) is 0.897, + so the drive squashes the loudest part of the waveform by about 1 dB; the + 0.85 is then a plain trim on top of that, not a unity make-up, and it is + part of what sets the voice's level along with PB_LEVEL. Restoring the + peak exactly would need 1.116, which would make this voice noticeably + louder than g_vBass. */ +#define PB_DRIVE 1.35f + +/* PB_LEVEL 0.22 costs a full voice of CPU for +0.26 dB in 60-140 Hz once + the bus compressor (3:1 at 0.30), the tape drive and the limiter have + had it - measured against a build with the voice absent. If this layer + is meant to be heard rather than merely present, 0.30 buys +0.71 dB and + 0.40 buys +1.39 dB, and the output peak is unchanged at 0.799 in all + three cases because the limiter is holding it. */ +#define PB_LEVEL 0.22f + +/* Envelope. The decay and the sustain leak are the existing bass's, and so is + the linear release, so the two layers rise and fall together when they are + both playing the part. The release has to be here and it has to be linear: + an exponential-only tail takes 2.3 seconds to reach silence, which is the + drone this file's voiceEnv comment is about. */ +#define PB_DECAY 0.99992f +#define PB_SUSTAIN 0.45f +#define PB_LEAK 0.99997f +#define PB_SLEW 0.02f /* tau = 1.1 ms; the note attack de-clicker */ +#define PB_REL (1.0f / (0.110f * (float)SRATE)) /* matches g_vBass */ + +/* Fold window. Anything outside it is moved by octaves rather than allowed + to leave the band this voice owns. D3 (146.8 Hz) is deliberately inside + the window. Note that this is not a rare edge case in the current song: + the bass part runs up to C4, and midi 40, 42, 43, 45, 46, 47 and 48 all + get dropped an octave, so this layer plays a flatter contour than g_vBass + over those passages. That is the intended trade - it keeps the weight in + band - but it is why the two layers are not simply the same line. */ +#define PB_FOLD_HI 150.0f +#define PB_FOLD_LO 58.0f + +typedef struct { + float phase, inc; /* oscillator */ + float amp, sustain, env; /* note envelope */ + float lp1, lp2, hp; /* band filters */ + float duck, rel; /* sidechain gain, and its recovery ramp */ + int gate, active; /* note held / voice running, as in Voice */ +} PumpBass; + +/* duck and rel start at 1: before the first kick the voice is not ducked. + gate and active start at 0, so nothing renders until the first note. */ +static PumpBass g_pb = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 1.0f, 1.0f, 0, 0 }; + + +/* ========================================================================= + trigger - anywhere above songTick(), which is the only caller. + ========================================================================= */ + +/* Stop the voice dead and clear everything it was holding. The filter states + have to go with it: they track the oscillator, not the envelope, so at the + moment a note ends they are sitting at whatever point of a full-amplitude + waveform the oscillator reached - measured -0.47 and -0.26, not the small + leftovers you would expect. Leaving them there is not a denormal risk, it + is the opposite, but it does mean the next note starts by flushing a stale + half-scale value through the filters. It is inaudible because the attack + slew covers it, and it is still wrong to leave lying around. */ +static void pumpBassOff(void) +{ + g_pb.active = 0; + g_pb.env = 0.0f; + g_pb.amp = 0.0f; + g_pb.sustain = 0.0f; + g_pb.lp1 = 0.0f; + g_pb.lp2 = 0.0f; + g_pb.hp = 0.0f; +} + +static void pumpBassNote(int note) +{ + float f = noteFreq(note); + while (f > PB_FOLD_HI) f *= 0.5f; + while (f < PB_FOLD_LO) f *= 2.0f; + g_pb.inc = f / (float)SRATE; + g_pb.amp = 1.0f; + g_pb.sustain = PB_SUSTAIN; + g_pb.gate = 1; + g_pb.active = 1; + /* The phase is deliberately not reset. At 80 Hz a phase jump is a step of + most of the waveform's amplitude, and on a voice this low that is a + click, not an attack. A new note only needs the pitch to change. */ +} + +/* Called where the existing bass releases, so a phrase does not drone through + the patterns that contain no bass at all. Clearing the gate is what ends + the note; clearing sustain as well just means amp is already heading down + if the release is ever lengthened. */ +static void pumpBassRelease(void) +{ + g_pb.gate = 0; + g_pb.sustain = 0.0f; +} + +/* Called from songTick on the same line that fires the kick. Resetting the + ramp is the whole trigger: it drops the target to the floor and the gain + slews down to meet it. Retriggering mid-pump is therefore free of clicks, + because the gain is never assigned, only chased. */ +static void pumpBassKick(void) +{ + g_pb.rel = 0.0f; +} + + +/* ========================================================================= + render - above renderAudio(). Uses softClip and noteFreq only, both of + which are already defined earlier in the file. + ========================================================================= */ + +static void renderPumpBass(float *l, float *r) +{ + float q, s3, shp, tgt, raw, s; + + /* The duck runs whether or not a note is sounding, so the envelope is + already in the right place when a note arrives in the middle of a + pump - which, on the downbeat, is every time. */ + if (g_pb.rel < 1.0f) { + g_pb.rel += PB_REL_INC; + if (g_pb.rel > 1.0f) g_pb.rel = 1.0f; + } + q = g_pb.rel * g_pb.rel; + s3 = q * (3.0f - 2.0f * g_pb.rel); /* smoothstep */ + shp = s3 + (q - s3) * PB_HOLD; /* pulled toward r*r: see PB_HOLD */ + tgt = PB_FLOOR + (1.0f - PB_FLOOR) * shp; + /* One pole chasing a shaped target does both jobs. Against the 3 ms + attack the ramp has only moved 2 percent, so the target is still the + floor and the drop is the pole's alone; against the 150 ms recovery the + pole lags by 1 ms, which is nothing except at the very top, where it + rounds off the corner where the curve meets unity. */ + g_pb.duck += (tgt - g_pb.duck) * PB_ATK; + + if (!g_pb.active) return; + + g_pb.phase += g_pb.inc; + if (g_pb.phase >= 1.0f) g_pb.phase -= 1.0f; + /* The saw runs DOWNWARD here, which is the one place this voice departs + from the existing bass and it is not cosmetic. A rising saw and an + in-phase square have fundamentals of opposite sign: 0.65 * -0.637 plus + 0.35 * +1.273 leaves 0.032, so the existing bass's own fundamental is + cancelled to nothing and its loudest partial is the octave, 16 dB above + the note. That is a fine mid-bass timbre and a useless 60-140 Hz voice. + Falling, the two add to 0.859 and every harmonic is at least 9 dB down + before the filter even runs. */ + raw = (1.0f - g_pb.phase * 2.0f) * 0.65f + + ((g_pb.phase < 0.5f) ? 0.35f : -0.35f); + /* Driven before the lowpass, never after. Saturating a filtered 98 Hz + tone would fold its third harmonic straight into 294 Hz, which is the + band this voice is required to stay out of. */ + raw = softClip(raw * PB_DRIVE) * 0.85f; + + g_pb.lp1 += (raw - g_pb.lp1) * PB_LP_A; + g_pb.lp2 += (g_pb.lp1 - g_pb.lp2) * PB_LP_A; + + g_pb.amp = (g_pb.amp - g_pb.sustain) * PB_DECAY + g_pb.sustain; + g_pb.sustain *= PB_LEAK; + if (g_pb.gate) { + g_pb.env += (g_pb.amp - g_pb.env) * PB_SLEW; + /* A held note whose sustain has leaked away is over too. Without this + the voice would keep running silently for the rest of the demo. */ + if (g_pb.amp < 0.0004f && g_pb.env < 0.0002f) { pumpBassOff(); return; } + } else { + /* Linear, and for the reason the voiceEnv comment above gives: an + exponential release of the same audible length is still ringing + seconds later. From a typical env of 0.7 this reaches silence in + 76 ms, and the sample it stops on is 1.3e-4, far below the + waveform's own sample-to-sample motion, so there is no click. */ + g_pb.env -= PB_REL; + if (g_pb.env <= 0.0f) { pumpBassOff(); return; } + } + + s = g_pb.lp2 * g_pb.env * g_pb.duck * PB_LEVEL; + + /* Highpass last, after the gain rather than before it. Measured, this is + insurance and not repair: bypass it and the sub-38 Hz content is still + 42 dB under the 60-140 band, because the waveform is odd-symmetric + (raw(1-ph) == -raw(ph)) and softClip is odd, so its DC is exactly zero, + and multiplying an AC signal by a gain step makes sidebands rather than + DC. What it does buy is 4.6 dB off the rumble the 3 ms gain step and + the note attack put down there, and a guarantee that no DC reaches the + master bus if anyone later makes the waveform asymmetric. It costs + 0.6 dB at G2, so do not add a second one. */ + g_pb.hp += (s - g_pb.hp) * PB_HP_A; + s -= g_pb.hp; + + *l += s; *r += s; /* mono: everything under 140 Hz is centred */ +} + + +/* ========================================================================= + notes - corrected integration instructions + ========================================================================= + +WHERE THE CODE GOES (all of it in cracktro/synth.h) + - 'state' next to the other voice statics, after g_gtr / g_sol / g_org and + before g_kickAmp. One struct, 48 bytes, no arrays: nothing lands on the + stack and the 2 KB rule is not in play. + - 'trigger' anywhere above songTick(). pumpBassOff() must come before + renderPumpBass(), which calls it. + - 'render' above renderAudio(). + +FOUR CALL SITES. Three are inside songTick()'s `if (g_playTick == 0)` block. + 1. THE RELEASE. The original draft of these notes pointed at + `if (r == 0) { g_vLead.sustain = 0.0f; g_vBass.sustain = 0.0f; }`. + That line does not exist. The existing bass releases from a rest + counter, at what is currently synth.h:353: + else if (++g_bassRest >= 4) g_vBass.gate = 0; + becomes + else if (++g_bassRest >= 4) { g_vBass.gate = 0; pumpBassRelease(); } + This fires only 11 times in the whole song, but three of those are the + 32-, 33- and 64-row rests where the arrangement clears the bass + entirely. Miss it and the voice plays through them. + 2. `n = g_sqBass[k]; if (n >= 0) { trigVoice(&g_vBass, n, ...); ... }` + add `pumpBassNote(n);` inside that same `if`, where n is known >= 0. + pumpBassNote does not range-check, and noteFreq() would index + g_baseFreq[-1] on a negative note - the same exposure trigVoice has. + 3. `case 1: g_kickAmp = 1.0f; ... break;` add `pumpBassKick();` there. + This is the sidechain key. Verified: `case 1:` is the only place + g_kickAmp is set to 1.0f, so this one site is sufficient today. If a + fill or a section change ever fires a kick from somewhere else, key it + there too, or the pump silently stops for that bar. + 4. In renderAudio()'s per-sample loop, immediately AFTER the + `if (g_vBass.active) { ... }` block closes - not inside it, because the + duck has to keep running when no bass note is sounding: + renderPumpBass(&l, &r); + +ROUTING - three things it must NOT be connected to: + - Not to dl/dr. The drum bus has its own parallel compressor with a 0.045 + threshold (confirmed at synth.h:635); a bass peaking near 0.09 would + hold that compressor down permanently and flatten the kick. + - Not to sendL/sendR. The echo is two rows, 10000 samples; a 98 Hz tone + repeated at 227 ms is mud. + - Not to rvL/rvR. The reverb combs are 1237-3457 samples, i.e. 28-78 ms. + A bass note's period is 8-15 ms, so those combs are not a room for this + voice, they are a comb filter that will notch or double the fundamental + differently for every note in the part. + +THE ONE CHANGE THAT WILL SILENTLY BREAK IT + The falling saw. Confirmed analytically and by DFT: the house pairing + saw*0.65 + square*0.35 with a RISING saw has H1 = +0.0318 and H2 = -0.2069, + so its second harmonic is 16 dB louder than its fundamental. Falling, H1 = + 0.8594. "Tidying" the oscillator back to match g_vBass drops this voice's + fundamental by 28 dB and moves its energy to the octave, where the lowpass + then removes it. If anyone touches that line, re-measure 60-140 Hz. + +LEVEL, AND SHARING THE BAND WITH g_vBass + In isolation this voice peaks at 0.093 under eighth-note kicks against + g_vBass's 0.102, and 0.118 on a note that lands without a kick. In the + finished master it is much less than that sounds: measured end to end + against a build with the voice absent, PB_LEVEL 0.22 adds +0.26 dB to + 60-140 Hz and +0.31 dB of tilt against 1-4 kHz. The bus compressor + (3:1 at 0.30), the tape drive and the limiter give the rest back. Sweep, + same song, same analysis: + PB_LEVEL 0.22 -> +0.26 dB band, +0.31 dB tilt, peak 0.800 + PB_LEVEL 0.30 -> +0.71 dB band, +0.86 dB tilt, peak 0.799 + PB_LEVEL 0.40 -> +1.39 dB band, +1.73 dB tilt, peak 0.799 + The peak does not move because the limiter is holding it, so raising the + level is safe. The risk with this voice is that it is inaudible, not that + it is too heavy. Because of the fundamental cancellation above, g_vBass's + audible energy is at the octave while this one's is at the note, so they + stack rather than double. To replace g_vBass outright, mute it and raise + PB_LEVEL to about 0.30; the part will get lower and much simpler. + +TUNING CONSTANTS, WHAT THEY MEAN + PB_FLOOR duck depth, 0.35 = -9 dB. Gate below 0.25, inaudible above 0.5. + PB_ATK one-pole, tau 1 ms -> measured 2.95 ms to reach the floor. + PB_REL_INC 1/(0.150 s * SRATE). Measured 151 ms to unity. + PB_HOLD the pump's character; 0 = soft smoothstep, 1 = cheesy swell. + PB_LP_A two poles at 190 Hz. This is what enforces the allocation. + PB_HP_A 38 Hz. Insurance, not repair - see the comment at the filter. + PB_REL the note release. Must match g_vBass's 110 ms and must stay + linear; an exponential of the same audible length still rings + seconds later. + PB_LEVEL see the sweep above. + PB_FOLD_* raise PB_FOLD_HI only if you want this layer to follow g_vBass + above D3. In the current song that would affect 7 of the 19 + pitches the part uses. + +MEASURED, on the compiled output rather than asserted + - attack 130 samples = 2.95 ms to within 5 percent of the floor + - recovery reaches unity at 151 ms, curve as listed under PB_HOLD + - 250-350 Hz band energy sits 18 dB under 60-140 Hz (table asks for 3) + - harmonics of a G2: 196 Hz -19.7 dB, 294 Hz -18.0 dB, 490 Hz -29.5 dB + - sub-38 Hz sits 47 dB under 60-140 with the highpass, 42 dB without + - release tail 76 ms from a typical env of 0.70; terminal sample 1.3e-4 + - note-on after a full stop: max sample step 0.003195, against 0.003180 + on a retrigger while already sounding, so the attack is not a click + - left and right outputs are bit-identical, so it is genuinely mono + +DENORMALS + Safe without relying on FTZ. The duck converges toward 1.0, not 0. The two + lowpasses are re-excited by every sample the voice is audible, and + pumpBassOff() zeroes them outright when the voice stops rather than + leaving them frozen mid-waveform. + +COST + About 25 flops, one softClip and three data-dependent branches per sample, + with no memory traffic outside the one struct. + +VERIFICATION + This exact text was pasted into a copy of cracktro/synth.h with the four + call sites above, and the whole demo was built with the project's own + flags - cl /O2 /Oi /Ot /GS- /Gy /fp:fast /std:c11 /W3 and link + /SUBSYSTEM:WINDOWS /ENTRY:entry /NODEFAULTLIB - with zero warnings and a + clean link, which is also the proof that nothing here reaches for libm or + __chkstk. It was then rendered offline to dump.wav and compared against a + baseline render with the voice absent; every dB figure quoted above comes + from that comparison or from a standalone unit harness, not from an + estimate. The user's own tree was not modified: all builds ran against + copies under the session scratchpad. +*/ + +/* ---------------------------------------------------------------- fmbell */ +/* ========================================================================== + S T A T E + ========================================================================== + Paste this block into synth.h after g_duck (line 227) and before + songTick() (line 329): songTick has to see bellTrig/epTrig, and + renderAudio (line 424) has to see fmRender. g_gtr (line 144), g_duck + (line 227), noteFreq (line 90) and lsin all exist above that point. + ========================================================================== */ + +/* ========================================================================== + 2 O P E R A T O R F M - bell and electric piano + -------------------------------------------------------------------------- + Both presets are the same four lines of arithmetic: a modulator sine added + into a carrier sine's phase argument. Everything that separates a struck + bell from a Rhodes lives in two numbers - the carrier/modulator frequency + ratio, and how fast the modulation index falls compared with the amplitude. + ========================================================================== */ + +typedef struct { + float cph, mph; /* carrier / modulator phase, 0..1 turns */ + float cinc, minc; /* per sample phase increments */ + float idx, idxFloor; /* modulation index and its resting value, RAD */ + float idxDec; /* per sample index decay multiplier */ + float amp, ampDec; + float atk; /* 0..1 attack ramp, kills the retrigger step */ + float hp, hp2, lp; /* one pole filter states */ + float split; /* e-piano only: body/tine crossover state */ + int on; +} FmVoice; + +static FmVoice g_bell, g_ep; + +/* ---- BELL --------------------------------------------------------------- + Ratio 7:5. Non-integer, so the sidebands land at f*|1 +- 1.4k| = + 0.4, 1, 1.8, 2.4, 3.2, 3.8, 4.6, 5.2 ... f. Those share no common + fundamental, which is exactly what makes metal read as struck rather than + plucked. 3.5 is the other classic choice and is more clangorous, but its + sidebands sit 2.5x further apart: at index 6.5 the top one lands near + f*33, which is past Nyquist for anything above D5 and folds back as + aliasing. 1.4 keeps the whole spectrum under Nyquist across the entire two + octaves the bell is allowed to play. + + At index 6.5 the significant sideband orders reach about N = I + 1.5*I^(1/3) + ~= 9, so the top partial sits at f*(1 + 1.4*9) = 13.6*f. On C5 that is + 7.1 kHz - the lid of the bell's allocation. The index was chosen for that, + not the other way round. + + The 0.4*f difference partial is BELOW the allocation floor (209 Hz on C5). + That is what the 400 Hz highpass is aimed at; it is not a generic + tidy-up. Real bells have a hum partial there too, and here it would sit in + the bass guitar's lap. */ +#define BELL_RATIO 1.4f +#define BELL_IDX0 6.5f /* radians of peak phase deviation */ +#define BELL_IDX_FLR 0.35f /* the tail keeps a little inharmonicity; */ + /* at 0 the ring decays to a pure sine and */ + /* stops sounding like metal halfway down */ +#define BELL_IDX_DEC 0.9998866f /* e-fold 0.20 s: the clang is gone in */ + /* about a third of a second */ +#define BELL_AMP_DEC 0.9999858f /* e-fold 1.6 s -> ~4 s of audible ring. */ + /* Eight times slower than the index, and */ + /* that ratio IS the bell. Bring it under */ + /* about 4x and it turns into a marimba. */ +#define BELL_GATE 0.0012f /* amp reaches this 10.7 s after the */ + /* strike, so the voice keeps stepping for */ + /* ~7 s after it stops being audible. That */ + /* is a CPU cost, not a sound: at this */ + /* level the output is 20 LSBs of a 16 bit */ + /* sample, so cutting it is inaudible. */ + +/* NOTE NUMBERS ARE NOT MIDI. This file's numbering is octave*12 + pitchclass + with C-0 == 0 (see the nC5 / nG3 macros at the top of synth.h), so nC5 is + 60, not 72. Written as MIDI these four constants would put both presets an + octave high, and the bell's top note would become E7 = 2637 Hz, whose top + sideband at 13.6*f is 35.9 kHz - folded back around Nyquist as exactly the + metallic buzz the ratio was chosen to avoid. Use the note macros, not + numbers, so the next reader cannot make the same mistake. */ +#define BELL_LO nC5 /* 523 Hz, the allocation floor */ +#define BELL_HI nE6 /* 1318 Hz; above this the top sideband */ + /* passes 18 kHz and the sine backend */ + /* (table or polynomial) gets gritty */ +#define BELL_GAIN 0.22f /* peaks alongside the lead's 0.20 and the */ + /* solo's 0.30, which is where a bell that */ + /* plays twice a bar belongs */ + +/* ---- E-PIANO ------------------------------------------------------------ + Ratio 1:1, so every sideband lands on a harmonic and the sustain is a + plain periodic tone. The whole DX7 electric piano is the index envelope: + a short, steep fall from 3.2 rad (5 audible harmonics, a hard woody bark) + to a floor of 0.20 (barely more than a sine, with just enough second and + third to stay alive). The bark is over in ~150 ms while the note rings for + seconds, and that mismatch is what the ear reads as a hammer hitting a + tine. A real DX7 EP adds a 14:1 operator for the tine click; with only two + operators the fast index attack has to carry it, which is why 3.2 and not + something politer. */ +#define EP_RATIO 1.0f +#define EP_IDX0 3.2f +#define EP_IDX_FLR 0.20f +#define EP_IDX_DEC 0.9995877f /* e-fold 55 ms - the bark. Four times */ + /* faster than the bell's, on purpose */ +#define EP_AMP_DEC 0.9999733f /* e-fold 0.85 s, a Rhodes without pedal */ +#define EP_GATE 0.0008f /* 5.7 s of stepping, same trade as above */ +#define EP_LO nG3 /* 196 Hz */ +#define EP_HI nG5 /* 784 Hz; at that note the 4th harmonic */ + /* is already at the 3 kHz lid and the */ + /* lowpass starts eating the tone */ +#define EP_GAIN 0.18f + +/* Shared 1.5 ms attack ramp. Both a bell hammer and a Rhodes tine take a + millisecond or two to get going, and starting from a hard zero is what + puts a tick on the front of every FM note. */ +#define FM_ATK 0.015f + +/* One pole coefficients, a = 1 - exp(-TAU*fc/SRATE). */ +#define BELL_HP 0.0554f /* 400 Hz. One pole, so this CUTS the */ + /* 0.4*f hum partial by 6.9 dB at 209 Hz, */ + /* it does not remove it. Together with */ + /* J1(6.5) being 16 dB under the carrier */ + /* that puts the partial 23 dB down, which */ + /* is out of the bass's way. A second pole */ + /* here would start thinning the strike. */ +#define BELL_LP 0.680f /* 8 kHz allocation lid (a tilt, not a */ + /* wall - it is one pole, -2.6 dB at 8 kHz */ + /* and -5.2 dB at 16 kHz) */ +#define EP_HP 0.0253f /* 180 Hz, applied TWICE. One pole leaves */ + /* 140 Hz only 4.3 dB down, and the mix */ + /* rule is that nothing below 140 Hz may */ + /* be off centre. Two poles put 140 Hz at */ + /* -8.7 dB and 100 Hz at -12.8 dB. What */ + /* actually keeps this voice out of the */ + /* mono band is that its lowest note is */ + /* 196 Hz (-5.5 dB through the pair) and */ + /* there is nothing below it but the */ + /* strike transient. Do not "simplify" */ + /* this back to one pole; if you ever let */ + /* EP_LO down an octave, this filter is */ + /* the thing that has to move first. */ +#define EP_LP 0.348f /* 3 kHz allocation lid */ +#define EP_SPLIT 0.1924f /* 1.5 kHz body/tine crossover, see render */ + +/* Equal power pan gains: g = cos/sin((1+p)*PI/4). */ +#define PAN62_FAR 0.9558f /* p = 0.62 */ +#define PAN62_NEAR 0.2940f +#define PAN30_FAR 0.8526f /* p = 0.30 */ +#define PAN30_NEAR 0.5225f + +/* The mirrored Haas copy sits 0.7 dB under the dry. At unity the two copies + are a symmetric pair of strikes and the image collapses to a hole in the + middle; much below this and precedence stops working, the copy reads as a + separate slapback event, and the left side is left uncounterweighted. + 0.92 leaves the long term left/right energy 0.6 dB apart, which is inside + the counterweight tolerance. */ +#define HAAS_LEVEL 0.92f + +/* 40 ms = 1764 samples. Strictly feedforward - nothing is fed back into it, + so it cannot ring and it has no tail to decay into denormals. */ +#define BELL_HAAS (SRATE * 40 / 1000) +static float g_bellHaas[BELL_HAAS]; +static int g_bellHaasPos = 0; + +/* Reverb send, written fresh every sample. The required render signature is + (l, r) only, so the send leaves by the side door; add these to rvL/rvR in + renderAudio and the bell gets its tail. Ignoring them costs two stores a + sample and nothing else. */ +static float g_fmSendL, g_fmSendR; + +/* Both presets sit 0.20 into the duck bus this file already maintains + (g_duck is 1.0 when nothing louder is playing): 20 percent depth is + 1.9 dB at full duck, enough to clear the lead without the listener + hearing a pump. This macro is the only place g_duck is named. */ +#define FM_DUCK (0.80f + 0.20f * g_duck) + + +/* ========================================================================== + T R I G G E R + ========================================================================== */ + +/* Advances both operators one sample and returns the enveloped carrier. + Kept separate from the render so the two presets share it verbatim. */ +static float fmStep(FmVoice *v) +{ + float m, x; + + v->mph += v->minc; if (v->mph >= 1.0f) v->mph -= 1.0f; + v->cph += v->cinc; if (v->cph >= 1.0f) v->cph -= 1.0f; + + m = lsin(v->mph * TAU); + /* The index is in radians because it is added to a radian argument. The + argument goes negative and past one turn constantly here, by up to + +-6.5 rad, and both of this project's lsin backends survive that: the + software build masks its table index, the D3D11 build's fsin_ range + reduces first. Neither of those is optional any more. */ + x = lsin(v->cph * TAU + v->idx * m); + + v->idx = (v->idx - v->idxFloor) * v->idxDec + v->idxFloor; + v->atk += (1.0f - v->atk) * FM_ATK; + v->amp *= v->ampDec; + + return x * v->amp * v->atk; +} + +static void fmTrig(FmVoice *v, int note, float vel, + float ratio, float idx0, float idxFloor, + float idxDec, float ampDec) +{ + float f, b, env, idxNew, m; + + if (vel < 0.0f) vel = 0.0f; + if (vel > 1.0f) vel = 1.0f; + f = noteFreq(note) / (float)SRATE; + + /* In FM, velocity is mostly brightness. Scaling the index far harder + than the level is what makes a soft note sound soft rather than + merely quiet - it is the single thing that separates a played FM + patch from a sequenced one. */ + b = 0.40f + 0.60f * vel; + idxNew = idx0 * b; + env = v->amp * v->atk; /* the envelope value we are standing on */ + + if (env < 0.02f) { + /* Faded out: start clean from zero phase, which is silent, so there + is nothing to step away from. */ + v->cph = 0.0f; v->mph = 0.0f; + env = 0.0f; + } else { + /* Still ringing, so the old waveform is allowed to continue - that + is what a second strike on a live bell does. But two things used + to step here in a single sample, both plainly audible on a voice + sitting at three quarters of full level: the envelope jumped + (+2.7 dB on a strike half a second in) and the carrier's argument + jumped by (idxOld - idxNew)*m, up to +-6.15 rad. + + The index has to change instantly - the index transient IS the + strike - so instead of ramping it, offset the carrier phase by + exactly the amount the index change moved the argument. The total + argument is then continuous across the seam and only its rate of + change differs, which is what "brighter from here on" should + sound like. The offset is a constant phase shift afterwards and a + bell does not care about absolute phase. */ + m = lsin(v->mph * TAU); + v->cph += (v->idx - idxNew) * m * (1.0f / TAU); + while (v->cph >= 1.0f) v->cph -= 1.0f; /* |offset| < 1.04 turns, */ + while (v->cph < 0.0f) v->cph += 1.0f; /* so at most two passes */ + } + + v->cinc = f; + v->minc = f * ratio; + v->idx = idxNew; + v->idxFloor = idxFloor * b; + v->idxDec = idxDec; + v->amp = 0.45f + 0.55f * vel; + v->ampDec = ampDec; + /* Leave amp*atk exactly where it was and let the 1.5 ms ramp carry it to + the new level. FM_ATK is documented as the thing that kills the + retrigger step; setting atk to 0 or leaving it at 1 both step. Dividing + is safe because amp is never below 0.45. atk landing above 1 (a soft + strike over a loud tail) is fine - the ramp slides back down to 1. */ + v->atk = env / v->amp; + v->on = 1; +} + +static void bellTrig(int note, float vel) +{ + if (note < 0) return; /* RST is not a note */ + /* Fold by octaves rather than clamp: a clamp turns a phrase into a + repeated note, folding keeps the pitch class. The range spans more + than an octave, so one pass of each loop always lands inside it. */ + while (note < BELL_LO) note += 12; + while (note > BELL_HI) note -= 12; + fmTrig(&g_bell, note, vel, BELL_RATIO, BELL_IDX0, BELL_IDX_FLR, + BELL_IDX_DEC, BELL_AMP_DEC); +} + +static void epTrig(int note, float vel) +{ + if (note < 0) return; + /* The e-piano is allocated the guitar's band and the guitar's space, so + it plays only where the guitar does not. Calling this under a ringing + guitar is not a mistake - this test is the gate, and it lives here so + the arrangement cannot forget it. 0.05 is about 0.4 s after a struck + chord and 2.7 s after a held one. */ + if (g_gtr.on && g_gtr.amp > 0.05f) return; + while (note < EP_LO) note += 12; + while (note > EP_HI) note -= 12; + fmTrig(&g_ep, note, vel, EP_RATIO, EP_IDX0, EP_IDX_FLR, + EP_IDX_DEC, EP_AMP_DEC); +} + + +/* ========================================================================== + R E N D E R + ========================================================================== */ + +static void fmRender(float *l, float *r) +{ + float dry = 0.0f, wet, x, body, tine, g; + + g_fmSendL = 0.0f; + g_fmSendR = 0.0f; + + /* ---- bell ------------------------------------------------------- */ + if (g_bell.on) { + if (g_bell.amp < BELL_GATE) { + g_bell.on = 0; + } else { + x = fmStep(&g_bell); + g_bell.hp += (x - g_bell.hp) * BELL_HP; /* 400 Hz highpass */ + x -= g_bell.hp; + g_bell.lp += (x - g_bell.lp) * BELL_LP; /* 8 kHz lid */ + dry = g_bell.lp * BELL_GAIN * FM_DUCK; + } + } + + /* The delay runs every sample whether or not the bell is sounding. + Skipping it while idle would freeze the line and the 40 ms already + inside it would never come out. Read before write, so this is a clean + BELL_HAAS sample delay and never returns the sample just stored. */ + wet = g_bellHaas[g_bellHaasPos]; + g_bellHaas[g_bellHaasPos] = dry; + if (++g_bellHaasPos >= BELL_HAAS) g_bellHaasPos = 0; + + /* Dry left at -0.62, the 40 ms copy at the mirror on the right. The + delayed copy IS the counterweight: precedence keeps the strike on the + left where it was struck, while the energy balances. */ + *l += dry * PAN62_FAR + wet * PAN62_NEAR * HAAS_LEVEL; + *r += dry * PAN62_NEAR + wet * PAN62_FAR * HAAS_LEVEL; + g_fmSendL += (dry + wet) * 0.55f; + g_fmSendR += (dry + wet) * 0.55f; + + /* ---- electric piano --------------------------------------------- */ + if (g_ep.on) { + if (g_ep.amp < EP_GATE) { + g_ep.on = 0; + } else { + x = fmStep(&g_ep); + g_ep.hp += (x - g_ep.hp) * EP_HP; /* 180 Hz, pole one */ + x -= g_ep.hp; + g_ep.hp2 += (x - g_ep.hp2) * EP_HP; /* 180 Hz, pole two */ + x -= g_ep.hp2; + g_ep.lp += (x - g_ep.lp) * EP_LP; /* 3 kHz lid */ + x = g_ep.lp; + + /* Split at 1.5 kHz and pan the halves to opposite sides. A real + stereo Rhodes does this - tonebar one side, tine pickup the + other - and it is how this voice gets its +-0.30 spread with + its own counterweight instead of needing a second oscillator. + body + tine is exactly x, so L+R is x*(FAR+NEAR), the same + constant a plain pan would give: nothing cancels in mono. The + audible consequence is that each note's bark opens to the + right and its sustain settles left; if that is not wanted, + replace the four lines below with a plain + *l += x*g*PAN30_FAR; *r += x*g*PAN30_NEAR; and accept that + the counterweight then has to come from somewhere else. */ + g_ep.split += (x - g_ep.split) * EP_SPLIT; + body = g_ep.split; + tine = x - body; + + g = EP_GAIN * FM_DUCK; + *l += (body * PAN30_FAR + tine * PAN30_NEAR) * g; + *r += (body * PAN30_NEAR + tine * PAN30_FAR ) * g; + g_fmSendL += x * g * 0.18f; + g_fmSendR += x * g * 0.18f; + } + } +} + +/* ---------------------------------------------------------------- chorus */ +/* ===================================================================== + state + ===================================================================== */ + +/* ---- chorus / ensemble send ------------------------------------------ */ + +/* One shared buffer: the three taps carry the same signal and only differ in + where they read it, so three copies would be three times the cache traffic + for nothing. 2048 samples is 46 ms, comfortably past the longest tap at + 24.5 ms, and a power of two so the wrap is a mask instead of a compare. */ +#define CHLEN 2048 +#define CHMASK (CHLEN - 1) +static float g_chBuf[CHLEN]; +static int g_chPos = 0; + +/* Base delays 11 / 17 / 23 ms. They are far enough apart that the three comb + patterns they impose on the pad never share a notch, and with the depths + below the three sweep ranges (344..626, 644..856, 948..1081 samples) do not + even overlap - the taps stay in their own lanes for the whole tune. */ +#define CH_BASE1 (0.0110f * (float)SRATE) +#define CH_BASE2 (0.0170f * (float)SRATE) +#define CH_BASE3 (0.0230f * (float)SRATE) + +/* Depths fall as the rates rise. Perceived detune is depth * 2pi * rate, so + holding that product roughly constant puts all three at 10.8 / 12.2 / 11.9 + cents peak - string-section territory. Equal depths would have made the + 0.73 Hz line a 27-cent seasick vibrato riding on two barely-moving ones, + and one loud wobble is exactly what an ensemble must not sound like. + + These stay constant while the tune plays. They are deliberately not scaled + by the wet amount; chorusRender() says why. */ +#define CH_DEP1 (0.0032f * (float)SRATE) +#define CH_DEP2 (0.0024f * (float)SRATE) +#define CH_DEP3 (0.0015f * (float)SRATE) + +/* 0.31 / 0.47 / 0.73 Hz. All three are primes on a 0.01 Hz grid, so the + combination only repeats every 100 seconds - longer than the tune runs. + A commensurate set like 0.3/0.6/0.9 collapses into one audible sweep every + few bars, which is the failure this voice exists to avoid. */ +#define CH_INC1 (0.31f / (float)SRATE) +#define CH_INC2 (0.47f / (float)SRATE) +#define CH_INC3 (0.73f / (float)SRATE) + +/* Start phases in turns, added at read time so the accumulators can stay + zero-initialised in BSS. Without them all three LFOs leave zero together + and sweep the same way for the first second, and the configuration that + recurs on the 100-second period is that same unison - one sweep, which is + the thing the prime rates above exist to prevent. Offsetting does not + change the period, it changes what recurs into a scattered pose. */ +#define CH_OFF1 0.00f +#define CH_OFF2 0.37f +#define CH_OFF3 0.71f + +static struct { + float ph1, ph2, ph3; /* LFO phases in turns, kept inside 0..1 */ + float hp; /* one-pole state for the input highpass */ + float lpL, lpR; /* wet-side darkening, one per output */ + float in; /* what the voices pushed in this sample */ + float amt, tgt; /* wet amount, glided */ +} g_ch; + + +/* ===================================================================== + trigger + ===================================================================== */ + +/* Called from songTick() when the arrangement wants a different amount of + ensemble: 0 is fully dry, 1 is the full three-tap spread. Safe to restate + every row with the same value - the glide in chorusRender() means only a + change costs anything. Open it to ~0.9 under the pad, pull it back to ~0.3 + when the solo enters so the centre stays readable. Because the amount now + moves wet gain only, 0.3 is the same ensemble quieter, not a shallower one. */ +static void chorusSet(float amount) +{ + g_ch.tgt = amount; +} + +/* Every voice that wants ensemble adds its mono contribution here, once per + sample, before chorusRender() runs. Mono on purpose: the send exists to + create the width, so feeding it an already-panned signal would just smear + a position that the mix already decided. */ +static void chorusFeed(float s) +{ + g_ch.in += s; +} + + +/* ===================================================================== + render + ===================================================================== */ + +/* The modulators must not come from lsin(). lsin() is a 4096-entry table read + with a truncated index, so its output is a staircase, and a staircase is + fine for an oscillator - one step is a sliver of phase error - but not for + something that positions a delay tap, where one step is a discontinuity in + the thing being read. At 0.31 Hz the index advances 1270 times a second, so + tap 1 freezes for about 35 samples and then jumps by up to 0.22 of a + sample: chTap() below would then be interpolating between two positions + that themselves arrive in jumps, putting back exactly the zipper it exists + to remove. Measured against a tap driven by a true sine, the table leaves + about 12 dB more inharmonic energy (-38 dB against the tap output, versus + -50 dB for the curve below), and it lands roughly 1270 Hz either side of + every partial, so it reads as grain on a sustained pad rather than as + anything harmonic. + + This parabola pair is continuous in value and in slope everywhere, so the + tap velocity never jumps at all. The 0.225 term is the usual accuracy + correction and earns its two multiplies: it leaves the peak at exactly 1.0, + so the depth constants keep meaning what they say, and the zero-crossing + slope within 0.8 percent of 2pi, so the cents figures above still hold. + Shape error against a real sine is 0.0011, which is nothing on a half-hertz + modulator - shape does not matter here, smoothness is the whole job. */ +static float chLfo(float ph) +{ + float x, ax, y, ay; + if (ph >= 1.0f) ph -= 1.0f; /* caller adds a start offset, both under 1 */ + x = ph * 2.0f - 1.0f; + ax = (x < 0.0f) ? -x : x; + y = 4.0f * x * (ax - 1.0f); + ay = (y < 0.0f) ? -y : y; + return y + 0.225f * (y * ay - y); +} + +/* Linear interpolation between the two samples straddling the fractional + delay. Reading at the nearest integer instead is the classic mistake: as the + tap creeps across a sample boundary the output jumps by a whole sample of + the signal, and that step is the zipper that makes home-made chorus sound + broken. CHLEN is added before the mask instead of leaning on a negative + value masking correctly: the longest tap is 1081 samples so one CHLEN is + always enough to clear zero, and it folds into the address arithmetic. */ +static float chTap(float d) +{ + int i = (int)d; + float f = d - (float)i; + int a = (g_chPos + CHLEN - i) & CHMASK; + int b = (a + CHMASK) & CHMASK; /* one sample older than a */ + return g_chBuf[a] + (g_chBuf[b] - g_chBuf[a]) * f; +} + +static void chorusRender(float *l, float *r) +{ + float x, m1, m2, m3, t1, t2, t3, wl, wr; + + /* One-pole glide, tau about 126 ms. It scales the wet gain and nothing + else. Scaling tap depth with it as well is tempting - the taps park at + their base delays when the send is shut - but it costs twice. A 0 -> 1 + step then sweeps tap 1 at 0.025 seconds of delay per second, a 43-cent + bend, four times the detune the tap is built to produce; and at a middle + setting like 0.3 the sweep shrinks to a third of its range, so the send + stops being an ensemble and becomes a nearly static three-notch comb - + the "you hear a filter, not players" failure the base delays are picked + to avoid. With depth fixed, a gain-only glide can neither click nor + bend. */ + g_ch.amt += (g_ch.tgt - g_ch.amt) * 0.00018f; + + /* Highpass at about 178 Hz before the delay lines. A moving tap turns low + frequencies into audible pitch drift, and the mix keeps everything under + 140 Hz mono anyway, so there is nothing down there this send is allowed + to widen. */ + x = g_ch.in; + g_ch.in = 0.0f; + g_ch.hp += (x - g_ch.hp) * 0.025f; + x -= g_ch.hp; + + g_chBuf[g_chPos] = x; + + g_ch.ph1 += CH_INC1; if (g_ch.ph1 >= 1.0f) g_ch.ph1 -= 1.0f; + g_ch.ph2 += CH_INC2; if (g_ch.ph2 >= 1.0f) g_ch.ph2 -= 1.0f; + g_ch.ph3 += CH_INC3; if (g_ch.ph3 >= 1.0f) g_ch.ph3 -= 1.0f; + + m1 = chLfo(g_ch.ph1 + CH_OFF1); + m2 = chLfo(g_ch.ph2 + CH_OFF2); + m3 = chLfo(g_ch.ph3 + CH_OFF3); + + t1 = chTap(CH_BASE1 + CH_DEP1 * m1); + t2 = chTap(CH_BASE2 + CH_DEP2 * m2); + t3 = chTap(CH_BASE3 + CH_DEP3 * m3); + + /* Taps 1 and 2 lean left, so tap 3 carries the right on its own and has to + be panned harder than either of them to counterweight both: 0.78 + 0.62 + + 0.10 and 0.22 + 0.38 + 0.90 each sum to 1.50. Every tap is present on + both sides at different gains, so a mono fold-down sums to t1 + t2 + t3 + with nothing cancelling - which is the point of the mirror rule. */ + wl = (t1 * 0.78f + t2 * 0.62f + t3 * 0.10f) * g_ch.amt * 0.60f; + wr = (t1 * 0.22f + t2 * 0.38f + t3 * 0.90f) * g_ch.amt * 0.60f; + + /* One pole at about 7.4 kHz. Bucket-brigade ensembles were dark, and the + droop also buries the high-frequency error that linear interpolation + leaves behind when a tap sits near a half-sample offset. */ + g_ch.lpL += (wl - g_ch.lpL) * 0.62f; + g_ch.lpR += (wr - g_ch.lpR) * 0.62f; + + *l += g_ch.lpL; + *r += g_ch.lpR; + + g_chPos = (g_chPos + 1) & CHMASK; +} + + +/* ===================================================================== + notes for the integrator (changes from the submitted version marked *) + ===================================================================== + +WIRING + +- Per sample, in the inner loop of renderAudio(): each voice that should be + widened calls chorusFeed(s) with its pre-pan mono signal, then + chorusRender(&l, &r) is called ONCE, after all voices and before the master + compressor / softClip / tape stage. chorusRender() zeroes the input + accumulator, so it must run every sample even when the mix is silent, or the + next sample eats a stale send. +- Feed it the pad, the organ and the guitar. Do NOT feed it kick, bass, dry + snare, solo or lead: those are the reserved centre elements and this send + deliberately moves everything it touches off-axis. +- chorusSet(amount) goes in songTick(), alongside the other per-section + parameter restates. It is idempotent; call it every row if that is simpler. +- Declaration order: chLfo() and chTap() must both come before chorusRender(), + and the whole block after SRATE. * It no longer needs lsin() or TAU. +- Memory: g_chBuf is 8 KB in BSS. Nothing goes on the stack; chorusRender()'s + frame is a handful of floats. The only undefined external the block leaves in + the object is _fltused, which the host translation unit already provides. + +CONSTANTS AND WHAT BREAKS IF YOU MOVE THEM + +- CH_INC1/2/3 (0.31 / 0.47 / 0.73 Hz): the non-commensurate set. Rounding any + of them to a "nicer" value that shares a factor with the others (0.5, 0.75, + 0.25) makes the three LFOs periodically align and the send audibly becomes a + single vibrato instead of an ensemble. That is the one change that destroys + the voice. +- * CH_OFF1/2/3: start-phase offsets, in turns. They stop the three taps + leaving zero together at t = 0 and at every 100-second recurrence. Any three + mutually unrelated values under 1.0 do the job; setting them all equal undoes + the point. +- CH_DEP1/2/3: chosen so depth * 2pi * rate is near-constant, giving 10.8 / + 12.2 / 11.9 cents peak detune. Raising them all to a common value makes the + fastest line dominate. Raising any one past about 0.006 s pushes it into + seasick territory. Lowering them all below about 0.0008 s and the pad stops + being an ensemble and goes back to being a stack of saws. +- CH_BASE1/2/3 (11 / 17 / 23 ms): keep them mutually non-harmonic. If two bases + land at a 2:1 ratio their comb notches stack and you hear a filter, not + players. They are also spaced widely enough that the sweep ranges do not + overlap, so no two taps ever read the same neighbourhood of the buffer. +- CHLEN 2048: the longest tap reads 1081 samples back (now always, since depth + no longer shrinks with the send amount). Deepening CH_DEP3 or lengthening + CH_BASE3 past ~46 ms total needs CHLEN raised to 4096; the mask wrap silently + aliases instead of clipping, so it would sound wrong rather than crash. +- * chLfo()'s 0.225: the accuracy term. Drop it and the modulator keeps its + peak but its zero-crossing slope rises to 8 per turn instead of 2pi, which + multiplies every detune figure above by 1.27. It is not decoration. +- Input highpass a = 0.025 (-3 dB at 177.7 Hz): protects the mono-below-140 Hz + rule and keeps the bass out of a moving tap. Lowering it lets the low end + wander in pitch and smears the mono fold-down. +- * Output lowpass a = 0.62: -3 dB at 7.4 kHz, not the 6.8 kHz previously + claimed (that figure came from the small-coefficient approximation, which is + not valid this far up). Taste plus interpolation-error masking. Push it + toward 1.0 and the linear-interpolation grit starts to show. +- * Glide a = 0.00018 (tau 126 ms): now a pure gain fade, so it cannot click + and cannot bend pitch, and it can be moved freely on taste alone. +- Output trim 0.60: the three taps are decorrelated, so they sum incoherently + and the wet lands at 0.60x the send RMS at amount = 1 (measured 0.601) - + deliberately under the dry so the ensemble supports rather than swamps. + * Be aware the L and R tap gains each sum to 1.50, so on material where the + taps momentarily correlate - transients, and anything low enough that 11 ms + and 23 ms are a small part of a cycle - the instantaneous wet gain reaches + 0.90. That is headroom the master limiter has to find; if the pad is already + near unity going in, trim the feed rather than this number. + +DELIBERATE OMISSIONS + +- There is no feedback path. Regeneration turns this into a flanger with + resonant notches, which is a different and much more attention-grabbing + effect. It also means there is no decaying recursion to worry about under + FTZ - the buffer is fed fresh every sample and the only state that can decay + to zero is the input highpass and the two output one-poles, all of which are + re-excited the moment a voice feeds again. +- No softClip on the wet. The send is well under unity and distorting a pad's + ensemble is not a sound anyone wants; the master limiter already catches the + sum. + +VERIFICATION + +- cl /nologo /c /O2 /Oi /Ot /GS- /Gy /fp:fast /std:c11 /W4 /DNDEBUG - clean, no + warnings. dumpbin /symbols shows _fltused as the only undefined external: no + libm, no __chkstk, no CRT. +- Tap ranges 344.0..626.2 / 643.9..855.5 / 948.1..1080.4, longest read 1080.4 + against a 2048 buffer; shortest 344, so the sample being written is never + read. Filters 177.7 Hz and 7397 Hz, glide tau 126.0 ms, LFO common period + 100 s, pan gains 1.50 per side with unity fold-down per tap - all measured, + not asserted. +- chLfo(): peak 1.0000000 at a quarter turn, peak slope 6.2324 per turn against + 2pi (ratio 0.9919), worst shape error 0.00109. +- LFO artifact A/B, one tap fed 1200/2400/3700 Hz, all three variants driven + from one shared double-precision phase so only the sine evaluation differs; + error spectrum against an exact-sine-driven tap, Welch-averaged, split by + whether it sits on a partial or away from one: + lsin() table : inharmonic error -37.9 dB rel. tap output + chLfo() : inharmonic error -49.5 dB rel. tap output + The component sitting on the partials is the same for both (~-32 dB) and is + benign - it is only a slightly different vibrato shape. +*/ + +/* ---------------------------------------------------------------- polyarp */ +/* ---- arpeggiated polysynth --------------------------------------------- + Three detuned saws a note, each note through its own resonant lowpass with + a fast envelope on the cutoff. The cutoff pluck is the whole point: without + it an arpeggiator sounds like one held chord being retriggered, because + every step has exactly the same spectrum. With it every step has its own + attack colour and the ear hears a sequence. + ------------------------------------------------------------------------- */ +#define ARP_VOICES 3 + +/* A dotted eighth at 132.3 BPM is 340.14 ms, which is 15000 samples at 44100. + That is exactly three of this song's 16th note rows, so the repeats land on + the grid rather than drifting against it. Written in terms of SPR so it + stays on the grid if the tempo constant ever moves. */ +#define ARP_DLY_LEN (SPR * 3) + +/* +-10.2 cents. Two different beat rates fall out of three saws spaced this + way - about 6 Hz between each outer saw and the centre, 12 Hz across the + outer pair up around C6 where this line sits - so the chorus never locks + into one throb. Past about 15 cents a short plucked note reads as out of + tune rather than wide. */ +#define ARP_DET_UP 1.00590f +#define ARP_DET_DN 0.99412f + +#define ARP_ATK 0.018f /* ~1.3 ms one pole; kills the step, keeps the pick */ +#define ARP_DEC 0.999821f /* amp to 8% in 320 ms: three steps of overlap */ +#define ARP_PLUCK_DEC 0.999245f /* cutoff env to 5% in 90 ms */ +#define ARP_PLUCK_SPAN 3400.0f /* Hz the cutoff jumps on every note */ +#define ARP_RES 0.29f /* 1/Q, so Q = 3.45, +10.9 dB at the peak */ +#define ARP_FC_MIN 300.0f +#define ARP_FC_MAX 4600.0f /* the resonant peak then lands at 4.75 kHz */ + +#define ARP_SWEEP_MID 0.92f /* cutoff multiplier when nothing has set a section */ +#define ARP_SWEEP_RANGE 0.80f /* so bright 0..1 spans 0.52x .. 1.32x */ +#define ARP_SWEEP_GLIDE 0.000012f /* ~1.9 s: this is the slow sweep itself */ + +#define ARP_HP_A 0.04456f /* one pole highpass at 320 Hz */ + +/* The 3 dB scoop at 2.4 kHz, to stay out of the solo's presence band. This is + a second state variable filter run on the bus, and what gets subtracted is + its bandpass output - a two pole band, 12 dB an octave on both skirts. + One pole filters cannot do this job: any pair of them, subtracted in + parallel or cascaded into a bandpass, has 6 dB skirts so wide that taking + 3 dB out at 2.4 kHz also takes 1.5 dB out at 800 Hz and 1 dB off the whole + top end, which is a dull voice rather than a hole for the solo. Measured + with these three numbers: -3.00 dB at 2400, bottoming at -3.34 dB near 2600, + -0.11 dB at 800 Hz and -0.61 dB at 7 kHz, so the shoulders stay flat. + F is 2*sin(pi*2400/SR). ARP_DIP_Q is damping, i.e. 1/Q, the same convention + as ARP_RES - it is 0.70 here for a Q of 1.43, about an octave wide - and K + is then whatever puts the centre on -3 dB. All three move together. */ +#define ARP_DIP_F 0.34028f +#define ARP_DIP_Q 0.70f +#define ARP_DIP_K 0.2230f + +#define ARP_DRIVE 0.42f /* how hard softClip works: tone */ +/* Same story as the pad: 0.34 measured 0.081 peak, which is a decoration + rather than the sequence the section is built on. */ +#define ARP_LEVEL 1.15f /* how loud the voice is: balance */ + +#define ARP_DLY_SEND 0.60f +#define ARP_DLY_FB 0.42f /* about three audible repeats */ +#define ARP_DLY_RET 0.50f +#define ARP_DLY_DAMP 0.30f /* damping lowpass at ~2.5 kHz inside the loop */ + +/* Constant power pan at -0.55: cos and sin of 0.3534 rad, 8.7 dB to the left. + Baked because there is no acosf here and the position never changes. The + delay return uses the same pair swapped, and that is the counterweight. */ +#define ARP_PAN_L 0.9382f +#define ARP_PAN_R 0.3461f + +typedef struct { + float p1, p2, p3; /* the three detuned saw phases */ + float i1, i2, i3; /* and their per sample increments */ + float amp, atk; + float fenv; /* the pluck: starts at the accent, falls to 0 */ + float fcFloor; /* key tracked, worked out once at the trigger */ + float lp, bp; /* this note's own state variable filter */ +} ArpVoice; + +static ArpVoice g_arpV[ARP_VOICES]; +static struct { + float sweep, sweepTgt; /* signed offsets from ARP_SWEEP_MID, see arpRender */ + float hp; /* 320 Hz highpass state */ + float eqLp, eqBp; /* the bus filter that makes the 2.4 kHz dip */ + float dampLp; /* delay feedback damping */ + int next, dpos; +} g_arp; +static float g_arpDly[ARP_DLY_LEN]; /* 60000 bytes, and it belongs in BSS */ + +/* Set the brightness of the section, 0 dark to 1 open. Call it once when a + section starts; arpRender glides to it over about two seconds, and that + glide is the slow sweep. A zeroed g_arp is already the mid setting, which + is the reason sweep is stored as a signed offset from ARP_SWEEP_MID rather + than as the multiplier itself. Keeping zeroed BSS meaningful is also why + this voice needs no init function. */ +static void arpSetSweep(float bright) +{ + if (bright < 0.0f) bright = 0.0f; + if (bright > 1.0f) bright = 1.0f; + g_arp.sweepTgt = (bright - 0.5f) * ARP_SWEEP_RANGE; +} + +/* One arp step. accent 0..1 scales the level and the depth of the pluck + together, so a pattern can lean on the downbeat; pass 1.0f for a flat arp. */ +static void arpTrig(int note, float accent) +{ + ArpVoice *v; + float hz, inc; + + /* Rests reach songTick as RST, which is -1, and noteFreq would index + g_baseFreq[-1] for it: -1/12 is 0 and -1%12 is -1 in C. Guarded here + rather than at the call site so a rest row is safe to pass straight + through, and guarded before the round robin so a rest does not steal + the slot out from under a note that is still sounding. */ + if (note < 0) return; + + if (accent < 0.15f) accent = 0.15f; /* a step too quiet to hear is a hole + in the arp, not a soft note */ + if (accent > 1.0f) accent = 1.0f; + + /* Round robin. With three slots and a monophonic arp line the slot that + comes up next is always the oldest, so this is also the quietest-voice + rule without having to look for the quietest voice. */ + v = &g_arpV[g_arp.next]; + if (++g_arp.next >= ARP_VOICES) g_arp.next = 0; + + hz = noteFreq(note); + inc = hz * (1.0f / (float)SRATE); + v->i1 = inc; + v->i2 = inc * ARP_DET_UP; + v->i3 = inc * ARP_DET_DN; + + /* Sixth, half, five sixths. sawAt returns 2*phase-1 before it advances, + so three saws all leaving zero together sum to -1 after the scaling, + and it is the same step on every note, which reads as a tick sitting on + top of the arp instead of as part of it. Spacing them a third apart is + what makes the detune beat; starting the set at 1/6 rather than at 0 is + what makes the three values -2/3, 0 and +2/3 sum to exactly zero. + Thirds measured from zero give -1, -1/3, +1/3, which still leaves -1/3 + going into a filter with Q of 3.45 on every note. */ + v->p1 = 0.166667f; + v->p2 = 0.5f; + v->p3 = 0.833333f; + + v->amp = accent; + v->atk = 0.0f; + v->fenv = accent; + + /* Key tracking. A fixed cutoff floor would mute the top of the arp and + leave the bottom undamped; 1.6 * f0 keeps roughly the first two + harmonics of any note alive, and the 240 Hz term stops the lowest notes + from closing the filter down into the highpass. */ + v->fcFloor = 240.0f + hz * 1.6f; + + /* The filter state is deliberately not cleared. A slot is normally reused + only after its previous note has decayed, and arpRender zeroes the state + on the way out; clearing it here would only change the case where a + still-loud voice is stolen, and there stopping the old tail dead on a + discontinuity is worse than letting the new note's reset saws arrive + through a filter that is already moving. */ +} + +static void arpRender(float *l, float *r) +{ + int i; + float sum = 0.0f, dry, wet, dk, sweep, ehp; + + g_arp.sweep += (g_arp.sweepTgt - g_arp.sweep) * ARP_SWEEP_GLIDE; + sweep = ARP_SWEEP_MID + g_arp.sweep; + + for (i = 0; i < ARP_VOICES; i++) { + ArpVoice *v = &g_arpV[i]; + float saw, fc, w, f, hp; + + if (v->amp <= 0.0004f) { + /* Park the slot. The state has to be cleared here rather than at + the trigger: the filter runs at full scale right up to this + threshold, so a slot picked up again with that still sitting in + lp would start the next note on a step. */ + if (v->amp != 0.0f) { v->amp = 0.0f; v->lp = 0.0f; v->bp = 0.0f; } + continue; + } + + saw = (sawAt(&v->p1, v->i1) + + sawAt(&v->p2, v->i2) + + sawAt(&v->p3, v->i3)) * (1.0f / 3.0f); + + fc = (v->fcFloor + ARP_PLUCK_SPAN * v->fenv) * sweep; + if (fc < ARP_FC_MIN) fc = ARP_FC_MIN; + if (fc > ARP_FC_MAX) fc = ARP_FC_MAX; + + /* The state variable filter wants f = 2*sin(pi*fc/SR). There is no + sinf here, and lsin is the wrong tool for a swept cutoff - 4096 + entries over a turn quantises this into roughly 21 Hz steps, and on + a 90 ms sweep those steps land as zipper noise. w here is 2*pi*fc/SR, + which is twice the angle the sine wants, so the two term series for + 2*sin(w/2) is w - w^3/24 - note the 24, not the 6 you would write + for sin(w) itself. Under 0.01 percent out at the top clamp. */ + w = fc * (TAU / (float)SRATE); + f = w - w * w * w * (1.0f / 24.0f); + + v->lp += f * v->bp; + hp = saw - v->lp - ARP_RES * v->bp; + v->bp += f * hp; + + v->atk += (1.0f - v->atk) * ARP_ATK; + v->amp *= ARP_DEC; + v->fenv *= ARP_PLUCK_DEC; + + sum += v->lp * v->amp * v->atk; + } + + /* Highpass first. Nothing this voice makes belongs under its own + allocation, and the arp is the element most likely to crowd the bass. */ + g_arp.hp += (sum - g_arp.hp) * ARP_HP_A; + dry = sum - g_arp.hp; + + /* Then the presence scoop: take a scaled bandpass away from the signal. */ + g_arp.eqLp += ARP_DIP_F * g_arp.eqBp; + ehp = dry - g_arp.eqLp - ARP_DIP_Q * g_arp.eqBp; + g_arp.eqBp += ARP_DIP_F * ehp; + dry -= g_arp.eqBp * ARP_DIP_K; + + /* Three overlapping notes through a resonant filter stack far higher at + the peak of a pluck than one does. At ARP_DRIVE the ordinary signal sits + in the near linear part of softClip and only the peaks get shaped. */ + dry = softClip(dry * ARP_DRIVE) * ARP_LEVEL; + + /* The dotted eighth. Read before write at the same index, so the line + delays by exactly ARP_DLY_LEN. Written before the duck is applied, so + the line always holds un-ducked material and the repeats duck along with + the dry instead of carrying a third of a second old duck value forward. */ + wet = g_arpDly[g_arp.dpos]; + g_arp.dampLp += (wet - g_arp.dampLp) * ARP_DLY_DAMP; + g_arpDly[g_arp.dpos] = dry * ARP_DLY_SEND + g_arp.dampLp * ARP_DLY_FB; + if (++g_arp.dpos >= ARP_DLY_LEN) g_arp.dpos = 0; + + dk = 0.70f + 0.30f * g_duck; /* ducks 0.30, same shape as the organ */ + dry *= dk; + wet *= ARP_DLY_RET * dk; + + /* Dry at -0.55, return at the mirror position: the same two gains swapped. */ + *l += dry * ARP_PAN_L + wet * ARP_PAN_R; + *r += dry * ARP_PAN_R + wet * ARP_PAN_L; +}