webgpu_compute_texture_pingpong.html 5.7 KB

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