webgpu_compute_texture_pingpong.html 5.5 KB

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