webgpu_storage_buffer.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
  11. <br />This example demonstrates fetching an external element from a StorageBuffer.
  12. <br />The left canvas uses the WebGPU Backend, while the right uses the WebGL Backend.
  13. <div id="timestamps" style="
  14. position: absolute;
  15. top: 60px;
  16. left: 0;
  17. padding: 10px;
  18. background: rgba( 0, 0, 0, 0.5 );
  19. color: #fff;
  20. font-family: monospace;
  21. font-size: 12px;
  22. line-height: 1.5;
  23. pointer-events: none;
  24. text-align: left;
  25. "></div>
  26. <div id="timestamps_webgl" style="
  27. position: absolute;
  28. top: 60px;
  29. right: 0;
  30. padding: 10px;
  31. background: rgba( 0, 0, 0, 0.5 );
  32. color: #fff;
  33. font-family: monospace;
  34. font-size: 12px;
  35. line-height: 1.5;
  36. pointer-events: none;
  37. text-align: left;
  38. "></div>
  39. </div>
  40. <script type="importmap">
  41. {
  42. "imports": {
  43. "three": "../build/three.webgpu.js",
  44. "three/tsl": "../build/three.webgpu.js",
  45. "three/addons/": "./jsm/"
  46. }
  47. }
  48. </script>
  49. <script type="module">
  50. import * as THREE from 'three';
  51. import { storageObject, If, vec3, uv, uint, float, Fn, instanceIndex, workgroupBarrier } from 'three/tsl';
  52. const timestamps = {
  53. webgpu: document.getElementById( 'timestamps' ),
  54. webgl: document.getElementById( 'timestamps_webgl' )
  55. };
  56. // WebGPU Backend
  57. init();
  58. // WebGL Backend
  59. init( true );
  60. async function init( forceWebGL = false ) {
  61. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  62. const camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  63. camera.position.z = 1;
  64. const scene = new THREE.Scene();
  65. // texture
  66. const size = 32; // non power of two buffer size is not well supported in WebGPU
  67. const barCount = 32;
  68. const type = [ 'float', 'vec2', 'vec3', 'vec4' ];
  69. const arrayBufferNodes = [];
  70. for ( let i = 0; i < type.length; i ++ ) {
  71. const typeSize = i + 1;
  72. const array = new Array( size * typeSize ).fill( 0 );
  73. const arrayBuffer = new THREE.StorageInstancedBufferAttribute( new Float32Array( array ), typeSize );
  74. arrayBufferNodes.push( storageObject( arrayBuffer, type[ i ], size ) );
  75. }
  76. const computeInitOrder = Fn( () => {
  77. for ( let i = 0; i < type.length; i ++ ) {
  78. arrayBufferNodes[ i ].element( instanceIndex ).assign( instanceIndex );
  79. }
  80. } );
  81. const computeInvertOrder = Fn( () => {
  82. for ( let i = 0; i < type.length; i ++ ) {
  83. const invertIndex = arrayBufferNodes[ i ].element( uint( size - 1 ).sub( instanceIndex ) ).toVar();
  84. workgroupBarrier();
  85. arrayBufferNodes[ i ].element( instanceIndex ).assign( invertIndex );
  86. }
  87. } );
  88. // compute
  89. const computeInit = computeInitOrder().compute( size );
  90. const compute = computeInvertOrder().compute( size );
  91. const material = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  92. material.colorNode = Fn( () => {
  93. const index = uint( uv().x.mul( size ).floor() ).toVar();
  94. If( index.greaterThanEqual( size ), () => {
  95. index.assign( uint( size ).sub( 1 ) );
  96. } );
  97. const color = vec3( 0, 0, 0 ).toVar();
  98. If( uv().y.greaterThan( 0.0 ), () => {
  99. const indexValue = arrayBufferNodes[ 0 ].element( index ).toVar();
  100. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  101. color.assign( vec3( value, 0, 0 ) );
  102. } );
  103. If( uv().y.greaterThan( 0.25 ), () => {
  104. const indexValue = arrayBufferNodes[ 1 ].element( index ).toVar();
  105. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  106. color.assign( vec3( 0, value, 0 ) );
  107. } );
  108. If( uv().y.greaterThan( 0.5 ), () => {
  109. const indexValue = arrayBufferNodes[ 2 ].element( index ).toVar();
  110. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  111. color.assign( vec3( 0, 0, value ) );
  112. } );
  113. If( uv().y.greaterThan( 0.75 ), () => {
  114. const indexValue = arrayBufferNodes[ 3 ].element( index ).toVar();
  115. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  116. color.assign( vec3( value, value, value ) );
  117. } );
  118. return color;
  119. } )();
  120. //
  121. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  122. scene.add( plane );
  123. const renderer = new THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL, trackTimestamp: true } );
  124. renderer.setPixelRatio( window.devicePixelRatio );
  125. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  126. document.body.appendChild( renderer.domElement );
  127. renderer.domElement.style.position = 'absolute';
  128. renderer.domElement.style.top = '0';
  129. renderer.domElement.style.left = '0';
  130. renderer.domElement.style.width = '50%';
  131. renderer.domElement.style.height = '100%';
  132. if ( forceWebGL ) {
  133. renderer.domElement.style.left = '50%';
  134. scene.background = new THREE.Color( 0x212121 );
  135. } else {
  136. scene.background = new THREE.Color( 0x313131 );
  137. }
  138. await renderer.computeAsync( computeInit );
  139. //
  140. renderer.info.autoReset = false;
  141. const stepAnimation = async function () {
  142. renderer.info.reset();
  143. await renderer.computeAsync( compute );
  144. await renderer.renderAsync( scene, camera );
  145. timestamps[ forceWebGL ? 'webgl' : 'webgpu' ].innerHTML = `
  146. Compute ${renderer.info.compute.frameCalls} pass in ${renderer.info.compute.timestamp.toFixed( 6 )}ms<br>
  147. Draw ${renderer.info.render.drawCalls} pass in ${renderer.info.render.timestamp.toFixed( 6 )}ms`;
  148. setTimeout( stepAnimation, 1000 );
  149. };
  150. stepAnimation();
  151. window.addEventListener( 'resize', onWindowResize );
  152. function onWindowResize() {
  153. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  154. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  155. const frustumHeight = camera.top - camera.bottom;
  156. camera.left = - frustumHeight * aspect / 2;
  157. camera.right = frustumHeight * aspect / 2;
  158. camera.updateProjectionMatrix();
  159. renderer.render( scene, camera );
  160. }
  161. }
  162. </script>
  163. </body>
  164. </html>