webgpu_compute_sort_bitonic.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. await renderer.init();
  213. const animate = () => {
  214. renderer.render( scene, camera );
  215. };
  216. renderer.setAnimationLoop( animate );
  217. setupDomElement( renderer );
  218. scene.background = new THREE.Color( 0x313131 );
  219. const bitonicSortModule = new BitonicSort(
  220. renderer,
  221. currentElementsStorage,
  222. {
  223. workgroupSize: 64,
  224. }
  225. );
  226. scene.add( createDisplayMesh2( currentElementsStorage, bitonicSortModule.infoStorage ) );
  227. // Initialize each value in the elements buffer.
  228. const computeInit = computeInitFn().compute( size );
  229. const computeReset = computeResetBuffersFn().compute( size );
  230. renderer.compute( computeInit );
  231. const stepAnimation = async function () {
  232. renderer.info.reset();
  233. if ( currentStep < bitonicSortModule.stepCount ) {
  234. bitonicSortModule.computeStep( renderer );
  235. currentStep ++;
  236. } else {
  237. renderer.compute( computeReset );
  238. currentStep = 0;
  239. }
  240. timestamps[ 'local_swap' ].innerHTML = constructInnerHTML( false, localColors );
  241. if ( currentStep === bitonicSortModule.stepCount ) {
  242. setTimeout( stepAnimation, 1000 );
  243. } else {
  244. setTimeout( stepAnimation, 100 );
  245. }
  246. };
  247. stepAnimation();
  248. window.addEventListener( 'resize', onWindowResize );
  249. function onWindowResize() {
  250. windowResizeCallback( renderer, scene, camera );
  251. }
  252. }
  253. initBitonicSort();
  254. // Global Swaps Only
  255. initGlobalSwapOnly();
  256. // When forceGlobalSwap is true, force all valid local swaps to be global swaps.
  257. async function initGlobalSwapOnly() {
  258. let currentStep = 0;
  259. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  260. const camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  261. camera.position.z = 1;
  262. const scene = new THREE.Scene();
  263. const infoArray = new Uint32Array( [ 3, 2, 2 ] );
  264. const infoBuffer = new THREE.StorageInstancedBufferAttribute( infoArray, 1 );
  265. const infoStorage = storage( infoBuffer, 'uint', infoBuffer.count ).setPBO( true ).setName( 'TheInfo' );
  266. const nextBlockHeightBuffer = new THREE.StorageInstancedBufferAttribute( new Uint32Array( 1 ).fill( 2 ), 1 );
  267. const nextBlockHeightStorage = storage( nextBlockHeightBuffer, 'uint', nextBlockHeightBuffer.count ).setPBO( true ).setName( 'NextBlockHeight' );
  268. const nextBlockHeightRead = storage( nextBlockHeightBuffer, 'uint', nextBlockHeightBuffer.count ).setPBO( true ).setName( 'NextBlockHeight' ).toReadOnly();
  269. const array = new Uint32Array( Array.from( { length: size }, ( _, i ) => {
  270. return i;
  271. } ) );
  272. randomizeDataArray( array );
  273. const currentElementsBuffer = new THREE.StorageInstancedBufferAttribute( array, 1 );
  274. const currentElementsStorage = storage( currentElementsBuffer, 'uint', size ).setPBO( true ).setName( 'Elements' );
  275. const tempBuffer = new THREE.StorageInstancedBufferAttribute( array, 1 );
  276. const tempStorage = storage( tempBuffer, 'uint', size ).setPBO( true ).setName( 'Temp' );
  277. const randomizedElementsBuffer = new THREE.StorageInstancedBufferAttribute( size, 1 );
  278. const randomizedElementsStorage = storage( randomizedElementsBuffer, 'uint', size ).setPBO( true ).setName( 'RandomizedElements' );
  279. // Swap the elements in local storage
  280. const globalCompareAndSwap = ( idxBefore, idxAfter ) => {
  281. // If the later element is less than the current element
  282. If( currentElementsStorage.element( idxAfter ).lessThan( currentElementsStorage.element( idxBefore ) ), () => {
  283. // Apply the swapped values to temporary storage.
  284. tempStorage.element( idxBefore ).assign( currentElementsStorage.element( idxAfter ) );
  285. tempStorage.element( idxAfter ).assign( currentElementsStorage.element( idxBefore ) );
  286. } ).Else( () => {
  287. // Otherwise apply the existing values to temporary storage.
  288. tempStorage.element( idxBefore ).assign( currentElementsStorage.element( idxBefore ) );
  289. tempStorage.element( idxAfter ).assign( currentElementsStorage.element( idxAfter ) );
  290. } );
  291. };
  292. const computeInitFn = Fn( () => {
  293. randomizedElementsStorage.element( instanceIndex ).assign( currentElementsStorage.element( instanceIndex ) );
  294. } );
  295. const computeBitonicStepFn = Fn( () => {
  296. const nextBlockHeight = nextBlockHeightStorage.element( 0 ).toVar();
  297. const nextAlgo = infoStorage.element( 0 ).toVar();
  298. // TODO: Convert to switch block.
  299. If( nextAlgo.equal( uint( StepType.FLIP_GLOBAL ) ), () => {
  300. const idx = getBitonicFlipIndices( instanceIndex, nextBlockHeight );
  301. globalCompareAndSwap( idx.x, idx.y );
  302. } ).ElseIf( nextAlgo.equal( uint( StepType.DISPERSE_GLOBAL ) ), () => {
  303. const idx = getBitonicDisperseIndices( instanceIndex, nextBlockHeight );
  304. globalCompareAndSwap( idx.x, idx.y );
  305. } );
  306. // Since this algorithm is global only, we execute an additional compute step to sync the current buffer with the output buffer.
  307. } );
  308. const computeSetAlgoFn = Fn( () => {
  309. const nextBlockHeight = nextBlockHeightStorage.element( 0 ).toVar();
  310. const nextAlgo = infoStorage.element( 0 );
  311. const highestBlockHeight = infoStorage.element( 2 ).toVar();
  312. nextBlockHeight.divAssign( 2 );
  313. If( nextBlockHeight.equal( 1 ), () => {
  314. highestBlockHeight.mulAssign( 2 );
  315. If( highestBlockHeight.equal( size * 2 ), () => {
  316. nextAlgo.assign( StepType.NONE );
  317. nextBlockHeight.assign( 0 );
  318. } ).Else( () => {
  319. nextAlgo.assign( StepType.FLIP_GLOBAL );
  320. nextBlockHeight.assign( highestBlockHeight );
  321. } );
  322. } ).Else( () => {
  323. nextAlgo.assign( StepType.DISPERSE_GLOBAL );
  324. } );
  325. nextBlockHeightStorage.element( 0 ).assign( nextBlockHeight );
  326. infoStorage.element( 2 ).assign( highestBlockHeight );
  327. } );
  328. const computeAlignCurrentFn = Fn( () => {
  329. currentElementsStorage.element( instanceIndex ).assign( tempStorage.element( instanceIndex ) );
  330. } );
  331. const computeResetBuffersFn = Fn( () => {
  332. currentElementsStorage.element( instanceIndex ).assign( randomizedElementsStorage.element( instanceIndex ) );
  333. } );
  334. const computeResetAlgoFn = Fn( () => {
  335. infoStorage.element( 0 ).assign( StepType.FLIP_GLOBAL );
  336. nextBlockHeightStorage.element( 0 ).assign( 2 );
  337. infoStorage.element( 2 ).assign( 2 );
  338. } );
  339. // Initialize each value in the elements buffer.
  340. const computeInit = computeInitFn().compute( size );
  341. // Swap a pair of elements in the elements buffer.
  342. const computeBitonicStep = computeBitonicStepFn().compute( size / 2 );
  343. // Set the conditions for the next swap.
  344. const computeSetAlgo = computeSetAlgoFn().compute( 1 );
  345. // Align the current buffer with the temp buffer if the previous sort was executed in a global scope.
  346. const computeAlignCurrent = computeAlignCurrentFn().compute( size );
  347. // Reset the buffers and algorithm information after a full bitonic sort has been completed.
  348. const computeResetBuffers = computeResetBuffersFn().compute( size );
  349. const computeResetAlgo = computeResetAlgoFn().compute( 1 );
  350. scene.add( createDisplayMesh( currentElementsStorage, infoStorage, nextBlockHeightRead ) );
  351. const renderer = new THREE.WebGPURenderer( { antialias: false } );
  352. renderer.setPixelRatio( window.devicePixelRatio );
  353. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  354. await renderer.init();
  355. const animate = () => {
  356. renderer.render( scene, camera );
  357. };
  358. renderer.setAnimationLoop( animate );
  359. setupDomElement( renderer );
  360. renderer.domElement.style.left = '50%';
  361. scene.background = new THREE.Color( 0x212121 );
  362. renderer.compute( computeInit );
  363. const stepAnimation = async function () {
  364. if ( currentStep !== MAX_STEPS ) {
  365. renderer.compute( computeBitonicStep );
  366. renderer.compute( computeAlignCurrent );
  367. renderer.compute( computeSetAlgo );
  368. currentStep ++;
  369. } else {
  370. renderer.compute( computeResetBuffers );
  371. renderer.compute( computeResetAlgo );
  372. currentStep = 0;
  373. }
  374. timestamps[ 'global_swap' ].innerHTML = constructInnerHTML( true, globalColors );
  375. if ( currentStep === MAX_STEPS ) {
  376. setTimeout( stepAnimation, 1000 );
  377. } else {
  378. setTimeout( stepAnimation, 100 );
  379. }
  380. };
  381. stepAnimation();
  382. window.addEventListener( 'resize', onWindowResize );
  383. function onWindowResize() {
  384. windowResizeCallback( renderer, scene, camera );
  385. }
  386. }
  387. </script>
  388. </body>
  389. </html>
粤ICP备19079148号