FSR1Node.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import { HalfFloatType, RenderTarget, Vector2, NodeMaterial, RendererUtils, QuadMesh, TempNode, NodeUpdateType } from 'three/webgpu';
  2. import { Fn, float, vec2, vec3, vec4, ivec2, int, uv, floor, fract, abs, max, min, clamp, saturate, sqrt, select, exp2, nodeObject, passTexture, textureSize, textureLoad, convertToTexture } from 'three/tsl';
  3. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  4. const _size = /*@__PURE__*/ new Vector2();
  5. let _rendererState;
  6. /**
  7. * Post processing node for applying AMD FidelityFX Super Resolution 1 (FSR 1).
  8. *
  9. * Combines two passes:
  10. * - **EASU** (Edge-Adaptive Spatial Upsampling): Uses 12 texture samples in a cross pattern
  11. * to detect local edge direction, then shapes an approximate Lanczos2 kernel into an
  12. * ellipse aligned with the detected edge.
  13. * - **RCAS** (Robust Contrast-Adaptive Sharpening): Uses a 5-tap cross pattern to apply
  14. * contrast-aware sharpening that is automatically limited per-pixel to avoid artifacts.
  15. *
  16. * Note: Only use FSR 1 if your application is fragment-shader bound and cannot afford to render
  17. * at full resolution. FSR 1 adds its own overhead, so simply shaded scenes will render faster
  18. * at native resolution without it. Besides, FSR 1 should always be used with an anti-aliased
  19. * source image.
  20. *
  21. * Reference: {@link https://gpuopen.com/fidelityfx-superresolution/}.
  22. *
  23. * @augments TempNode
  24. * @three_import import { fsr1 } from 'three/addons/tsl/display/fsr1/FSR1Node.js';
  25. */
  26. class FSR1Node extends TempNode {
  27. static get type() {
  28. return 'FSR1Node';
  29. }
  30. /**
  31. * Constructs a new FSR 1 node.
  32. *
  33. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  34. * @param {Node<float>} [sharpness=0.2] - RCAS sharpening strength. 0 = maximum sharpening, 2 = no sharpening.
  35. * @param {Node<bool>} [denoise=false] - Whether to attenuate RCAS sharpening in noisy areas.
  36. */
  37. constructor( textureNode, sharpness = 0.2, denoise = false ) {
  38. super( 'vec4' );
  39. /**
  40. * The texture node that represents the input of the effect.
  41. *
  42. * @type {TextureNode}
  43. */
  44. this.textureNode = textureNode;
  45. /**
  46. * RCAS sharpening strength. 0 = maximum, 2 = none.
  47. *
  48. * @type {Node<float>}
  49. */
  50. this.sharpness = nodeObject( sharpness );
  51. /**
  52. * Whether to attenuate RCAS sharpening in noisy areas.
  53. *
  54. * @type {Node<bool>}
  55. */
  56. this.denoise = nodeObject( denoise );
  57. /**
  58. * The render target for the EASU upscale pass.
  59. *
  60. * @private
  61. * @type {RenderTarget}
  62. */
  63. this._easuRT = new RenderTarget( 1, 1, { depthBuffer: false, type: HalfFloatType } );
  64. this._easuRT.texture.name = 'FSR1Node.easu';
  65. /**
  66. * The render target for the RCAS sharpen pass.
  67. *
  68. * @private
  69. * @type {RenderTarget}
  70. */
  71. this._rcasRT = new RenderTarget( 1, 1, { depthBuffer: false, type: HalfFloatType } );
  72. this._rcasRT.texture.name = 'FSR1Node.rcas';
  73. /**
  74. * The result of the effect as a texture node.
  75. *
  76. * @private
  77. * @type {PassTextureNode}
  78. */
  79. this._textureNode = passTexture( this, this._rcasRT.texture );
  80. /**
  81. * The material for the EASU pass.
  82. *
  83. * @private
  84. * @type {?NodeMaterial}
  85. */
  86. this._easuMaterial = null;
  87. /**
  88. * The material for the RCAS pass.
  89. *
  90. * @private
  91. * @type {?NodeMaterial}
  92. */
  93. this._rcasMaterial = null;
  94. /**
  95. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  96. * its effect once per frame in `updateBefore()`.
  97. *
  98. * @type {string}
  99. * @default 'frame'
  100. */
  101. this.updateBeforeType = NodeUpdateType.FRAME;
  102. }
  103. /**
  104. * Sets the output size of the effect.
  105. *
  106. * @param {number} width - The width in pixels.
  107. * @param {number} height - The height in pixels.
  108. */
  109. setSize( width, height ) {
  110. this._easuRT.setSize( width, height );
  111. this._rcasRT.setSize( width, height );
  112. }
  113. /**
  114. * This method is used to render the effect once per frame.
  115. *
  116. * @param {NodeFrame} frame - The current node frame.
  117. */
  118. updateBefore( frame ) {
  119. const { renderer } = frame;
  120. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  121. //
  122. renderer.getDrawingBufferSize( _size );
  123. this.setSize( _size.x, _size.y );
  124. // EASU pass
  125. renderer.setRenderTarget( this._easuRT );
  126. _quadMesh.material = this._easuMaterial;
  127. _quadMesh.name = 'FSR1 [ EASU Pass ]';
  128. _quadMesh.render( renderer );
  129. // RCAS pass
  130. renderer.setRenderTarget( this._rcasRT );
  131. _quadMesh.material = this._rcasMaterial;
  132. _quadMesh.name = 'FSR1 [ RCAS Pass ]';
  133. _quadMesh.render( renderer );
  134. //
  135. RendererUtils.restoreRendererState( renderer, _rendererState );
  136. }
  137. /**
  138. * Returns the result of the effect as a texture node.
  139. *
  140. * @return {PassTextureNode} A texture node that represents the result of the effect.
  141. */
  142. getTextureNode() {
  143. return this._textureNode;
  144. }
  145. /**
  146. * This method is used to setup the effect's TSL code.
  147. *
  148. * @param {NodeBuilder} builder - The current node builder.
  149. * @return {PassTextureNode}
  150. */
  151. setup( builder ) {
  152. const textureNode = this.textureNode;
  153. const inputTex = textureNode.value;
  154. // Note on performance: Compared to the orginal FSR1 code, texture sampling does
  155. // not make use of textureGather() yet. This is only available with WebGPU so the
  156. // WebGL 2 backend needs a fallback. Besides, in WebGPU and WebGL 2 we also
  157. // can't make use of packed math (e.g. FP16) which would considerably lower
  158. // the arithmetic costs (e.g. two 16-bit ops in parallel).
  159. // Accumulate edge direction and length for one bilinear quadrant.
  160. const _accumulateEdge = ( dir, len, w, aL, bL, cL, dL, eL ) => {
  161. const dc = dL.sub( cL ).toConst();
  162. const cb = cL.sub( bL ).toConst();
  163. const dirX = dL.sub( bL ).toConst();
  164. const lenX = max( abs( dc ), abs( cb ) ).toConst();
  165. const sLenX = saturate( abs( dirX ).div( max( lenX, float( 1.0 / 65536.0 ) ) ) ).toConst();
  166. dir.x.addAssign( dirX.mul( w ) );
  167. len.addAssign( sLenX.mul( sLenX ).mul( w ) );
  168. const ec = eL.sub( cL ).toConst();
  169. const ca = cL.sub( aL ).toConst();
  170. const dirY = eL.sub( aL ).toConst();
  171. const lenY = max( abs( ec ), abs( ca ) ).toConst();
  172. const sLenY = saturate( abs( dirY ).div( max( lenY, float( 1.0 / 65536.0 ) ) ) ).toConst();
  173. dir.y.addAssign( dirY.mul( w ) );
  174. len.addAssign( sLenY.mul( sLenY ).mul( w ) );
  175. };
  176. // Compute an approximate Lanczos2 tap weight and accumulate.
  177. const _accumulateTap = ( aC, aW, offset, dir, len2, lob, clp, color ) => {
  178. const vx = offset.x.mul( dir.x ).add( offset.y.mul( dir.y ) ).toConst();
  179. const vy = offset.x.mul( dir.y ).negate().add( offset.y.mul( dir.x ) ).toConst();
  180. const sx = vx.mul( len2.x ).toConst();
  181. const sy = vy.mul( len2.y ).toConst();
  182. const d2 = min( sx.mul( sx ).add( sy.mul( sy ) ), clp ).toConst();
  183. const wB = d2.mul( 2.0 / 5.0 ).sub( 1.0 ).toConst();
  184. const wA = d2.mul( lob ).sub( 1.0 ).toConst();
  185. const w = wB.mul( wB ).mul( 25.0 / 16.0 ).sub( 25.0 / 16.0 - 1.0 ).mul( wA.mul( wA ) ).toConst();
  186. aC.addAssign( color.mul( w ) );
  187. aW.addAssign( w );
  188. };
  189. // EASU pass: edge-adaptive spatial upsampling.
  190. const easu = Fn( () => {
  191. const targetUV = uv();
  192. const texSize = vec2( textureSize( textureNode ) );
  193. const pp = targetUV.mul( texSize ).sub( 0.5 ).toConst();
  194. const fp = floor( pp ).toConst();
  195. const f = fract( pp ).toConst();
  196. // Fetch exact texel values at integer coordinates (no filtering).
  197. const ifp = ivec2( int( fp.x ), int( fp.y ) ).toConst();
  198. const tap = ( dx, dy ) => textureLoad( inputTex, ifp.add( ivec2( dx, dy ) ) );
  199. // 12-tap cross pattern:
  200. // b c
  201. // e f g h
  202. // i j k l
  203. // n o
  204. const sB = tap( 0, - 1 ), sC = tap( 1, - 1 );
  205. const sE = tap( - 1, 0 ), sF = tap( 0, 0 ), sG = tap( 1, 0 ), sH = tap( 2, 0 );
  206. const sI = tap( - 1, 1 ), sJ = tap( 0, 1 ), sK = tap( 1, 1 ), sL = tap( 2, 1 );
  207. const sN = tap( 0, 2 ), sO = tap( 1, 2 );
  208. // Approximate luminance for edge detection.
  209. const luma = ( s ) => s.r.mul( 0.5 ).add( s.g ).add( s.b.mul( 0.5 ) );
  210. const bL = luma( sB ), cL = luma( sC );
  211. const eL = luma( sE ), fL = luma( sF ), gL = luma( sG ), hL = luma( sH );
  212. const iL = luma( sI ), jL = luma( sJ ), kL = luma( sK ), lL = luma( sL );
  213. const nL = luma( sN ), oL = luma( sO );
  214. // Accumulate edge direction and length from 4 bilinear quadrants.
  215. const dir = vec2( 0 ).toVar();
  216. const len = float( 0 ).toVar();
  217. const w0 = float( 1 ).sub( f.x ).mul( float( 1 ).sub( f.y ) ).toConst();
  218. const w1 = f.x.mul( float( 1 ).sub( f.y ) ).toConst();
  219. const w2 = float( 1 ).sub( f.x ).mul( f.y ).toConst();
  220. const w3 = f.x.mul( f.y ).toConst();
  221. _accumulateEdge( dir, len, w0, bL, eL, fL, gL, jL );
  222. _accumulateEdge( dir, len, w1, cL, fL, gL, hL, kL );
  223. _accumulateEdge( dir, len, w2, fL, iL, jL, kL, nL );
  224. _accumulateEdge( dir, len, w3, gL, jL, kL, lL, oL );
  225. // Normalize direction, defaulting to (1, 0) when gradient is negligible.
  226. const dirSq = dir.x.mul( dir.x ).add( dir.y.mul( dir.y ) ).toConst();
  227. const zro = dirSq.lessThan( 1.0 / 32768.0 ).toConst();
  228. const rDirLen = float( 1.0 ).div( sqrt( max( dirSq, float( 1.0 / 32768.0 ) ) ) ).toConst();
  229. dir.x.assign( select( zro, float( 1.0 ), dir.x ) );
  230. dir.mulAssign( select( zro, float( 1.0 ), rDirLen ) );
  231. // Shape the kernel based on edge strength.
  232. len.assign( len.mul( 0.5 ) );
  233. len.mulAssign( len );
  234. // Stretch factor: 1.0 for axis-aligned edges, sqrt(2) on diagonals.
  235. const stretch = dir.x.mul( dir.x ).add( dir.y.mul( dir.y ) ).div( max( abs( dir.x ), abs( dir.y ) ) ).toConst();
  236. // Anisotropic lengths: x stretches along edge, y shrinks perpendicular.
  237. const len2 = vec2(
  238. float( 1.0 ).add( stretch.sub( 1.0 ).mul( len ) ),
  239. float( 1.0 ).sub( len.mul( 0.5 ) )
  240. ).toConst();
  241. // Negative lobe: strong on flat areas (0.5), reduced on edges (0.21).
  242. const lob = float( 0.5 ).add( float( 1.0 / 4.0 - 0.04 - 0.5 ).mul( len ) ).toConst();
  243. const clp = float( 1.0 ).div( lob ).toConst();
  244. // Accumulate weighted taps.
  245. const aC = vec4( 0 ).toVar();
  246. const aW = float( 0 ).toVar();
  247. _accumulateTap( aC, aW, vec2( 0, - 1 ).sub( f ), dir, len2, lob, clp, sB );
  248. _accumulateTap( aC, aW, vec2( 1, - 1 ).sub( f ), dir, len2, lob, clp, sC );
  249. _accumulateTap( aC, aW, vec2( - 1, 0 ).sub( f ), dir, len2, lob, clp, sE );
  250. _accumulateTap( aC, aW, vec2( 0, 0 ).sub( f ), dir, len2, lob, clp, sF );
  251. _accumulateTap( aC, aW, vec2( 1, 0 ).sub( f ), dir, len2, lob, clp, sG );
  252. _accumulateTap( aC, aW, vec2( 2, 0 ).sub( f ), dir, len2, lob, clp, sH );
  253. _accumulateTap( aC, aW, vec2( - 1, 1 ).sub( f ), dir, len2, lob, clp, sI );
  254. _accumulateTap( aC, aW, vec2( 0, 1 ).sub( f ), dir, len2, lob, clp, sJ );
  255. _accumulateTap( aC, aW, vec2( 1, 1 ).sub( f ), dir, len2, lob, clp, sK );
  256. _accumulateTap( aC, aW, vec2( 2, 1 ).sub( f ), dir, len2, lob, clp, sL );
  257. _accumulateTap( aC, aW, vec2( 0, 2 ).sub( f ), dir, len2, lob, clp, sN );
  258. _accumulateTap( aC, aW, vec2( 1, 2 ).sub( f ), dir, len2, lob, clp, sO );
  259. // Normalize.
  260. aC.divAssign( aW );
  261. // Anti-ringing: clamp to min/max of the 4 nearest samples (f, g, j, k).
  262. const min4 = min( min( sF, sG ), min( sJ, sK ) ).toConst();
  263. const max4 = max( max( sF, sG ), max( sJ, sK ) ).toConst();
  264. return clamp( aC, min4, max4 );
  265. } );
  266. // RCAS pass: robust contrast-adaptive sharpening.
  267. const easuTex = this._easuRT.texture;
  268. const rcas = Fn( () => {
  269. const targetUV = uv();
  270. const texSize = vec2( textureSize( textureLoad( easuTex ) ) );
  271. const p = ivec2( int( floor( targetUV.x.mul( texSize.x ) ) ), int( floor( targetUV.y.mul( texSize.y ) ) ) ).toConst();
  272. const e = textureLoad( easuTex, p );
  273. const b = textureLoad( easuTex, p.add( ivec2( 0, - 1 ) ) );
  274. const d = textureLoad( easuTex, p.add( ivec2( - 1, 0 ) ) );
  275. const f = textureLoad( easuTex, p.add( ivec2( 1, 0 ) ) );
  276. const h = textureLoad( easuTex, p.add( ivec2( 0, 1 ) ) );
  277. // Approximate luminance (luma times 2).
  278. const luma = ( s ) => s.g.add( s.b.add( s.r ).mul( 0.5 ) );
  279. const bL = luma( b );
  280. const dL = luma( d );
  281. const eL = luma( e );
  282. const fL = luma( f );
  283. const hL = luma( h );
  284. // Sharpening amount from user parameter.
  285. const con = exp2( this.sharpness.negate() ).toConst();
  286. // Min and max of ring.
  287. const mn4 = min( min( b.rgb, d.rgb ), min( f.rgb, h.rgb ) ).toConst();
  288. const mx4 = max( max( b.rgb, d.rgb ), max( f.rgb, h.rgb ) ).toConst();
  289. // Compute adaptive lobe weight.
  290. // Limiters based on how much sharpening the local contrast can tolerate.
  291. const RCAS_LIMIT = float( 0.25 - 1.0 / 16.0 ).toConst();
  292. const hitMin = min( mn4, e.rgb ).div( mx4.mul( 4.0 ) ).toConst();
  293. const hitMax = vec3( 1.0 ).sub( max( mx4, e.rgb ) ).div( mn4.mul( 4.0 ).sub( 4.0 ) ).toConst();
  294. const lobeRGB = max( hitMin.negate(), hitMax ).toConst();
  295. const lobe = max(
  296. RCAS_LIMIT.negate(),
  297. min( max( lobeRGB.r, max( lobeRGB.g, lobeRGB.b ) ), float( 0.0 ) )
  298. ).mul( con ).toConst();
  299. // Noise attenuation.
  300. const nz = bL.add( dL ).add( fL ).add( hL ).mul( 0.25 ).sub( eL ).toConst();
  301. const nzRange = max( max( bL, dL ), max( eL, max( fL, hL ) ) ).sub( min( min( bL, dL ), min( eL, min( fL, hL ) ) ) ).toConst();
  302. const nzFactor = float( 1.0 ).sub( abs( nz ).div( max( nzRange, float( 1.0 / 65536.0 ) ) ).saturate().mul( 0.5 ) ).toConst();
  303. const effectiveLobe = this.denoise.equal( true ).select( lobe.mul( nzFactor ), lobe ).toConst();
  304. // Resolve: weighted blend of cross neighbors and center.
  305. const result = b.rgb.add( d.rgb ).add( f.rgb ).add( h.rgb ).mul( effectiveLobe ).add( e.rgb )
  306. .div( effectiveLobe.mul( 4.0 ).add( 1.0 ) ).toConst();
  307. return vec4( result, e.a );
  308. } );
  309. //
  310. const context = builder.getSharedContext();
  311. const easuMaterial = this._easuMaterial || ( this._easuMaterial = new NodeMaterial() );
  312. easuMaterial.fragmentNode = easu().context( context );
  313. easuMaterial.name = 'FSR1_EASU';
  314. easuMaterial.needsUpdate = true;
  315. const rcasMaterial = this._rcasMaterial || ( this._rcasMaterial = new NodeMaterial() );
  316. rcasMaterial.fragmentNode = rcas().context( context );
  317. rcasMaterial.name = 'FSR1_RCAS';
  318. rcasMaterial.needsUpdate = true;
  319. //
  320. const properties = builder.getNodeProperties( this );
  321. properties.textureNode = textureNode;
  322. //
  323. return this._textureNode;
  324. }
  325. /**
  326. * Frees internal resources. This method should be called
  327. * when the effect is no longer required.
  328. */
  329. dispose() {
  330. this._easuRT.dispose();
  331. this._rcasRT.dispose();
  332. if ( this._easuMaterial !== null ) this._easuMaterial.dispose();
  333. if ( this._rcasMaterial !== null ) this._rcasMaterial.dispose();
  334. }
  335. }
  336. export default FSR1Node;
  337. /**
  338. * TSL function for creating an FSR 1 node for post processing.
  339. *
  340. * @tsl
  341. * @function
  342. * @param {Node<vec4>} node - The node that represents the input of the effect.
  343. * @param {(number|Node<float>)} [sharpness=0.2] - RCAS sharpening strength. 0 = maximum, 2 = none.
  344. * @param {(boolean|Node<bool>)} [denoise=false] - Whether to attenuate RCAS sharpening in noisy areas.
  345. * @returns {FSR1Node}
  346. */
  347. export const fsr1 = ( node, sharpness, denoise ) => new FSR1Node( convertToTexture( node ), sharpness, denoise );
粤ICP备19079148号