webgpu_storage_buffer.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.module.js",
  44. "three/addons/": "./jsm/",
  45. "three/nodes": "./jsm/nodes/Nodes.js"
  46. }
  47. }
  48. </script>
  49. <script type="module">
  50. import * as THREE from 'three';
  51. import { storageObject, If, vec3, uv, uint, float, tslFn, instanceIndex, MeshBasicNodeMaterial } from 'three/nodes';
  52. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  53. import StorageInstancedBufferAttribute from 'three/addons/renderers/common/StorageInstancedBufferAttribute.js';
  54. const timestamps = {
  55. webgpu: document.getElementById( 'timestamps' ),
  56. webgl: document.getElementById( 'timestamps_webgl' )
  57. };
  58. // WebGPU Backend
  59. init();
  60. // WebGL Backend
  61. init( true );
  62. async function init( forceWebGL = false ) {
  63. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  64. const camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  65. camera.position.z = 1;
  66. const scene = new THREE.Scene();
  67. // texture
  68. const size = 32; // non power of two buffer size is not well supported in WebGPU
  69. const barCount = 32;
  70. const type = [ 'float', 'vec2', 'vec3', 'vec4' ];
  71. const arrayBufferNodes = [];
  72. for ( let i = 0; i < type.length; i ++ ) {
  73. const typeSize = i + 1;
  74. const array = new Array( size * typeSize ).fill( 0 );
  75. const arrayBuffer = new StorageInstancedBufferAttribute( new Float32Array( array ), typeSize );
  76. arrayBufferNodes.push( storageObject( arrayBuffer, type[ i ], size ) );
  77. }
  78. const computeInitOrder = tslFn( () => {
  79. for ( let i = 0; i < type.length; i ++ ) {
  80. arrayBufferNodes[ i ].element( instanceIndex ).assign( instanceIndex );
  81. }
  82. } );
  83. const computeInvertOrder = tslFn( () => {
  84. for ( let i = 0; i < type.length; i ++ ) {
  85. const invertIndex = arrayBufferNodes[ i ].element( uint( size - 1 ).sub( instanceIndex ) );
  86. arrayBufferNodes[ i ].element( instanceIndex ).assign( invertIndex );
  87. }
  88. } );
  89. // compute
  90. const computeInit = computeInitOrder().compute( size );
  91. const compute = computeInvertOrder().compute( size );
  92. const material = new MeshBasicNodeMaterial( { color: 0x00ff00 } );
  93. material.colorNode = tslFn( () => {
  94. const index = uint( uv().x.mul( size ).floor() ).toVar();
  95. If( index.greaterThanEqual( size ), () => {
  96. index.assign( uint( size ).sub( 1 ) );
  97. } );
  98. const color = vec3( 0, 0, 0 ).toVar();
  99. If( uv().y.greaterThan( 0.0 ), () => {
  100. const indexValue = arrayBufferNodes[ 0 ].element( index ).toVar();
  101. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  102. color.assign( vec3( value, 0, 0 ) );
  103. } );
  104. If( uv().y.greaterThan( 0.25 ), () => {
  105. const indexValue = arrayBufferNodes[ 1 ].element( index ).toVar();
  106. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  107. color.assign( vec3( 0, value, 0 ) );
  108. } );
  109. If( uv().y.greaterThan( 0.5 ), () => {
  110. const indexValue = arrayBufferNodes[ 2 ].element( index ).toVar();
  111. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  112. color.assign( vec3( 0, 0, value ) );
  113. } );
  114. If( uv().y.greaterThan( 0.75 ), () => {
  115. const indexValue = arrayBufferNodes[ 3 ].element( index ).toVar();
  116. const value = float( indexValue ).div( float( size ) ).mul( barCount ).floor().div( barCount );
  117. color.assign( vec3( value, value, value ) );
  118. } );
  119. return color;
  120. } )();
  121. //
  122. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  123. scene.add( plane );
  124. const renderer = new WebGPURenderer( { antialias: false, forceWebGL: forceWebGL, trackTimestamp: true } );
  125. renderer.setPixelRatio( window.devicePixelRatio );
  126. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  127. document.body.appendChild( renderer.domElement );
  128. renderer.domElement.style.position = 'absolute';
  129. renderer.domElement.style.top = '0';
  130. renderer.domElement.style.left = '0';
  131. renderer.domElement.style.width = '50%';
  132. renderer.domElement.style.height = '100%';
  133. if ( forceWebGL ) {
  134. renderer.domElement.style.left = '50%';
  135. scene.background = new THREE.Color( 0x212121 );
  136. } else {
  137. scene.background = new THREE.Color( 0x313131 );
  138. }
  139. await renderer.computeAsync( computeInit );
  140. //
  141. renderer.info.autoReset = false;
  142. const stepAnimation = async function () {
  143. renderer.info.reset();
  144. await renderer.computeAsync( compute );
  145. await renderer.renderAsync( scene, camera );
  146. timestamps[ forceWebGL ? 'webgl' : 'webgpu' ].innerHTML = `
  147. Compute ${renderer.info.compute.frameCalls} pass in ${renderer.info.compute.timestamp.toFixed( 6 )}ms<br>
  148. Draw ${renderer.info.render.drawCalls} pass in ${renderer.info.render.timestamp.toFixed( 6 )}ms`;
  149. setTimeout( stepAnimation, 1000 );
  150. };
  151. stepAnimation();
  152. window.addEventListener( 'resize', onWindowResize );
  153. function onWindowResize() {
  154. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  155. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  156. const frustumHeight = camera.top - camera.bottom;
  157. camera.left = - frustumHeight * aspect / 2;
  158. camera.right = frustumHeight * aspect / 2;
  159. camera.updateProjectionMatrix();
  160. renderer.render( scene, camera );
  161. }
  162. }
  163. </script>
  164. </body>
  165. </html>