webgpu_compute_reduce.html 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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, countTrailingZeros } 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) -> TSL function 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 = countTrailingZeros( subgroupSize ).toVar( 'subgroupSizeLog' );
  600. const spineSize = uint( workgroupSize ).shiftRight( subgroupSizeLog );
  601. const spineSizeLog = countTrailingZeros( 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 }, () => {
  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. await renderer.init();
  698. // Unfortunately, need to arbitrarily run compute shader to get access to device limits
  699. renderer.compute( computeResetBuffer );
  700. if ( renderer.backend.device !== null ) {
  701. maxWorkgroupSize = renderer.backend.device.limits.maxComputeWorkgroupSizeX;
  702. }
  703. // Create and store dispatches of reduction of certain size. Map each set of dispatches to algorithm name.
  704. const computeReduce0Fn = Fn( () => {
  705. const { numThreadsDispatched } = effectController;
  706. inputStorage.element( instanceIndex ).addAssign( inputStorage.element( instanceIndex.add( numThreadsDispatched ) ) );
  707. } )();
  708. const reduce0Calls = [];
  709. for ( let i = size / 2; i >= 1; i /= 2 ) {
  710. const reduce0 = computeReduce0Fn.compute( i, [ maxWorkgroupSize ] );
  711. reduce0Calls.push( reduce0 );
  712. }
  713. const reduce1Calls = [
  714. // Accumulation
  715. createReduce1Fn( {
  716. dispatchSize: maxWorkgroupSize * maxWorkgroupSize,
  717. workgroupSize: maxWorkgroupSize,
  718. numElements: size,
  719. inputBuffer: inputStorage,
  720. } ),
  721. // 1 Block accumulation
  722. createReduce1Fn( {
  723. dispatchSize: maxWorkgroupSize,
  724. numElements: maxWorkgroupSize * maxWorkgroupSize,
  725. workgroupSize: maxWorkgroupSize,
  726. inputBuffer: inputStorage,
  727. } ),
  728. // Final result
  729. createReduce1Fn( {
  730. dispatchSize: 1,
  731. numElements: maxWorkgroupSize,
  732. workgroupSize: 1,
  733. inputBuffer: inputStorage
  734. } ),
  735. ];
  736. const reduce2Calls = [
  737. // Accumulate within workgroups
  738. createReduce2Fn( {
  739. workgroupSize: maxWorkgroupSize,
  740. dispatchSize: maxWorkgroupSize * maxWorkgroupSize,
  741. numElements: size,
  742. inputBuffer: inputStorage,
  743. } ),
  744. // 1 Block accumulation
  745. createReduce2Fn( {
  746. workgroupSize: maxWorkgroupSize,
  747. dispatchSize: maxWorkgroupSize,
  748. numElements: maxWorkgroupSize,
  749. inputBuffer: inputStorage,
  750. } ),
  751. ];
  752. const reduce3Calls = [
  753. createReduce3Fn( {
  754. inputBuffer: inputStorage,
  755. intermediateBuffer: workgroupSumsStorage,
  756. workgroupSize: maxWorkgroupSize,
  757. workPerThread: 4,
  758. rowSize: rowSize,
  759. vectorized: false,
  760. } ).compute( maxWorkgroupSize * numRows, [ maxWorkgroupSize ] ),
  761. createReduce3Fn( {
  762. inputBuffer: workgroupSumsStorage,
  763. intermediateBuffer: inputStorage,
  764. workgroupSize: 32,
  765. workPerThread: 4,
  766. rowSize: rowSize,
  767. vectorized: false
  768. } ).compute( 32, [ 32 ] )
  769. ];
  770. const reduce4Calls = [
  771. createReduce4Fn( {
  772. size: size,
  773. inputBuffer: inputVectorizedStorage,
  774. intermediateBuffer: workgroupSumsStorage,
  775. workgroupSize: maxWorkgroupSize,
  776. workPerThread: 4,
  777. } ),
  778. createReduce3Fn( {
  779. inputBuffer: workgroupSumsStorage,
  780. intermediateBuffer: inputStorage,
  781. workgroupSize: 32,
  782. workPerThread: 4,
  783. rowSize: rowSize,
  784. vectorized: false
  785. } ).compute( 32, [ 32 ] )
  786. ];
  787. const incorrectBaselineCalls = [
  788. createIncorrectBaselineFn( {
  789. inputBuffer: inputStorage,
  790. } ).compute( size ),
  791. ];
  792. const calls = {
  793. 'Reduce 0 (N/2)': reduce0Calls,
  794. 'Reduce 1 (Naive Accumulate)': reduce1Calls,
  795. 'Reduce 2 (Workgroup Reduction)': reduce2Calls,
  796. 'Reduce 3 (Subgroup Reduce)': reduce3Calls,
  797. 'Reduce 4 (Subgroup Optimized)': reduce4Calls,
  798. 'Incorrect Baseline': incorrectBaselineCalls
  799. };
  800. const getColor = ( bufferToCheck, colorChanger, width, height ) => {
  801. const subtracter = float( colorChanger ).div( width.mul( height ) );
  802. const color = vec3( subtracter.oneMinus() ).toVar();
  803. const { highlight } = effectController;
  804. // Validate that element 0 is equal to expected result of reduction
  805. If( highlight.equal( 1 ), () => {
  806. If( ( bufferToCheck.element( 0 ) ).equal( size ), () => {
  807. color.assign( vec3( 0.0, subtracter.oneMinus(), 0.0 ) );
  808. } ).Else( () => {
  809. color.assign( vec3( subtracter.oneMinus(), 0.0, 0.0 ) );
  810. } );
  811. } );
  812. return color;
  813. };
  814. const displayNodes = leftSideDisplay ? leftDisplayColorNodes : rightDisplayColorNodes;
  815. displayNodes[ 'Input Grid' ] = Fn( () => {
  816. const { gridElementWidth, gridElementHeight, gridDisplayWidth, gridDisplayHeight } = unifiedEffectController;
  817. const newUV = uv().mul( vec2( gridDisplayWidth, gridDisplayHeight ) );
  818. const pixel = uvec2( uint( floor( newUV.x ) ), uint( floor( newUV.y ) ) );
  819. const elementIndex = uint( gridDisplayWidth ).mul( pixel.y ).add( pixel.x );
  820. const colorChanger = uint( 0 ).toVar();
  821. const color = vec3( 0 ).toVar( 'color' );
  822. colorChanger.assign( inputStorage.element( elementIndex ) );
  823. color.assign( getColor( inputStorage, colorChanger, gridElementWidth, gridElementHeight ) );
  824. return color;
  825. } )();
  826. displayNodes[ 'Input Log2' ] = Fn( () => {
  827. const { gridElementWidth, gridElementHeight } = unifiedEffectController;
  828. const newUV = uv().mul( vec2( Math.log2( size ) ), 1 );
  829. const colorChanger = uint( 0 ).toVar();
  830. const color = vec3( 0 ).toVar( 'color' );
  831. colorChanger.assign( inputStorage.element( uint( 1 ).shiftLeft( newUV.x ) ) );
  832. color.assign( getColor( inputStorage, colorChanger, gridElementWidth, gridElementHeight ) );
  833. return color;
  834. } )();
  835. displayNodes[ 'Input Element 0' ] = Fn( () => {
  836. const { gridElementWidth, gridElementHeight } = unifiedEffectController;
  837. const colorChanger = uint( 0 ).toVar();
  838. const color = vec3( 0 ).toVar( 'color' );
  839. // Clamp display of single element to shade where green is still readable
  840. colorChanger.assign( clamp( inputStorage.element( 0 ), 0, size / 2 ) );
  841. color.assign( getColor( inputStorage, colorChanger, gridElementWidth, gridElementHeight ) );
  842. return color;
  843. } )();
  844. displayNodes[ 'Workgroup Sum Grid' ] = Fn( () => {
  845. const width = uint( 8 );
  846. const height = uint( 16 );
  847. const newUV = uv().mul( vec2( width, height ) );
  848. const pixel = uvec2( uint( floor( newUV.x ) ), uint( floor( newUV.y ) ) );
  849. const elementIndex = uint( width ).mul( pixel.y ).add( pixel.x );
  850. const colorChanger = uint( 0 ).toVar();
  851. const color = vec3( 0 ).toVar( 'color' );
  852. colorChanger.assign( workgroupSumsStorage.element( elementIndex ) );
  853. color.assign( getColor( inputStorage, colorChanger, width, height ) );
  854. return color;
  855. } )();
  856. ( leftSideDisplay ? leftMaterial : rightMaterial ).colorNode = displayNodes[ effectController.displayMode ];
  857. ( leftSideDisplay ? leftMaterial : rightMaterial ).needsUpdate = true;
  858. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), ( leftSideDisplay ? leftMaterial : rightMaterial ) );
  859. scene.add( plane );
  860. const animate = () => {
  861. renderer.render( scene, camera );
  862. };
  863. renderer.setAnimationLoop( animate );
  864. document.body.appendChild( renderer.domElement );
  865. renderer.domElement.style.position = 'absolute';
  866. renderer.domElement.style.top = '0';
  867. renderer.domElement.style.left = '0';
  868. renderer.domElement.style.width = '50%';
  869. renderer.domElement.style.height = '100%';
  870. if ( ! leftSideDisplay ) {
  871. renderer.domElement.style.left = '50%';
  872. scene.background = new THREE.Color( 0x212121 );
  873. } else {
  874. scene.background = new THREE.Color( 0x313131 );
  875. }
  876. renderer.info.autoReset = false;
  877. const stepAnimation = async function () {
  878. const currentAlgorithm = effectController.algo;
  879. const state = effectController.state;
  880. const stateController = leftSideDisplay ? stateLeftController : stateRightController;
  881. if ( state === 'Reset' ) {
  882. renderer.compute( computeResetBuffer );
  883. renderer.compute( computeResetWorkgroupSums );
  884. } else if ( state === 'Run Algo' ) {
  885. renderer.info.reset();
  886. const cpuTime = 0;
  887. switch ( currentAlgorithm ) {
  888. case 'Reduce 0 (N/2)': {
  889. let m = size / 2;
  890. for ( let i = 0; i < reduce0Calls.length; i ++ ) {
  891. effectController.numThreadsDispatched.value = m;
  892. const reduce0 = reduce0Calls[ i ];
  893. // Do a reduction step
  894. renderer.compute( reduce0 );
  895. renderer.resolveTimestampsAsync( THREE.TimestampQuery.COMPUTE );
  896. m /= 2;
  897. }
  898. break;
  899. }
  900. default: {
  901. const currentAlgoCalls = calls[ currentAlgorithm ];
  902. for ( let i = 0; i < currentAlgoCalls.length; i ++ ) {
  903. renderer.compute( currentAlgoCalls[ i ] );
  904. renderer.resolveTimestampsAsync( THREE.TimestampQuery.COMPUTE );
  905. }
  906. break;
  907. }
  908. }
  909. // DEBUG: const reductionResult = new Uint32Array( await renderer.getArrayBufferAsync( currentBuffer ) )[0];
  910. let passInfoString = '';
  911. if ( effectController.algo.substring( 0, 3 ) === 'CPU' ) {
  912. passInfoString = `Ran in ${cpuTime}ms<br>`;
  913. } else {
  914. passInfoString = `${renderer.info.compute.frameCalls} pass in ${renderer.info.compute.timestamp.toFixed( 6 )}ms<br>`;
  915. }
  916. timestamps[ leftSideDisplay ? 'left_side_display' : 'right_side_display' ].innerHTML = `
  917. Compute ${effectController.algo}: ${passInfoString}`;
  918. }
  919. renderer.render( scene, camera );
  920. renderer.resolveTimestampsAsync( THREE.TimestampQuery.RENDER );
  921. // Validate next state
  922. if ( state === 'Run Algo' ) {
  923. stateController.setValue( 'Validate' );
  924. effectController.highlight.value = 1;
  925. } else if ( state === 'Validate' ) {
  926. stateController.setValue( 'Reset' );
  927. effectController.highlight.value = 0;
  928. } else if ( state === 'Reset' ) {
  929. stateController.setValue( 'Run Algo' );
  930. }
  931. setTimeout( stepAnimation, 1000 );
  932. };
  933. window.addEventListener( 'resize', onWindowResize );
  934. function onWindowResize() {
  935. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  936. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  937. const frustumHeight = camera.top - camera.bottom;
  938. camera.left = - frustumHeight * aspect / 2;
  939. camera.right = frustumHeight * aspect / 2;
  940. camera.updateProjectionMatrix();
  941. renderer.render( scene, camera );
  942. }
  943. setTimeout( stepAnimation, 1000 );
  944. }
  945. </script>
  946. </body>
  947. </html>
粤ICP备19079148号