webgpu_storage_buffer.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/webgpu": "../build/three.webgpu.js",
  45. "three/tsl": "../build/three.tsl.js",
  46. "three/addons/": "./jsm/"
  47. }
  48. }
  49. </script>
  50. <script type="module">
  51. import * as THREE from 'three/webgpu';
  52. import { storage, If, vec3, uv, uint, float, Fn, instanceIndex, workgroupBarrier } from 'three/tsl';
  53. const timestamps = {
  54. webgpu: document.getElementById( 'timestamps' ),
  55. webgl: document.getElementById( 'timestamps_webgl' )
  56. };
  57. // WebGPU Backend
  58. init();
  59. // WebGL Backend
  60. init( true );
  61. async function init( forceWebGL = false ) {
  62. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  63. const camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  64. camera.position.z = 1;
  65. const scene = new THREE.Scene();
  66. // texture
  67. const size = 32; // non power of two buffer size is not well supported in WebGPU
  68. const barCount = 32;
  69. const type = [ 'float', 'vec2', 'vec3', 'vec4' ];
  70. const arrayBufferNodes = [];
  71. for ( let i = 0; i < type.length; i ++ ) {
  72. const typeSize = i + 1;
  73. const array = new Array( size * typeSize ).fill( 0 );
  74. const arrayBuffer = new THREE.StorageInstancedBufferAttribute( new Float32Array( array ), typeSize );
  75. arrayBufferNodes.push( storage( arrayBuffer, type[ i ], size ).setPBO( true ) );
  76. }
  77. const computeInitOrder = Fn( () => {
  78. for ( let i = 0; i < type.length; i ++ ) {
  79. arrayBufferNodes[ i ].element( instanceIndex ).assign( instanceIndex );
  80. }
  81. } );
  82. const computeInvertOrder = Fn( () => {
  83. for ( let i = 0; i < type.length; i ++ ) {
  84. const invertIndex = arrayBufferNodes[ i ].element( uint( size - 1 ).sub( instanceIndex ) ).toVar();
  85. workgroupBarrier();
  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 THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  93. material.colorNode = Fn( () => {
  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 THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL, trackTimestamp: true } );
  125. renderer.setPixelRatio( window.devicePixelRatio );
  126. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  127. await renderer.init();
  128. document.body.appendChild( renderer.domElement );
  129. renderer.domElement.style.position = 'absolute';
  130. renderer.domElement.style.top = '0';
  131. renderer.domElement.style.left = '0';
  132. renderer.domElement.style.width = '50%';
  133. renderer.domElement.style.height = '100%';
  134. if ( forceWebGL ) {
  135. renderer.domElement.style.left = '50%';
  136. scene.background = new THREE.Color( 0x212121 );
  137. } else {
  138. scene.background = new THREE.Color( 0x313131 );
  139. }
  140. await renderer.computeAsync( computeInit );
  141. //
  142. renderer.info.autoReset = false;
  143. const stepAnimation = async function () {
  144. renderer.info.reset();
  145. await renderer.computeAsync( compute );
  146. renderer.render( scene, camera );
  147. renderer.resolveTimestampsAsync( THREE.TimestampQuery.COMPUTE );
  148. renderer.resolveTimestampsAsync( THREE.TimestampQuery.RENDER );
  149. timestamps[ forceWebGL ? 'webgl' : 'webgpu' ].innerHTML = `
  150. Compute ${renderer.info.compute.frameCalls} pass in ${renderer.info.compute.timestamp.toFixed( 6 )}ms<br>
  151. Draw ${renderer.info.render.drawCalls} pass in ${renderer.info.render.timestamp.toFixed( 6 )}ms`;
  152. setTimeout( stepAnimation, 1000 );
  153. };
  154. stepAnimation();
  155. window.addEventListener( 'resize', onWindowResize );
  156. function onWindowResize() {
  157. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  158. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  159. const frustumHeight = camera.top - camera.bottom;
  160. camera.left = - frustumHeight * aspect / 2;
  161. camera.right = frustumHeight * aspect / 2;
  162. camera.updateProjectionMatrix();
  163. renderer.render( scene, camera );
  164. }
  165. }
  166. </script>
  167. </body>
  168. </html>
粤ICP备19079148号