GPUComputationRenderer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import {
  2. ClampToEdgeWrapping,
  3. DataTexture,
  4. FloatType,
  5. NearestFilter,
  6. RGBAFormat,
  7. ShaderMaterial,
  8. WebGLRenderTarget
  9. } from 'three';
  10. import { FullScreenQuad } from '../postprocessing/Pass.js';
  11. /**
  12. * GPUComputationRenderer, based on SimulationRenderer by zz85
  13. *
  14. * The GPUComputationRenderer uses the concept of variables. These variables are RGBA float textures that hold 4 floats
  15. * for each compute element (texel)
  16. *
  17. * Each variable has a fragment shader that defines the computation made to obtain the variable in question.
  18. * You can use as many variables you need, and make dependencies so you can use textures of other variables in the shader
  19. * (the sampler uniforms are added automatically) Most of the variables will need themselves as dependency.
  20. *
  21. * The renderer has actually two render targets per variable, to make ping-pong. Textures from the current frame are used
  22. * as inputs to render the textures of the next frame.
  23. *
  24. * The render targets of the variables can be used as input textures for your visualization shaders.
  25. *
  26. * Variable names should be valid identifiers and should not collide with THREE GLSL used identifiers.
  27. * a common approach could be to use 'texture' prefixing the variable name; i.e texturePosition, textureVelocity...
  28. *
  29. * The size of the computation (sizeX * sizeY) is defined as 'resolution' automatically in the shader. For example:
  30. * #DEFINE resolution vec2( 1024.0, 1024.0 )
  31. *
  32. * -------------
  33. *
  34. * Basic use:
  35. *
  36. * // Initialization...
  37. *
  38. * // Create computation renderer
  39. * const gpuCompute = new GPUComputationRenderer( 1024, 1024, renderer );
  40. *
  41. * // Create initial state float textures
  42. * const pos0 = gpuCompute.createTexture();
  43. * const vel0 = gpuCompute.createTexture();
  44. * // and fill in here the texture data...
  45. *
  46. * // Add texture variables
  47. * const velVar = gpuCompute.addVariable( "textureVelocity", fragmentShaderVel, pos0 );
  48. * const posVar = gpuCompute.addVariable( "texturePosition", fragmentShaderPos, vel0 );
  49. *
  50. * // Add variable dependencies
  51. * gpuCompute.setVariableDependencies( velVar, [ velVar, posVar ] );
  52. * gpuCompute.setVariableDependencies( posVar, [ velVar, posVar ] );
  53. *
  54. * // Add custom uniforms
  55. * velVar.material.uniforms.time = { value: 0.0 };
  56. *
  57. * // Check for completeness
  58. * const error = gpuCompute.init();
  59. * if ( error !== null ) {
  60. * console.error( error );
  61. * }
  62. *
  63. *
  64. * // In each frame...
  65. *
  66. * // Compute!
  67. * gpuCompute.compute();
  68. *
  69. * // Update texture uniforms in your visualization materials with the gpu renderer output
  70. * myMaterial.uniforms.myTexture.value = gpuCompute.getCurrentRenderTarget( posVar ).texture;
  71. *
  72. * // Do your rendering
  73. * renderer.render( myScene, myCamera );
  74. *
  75. * -------------
  76. *
  77. * Also, you can use utility functions to create ShaderMaterial and perform computations (rendering between textures)
  78. * Note that the shaders can have multiple input textures.
  79. *
  80. * const myFilter1 = gpuCompute.createShaderMaterial( myFilterFragmentShader1, { theTexture: { value: null } } );
  81. * const myFilter2 = gpuCompute.createShaderMaterial( myFilterFragmentShader2, { theTexture: { value: null } } );
  82. *
  83. * const inputTexture = gpuCompute.createTexture();
  84. *
  85. * // Fill in here inputTexture...
  86. *
  87. * myFilter1.uniforms.theTexture.value = inputTexture;
  88. *
  89. * const myRenderTarget = gpuCompute.createRenderTarget();
  90. * myFilter2.uniforms.theTexture.value = myRenderTarget.texture;
  91. *
  92. * const outputRenderTarget = gpuCompute.createRenderTarget();
  93. *
  94. * // Now use the output texture where you want:
  95. * myMaterial.uniforms.map.value = outputRenderTarget.texture;
  96. *
  97. * // And compute each frame, before rendering to screen:
  98. * gpuCompute.doRenderTarget( myFilter1, myRenderTarget );
  99. * gpuCompute.doRenderTarget( myFilter2, outputRenderTarget );
  100. *
  101. *
  102. *
  103. * @param {int} sizeX Computation problem size is always 2d: sizeX * sizeY elements.
  104. * @param {int} sizeY Computation problem size is always 2d: sizeX * sizeY elements.
  105. * @param {WebGLRenderer} renderer The renderer
  106. */
  107. class GPUComputationRenderer {
  108. constructor( sizeX, sizeY, renderer ) {
  109. this.variables = [];
  110. this.currentTextureIndex = 0;
  111. let dataType = FloatType;
  112. const passThruUniforms = {
  113. passThruTexture: { value: null }
  114. };
  115. const passThruShader = createShaderMaterial( getPassThroughFragmentShader(), passThruUniforms );
  116. const quad = new FullScreenQuad( passThruShader );
  117. this.setDataType = function ( type ) {
  118. dataType = type;
  119. return this;
  120. };
  121. this.addVariable = function ( variableName, computeFragmentShader, initialValueTexture ) {
  122. const material = this.createShaderMaterial( computeFragmentShader );
  123. const variable = {
  124. name: variableName,
  125. initialValueTexture: initialValueTexture,
  126. material: material,
  127. dependencies: null,
  128. renderTargets: [],
  129. wrapS: null,
  130. wrapT: null,
  131. minFilter: NearestFilter,
  132. magFilter: NearestFilter
  133. };
  134. this.variables.push( variable );
  135. return variable;
  136. };
  137. this.setVariableDependencies = function ( variable, dependencies ) {
  138. variable.dependencies = dependencies;
  139. };
  140. this.init = function () {
  141. if ( renderer.capabilities.maxVertexTextures === 0 ) {
  142. return 'No support for vertex shader textures.';
  143. }
  144. for ( let i = 0; i < this.variables.length; i ++ ) {
  145. const variable = this.variables[ i ];
  146. // Creates rendertargets and initialize them with input texture
  147. variable.renderTargets[ 0 ] = this.createRenderTarget( sizeX, sizeY, variable.wrapS, variable.wrapT, variable.minFilter, variable.magFilter );
  148. variable.renderTargets[ 1 ] = this.createRenderTarget( sizeX, sizeY, variable.wrapS, variable.wrapT, variable.minFilter, variable.magFilter );
  149. this.renderTexture( variable.initialValueTexture, variable.renderTargets[ 0 ] );
  150. this.renderTexture( variable.initialValueTexture, variable.renderTargets[ 1 ] );
  151. // Adds dependencies uniforms to the ShaderMaterial
  152. const material = variable.material;
  153. const uniforms = material.uniforms;
  154. if ( variable.dependencies !== null ) {
  155. for ( let d = 0; d < variable.dependencies.length; d ++ ) {
  156. const depVar = variable.dependencies[ d ];
  157. if ( depVar.name !== variable.name ) {
  158. // Checks if variable exists
  159. let found = false;
  160. for ( let j = 0; j < this.variables.length; j ++ ) {
  161. if ( depVar.name === this.variables[ j ].name ) {
  162. found = true;
  163. break;
  164. }
  165. }
  166. if ( ! found ) {
  167. return 'Variable dependency not found. Variable=' + variable.name + ', dependency=' + depVar.name;
  168. }
  169. }
  170. uniforms[ depVar.name ] = { value: null };
  171. material.fragmentShader = '\nuniform sampler2D ' + depVar.name + ';\n' + material.fragmentShader;
  172. }
  173. }
  174. }
  175. this.currentTextureIndex = 0;
  176. return null;
  177. };
  178. this.compute = function () {
  179. const currentTextureIndex = this.currentTextureIndex;
  180. const nextTextureIndex = this.currentTextureIndex === 0 ? 1 : 0;
  181. for ( let i = 0, il = this.variables.length; i < il; i ++ ) {
  182. const variable = this.variables[ i ];
  183. // Sets texture dependencies uniforms
  184. if ( variable.dependencies !== null ) {
  185. const uniforms = variable.material.uniforms;
  186. for ( let d = 0, dl = variable.dependencies.length; d < dl; d ++ ) {
  187. const depVar = variable.dependencies[ d ];
  188. uniforms[ depVar.name ].value = depVar.renderTargets[ currentTextureIndex ].texture;
  189. }
  190. }
  191. // Performs the computation for this variable
  192. this.doRenderTarget( variable.material, variable.renderTargets[ nextTextureIndex ] );
  193. }
  194. this.currentTextureIndex = nextTextureIndex;
  195. };
  196. this.getCurrentRenderTarget = function ( variable ) {
  197. return variable.renderTargets[ this.currentTextureIndex ];
  198. };
  199. this.getAlternateRenderTarget = function ( variable ) {
  200. return variable.renderTargets[ this.currentTextureIndex === 0 ? 1 : 0 ];
  201. };
  202. this.dispose = function () {
  203. quad.dispose();
  204. const variables = this.variables;
  205. for ( let i = 0; i < variables.length; i ++ ) {
  206. const variable = variables[ i ];
  207. if ( variable.initialValueTexture ) variable.initialValueTexture.dispose();
  208. const renderTargets = variable.renderTargets;
  209. for ( let j = 0; j < renderTargets.length; j ++ ) {
  210. const renderTarget = renderTargets[ j ];
  211. renderTarget.dispose();
  212. }
  213. }
  214. };
  215. function addResolutionDefine( materialShader ) {
  216. materialShader.defines.resolution = 'vec2( ' + sizeX.toFixed( 1 ) + ', ' + sizeY.toFixed( 1 ) + ' )';
  217. }
  218. this.addResolutionDefine = addResolutionDefine;
  219. // The following functions can be used to compute things manually
  220. function createShaderMaterial( computeFragmentShader, uniforms ) {
  221. uniforms = uniforms || {};
  222. const material = new ShaderMaterial( {
  223. name: 'GPUComputationShader',
  224. uniforms: uniforms,
  225. vertexShader: getPassThroughVertexShader(),
  226. fragmentShader: computeFragmentShader
  227. } );
  228. addResolutionDefine( material );
  229. return material;
  230. }
  231. this.createShaderMaterial = createShaderMaterial;
  232. this.createRenderTarget = function ( sizeXTexture, sizeYTexture, wrapS, wrapT, minFilter, magFilter ) {
  233. sizeXTexture = sizeXTexture || sizeX;
  234. sizeYTexture = sizeYTexture || sizeY;
  235. wrapS = wrapS || ClampToEdgeWrapping;
  236. wrapT = wrapT || ClampToEdgeWrapping;
  237. minFilter = minFilter || NearestFilter;
  238. magFilter = magFilter || NearestFilter;
  239. const renderTarget = new WebGLRenderTarget( sizeXTexture, sizeYTexture, {
  240. wrapS: wrapS,
  241. wrapT: wrapT,
  242. minFilter: minFilter,
  243. magFilter: magFilter,
  244. format: RGBAFormat,
  245. type: dataType,
  246. depthBuffer: false
  247. } );
  248. return renderTarget;
  249. };
  250. this.createTexture = function () {
  251. const data = new Float32Array( sizeX * sizeY * 4 );
  252. const texture = new DataTexture( data, sizeX, sizeY, RGBAFormat, FloatType );
  253. texture.needsUpdate = true;
  254. return texture;
  255. };
  256. this.renderTexture = function ( input, output ) {
  257. // Takes a texture, and render out in rendertarget
  258. // input = Texture
  259. // output = RenderTarget
  260. passThruUniforms.passThruTexture.value = input;
  261. this.doRenderTarget( passThruShader, output );
  262. passThruUniforms.passThruTexture.value = null;
  263. };
  264. this.doRenderTarget = function ( material, output ) {
  265. const currentRenderTarget = renderer.getRenderTarget();
  266. const currentXrEnabled = renderer.xr.enabled;
  267. const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  268. renderer.xr.enabled = false; // Avoid camera modification
  269. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  270. quad.material = material;
  271. renderer.setRenderTarget( output );
  272. quad.render( renderer );
  273. quad.material = passThruShader;
  274. renderer.xr.enabled = currentXrEnabled;
  275. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  276. renderer.setRenderTarget( currentRenderTarget );
  277. };
  278. // Shaders
  279. function getPassThroughVertexShader() {
  280. return 'void main() {\n' +
  281. '\n' +
  282. ' gl_Position = vec4( position, 1.0 );\n' +
  283. '\n' +
  284. '}\n';
  285. }
  286. function getPassThroughFragmentShader() {
  287. return 'uniform sampler2D passThruTexture;\n' +
  288. '\n' +
  289. 'void main() {\n' +
  290. '\n' +
  291. ' vec2 uv = gl_FragCoord.xy / resolution.xy;\n' +
  292. '\n' +
  293. ' gl_FragColor = texture2D( passThruTexture, uv );\n' +
  294. '\n' +
  295. '}\n';
  296. }
  297. }
  298. }
  299. export { GPUComputationRenderer };