webgpu_compute_sort_bitonic.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - storage pbo external element</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. .swap_area {
  11. position: absolute;
  12. top: 150px;
  13. padding: 10px;
  14. background: rgba( 0, 0, 0, 0.5 );
  15. color: #fff;
  16. font-family: monospace;
  17. font-size: 12px;
  18. line-height: 1.5;
  19. pointer-events: none;
  20. text-align: left;
  21. }
  22. </style>
  23. <div id="info">
  24. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
  25. <br /> This example demonstrates a bitonic sort running step by step in a compute shader.
  26. <br /> The left canvas swaps values within workgroup local arrays. The right swaps values within storage buffers.
  27. <br /> Reference implementation by <a href="https://poniesandlight.co.uk/reflect/bitonic_merge_sort/">Tim Gfrerer</a>
  28. <br />
  29. <div id="local_swap" class="swap_area" style="left: 0;"></div>
  30. <div id="global_swap" class="swap_area" style="right: 0;"></div>
  31. </div>
  32. <script type="importmap">
  33. {
  34. "imports": {
  35. "three": "../build/three.webgpu.js",
  36. "three/webgpu": "../build/three.webgpu.js",
  37. "three/tsl": "../build/three.tsl.js",
  38. "three/addons/": "./jsm/"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three/webgpu';
  44. import { storage, If, vec3, not, uniform, uv, uint, Fn, vec2, abs, int, uvec2, floor, instanceIndex } from 'three/tsl';
  45. import { BitonicSort, getBitonicDisperseIndices, getBitonicFlipIndices } from 'three/addons/gpgpu/BitonicSort.js';
  46. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  47. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  48. const StepType = {
  49. NONE: 0,
  50. // Swap values within workgroup local values
  51. SWAP_LOCAL: 1,
  52. DISPERSE_LOCAL: 2,
  53. // Swap values within global data buffer.
  54. FLIP_GLOBAL: 3,
  55. DISPERSE_GLOBAL: 4,
  56. };
  57. const timestamps = {
  58. local_swap: document.getElementById( 'local_swap' ),
  59. global_swap: document.getElementById( 'global_swap' )
  60. };
  61. const localColors = [ 'rgb(203, 64, 203)', 'rgb(0, 215, 215)' ];
  62. const globalColors = [ 'rgb(1, 150, 1)', 'red' ];
  63. // Total number of elements and the dimensions of the display grid.
  64. const size = 16384;
  65. const gridDim = Math.sqrt( size );
  66. const getNumSteps = () => {
  67. const n = Math.log2( size );
  68. return ( n * ( n + 1 ) ) / 2;
  69. };
  70. // Total number of steps in a bitonic sort with 'size' elements.
  71. const MAX_STEPS = getNumSteps();
  72. const effectController = {
  73. // Sqr root of 16834
  74. gridWidth: uniform( gridDim ),
  75. gridHeight: uniform( gridDim ),
  76. highlight: uniform( 1 ),
  77. stepBitonic: true,
  78. 'Display Mode': 'Swap Zone Highlight'
  79. };
  80. const gui = new GUI();
  81. gui.add( effectController, 'Display Mode', [ 'Elements', 'Swap Zone Highlight' ] ).onChange( () => {
  82. if ( effectController[ 'Display Mode' ] === 'Elements' ) {
  83. effectController.highlight.value = 0;
  84. } else {
  85. effectController.highlight.value = 1;
  86. }
  87. } );
  88. if ( WebGPU.isAvailable() === false ) {
  89. document.body.appendChild( WebGPU.getErrorMessage() );
  90. throw new Error( 'No WebGPU support' );
  91. }
  92. // Display utilities
  93. const getElementIndex = Fn( ( [ uvNode, gridWidth, gridHeight ] ) => {
  94. const newUV = uvNode.mul( vec2( gridWidth, gridHeight ) );
  95. const pixel = uvec2( uint( floor( newUV.x ) ), uint( floor( newUV.y ) ) );
  96. const elementIndex = uint( gridWidth ).mul( pixel.y ).add( pixel.x );
  97. return elementIndex;
  98. }, {
  99. uvNode: 'vec2',
  100. gridWidth: 'uint',
  101. gridHeight: 'uint',
  102. return: 'uint'
  103. } );
  104. const getColor = Fn( ( [ colorChanger, gridWidth, gridHeight ] ) => {
  105. const subtracter = colorChanger.div( gridWidth.mul( gridHeight ) );
  106. return vec3( subtracter.oneMinus() ).toVar();
  107. }, {
  108. colorChanger: 'float',
  109. gridWidth: 'float',
  110. gridHeight: 'float',
  111. return: 'vec3'
  112. } );
  113. const randomizeDataArray = ( array ) => {
  114. let currentIndex = array.length;
  115. while ( currentIndex !== 0 ) {
  116. const randomIndex = Math.floor( Math.random() * currentIndex );
  117. currentIndex -= 1;
  118. [ array[ currentIndex ], array[ randomIndex ] ] = [
  119. array[ randomIndex ],
  120. array[ currentIndex ],
  121. ];
  122. }
  123. };
  124. const windowResizeCallback = ( renderer, scene, camera ) => {
  125. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  126. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  127. const frustumHeight = camera.top - camera.bottom;
  128. camera.left = - frustumHeight * aspect / 2;
  129. camera.right = frustumHeight * aspect / 2;
  130. camera.updateProjectionMatrix();
  131. renderer.render( scene, camera );
  132. };
  133. const constructInnerHTML = ( isGlobal, colorsArr ) => {
  134. return `
  135. Compute ${isGlobal ? 'Global' : 'Local'}:
  136. <div style="display: flex; flex-direction:row; justify-content: center; align-items: center;">
  137. ${isGlobal ? 'Global Swaps' : 'Local Swaps'} Compare Region&nbsp;
  138. <div style="background-color: ${ colorsArr[ 0 ]}; width:12.5px; height: 1em; border-radius: 20%;"></div>
  139. &nbsp;to Region&nbsp;
  140. <div style="background-color: ${ colorsArr[ 1 ] }; width:12.5px; height: 1em; border-radius: 20%;"></div>
  141. </div>`;
  142. };
  143. const createDisplayMesh = ( elementsStorage, algoStorage = null, blockHeightStorage = null ) => {
  144. const material = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  145. const display = Fn( () => {
  146. const { gridWidth, gridHeight, highlight } = effectController;
  147. const elementIndex = getElementIndex( uv(), gridWidth, gridHeight );
  148. const color = getColor( elementsStorage.element( elementIndex ), gridWidth, gridHeight ).toVar();
  149. if ( algoStorage !== null && blockHeightStorage !== null ) {
  150. If( highlight.equal( 1 ).and( not( algoStorage.element( 0 ).equal( StepType.NONE ) ) ), () => {
  151. const boolCheck = int( elementIndex.mod( blockHeightStorage.element( 0 ) ).lessThan( blockHeightStorage.element( 0 ).div( 2 ) ) );
  152. color.z.assign( algoStorage.element( 0 ).lessThanEqual( StepType.DISPERSE_LOCAL ) );
  153. color.x.mulAssign( boolCheck );
  154. color.y.mulAssign( abs( boolCheck.sub( 1 ) ) );
  155. } );
  156. }
  157. return color;
  158. } );
  159. material.colorNode = display();
  160. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  161. return plane;
  162. };
  163. const createDisplayMesh2 = ( elementsStorage, infoStorage ) => {
  164. const material = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  165. const display = Fn( () => {
  166. const { gridWidth, gridHeight, highlight } = effectController;
  167. const elementIndex = getElementIndex( uv(), gridWidth, gridHeight );
  168. const color = getColor( elementsStorage.element( elementIndex ), gridWidth, gridHeight ).toVar();
  169. If( highlight.equal( 1 ).and( not( infoStorage.element( 0 ).equal( StepType.SWAP_LOCAL ) ) ), () => {
  170. const boolCheck = int( elementIndex.mod( infoStorage.element( 1 ) ).lessThan( infoStorage.element( 1 ).div( 2 ) ) );
  171. color.z.assign( infoStorage.element( 0 ).lessThanEqual( StepType.DISPERSE_LOCAL ) );
  172. color.x.mulAssign( boolCheck );
  173. color.y.mulAssign( abs( boolCheck.sub( 1 ) ) );
  174. } );
  175. return color;
  176. } );
  177. material.colorNode = display();
  178. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  179. return plane;
  180. };
  181. const setupDomElement = ( renderer ) => {
  182. document.body.appendChild( renderer.domElement );
  183. renderer.domElement.style.position = 'absolute';
  184. renderer.domElement.style.top = '0';
  185. renderer.domElement.style.left = '0';
  186. renderer.domElement.style.width = '50%';
  187. renderer.domElement.style.height = '100%';
  188. };
  189. async function initBitonicSort() {
  190. let currentStep = 0;
  191. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  192. const camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  193. camera.position.z = 1;
  194. const scene = new THREE.Scene();
  195. const array = new Uint32Array( Array.from( { length: size }, ( _, i ) => {
  196. return i;
  197. } ) );
  198. randomizeDataArray( array );
  199. const currentElementsBuffer = new THREE.StorageInstancedBufferAttribute( array, 1 );
  200. const currentElementsStorage = storage( currentElementsBuffer, 'uint', size ).setPBO( true ).setName( 'Elements' );
  201. const randomizedElementsBuffer = new THREE.StorageInstancedBufferAttribute( size, 1 );
  202. const randomizedElementsStorage = storage( randomizedElementsBuffer, 'uint', size ).setPBO( true ).setName( 'RandomizedElements' );
  203. const computeInitFn = Fn( () => {
  204. randomizedElementsStorage.element( instanceIndex ).assign( currentElementsStorage.element( instanceIndex ) );
  205. } );
  206. const computeResetBuffersFn = Fn( () => {
  207. currentElementsStorage.element( instanceIndex ).assign( randomizedElementsStorage.element( instanceIndex ) );
  208. } );
  209. const renderer = new THREE.WebGPURenderer( { antialias: false } );
  210. renderer.setPixelRatio( window.devicePixelRatio );
  211. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  212. const animate = () => {
  213. renderer.render( scene, camera );
  214. };
  215. renderer.setAnimationLoop( animate );
  216. setupDomElement( renderer );
  217. scene.background = new THREE.Color( 0x313131 );
  218. const bitonicSortModule = new BitonicSort(
  219. renderer,
  220. currentElementsStorage,
  221. {
  222. workgroupSize: 64,
  223. }
  224. );
  225. scene.add( createDisplayMesh2( currentElementsStorage, bitonicSortModule.infoStorage ) );
  226. // Initialize each value in the elements buffer.
  227. const computeInit = computeInitFn().compute( size );
  228. const computeReset = computeResetBuffersFn().compute( size );
  229. await renderer.computeAsync( computeInit );
  230. const stepAnimation = async function () {
  231. renderer.info.reset();
  232. if ( currentStep < bitonicSortModule.stepCount ) {
  233. bitonicSortModule.computeStep( renderer );
  234. currentStep ++;
  235. } else {
  236. renderer.compute( computeReset );
  237. currentStep = 0;
  238. }
  239. timestamps[ 'local_swap' ].innerHTML = constructInnerHTML( false, localColors );
  240. if ( currentStep === bitonicSortModule.stepCount ) {
  241. setTimeout( stepAnimation, 1000 );
  242. } else {
  243. setTimeout( stepAnimation, 100 );
  244. }
  245. };
  246. stepAnimation();
  247. window.addEventListener( 'resize', onWindowResize );
  248. function onWindowResize() {
  249. windowResizeCallback( renderer, scene, camera );
  250. }
  251. }
  252. initBitonicSort();
  253. // Global Swaps Only
  254. initGlobalSwapOnly();
  255. // When forceGlobalSwap is true, force all valid local swaps to be global swaps.
  256. async function initGlobalSwapOnly() {
  257. let currentStep = 0;
  258. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  259. const camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  260. camera.position.z = 1;
  261. const scene = new THREE.Scene();
  262. const infoArray = new Uint32Array( [ 3, 2, 2 ] );
  263. const infoBuffer = new THREE.StorageInstancedBufferAttribute( infoArray, 1 );
  264. const infoStorage = storage( infoBuffer, 'uint', infoBuffer.count ).setPBO( true ).setName( 'TheInfo' );
  265. const nextBlockHeightBuffer = new THREE.StorageInstancedBufferAttribute( new Uint32Array( 1 ).fill( 2 ), 1 );
  266. const nextBlockHeightStorage = storage( nextBlockHeightBuffer, 'uint', nextBlockHeightBuffer.count ).setPBO( true ).setName( 'NextBlockHeight' );
  267. const nextBlockHeightRead = storage( nextBlockHeightBuffer, 'uint', nextBlockHeightBuffer.count ).setPBO( true ).setName( 'NextBlockHeight' ).toReadOnly();
  268. const array = new Uint32Array( Array.from( { length: size }, ( _, i ) => {
  269. return i;
  270. } ) );
  271. randomizeDataArray( array );
  272. const currentElementsBuffer = new THREE.StorageInstancedBufferAttribute( array, 1 );
  273. const currentElementsStorage = storage( currentElementsBuffer, 'uint', size ).setPBO( true ).setName( 'Elements' );
  274. const tempBuffer = new THREE.StorageInstancedBufferAttribute( array, 1 );
  275. const tempStorage = storage( tempBuffer, 'uint', size ).setPBO( true ).setName( 'Temp' );
  276. const randomizedElementsBuffer = new THREE.StorageInstancedBufferAttribute( size, 1 );
  277. const randomizedElementsStorage = storage( randomizedElementsBuffer, 'uint', size ).setPBO( true ).setName( 'RandomizedElements' );
  278. // Swap the elements in local storage
  279. const globalCompareAndSwap = ( idxBefore, idxAfter ) => {
  280. // If the later element is less than the current element
  281. If( currentElementsStorage.element( idxAfter ).lessThan( currentElementsStorage.element( idxBefore ) ), () => {
  282. // Apply the swapped values to temporary storage.
  283. tempStorage.element( idxBefore ).assign( currentElementsStorage.element( idxAfter ) );
  284. tempStorage.element( idxAfter ).assign( currentElementsStorage.element( idxBefore ) );
  285. } ).Else( () => {
  286. // Otherwise apply the existing values to temporary storage.
  287. tempStorage.element( idxBefore ).assign( currentElementsStorage.element( idxBefore ) );
  288. tempStorage.element( idxAfter ).assign( currentElementsStorage.element( idxAfter ) );
  289. } );
  290. };
  291. const computeInitFn = Fn( () => {
  292. randomizedElementsStorage.element( instanceIndex ).assign( currentElementsStorage.element( instanceIndex ) );
  293. } );
  294. const computeBitonicStepFn = Fn( () => {
  295. const nextBlockHeight = nextBlockHeightStorage.element( 0 ).toVar();
  296. const nextAlgo = infoStorage.element( 0 ).toVar();
  297. // TODO: Convert to switch block.
  298. If( nextAlgo.equal( uint( StepType.FLIP_GLOBAL ) ), () => {
  299. const idx = getBitonicFlipIndices( instanceIndex, nextBlockHeight );
  300. globalCompareAndSwap( idx.x, idx.y );
  301. } ).ElseIf( nextAlgo.equal( uint( StepType.DISPERSE_GLOBAL ) ), () => {
  302. const idx = getBitonicDisperseIndices( instanceIndex, nextBlockHeight );
  303. globalCompareAndSwap( idx.x, idx.y );
  304. } );
  305. // Since this algorithm is global only, we execute an additional compute step to sync the current buffer with the output buffer.
  306. } );
  307. const computeSetAlgoFn = Fn( () => {
  308. const nextBlockHeight = nextBlockHeightStorage.element( 0 ).toVar();
  309. const nextAlgo = infoStorage.element( 0 );
  310. const highestBlockHeight = infoStorage.element( 2 ).toVar();
  311. nextBlockHeight.divAssign( 2 );
  312. If( nextBlockHeight.equal( 1 ), () => {
  313. highestBlockHeight.mulAssign( 2 );
  314. If( highestBlockHeight.equal( size * 2 ), () => {
  315. nextAlgo.assign( StepType.NONE );
  316. nextBlockHeight.assign( 0 );
  317. } ).Else( () => {
  318. nextAlgo.assign( StepType.FLIP_GLOBAL );
  319. nextBlockHeight.assign( highestBlockHeight );
  320. } );
  321. } ).Else( () => {
  322. nextAlgo.assign( StepType.DISPERSE_GLOBAL );
  323. } );
  324. nextBlockHeightStorage.element( 0 ).assign( nextBlockHeight );
  325. infoStorage.element( 2 ).assign( highestBlockHeight );
  326. } );
  327. const computeAlignCurrentFn = Fn( () => {
  328. currentElementsStorage.element( instanceIndex ).assign( tempStorage.element( instanceIndex ) );
  329. } );
  330. const computeResetBuffersFn = Fn( () => {
  331. currentElementsStorage.element( instanceIndex ).assign( randomizedElementsStorage.element( instanceIndex ) );
  332. } );
  333. const computeResetAlgoFn = Fn( () => {
  334. infoStorage.element( 0 ).assign( StepType.FLIP_GLOBAL );
  335. nextBlockHeightStorage.element( 0 ).assign( 2 );
  336. infoStorage.element( 2 ).assign( 2 );
  337. } );
  338. // Initialize each value in the elements buffer.
  339. const computeInit = computeInitFn().compute( size );
  340. // Swap a pair of elements in the elements buffer.
  341. const computeBitonicStep = computeBitonicStepFn().compute( size / 2 );
  342. // Set the conditions for the next swap.
  343. const computeSetAlgo = computeSetAlgoFn().compute( 1 );
  344. // Align the current buffer with the temp buffer if the previous sort was executed in a global scope.
  345. const computeAlignCurrent = computeAlignCurrentFn().compute( size );
  346. // Reset the buffers and algorithm information after a full bitonic sort has been completed.
  347. const computeResetBuffers = computeResetBuffersFn().compute( size );
  348. const computeResetAlgo = computeResetAlgoFn().compute( 1 );
  349. scene.add( createDisplayMesh( currentElementsStorage, infoStorage, nextBlockHeightRead ) );
  350. const renderer = new THREE.WebGPURenderer( { antialias: false } );
  351. renderer.setPixelRatio( window.devicePixelRatio );
  352. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  353. const animate = () => {
  354. renderer.render( scene, camera );
  355. };
  356. renderer.setAnimationLoop( animate );
  357. setupDomElement( renderer );
  358. renderer.domElement.style.left = '50%';
  359. scene.background = new THREE.Color( 0x212121 );
  360. await renderer.computeAsync( computeInit );
  361. const stepAnimation = async function () {
  362. if ( currentStep !== MAX_STEPS ) {
  363. renderer.compute( computeBitonicStep );
  364. renderer.compute( computeAlignCurrent );
  365. renderer.compute( computeSetAlgo );
  366. currentStep ++;
  367. } else {
  368. renderer.compute( computeResetBuffers );
  369. renderer.compute( computeResetAlgo );
  370. currentStep = 0;
  371. }
  372. timestamps[ 'global_swap' ].innerHTML = constructInnerHTML( true, globalColors );
  373. if ( currentStep === MAX_STEPS ) {
  374. setTimeout( stepAnimation, 1000 );
  375. } else {
  376. setTimeout( stepAnimation, 100 );
  377. }
  378. };
  379. stepAnimation();
  380. window.addEventListener( 'resize', onWindowResize );
  381. function onWindowResize() {
  382. windowResizeCallback( renderer, scene, camera );
  383. }
  384. }
  385. </script>
  386. </body>
  387. </html>
粤ICP备19079148号