webgpu_compute_reduce.html 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - compute reduction</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <style>
  10. #reduction-panel {
  11. background-color: #111;
  12. width: 100%;
  13. display: flex;
  14. position: fixed;
  15. height: auto;
  16. bottom: 0px;
  17. z-index: 99;
  18. flex-direction: column;
  19. justify-content: center;
  20. align-items: center;
  21. border-left: 2px solid #222;
  22. text-align: center;
  23. }
  24. #panel-title {
  25. width: fit-content;
  26. }
  27. .thread-row {
  28. display: flex;
  29. flex-direction: row;
  30. align-items: center;
  31. margin: 4px 0;
  32. position: relative;
  33. }
  34. .thread {
  35. width: 16px;
  36. height: 16px;
  37. background-color: #444;
  38. margin-right: 2px;
  39. transition: background-color 0.5s, transform 0.5s;
  40. }
  41. .stage-display {
  42. display: flex;
  43. flex-direction: column;
  44. justify-content: center;
  45. margin-bottom: 5px;
  46. }
  47. .stage-label {
  48. font-size: 1.2em;
  49. color: #aaa;
  50. font-style: bold;
  51. margin-top: 6px;
  52. margin-bottom: 20px;
  53. }
  54. .thread {
  55. display: flex;
  56. justify-content: center;
  57. align-items: center;
  58. width: 40px;
  59. height: 40px;
  60. margin: 2px;
  61. border: 1px solid rgba(255, 255, 255, 0.2);
  62. border-radius: 4px;
  63. background: linear-gradient(180deg, rgba(255,255,255,0.05), rgba(0,0,0,0.2));
  64. box-shadow: inset 0 0 2px rgba(255,255,255,0.1);
  65. font-family: monospace;
  66. color: white;
  67. }
  68. .thread_data {
  69. display: block;
  70. max-width: 100%;
  71. padding: 0 2px;
  72. white-space: nowrap;
  73. overflow: hidden;
  74. text-overflow: ellipsis;
  75. font-size: clamp(8px, 2vw, 14px);
  76. text-align: center;
  77. }
  78. .subgroup {
  79. display: flex;
  80. position: relative;
  81. margin-left: 10px;
  82. margin-right: 10px;
  83. }
  84. .subgroup::before {
  85. /* label text for each subgroup label */
  86. content: "subgroupAdd()";
  87. position: absolute;
  88. top: -20px;
  89. /* Hide until animation is displayed */
  90. opacity: 0;
  91. z-index: 100;
  92. transition: opacity 0.5s ease;
  93. font-weight: bold;
  94. color: white;
  95. width: 100%;
  96. }
  97. .subgroup::after {
  98. content: attr(data-label);
  99. position: absolute;
  100. bottom: -20px;
  101. opacity: 1;
  102. z-index: 100;
  103. color: gray;
  104. width: 100%;
  105. }
  106. .reduction-stage {
  107. margin-bottom: 20px;
  108. }
  109. @keyframes labelAbsorb {
  110. 0% {
  111. opacity: 0;
  112. transform: translateY(-50%);
  113. }
  114. 40% {
  115. opacity: 1;
  116. transform: translateY(0%);
  117. }
  118. 60% {
  119. opacity: 1;
  120. transform: translateY(0%);
  121. }
  122. 80% {
  123. opacity: 1;
  124. transform: translate(0%, -20%);
  125. }
  126. 100% {
  127. opacity: 0;
  128. transform: translate(0%, 100%);
  129. }
  130. }
  131. .subgroup.anim::before {
  132. opacity: 0;
  133. animation-name: labelAbsorb;
  134. animation-duration: 1.5s;
  135. transition:
  136. transform 0.6s ease-out,
  137. opacity 0.3s ease-in 0.3s;
  138. }
  139. </style>
  140. <div id="info">
  141. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
  142. <br /> This example demonstrates the performance of various simple parallel reduction kernels.
  143. <br /> Reference implementations are translated from the CUDA/WGSL code present in the following books/repos:
  144. <br /> Impl. 0 - 2: <a href="https://www.cambridge.org/core/books/programming-in-parallel-with-cuda/C43652A69033C25AD6933368CDBE084C"><i>Programming in Parallel with CUDA</i></a> by <a href="https://people.bss.phy.cam.ac.uk/~rea1/">Richard Ansorge</a>
  145. <br /> Impl. 3: <a href="https://github.com/frost-beta/betann/blob/main/betann/wgsl/reduce_all.wgsl"><i>betann reduce_all kernel</i></a> by <a href="https://github.com/zcbenz">zcbenz</a>
  146. <br /> Impl. 4: <a href="https://github.com/b0nes164/GPUPrefixSums/blob/main/GPUPrefixSumsWebGPUapis/SharedShaders/rts.wgsl"><i>GPUPrefixSums reduction approach</i></a> by <a href="https://github.com/b0nes164">b0nes164</a>
  147. <div id="left_side_display" style="position: absolute;top: 150px;left: 0;padding: 10px;background: rgba( 0, 0, 0, 0.5 );color: #fff;font-family: monospace;font-size: 12px;line-height: 1.5;pointer-events: none;text-align: left;"></div>
  148. <div id="right_side_display" style="position: absolute;top: 150px;right: 0;padding: 10px;background: rgba( 0, 0, 0, 0.5 );color: #fff;font-family: monospace;font-size: 12px;line-height: 1.5;pointer-events: none;text-align: left;"></div>
  149. </div>
  150. <div id="reduction-panel">
  151. <h3 id="panel-title" style="flex: 0 0 auto;">Subgroup Reduction Explanation</h3>
  152. <div class="reduction-stage" id="subgroup-reduction-stage">
  153. <div class="stage-label">Use subgroupAdd() to capture reduction of each workgroup's subgroups (Hover for animation)</div>
  154. <div class="stage-display">
  155. <div id="workgroup_threads" style="display: flex; justify-content: center; margin-bottom: 20px;"></div>
  156. <div id="subgroup_reduction" style="display: flex; justify-content: center; margin-bottom: 5px;"></div>
  157. </div>
  158. </div>
  159. </div>
  160. <script type="importmap">
  161. {
  162. "imports": {
  163. "three": "../build/three.webgpu.js",
  164. "three/webgpu": "../build/three.webgpu.js",
  165. "three/tsl": "../build/three.tsl.js",
  166. "three/addons/": "./jsm/"
  167. }
  168. }
  169. </script>
  170. <script type="module">
  171. import * as THREE from 'three/webgpu';
  172. import { instancedArray, Loop, If, vec3, dot, clamp, storage, uvec4, subgroupAdd, uniform, uv, uint, float, Fn, vec2, invocationLocalIndex, invocationSubgroupIndex, uvec2, floor, instanceIndex, workgroupId, workgroupBarrier, workgroupArray, subgroupSize, select, log2 } from 'three/tsl';
  173. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  174. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  175. const timestamps = {
  176. left_side_display: document.getElementById( 'left_side_display' ),
  177. right_side_display: document.getElementById( 'right_side_display' )
  178. };
  179. const divRoundUp = ( size, part_size ) => {
  180. return Math.floor( ( size + part_size - 1 ) / part_size );
  181. };
  182. const cssSubgroupSize = 4;
  183. const cssWorkgroupSize = 16;
  184. const workgroupThreadsContainer = document.getElementById( 'workgroup_threads' );
  185. const subgroupReductionContainer = document.getElementById( 'subgroup_reduction' );
  186. document.getElementById( 'panel-title' ).textContent += ` (Subgroup Size: ${cssSubgroupSize}, Workgroup Size: ${cssWorkgroupSize})`;
  187. const createThreadWithData = ( data ) => {
  188. const threadEle = document.createElement( 'div' );
  189. threadEle.className = 'thread';
  190. const threadData = document.createElement( 'span' );
  191. threadData.textContent = data; // safer than innerHTML for just text
  192. threadData.className = 'thread_data';
  193. threadEle.append( threadData );
  194. return threadEle;
  195. };
  196. // Create thread elements
  197. const workgroupThreads = [];
  198. const initialSubgroups = [];
  199. const initialData = [];
  200. let currentSubgroupDiv = null;
  201. for ( let i = 0; i < cssWorkgroupSize; i ++ ) {
  202. if ( i % cssSubgroupSize === 0 ) {
  203. const currentSubgroupIndex = Math.floor( i / cssSubgroupSize );
  204. const subgroupReductionThread = createThreadWithData( 0 );
  205. subgroupReductionThread.id = `subgroup_reduction_element_${currentSubgroupIndex}`;
  206. subgroupReductionContainer.appendChild( subgroupReductionThread );
  207. currentSubgroupDiv = document.createElement( 'div' );
  208. currentSubgroupDiv.className = 'subgroup';
  209. currentSubgroupDiv.setAttribute( 'data-label', `Threads ${currentSubgroupIndex * cssSubgroupSize}-${( currentSubgroupIndex + 1 ) * cssSubgroupSize - 1}` );
  210. initialSubgroups.push( currentSubgroupDiv );
  211. workgroupThreadsContainer.appendChild( currentSubgroupDiv );
  212. }
  213. const data = Math.floor( Math.random() * 9 ) + 1;
  214. initialData.push( data );
  215. const thread = createThreadWithData( data );
  216. workgroupThreads.push( thread );
  217. currentSubgroupDiv.appendChild( thread );
  218. }
  219. const deactivateLabelAnimation = ( subgroupDiv, idx ) => {
  220. subgroupDiv.classList.remove( 'anim' );
  221. const subgroupReductionBufferElement = document.getElementById( `subgroup_reduction_element_${idx}` ).querySelector( '.thread_data' );
  222. subgroupReductionBufferElement.innerHTML = 0;
  223. };
  224. const activateLabelAnimation = ( subgroupDiv, idx ) => {
  225. const threads = Array.from( subgroupDiv.children );
  226. let total = 0;
  227. for ( let i = idx * cssSubgroupSize; i < idx * cssSubgroupSize + cssSubgroupSize; i ++ ) {
  228. total += initialData[ i ];
  229. }
  230. subgroupDiv.classList.add( 'anim' );
  231. setTimeout( () => {
  232. threads.forEach( t => {
  233. t.querySelector( '.thread_data' ).textContent = total;
  234. } );
  235. const subgroupReductionBufferElement = document.getElementById( `subgroup_reduction_element_${idx}` ).querySelector( '.thread_data' );
  236. subgroupReductionBufferElement.innerHTML = total;
  237. }, 1000 );
  238. // Remove the class after the animation ends so it can be triggered again
  239. setTimeout( () => {
  240. subgroupDiv.classList.remove( 'anim' );
  241. }, 1500 ); // matches animation duration in CSS
  242. };
  243. document.getElementById( 'subgroup-reduction-stage' ).addEventListener( 'mouseenter', () => {
  244. initialSubgroups.forEach( ( subgroupDiv, idx ) => {
  245. activateLabelAnimation( subgroupDiv, idx );
  246. } );
  247. } );
  248. document.getElementById( 'subgroup-reduction-stage' ).addEventListener( 'mouseleave', () => {
  249. initialSubgroups.forEach( ( subgroupDiv, idx ) => {
  250. deactivateLabelAnimation( subgroupDiv, idx );
  251. } );
  252. workgroupThreads.forEach( ( thread, idx ) => {
  253. thread.querySelector( '.thread_data' ).textContent = initialData[ idx ];
  254. } );
  255. } );
  256. if ( WebGPU.isAvailable() === false ) {
  257. document.body.appendChild( WebGPU.getErrorMessage() );
  258. throw new Error( 'No WebGPU support' );
  259. }
  260. // Total number of elements and the dimensions of the display grid.
  261. const size = 262144;
  262. const vecSize = divRoundUp( size, 4 );
  263. // Grid display is gridDim x gridDim
  264. const gridDim = Math.sqrt( size );
  265. let maxWorkgroupSize = 64;
  266. // Algorithm speed increase as you iterate through algorithms array
  267. const algorithms = [
  268. 'Reduce 0 (N/2)',
  269. 'Reduce 1 (Naive Accumulate)',
  270. 'Reduce 2 (Workgroup Reduction)',
  271. 'Reduce 3 (Subgroup Reduce)',
  272. 'Reduce 4 (Subgroup Optimized)',
  273. 'Incorrect Baseline',
  274. ];
  275. // Input Grid: Displays input data in a grid format
  276. // Input Log2: Displays input grid data's logarithmic indices horizontally (1, 2, 4, 8, 16, ..., size)
  277. // Input Element 0: Displays clamped input[0]
  278. const displayModes = [ 'Input Grid', 'Input Log2', 'Input Element 0', 'Workgroup Sum Grid' ];
  279. // Holds uniforms for both displays as well as debug information
  280. const unifiedEffectController = {
  281. // Number of elements in the grid
  282. gridElementWidth: uniform( gridDim ),
  283. gridElementHeight: uniform( gridDim ),
  284. // Number of elements in the grid being displayed
  285. gridDisplayWidth: uniform( gridDim ),
  286. gridDisplayHeight: uniform( gridDim ),
  287. // How to display end result of reduction.
  288. // Ideally this is unique to the reduction method being deployed
  289. 'Display Mode': 'Input Log2',
  290. loggedBuffer: 'Input Buffer',
  291. elementsReduced: size,
  292. };
  293. const leftEffectController = {
  294. // Current reduction algorithm being executed by this side
  295. algo: 'Reduce 0 (N/2)',
  296. // Flag indicating whether to highlight element in validation check
  297. highlight: uniform( 0 ),
  298. // Uniform that corresponds to the index of the current algorithm within the algorithms array
  299. currentAlgo: uniform( 0 ),
  300. // Current state of reduction (Running, validating, resetting)
  301. state: 'Run Algo',
  302. // Current display mode
  303. displayMode: 'Input Log2',
  304. // Reduce 0 specific uniform
  305. numThreadsDispatched: uniform( size / 2 ),
  306. // The subgroup size used by this side's device
  307. };
  308. const rightEffectController = {
  309. algo: 'Reduce 4 (Subgroup Optimized)',
  310. currentAlgo: uniform( 3 ),
  311. highlight: uniform( 0 ),
  312. displayMode: 'Input Element 0',
  313. state: 'Run Algo',
  314. numThreadsDispatched: uniform( size / 2 )
  315. };
  316. const leftMaterial = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  317. const rightMaterial = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  318. const leftDisplayColorNodes = {};
  319. const rightDisplayColorNodes = {};
  320. const gui = new GUI();
  321. gui.add( leftEffectController, 'algo', algorithms ).onChange( () => {
  322. leftEffectController.currentAlgo.value = algorithms.findIndex( val => val === leftEffectController.algo );
  323. } );
  324. gui.add( rightEffectController, 'algo', algorithms ).onChange( () => {
  325. rightEffectController.currentAlgo.value = algorithms.findIndex( val => val === rightEffectController.algo );
  326. } );
  327. gui.add( leftEffectController, 'displayMode', displayModes ).name( 'Left Display Mode' ).onChange( () => {
  328. leftMaterial.colorNode = leftDisplayColorNodes[ leftEffectController.displayMode ];
  329. leftMaterial.needsUpdate = true;
  330. } );
  331. gui.add( rightEffectController, 'displayMode', displayModes ).name( 'Right Display Mode' ).onChange( () => {
  332. rightMaterial.colorNode = rightDisplayColorNodes[ rightEffectController.displayMode ];
  333. rightMaterial.needsUpdate = true;
  334. } );
  335. const debugFolder = gui.addFolder( 'Debug' );
  336. const elementsReducedController = debugFolder.add( unifiedEffectController, 'elementsReduced' ).name( 'Elements Reduced' );
  337. elementsReducedController.disable();
  338. const stateLeftController = debugFolder.add( leftEffectController, 'state' ).name( 'Left Display State' );
  339. const stateRightController = debugFolder.add( rightEffectController, 'state' ).name( 'Right Display State' );
  340. stateLeftController.disable();
  341. stateRightController.disable();
  342. debugFolder.add( unifiedEffectController, 'loggedBuffer', [ 'Input Buffer', 'Input Vectorized Buffer', 'Workgroup Sums Buffer', 'Debug Buffer' ] ).name( 'Buffer to Log' );
  343. debugFolder.close();
  344. // HELPER FUNCTIONS
  345. const pow2Ceil = Fn( ( [ x ] ) => {
  346. If( x.equal( uint( 0 ) ), () => {
  347. return uint( 1 );
  348. } );
  349. const val = x.sub( 1 ).toVar( 'val' );
  350. val.assign( val.bitOr( val.shiftRight( 1 ) ) );
  351. val.assign( val.bitOr( val.shiftRight( 2 ) ) );
  352. val.assign( val.bitOr( val.shiftRight( 4 ) ) );
  353. val.assign( val.bitOr( val.shiftRight( 8 ) ) );
  354. val.assign( val.bitOr( val.shiftRight( 16 ) ) );
  355. return val.add( 1 );
  356. } ).setLayout( {
  357. name: 'pow2Ceil',
  358. type: 'uint',
  359. inputs: [
  360. { name: 'x', type: 'uint' }
  361. ]
  362. } );
  363. // ALGORITHM CONSTRUCTORS
  364. // REDUCE 1
  365. // Thanks to Sam0oneau of Graphics Programming Discord for the explanation.
  366. // (Graphics Programming Discord Message Link): https://discord.com/channels/318590007881236480/374061825454768129/1391248956171882597
  367. /* Reduce 1 Example (Assume Workgroup Size 256, numElements: 262144) -> Initial currentBuffer State: | 1, 1, 1, 1, ... |
  368. *
  369. * KERNEL 1:
  370. * Executes 256 threads by 256 workgroups. Each thread loops 4 times and accesses elements
  371. * at the indices below.
  372. * Thread 1 Thread 2 Thread 3
  373. * | 0, 65536, ..., n * 65536 | 1, 65537, .... (n * 65536) + 1 | 1, 65538, .... (n * 65536) + 2 | etc
  374. * Buffer Values: | 4, 4, 4, 4, ...|
  375. *
  376. * KERNEL 2:
  377. * Executes 256 threads by one workgroup. Each thread loops 1024 times
  378. * Thread 1 Thread 2 Thread 3
  379. * | 0, 256, ...., n * 256 | 1, 257, ... (n * 256) + 1 | 2, 258, ... (n * 256) + 3 | etc
  380. * Buffer Values: | 1024, 1024, 1024, 1024, ... |
  381. *
  382. * KERNEL 3:
  383. * Executes 1 thread by one workgroup. Single thread loops 256 times
  384. * Thread 1
  385. * | 0, 1, 2, 3, 4, 5, 6 ... etc|
  386. * Buffer Values: [262144, 1024, 1024]
  387. */
  388. const createReduce1Fn = ( createReduce1FnProps ) => {
  389. const { dispatchSize, numElements, inputBuffer, workgroupSize } = createReduce1FnProps;
  390. const fnDef = Fn( () => {
  391. const dispatch = uint( dispatchSize ).toVar( 'dispatchSize' );
  392. const tSum = uint( 0 ).toVar();
  393. const k = instanceIndex.toVar( 'k' );
  394. Loop( k.lessThan( uint( numElements ) ), ( ) => {
  395. tSum.addAssign( inputBuffer.element( k ) );
  396. k.addAssign( uint( dispatch ) );
  397. } );
  398. inputBuffer.element( instanceIndex ).assign( tSum );
  399. } )().compute( dispatchSize, [ workgroupSize ] );
  400. return fnDef;
  401. };
  402. // REDUCE 2
  403. // For non power of 2 # of workgroups
  404. const createReduce2Fn = ( createReduce2FnProps ) => {
  405. const { workgroupSize, dispatchSize, numElements, inputBuffer } = createReduce2FnProps;
  406. const fnDef = Fn( () => {
  407. const tSum = workgroupArray( 'uint', workgroupSize );
  408. const k = instanceIndex.toVar( 'k' );
  409. tSum.element( invocationLocalIndex ).assign( uint( 0 ) );
  410. Loop( k.lessThan( uint( numElements ) ), () => {
  411. tSum.element( invocationLocalIndex ).addAssign( inputBuffer.element( k ) );
  412. k.addAssign( uint( dispatchSize ) );
  413. } );
  414. workgroupBarrier();
  415. // Reset the loop condition (account for numWorkgroups % 2 != 0)
  416. k.assign( pow2Ceil( uint( workgroupSize ) ).div( 2 ) );
  417. Loop( k.greaterThan( 0 ), () => {
  418. If( invocationLocalIndex.lessThan( k ).and( invocationLocalIndex.add( k ).lessThan( workgroupSize ) ), () => {
  419. tSum.element( invocationLocalIndex ).addAssign( tSum.element( invocationLocalIndex.add( k ) ) );
  420. } );
  421. workgroupBarrier();
  422. k.divAssign( 2 );
  423. } );
  424. If( invocationLocalIndex.equal( uint( 0 ) ), () => {
  425. inputBuffer.element( workgroupId.x ).assign( tSum.element( uint( 0 ) ) );
  426. } );
  427. } )().compute( dispatchSize, [ workgroupSize ] );
  428. return fnDef;
  429. };
  430. // REDUCE 3
  431. /* Create array with enough indices for worst-case subgroup size */
  432. const createSubgroupArray = ( type, workgroupSize, minSubgroupSize = 4 ) => {
  433. return workgroupArray( 'uint', workgroupSize / minSubgroupSize );
  434. };
  435. // zcbenz implementation
  436. // https://github.com/frost-beta/betann/blob/8aa2701caf63fb29bd4cd2454e656973342c1588/betann/wgsl/reduce_ops.wgsl#L71
  437. const RowReduce = ( rowReduceProps ) => {
  438. const { workgroupSize, inputBuffer, total, rowOffset, currentRowSize, workPerThread, vectorized } = rowReduceProps;
  439. // Number of unvectorized elements each workgroup can ingest
  440. // At workgroupSize of 256, blockSize will be 1024
  441. const blockSize = uint( workgroupSize ).mul( workPerThread );
  442. const block = uint( 0 ).toVar( 'block' );
  443. // At rowSize of 2048, there will be two blocks
  444. const blockLimiter = currentRowSize.div( blockSize ).toVar( 'blockLimiter' );
  445. Loop( block.lessThan( blockLimiter ), () => {
  446. const blockOffset = block.mul( blockSize );
  447. const startThread = blockOffset.add( invocationLocalIndex.mul( workPerThread ) );
  448. const localThreadOffset = uint( 0 ).toVar( 'localThreadOffset' );
  449. Loop( localThreadOffset.lessThan( workPerThread ), () => {
  450. const inputElement = inputBuffer.element( rowOffset.add( startThread ).addLocal );
  451. if ( vectorized ) {
  452. const value = dot( inputElement, uvec4( 1 ) );
  453. total.addAssign( value );
  454. } else {
  455. const inputElement = inputBuffer.element( rowOffset.add( startThread ).add( localThreadOffset ) );
  456. total.addAssign( inputElement );
  457. }
  458. // Increment up a thread
  459. localThreadOffset.addAssign( 1 );
  460. } );
  461. // Increment up a block
  462. block.addAssign( 1 );
  463. } );
  464. // Ignoring left over check for this example, since we know ahead of time the value of leftover (2048 % 1024 === 0)
  465. };
  466. const WorkgroupReduce = ( workgroupReduceProps ) => {
  467. const { total, workgroupSize } = workgroupReduceProps;
  468. const subgroupSums = createSubgroupArray( 'uint', workgroupSize );
  469. // Assign sum of all values in subgroup to total
  470. total.assign( subgroupAdd( total ) );
  471. const delta = uint( workgroupSize ).div( subgroupSize ).toVar( 'delta' );
  472. const subgroupMetaRank = invocationLocalIndex.div( subgroupSize );
  473. Loop( float( delta ).greaterThan( 1.0 ), () => {
  474. If( invocationSubgroupIndex.equal( 0 ), () => {
  475. // Each subgroup will populate the subgroupSums array
  476. subgroupSums.element( subgroupMetaRank ).assign( total );
  477. } );
  478. // Ensure that all subgroups in the workgroup have populated the workgroup memory array
  479. workgroupBarrier();
  480. // Thread 0 - subgroupsInWorkgroup will assign a value to total
  481. total.assign( select( invocationLocalIndex.lessThan( delta ), subgroupSums.element( invocationLocalIndex ), 0 ).uniformFlow() );
  482. // # of subgroups in workgroup is invariably less than # of threads in subgroup, so subgroupAdd will still sync here
  483. total.assign( subgroupAdd( total ) );
  484. delta.divAssign( subgroupSize );
  485. } );
  486. };
  487. const createReduce3Fn = ( createReduce3FnProps ) => {
  488. const { workgroupSize, workPerThread, inputBuffer, intermediateBuffer, rowSize } = createReduce3FnProps;
  489. const fnDef = Fn( () => {
  490. const inputSize = uint( inputBuffer.bufferCount.length );
  491. const rowOffset = workgroupId.x.mul( rowSize );
  492. // If the current rows elements exceed the bounds of the input
  493. // Select either 0 or number of elements left,
  494. // otherwise, select existing ROW_SIZE
  495. const currentRowSize = select(
  496. ( rowOffset.add( rowSize ) ).greaterThan( inputSize ),
  497. select( inputSize.greaterThan( rowOffset ), inputSize.sub( rowOffset ), 0 ).uniformFlow(),
  498. rowSize,
  499. ).uniformFlow();
  500. const total = uint( 0 ).toVar( 'total' );
  501. RowReduce( {
  502. inputBuffer: inputBuffer,
  503. total: total,
  504. rowOffset: rowOffset,
  505. currentRowSize: currentRowSize,
  506. workPerThread: workPerThread,
  507. workgroupSize: workgroupSize,
  508. } );
  509. WorkgroupReduce( {
  510. total: total,
  511. workgroupSize: workgroupSize,
  512. } );
  513. // Populate each workgroup with its reduction
  514. If( invocationLocalIndex.equal( 0 ), () => {
  515. intermediateBuffer.element( workgroupId.x ).assign( total );
  516. } );
  517. } )();
  518. return fnDef;
  519. };
  520. // REDUCE 4
  521. // b0nes164 inspired implementation with vec4
  522. const createReduce4Fn = ( props ) => {
  523. // Can't pass in subgroup size since we can't always be certain what size is at runtime
  524. const { size, workPerThread, workgroupSize, inputBuffer, intermediateBuffer } = props;
  525. const ELEMENTS_PER_VEC4 = 4;
  526. // The number of individual elements a single workgroup will access
  527. const partitionSize = workgroupSize * workPerThread * ELEMENTS_PER_VEC4;
  528. const vecSize = divRoundUp( size, ELEMENTS_PER_VEC4 );
  529. // Can also be calculated using divRoundUp( vecSize, workgroupSize * workPerThread );
  530. const numWorkgroups = divRoundUp( size, partitionSize );
  531. // Currently no way to specify dispatch size in increments of workgroups, so we convert to numInvocations
  532. const numInvocations = numWorkgroups * workgroupSize;
  533. const fnDef = Fn( () => {
  534. const perSubgroupReductionArray = createSubgroupArray( 'uint', workgroupSize );
  535. // Get the index of the subgroup within the workgroup
  536. const subgroupMetaRank = invocationLocalIndex.div( subgroupSize );
  537. // Each subgroup block scans across 4 subgroups. So when we move into a new subgroup,
  538. // align that subgroups' accesses to the next 4 subgroups
  539. const subgroupOffset = subgroupMetaRank.mul( subgroupSize ).mul( workPerThread );
  540. subgroupOffset.addAssign( invocationSubgroupIndex );
  541. // Per workgroup, offset by number of vectorized elements scanned per workgroup
  542. const workgroupOffset = workgroupId.x.mul( uint( maxWorkgroupSize ).mul( workPerThread ) );
  543. const startThread = subgroupOffset.add( workgroupOffset );
  544. const subgroupReduction = uint( 0 );
  545. // Each thread will accumulate values from across 'workPerThread' subgroups
  546. If( workgroupId.x.lessThan( uint( numWorkgroups ).sub( 1 ) ), () => {
  547. Loop( {
  548. start: uint( 0 ),
  549. end: workPerThread,
  550. type: 'uint',
  551. condition: '<',
  552. name: 'currentSubgroupInBlock'
  553. }, () => {
  554. // Get vectorized element from input array
  555. const val = inputBuffer.element( startThread );
  556. // Sum values within vec4 together by using result of dot product
  557. subgroupReduction.addAssign( dot( uvec4( 1 ), val ) );
  558. // Increment so thread will scan value in next subgroup
  559. startThread.addAssign( subgroupSize );
  560. } );
  561. } );
  562. // Ensure that the last workgroup does not access out of bounds indices
  563. If( workgroupId.x.equal( uint( numWorkgroups ).sub( 1 ) ), () => {
  564. Loop( {
  565. start: uint( 0 ),
  566. end: workPerThread,
  567. type: 'uint',
  568. condition: '<',
  569. name: 'currentSubgroupInBlock'
  570. }, () => {
  571. // Ensure index is less than number of available vectors in inputBuffer
  572. const val = select( startThread.lessThan( uint( vecSize ) ), inputBuffer.element( startThread ), uvec4( 0 ) ).uniformFlow();
  573. subgroupReduction.addAssign( dot( val, uvec4( 1 ) ) );
  574. startThread.addAssign( subgroupSize );
  575. } );
  576. } );
  577. subgroupReduction.assign( subgroupAdd( subgroupReduction ) );
  578. // Assuming that each element in the input buffer is 1, we generally expect each invocation's subgroupReduction
  579. // value to be ELEMENTS_PER_VEC4 * workPerThread * subgroupSize
  580. // Delegate one thread per subgroup to assign each subgroup's reduction to the workgroup array
  581. If( invocationSubgroupIndex.equal( uint( 0 ) ), () => {
  582. perSubgroupReductionArray.element( subgroupMetaRank ).assign( subgroupReduction );
  583. } );
  584. // Ensure that each workgroup has populated the perSubgroupReductionArray with data
  585. // from each of it's subgroups
  586. workgroupBarrier();
  587. if ( props.debugBuffer ) {
  588. If( invocationLocalIndex.equal( uint( 0 ) ), () => {
  589. props.debugBuffer.element( workgroupId.x ).assign( subgroupReduction );
  590. } );
  591. workgroupBarrier();
  592. }
  593. // WORKGROUP LEVEL REDUCE
  594. // Multiple approaches here
  595. // log2(subgroupSize) -> TSL log2 function
  596. // countTrailingZeros/findLSB(subgroupSize) -> Currently unsupported function in TSL that counts trailing zeros in number bit representation
  597. // Can technically petition GPU for subgroupSize in shader and calculate logs on CPU at cost of shader being generalizable across devices
  598. // May also break if subgroupSize changes when device is lost or if program is rerun on lower power device
  599. const subgroupSizeLog = uint( log2( float( subgroupSize ) ) ).toVar( 'subgroupSizeLog' );
  600. const spineSize = uint( workgroupSize ).shiftRight( subgroupSizeLog );
  601. const spineSizeLog = uint( log2( float( spineSize ) ) ).toVar( 'spineSizeLog' );
  602. // Align size to powers of subgroupSize
  603. const squaredSubgroupLog = ( spineSizeLog.add( subgroupSizeLog ).sub( 1 ) );
  604. squaredSubgroupLog.divAssign( subgroupSizeLog );
  605. squaredSubgroupLog.mulAssign( subgroupSizeLog );
  606. const alignedSize = ( uint( 1 ).shiftLeft( squaredSubgroupLog ) ).toVar( 'alignedSize' );
  607. // aligned size 2 * 4
  608. const offset = uint( 0 );
  609. // In cases where the number of subgroups in a workgroup is greater than the subgroup size itself,
  610. // we need to iterate over the array again to capture all the data in the workgroup array buffer
  611. Loop( { start: subgroupSize, end: alignedSize, condition: '<=', name: 'j', type: 'uint', update: '<<= subgroupSizeLog' }, () => {
  612. const subgroupIndex = ( ( invocationLocalIndex.add( 1 ) ).shiftLeft( offset ) ).sub( 1 );
  613. const isValidSubgroupIndex = subgroupIndex.lessThan( spineSize ).toVar( 'isValidSubgroupIndex' );
  614. // Reduce values within the local workgroup memory.
  615. // Set toVar to ensure subgroupAdd executes before (not within) the if statement.
  616. const t = subgroupAdd(
  617. select(
  618. isValidSubgroupIndex,
  619. perSubgroupReductionArray.element( subgroupIndex ),
  620. 0
  621. ).uniformFlow()
  622. ).toVar( 't' );
  623. // Can assign back to workgroupArray since all
  624. // subgroup threads work in lockstop for subgroupAdd
  625. If( isValidSubgroupIndex, () => {
  626. perSubgroupReductionArray.element( subgroupIndex ).assign( t );
  627. } );
  628. // Ensure all threads have completed work
  629. workgroupBarrier();
  630. offset.addAssign( subgroupSizeLog );
  631. } );
  632. // Assign single thread from workgroup to assign workgroup reduction
  633. If( invocationLocalIndex.equal( uint( 0 ) ), () => {
  634. const reducedWorkgroupSum = perSubgroupReductionArray.element( uint( spineSize ).sub( 1 ) );
  635. intermediateBuffer.element( workgroupId.x ).assign( reducedWorkgroupSum );
  636. } );
  637. } )().compute( numInvocations, [ maxWorkgroupSize ] );
  638. return fnDef;
  639. };
  640. // INCORRECT BASELINE
  641. const createIncorrectBaselineFn = ( incorrectBaselineProps ) => {
  642. const { inputBuffer } = incorrectBaselineProps;
  643. const fnDef = Fn( () => {
  644. inputBuffer.element( instanceIndex ).assign( 99999 );
  645. } )();
  646. return fnDef;
  647. };
  648. init();
  649. init( false );
  650. async function init( leftSideDisplay = true ) {
  651. const effectController = leftSideDisplay ? leftEffectController : rightEffectController;
  652. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  653. const camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  654. camera.position.z = 1;
  655. const scene = new THREE.Scene();
  656. const array = new Uint32Array( Array.from( { length: size }, ( _, i ) => {
  657. return 1;
  658. } ) );
  659. // Represents array of data as uints in compute shader.
  660. const inputStorage = instancedArray( array, 'uint' ).setPBO( true ).setName( `Current_${leftSideDisplay ? 'Left' : 'Right'}` );
  661. // Represents array of data as vec4s in compute shader;
  662. const inputVec4BufferAttribute = new THREE.StorageInstancedBufferAttribute( array, 4 );
  663. const inputVectorizedStorage = storage( inputVec4BufferAttribute, 'uvec4', vecSize ).setPBO( true ).setName( `CurrentVectorized_${leftSideDisplay ? 'Left' : 'Right'}` );
  664. // Reduce 3 Calculations
  665. const workPerThread = 4;
  666. const numRows = workPerThread * 32;
  667. const rowSize = divRoundUp( size, numRows );
  668. const workgroupSumsArray = new Uint32Array( numRows );
  669. const workgroupSumsStorage = instancedArray( workgroupSumsArray, 'uint' ).setPBO( true ).setName( `WorkgroupSums_${leftSideDisplay ? 'Left' : 'Right'}` );
  670. const debugArray = new Uint32Array( 1024 );
  671. const debugStorage = instancedArray( debugArray, 'uint' ).setPBO( true ).setName( `Debug_${leftSideDisplay ? 'Left' : 'Right'}` );
  672. const buffers = {
  673. 'Input Buffer': inputStorage,
  674. 'Input Vectorized Buffer': inputVectorizedStorage,
  675. 'Workgroup Sums Buffer': workgroupSumsStorage,
  676. 'Debug Buffer': debugStorage,
  677. };
  678. const logFunctionName = `Log ${leftSideDisplay ? 'Left' : 'Right'} Side`;
  679. const functionObj = {};
  680. functionObj[ logFunctionName ] = async() => {
  681. const selectedBuffer = buffers[ unifiedEffectController.loggedBuffer ];
  682. console.log( new Uint32Array( await renderer.getArrayBufferAsync( selectedBuffer.value ) ) );
  683. };
  684. debugFolder.add( functionObj, `Log ${leftSideDisplay ? 'Left' : 'Right'} Side` );
  685. const computeResetBufferFn = Fn( () => {
  686. inputStorage.element( instanceIndex ).assign( 1 );
  687. } );
  688. const computeResetWorkgroupSumsFn = Fn( () => {
  689. workgroupSumsStorage.element( instanceIndex ).assign( 0 );
  690. } );
  691. // Re-initialize compute buffer
  692. const computeResetBuffer = computeResetBufferFn().compute( size );
  693. const computeResetWorkgroupSums = computeResetWorkgroupSumsFn().compute( 256 );
  694. const renderer = new THREE.WebGPURenderer( { antialias: false, trackTimestamp: true } );
  695. renderer.setPixelRatio( window.devicePixelRatio );
  696. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  697. // Unfortunately, need to arbitrarily run compute shader to get access to device limits
  698. await renderer.computeAsync( computeResetBuffer );
  699. if ( renderer.backend.device !== null ) {
  700. maxWorkgroupSize = renderer.backend.device.limits.maxComputeWorkgroupSizeX;
  701. }
  702. // Create and store dispatches of reduction of certain size. Map each set of dispatches to algorithm name.
  703. const computeReduce0Fn = Fn( () => {
  704. const { numThreadsDispatched } = effectController;
  705. inputStorage.element( instanceIndex ).addAssign( inputStorage.element( instanceIndex.add( numThreadsDispatched ) ) );
  706. } )();
  707. const reduce0Calls = [];
  708. for ( let i = size / 2; i >= 1; i /= 2 ) {
  709. const reduce0 = computeReduce0Fn.compute( i, [ maxWorkgroupSize ] );
  710. reduce0Calls.push( reduce0 );
  711. }
  712. const reduce1Calls = [
  713. // Accumulation
  714. createReduce1Fn( {
  715. dispatchSize: maxWorkgroupSize * maxWorkgroupSize,
  716. workgroupSize: maxWorkgroupSize,
  717. numElements: size,
  718. inputBuffer: inputStorage,
  719. } ),
  720. // 1 Block accumulation
  721. createReduce1Fn( {
  722. dispatchSize: maxWorkgroupSize,
  723. numElements: maxWorkgroupSize * maxWorkgroupSize,
  724. workgroupSize: maxWorkgroupSize,
  725. inputBuffer: inputStorage,
  726. } ),
  727. // Final result
  728. createReduce1Fn( {
  729. dispatchSize: 1,
  730. numElements: maxWorkgroupSize,
  731. workgroupSize: 1,
  732. inputBuffer: inputStorage
  733. } ),
  734. ];
  735. const reduce2Calls = [
  736. // Accumulate within workgroups
  737. createReduce2Fn( {
  738. workgroupSize: maxWorkgroupSize,
  739. dispatchSize: maxWorkgroupSize * maxWorkgroupSize,
  740. numElements: size,
  741. inputBuffer: inputStorage,
  742. } ),
  743. // 1 Block accumulation
  744. createReduce2Fn( {
  745. workgroupSize: maxWorkgroupSize,
  746. dispatchSize: maxWorkgroupSize,
  747. numElements: maxWorkgroupSize,
  748. inputBuffer: inputStorage,
  749. } ),
  750. ];
  751. const reduce3Calls = [
  752. createReduce3Fn( {
  753. inputBuffer: inputStorage,
  754. intermediateBuffer: workgroupSumsStorage,
  755. workgroupSize: maxWorkgroupSize,
  756. workPerThread: 4,
  757. rowSize: rowSize,
  758. vectorized: false,
  759. } ).compute( maxWorkgroupSize * numRows, [ maxWorkgroupSize ] ),
  760. createReduce3Fn( {
  761. inputBuffer: workgroupSumsStorage,
  762. intermediateBuffer: inputStorage,
  763. workgroupSize: 32,
  764. workPerThread: 4,
  765. rowSize: rowSize,
  766. vectorized: false
  767. } ).compute( 32, [ 32 ] )
  768. ];
  769. const reduce4Calls = [
  770. createReduce4Fn( {
  771. size: size,
  772. inputBuffer: inputVectorizedStorage,
  773. intermediateBuffer: workgroupSumsStorage,
  774. workgroupSize: maxWorkgroupSize,
  775. workPerThread: 4,
  776. } ),
  777. createReduce3Fn( {
  778. inputBuffer: workgroupSumsStorage,
  779. intermediateBuffer: inputStorage,
  780. workgroupSize: 32,
  781. workPerThread: 4,
  782. rowSize: rowSize,
  783. vectorized: false
  784. } ).compute( 32, [ 32 ] )
  785. ];
  786. const incorrectBaselineCalls = [
  787. createIncorrectBaselineFn( {
  788. inputBuffer: inputStorage,
  789. } ).compute( size ),
  790. ];
  791. const calls = {
  792. 'Reduce 0 (N/2)': reduce0Calls,
  793. 'Reduce 1 (Naive Accumulate)': reduce1Calls,
  794. 'Reduce 2 (Workgroup Reduction)': reduce2Calls,
  795. 'Reduce 3 (Subgroup Reduce)': reduce3Calls,
  796. 'Reduce 4 (Subgroup Optimized)': reduce4Calls,
  797. 'Incorrect Baseline': incorrectBaselineCalls
  798. };
  799. const getColor = ( bufferToCheck, colorChanger, width, height ) => {
  800. const subtracter = float( colorChanger ).div( width.mul( height ) );
  801. const color = vec3( subtracter.oneMinus() ).toVar();
  802. const { highlight } = effectController;
  803. // Validate that element 0 is equal to expected result of reduction
  804. If( highlight.equal( 1 ), () => {
  805. If( ( bufferToCheck.element( 0 ) ).equal( size ), () => {
  806. color.assign( vec3( 0.0, subtracter.oneMinus(), 0.0 ) );
  807. } ).Else( () => {
  808. color.assign( vec3( subtracter.oneMinus(), 0.0, 0.0 ) );
  809. } );
  810. } );
  811. return color;
  812. };
  813. const displayNodes = leftSideDisplay ? leftDisplayColorNodes : rightDisplayColorNodes;
  814. displayNodes[ 'Input Grid' ] = Fn( () => {
  815. const { gridElementWidth, gridElementHeight, gridDisplayWidth, gridDisplayHeight } = unifiedEffectController;
  816. const newUV = uv().mul( vec2( gridDisplayWidth, gridDisplayHeight ) );
  817. const pixel = uvec2( uint( floor( newUV.x ) ), uint( floor( newUV.y ) ) );
  818. const elementIndex = uint( gridDisplayWidth ).mul( pixel.y ).add( pixel.x );
  819. const colorChanger = uint( 0 ).toVar();
  820. const color = vec3( 0 ).toVar( 'color' );
  821. colorChanger.assign( inputStorage.element( elementIndex ) );
  822. color.assign( getColor( inputStorage, colorChanger, gridElementWidth, gridElementHeight ) );
  823. return color;
  824. } )();
  825. displayNodes[ 'Input Log2' ] = Fn( () => {
  826. const { gridElementWidth, gridElementHeight } = unifiedEffectController;
  827. const newUV = uv().mul( vec2( Math.log2( size ) ), 1 );
  828. const colorChanger = uint( 0 ).toVar();
  829. const color = vec3( 0 ).toVar( 'color' );
  830. colorChanger.assign( inputStorage.element( uint( 1 ).shiftLeft( newUV.x ) ) );
  831. color.assign( getColor( inputStorage, colorChanger, gridElementWidth, gridElementHeight ) );
  832. return color;
  833. } )();
  834. displayNodes[ 'Input Element 0' ] = Fn( () => {
  835. const { gridElementWidth, gridElementHeight } = unifiedEffectController;
  836. const colorChanger = uint( 0 ).toVar();
  837. const color = vec3( 0 ).toVar( 'color' );
  838. // Clamp display of single element to shade where green is still readable
  839. colorChanger.assign( clamp( inputStorage.element( 0 ), 0, size / 2 ) );
  840. color.assign( getColor( inputStorage, colorChanger, gridElementWidth, gridElementHeight ) );
  841. return color;
  842. } )();
  843. displayNodes[ 'Workgroup Sum Grid' ] = Fn( () => {
  844. const width = uint( 8 );
  845. const height = uint( 16 );
  846. const newUV = uv().mul( vec2( width, height ) );
  847. const pixel = uvec2( uint( floor( newUV.x ) ), uint( floor( newUV.y ) ) );
  848. const elementIndex = uint( width ).mul( pixel.y ).add( pixel.x );
  849. const colorChanger = uint( 0 ).toVar();
  850. const color = vec3( 0 ).toVar( 'color' );
  851. colorChanger.assign( workgroupSumsStorage.element( elementIndex ) );
  852. color.assign( getColor( inputStorage, colorChanger, width, height ) );
  853. return color;
  854. } )();
  855. ( leftSideDisplay ? leftMaterial : rightMaterial ).colorNode = displayNodes[ effectController.displayMode ];
  856. ( leftSideDisplay ? leftMaterial : rightMaterial ).needsUpdate = true;
  857. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), ( leftSideDisplay ? leftMaterial : rightMaterial ) );
  858. scene.add( plane );
  859. const animate = () => {
  860. renderer.render( scene, camera );
  861. };
  862. renderer.setAnimationLoop( animate );
  863. document.body.appendChild( renderer.domElement );
  864. renderer.domElement.style.position = 'absolute';
  865. renderer.domElement.style.top = '0';
  866. renderer.domElement.style.left = '0';
  867. renderer.domElement.style.width = '50%';
  868. renderer.domElement.style.height = '100%';
  869. if ( ! leftSideDisplay ) {
  870. renderer.domElement.style.left = '50%';
  871. scene.background = new THREE.Color( 0x212121 );
  872. } else {
  873. scene.background = new THREE.Color( 0x313131 );
  874. }
  875. renderer.info.autoReset = false;
  876. const stepAnimation = async function () {
  877. const currentAlgorithm = effectController.algo;
  878. const state = effectController.state;
  879. const stateController = leftSideDisplay ? stateLeftController : stateRightController;
  880. if ( state === 'Reset' ) {
  881. renderer.computeAsync( computeResetBuffer );
  882. renderer.computeAsync( computeResetWorkgroupSums );
  883. } else if ( state === 'Run Algo' ) {
  884. renderer.info.reset();
  885. const cpuTime = 0;
  886. switch ( currentAlgorithm ) {
  887. case 'Reduce 0 (N/2)': {
  888. let m = size / 2;
  889. for ( let i = 0; i < reduce0Calls.length; i ++ ) {
  890. effectController.numThreadsDispatched.value = m;
  891. const reduce0 = reduce0Calls[ i ];
  892. // Do a reduction step
  893. renderer.computeAsync( reduce0 );
  894. renderer.resolveTimestampsAsync( THREE.TimestampQuery.COMPUTE );
  895. m /= 2;
  896. }
  897. break;
  898. }
  899. default: {
  900. const currentAlgoCalls = calls[ currentAlgorithm ];
  901. for ( let i = 0; i < currentAlgoCalls.length; i ++ ) {
  902. renderer.computeAsync( currentAlgoCalls[ i ] );
  903. renderer.resolveTimestampsAsync( THREE.TimestampQuery.COMPUTE );
  904. }
  905. break;
  906. }
  907. }
  908. // DEBUG: const reductionResult = new Uint32Array( await renderer.getArrayBufferAsync( currentBuffer ) )[0];
  909. let passInfoString = '';
  910. if ( effectController.algo.substring( 0, 3 ) === 'CPU' ) {
  911. passInfoString = `Ran in ${cpuTime}ms<br>`;
  912. } else {
  913. passInfoString = `${renderer.info.compute.frameCalls} pass in ${renderer.info.compute.timestamp.toFixed( 6 )}ms<br>`;
  914. }
  915. timestamps[ leftSideDisplay ? 'left_side_display' : 'right_side_display' ].innerHTML = `
  916. Compute ${effectController.algo}: ${passInfoString}`;
  917. }
  918. renderer.render( scene, camera );
  919. renderer.resolveTimestampsAsync( THREE.TimestampQuery.RENDER );
  920. // Validate next state
  921. if ( state === 'Run Algo' ) {
  922. stateController.setValue( 'Validate' );
  923. effectController.highlight.value = 1;
  924. } else if ( state === 'Validate' ) {
  925. stateController.setValue( 'Reset' );
  926. effectController.highlight.value = 0;
  927. } else if ( state === 'Reset' ) {
  928. stateController.setValue( 'Run Algo' );
  929. }
  930. setTimeout( stepAnimation, 1000 );
  931. };
  932. window.addEventListener( 'resize', onWindowResize );
  933. function onWindowResize() {
  934. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  935. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  936. const frustumHeight = camera.top - camera.bottom;
  937. camera.left = - frustumHeight * aspect / 2;
  938. camera.right = frustumHeight * aspect / 2;
  939. camera.updateProjectionMatrix();
  940. renderer.render( scene, camera );
  941. }
  942. setTimeout( stepAnimation, 1000 );
  943. }
  944. </script>
  945. </body>
  946. </html>
粤ICP备19079148号