webgpu_storage_buffer.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 demonstrate the fetch of external element from a StorageBuffer.
  12. <br /> Left canvas is using WebGPU Backend, right canvas is 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 } 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 ) );
  84. arrayBufferNodes[ i ].element( instanceIndex ).assign( invertIndex );
  85. }
  86. } );
  87. // compute
  88. const computeInit = computeInitOrder().compute( size );
  89. const compute = computeInvertOrder().compute( size );
  90. const material = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  91. material.colorNode = Fn( () => {
  92. const index = uint( uv().x.mul( size ).floor() ).toVar();
  93. If( index.greaterThanEqual( size ), () => {
  94. index.assign( uint( size ).sub( 1 ) );
  95. } );
  96. const color = vec3( 0, 0, 0 ).toVar();
  97. If( uv().y.greaterThan( 0.0 ), () => {
  98. const indexValue = arrayBufferNodes[ 0 ].element( index ).toVar();
  99. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  100. color.assign( vec3( value, 0, 0 ) );
  101. } );
  102. If( uv().y.greaterThan( 0.25 ), () => {
  103. const indexValue = arrayBufferNodes[ 1 ].element( index ).toVar();
  104. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  105. color.assign( vec3( 0, value, 0 ) );
  106. } );
  107. If( uv().y.greaterThan( 0.5 ), () => {
  108. const indexValue = arrayBufferNodes[ 2 ].element( index ).toVar();
  109. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  110. color.assign( vec3( 0, 0, value ) );
  111. } );
  112. If( uv().y.greaterThan( 0.75 ), () => {
  113. const indexValue = arrayBufferNodes[ 3 ].element( index ).toVar();
  114. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  115. color.assign( vec3( value, value, value ) );
  116. } );
  117. return color;
  118. } )();
  119. //
  120. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  121. scene.add( plane );
  122. const renderer = new THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL, trackTimestamp: true } );
  123. renderer.setPixelRatio( window.devicePixelRatio );
  124. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  125. document.body.appendChild( renderer.domElement );
  126. renderer.domElement.style.position = 'absolute';
  127. renderer.domElement.style.top = '0';
  128. renderer.domElement.style.left = '0';
  129. renderer.domElement.style.width = '50%';
  130. renderer.domElement.style.height = '100%';
  131. if ( forceWebGL ) {
  132. renderer.domElement.style.left = '50%';
  133. scene.background = new THREE.Color( 0x212121 );
  134. } else {
  135. scene.background = new THREE.Color( 0x313131 );
  136. }
  137. await renderer.computeAsync( computeInit );
  138. //
  139. renderer.info.autoReset = false;
  140. const stepAnimation = async function () {
  141. renderer.info.reset();
  142. await renderer.computeAsync( compute );
  143. await renderer.renderAsync( scene, camera );
  144. timestamps[ forceWebGL ? 'webgl' : 'webgpu' ].innerHTML = `
  145. Compute ${renderer.info.compute.frameCalls} pass in ${renderer.info.compute.timestamp.toFixed( 6 )}ms<br>
  146. Draw ${renderer.info.render.drawCalls} pass in ${renderer.info.render.timestamp.toFixed( 6 )}ms`;
  147. setTimeout( stepAnimation, 1000 );
  148. };
  149. stepAnimation();
  150. window.addEventListener( 'resize', onWindowResize );
  151. function onWindowResize() {
  152. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  153. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  154. const frustumHeight = camera.top - camera.bottom;
  155. camera.left = - frustumHeight * aspect / 2;
  156. camera.right = frustumHeight * aspect / 2;
  157. camera.updateProjectionMatrix();
  158. renderer.render( scene, camera );
  159. }
  160. }
  161. </script>
  162. </body>
  163. </html>