webgpu_compute_texture_pingpong.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <html lang="en">
  2. <head>
  3. <title>three.js - WebGPU - Compute Ping/Pong Texture</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> WebGPU - Compute Ping/Pong Texture
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/tsl": "../build/three.webgpu.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { storageTexture, wgslFn, code, instanceIndex, uniform } from 'three/tsl';
  24. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  25. let camera, scene, renderer;
  26. let computeInitNode, computeToPing, computeToPong;
  27. let pingTexture, pongTexture;
  28. let material;
  29. let phase = true;
  30. let lastUpdate = - 1;
  31. const seed = uniform( new THREE.Vector2() );
  32. init();
  33. function init() {
  34. if ( WebGPU.isAvailable() === false ) {
  35. document.body.appendChild( WebGPU.getErrorMessage() );
  36. throw new Error( 'No WebGPU support' );
  37. }
  38. const aspect = window.innerWidth / window.innerHeight;
  39. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  40. camera.position.z = 1;
  41. scene = new THREE.Scene();
  42. // texture
  43. const hdr = true;
  44. const width = 512, height = 512;
  45. pingTexture = new THREE.StorageTexture( width, height );
  46. pongTexture = new THREE.StorageTexture( width, height );
  47. if ( hdr ) {
  48. pingTexture.type = THREE.HalfFloatType;
  49. pongTexture.type = THREE.HalfFloatType;
  50. }
  51. const wgslFormat = hdr ? 'rgba16float' : 'rgba8unorm';
  52. const readPing = storageTexture( pingTexture ).setAccess( 'read-only' );
  53. const writePing = storageTexture( pingTexture ).setAccess( 'write-only' );
  54. const readPong = storageTexture( pongTexture ).setAccess( 'read-only' );
  55. const writePong = storageTexture( pongTexture ).setAccess( 'write-only' );
  56. // compute init
  57. const rand2 = code( `
  58. fn rand2( n: vec2f ) -> f32 {
  59. return fract( sin( dot( n, vec2f( 12.9898, 4.1414 ) ) ) * 43758.5453 );
  60. }
  61. fn blur( image : texture_storage_2d<${wgslFormat}, read>, uv : vec2i ) -> vec4f {
  62. var color = vec4f( 0.0 );
  63. color += textureLoad( image, uv + vec2i( - 1, 1 ));
  64. color += textureLoad( image, uv + vec2i( - 1, - 1 ));
  65. color += textureLoad( image, uv + vec2i( 0, 0 ));
  66. color += textureLoad( image, uv + vec2i( 1, - 1 ));
  67. color += textureLoad( image, uv + vec2i( 1, 1 ));
  68. return color / 5.0;
  69. }
  70. fn getUV( posX: u32, posY: u32 ) -> vec2f {
  71. let uv = vec2f( f32( posX ) / ${ width }.0, f32( posY ) / ${ height }.0 );
  72. return uv;
  73. }
  74. ` );
  75. const computeInitWGSL = wgslFn( `
  76. fn computeInitWGSL( writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32, seed: vec2f ) -> void {
  77. let posX = index % ${ width };
  78. let posY = index / ${ width };
  79. let indexUV = vec2u( posX, posY );
  80. let uv = getUV( posX, posY );
  81. let r = rand2( uv + seed * 100 ) - rand2( uv + seed * 300 );
  82. let g = rand2( uv + seed * 200 ) - rand2( uv + seed * 300 );
  83. let b = rand2( uv + seed * 200 ) - rand2( uv + seed * 100 );
  84. textureStore( writeTex, indexUV, vec4( r, g, b, 1 ) );
  85. }
  86. `, [ rand2 ] );
  87. computeInitNode = computeInitWGSL( { writeTex: storageTexture( pingTexture ), index: instanceIndex, seed } ).compute( width * height );
  88. // compute loop
  89. const computePingPongWGSL = wgslFn( `
  90. fn computePingPongWGSL( readTex: texture_storage_2d<${wgslFormat}, read>, writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32 ) -> void {
  91. let posX = index % ${ width };
  92. let posY = index / ${ width };
  93. let indexUV = vec2i( i32( posX ), i32( posY ) );
  94. let color = blur( readTex, indexUV ).rgb;
  95. textureStore( writeTex, indexUV, vec4f( color * 1.05, 1 ) );
  96. }
  97. `, [ rand2 ] );
  98. //
  99. computeToPong = computePingPongWGSL( { readTex: readPing, writeTex: writePong, index: instanceIndex } ).compute( width * height );
  100. computeToPing = computePingPongWGSL( { readTex: readPong, writeTex: writePing, index: instanceIndex } ).compute( width * height );
  101. //
  102. material = new THREE.MeshBasicMaterial( { color: 0xffffff, map: pongTexture } );
  103. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  104. scene.add( plane );
  105. renderer = new THREE.WebGPURenderer( { antialias: true } );
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. renderer.setAnimationLoop( render );
  109. document.body.appendChild( renderer.domElement );
  110. window.addEventListener( 'resize', onWindowResize );
  111. // compute init
  112. renderer.computeAsync( computeInitNode );
  113. }
  114. function onWindowResize() {
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. const aspect = window.innerWidth / window.innerHeight;
  117. const frustumHeight = camera.top - camera.bottom;
  118. camera.left = - frustumHeight * aspect / 2;
  119. camera.right = frustumHeight * aspect / 2;
  120. camera.updateProjectionMatrix();
  121. }
  122. function render() {
  123. const time = performance.now();
  124. const seconds = Math.floor( time / 1000 );
  125. // reset every second
  126. if ( phase && seconds !== lastUpdate ) {
  127. seed.value.set( Math.random(), Math.random() );
  128. renderer.compute( computeInitNode );
  129. lastUpdate = seconds;
  130. }
  131. // compute step
  132. renderer.compute( phase ? computeToPong : computeToPing );
  133. material.map = phase ? pongTexture : pingTexture;
  134. phase = ! phase;
  135. // render step
  136. // update material texture node
  137. renderer.render( scene, camera );
  138. }
  139. </script>
  140. </body>
  141. </html>