webgpu_storage_buffer.html 7.0 KB

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