giRSMManager.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { RawTexture } from "../../Materials/Textures/rawTexture.js";
  3. import { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture.js";
  4. import { PostProcess } from "../../PostProcesses/postProcess.js";
  5. import { Layer } from "../../Layers/layer.js";
  6. import { Matrix } from "../../Maths/math.vector.js";
  7. import { MaterialPluginBase } from "../../Materials/materialPluginBase.js";
  8. import { PBRBaseMaterial } from "../../Materials/PBR/pbrBaseMaterial.js";
  9. import { GeometryBufferRenderer } from "../geometryBufferRenderer.js";
  10. import { BaseTexture } from "../../Materials/Textures/baseTexture.js";
  11. import { expandToProperty, serialize } from "../../Misc/decorators.js";
  12. import { MaterialDefines } from "../../Materials/materialDefines.js";
  13. import { RegisterClass } from "../../Misc/typeStore.js";
  14. import "../../Shaders/bilateralBlur.fragment.js";
  15. import "../../Shaders/bilateralBlurQuality.fragment.js";
  16. import "../../Shaders/rsmGlobalIllumination.fragment.js";
  17. import "../../Shaders/rsmFullGlobalIllumination.fragment.js";
  18. /**
  19. * Class used to manage the global illumination contribution calculated from reflective shadow maps (RSM).
  20. */
  21. export class GIRSMManager {
  22. /**
  23. * Enables or disables the manager. Default is false.
  24. * If disabled, the global illumination won't be calculated and the scene will be rendered normally, without any global illumination contribution.
  25. */
  26. get enable() {
  27. return this._enable;
  28. }
  29. set enable(enable) {
  30. if (this._giRSM.length === 0) {
  31. enable = false;
  32. }
  33. if (enable === this._enable) {
  34. return;
  35. }
  36. this._enable = enable;
  37. this._debugLayer.isEnabled = this._showOnlyGI && enable;
  38. this._materialsWithRenderPlugin.forEach((mat) => {
  39. if (mat.pluginManager) {
  40. const plugin = mat.pluginManager.getPlugin(GIRSMRenderPluginMaterial.Name);
  41. plugin.isEnabled = enable;
  42. }
  43. });
  44. this.recreateResources(!enable);
  45. }
  46. /**
  47. * Defines if the global illumination contribution should be blurred or not (using a bilateral blur). Default is true.
  48. */
  49. get enableBlur() {
  50. return this._enableBlur;
  51. }
  52. set enableBlur(enable) {
  53. if (enable === this._enableBlur) {
  54. return;
  55. }
  56. this._enableBlur = enable;
  57. this.recreateResources();
  58. }
  59. /**
  60. * Defines if the blur should be done with a better quality but slower or not. Default is false.
  61. */
  62. get useQualityBlur() {
  63. return this._useQualityBlur;
  64. }
  65. set useQualityBlur(enable) {
  66. if (enable === this._useQualityBlur) {
  67. return;
  68. }
  69. this._useQualityBlur = enable;
  70. this.recreateResources();
  71. }
  72. /**
  73. * Defines if the blur should be done at full resolution or not. Default is false.
  74. * If this setting is enabled, upampling will be disabled (ignored) as it is not needed anymore.
  75. */
  76. get fullSizeBlur() {
  77. return this._forceFullSizeBlur;
  78. }
  79. set fullSizeBlur(mode) {
  80. if (this._forceFullSizeBlur === mode) {
  81. return;
  82. }
  83. this._forceFullSizeBlur = mode;
  84. this.recreateResources();
  85. }
  86. /**
  87. * Defines if the upsampling should be done with a better quality but slower or not. Default is false.
  88. */
  89. get useQualityUpsampling() {
  90. return this._useQualityUpsampling;
  91. }
  92. set useQualityUpsampling(enable) {
  93. if (enable === this._useQualityUpsampling) {
  94. return;
  95. }
  96. this._useQualityUpsampling = enable;
  97. this.recreateResources();
  98. }
  99. /**
  100. * Defines if the debug layer should be enabled or not. Default is false.
  101. * Use this setting for debugging purpose, to show the global illumination contribution only.
  102. */
  103. get showOnlyGI() {
  104. return this._showOnlyGI;
  105. }
  106. set showOnlyGI(show) {
  107. if (this._showOnlyGI === show) {
  108. return;
  109. }
  110. this._showOnlyGI = show;
  111. this._debugLayer.isEnabled = show;
  112. }
  113. /**
  114. * Sets the output dimensions of the final process. It should normally be the same as the output dimensions of the screen.
  115. * @param dimensions The dimensions of the output texture (width and height)
  116. */
  117. setOutputDimensions(dimensions) {
  118. this._outputDimensions = dimensions;
  119. this.recreateResources();
  120. }
  121. /**
  122. * Sets the dimensions of the GI texture. Try to use the smallest size possible for better performance.
  123. * @param dimensions The dimensions of the GI texture (width and height)
  124. */
  125. setGITextureDimensions(dimensions) {
  126. this._giTextureDimensions = dimensions;
  127. this.recreateResources();
  128. }
  129. /**
  130. * Gets or sets the texture type used by the GI texture. Default is 11.
  131. */
  132. get giTextureType() {
  133. return this._giTextureType;
  134. }
  135. set giTextureType(textureType) {
  136. if (this._giTextureType === textureType) {
  137. return;
  138. }
  139. this._giTextureType = textureType;
  140. this.recreateResources();
  141. }
  142. /**
  143. * Gets the list of GIRSM used by the manager.
  144. */
  145. get giRSM() {
  146. return this._giRSM;
  147. }
  148. /**
  149. * Adds a (list of) GIRSM to the manager.
  150. * @param rsm The GIRSM (or array of GIRSM) to add to the manager
  151. */
  152. addGIRSM(rsm) {
  153. if (Array.isArray(rsm)) {
  154. this._giRSM.push(...rsm);
  155. }
  156. else {
  157. this._giRSM.push(rsm);
  158. }
  159. this.recreateResources();
  160. }
  161. /**
  162. * Removes a (list of) GIRSM from the manager.
  163. * @param rsm The GIRSM (or array of GIRSM) to remove from the manager
  164. */
  165. removeGIRSM(rsm) {
  166. if (Array.isArray(rsm)) {
  167. for (let i = 0; i < rsm.length; ++i) {
  168. const idx = this._giRSM.indexOf(rsm[i]);
  169. if (idx !== -1) {
  170. this._giRSM.splice(idx, 1);
  171. }
  172. }
  173. }
  174. else {
  175. const idx = this._giRSM.indexOf(rsm);
  176. if (idx !== -1) {
  177. this._giRSM.splice(idx, 1);
  178. }
  179. }
  180. if (this._giRSM.length === 0) {
  181. this.enable = false;
  182. }
  183. else {
  184. this.recreateResources();
  185. }
  186. }
  187. /**
  188. * Add a material to the manager. This will enable the global illumination contribution for the material.
  189. * @param material Material that will be affected by the global illumination contribution. If not provided, all materials of the scene will be affected.
  190. */
  191. addMaterial(material) {
  192. if (material) {
  193. this._addGISupportToMaterial(material);
  194. }
  195. else {
  196. this._scene.meshes.forEach((mesh) => {
  197. if (mesh.getTotalVertices() > 0 && mesh.isEnabled() && mesh.material) {
  198. this._addGISupportToMaterial(mesh.material);
  199. }
  200. });
  201. }
  202. }
  203. /**
  204. * Gets the list of GPU counters used by the manager.
  205. * GPU timing measurements must be enabled for the counters to be filled (engine.enableGPUTimingMeasurements = true).
  206. * Only available with WebGPU. You will still get the list of counters with other engines but the values will always be 0.
  207. */
  208. get countersGPU() {
  209. return this._counters;
  210. }
  211. /**
  212. * Recreates the resources used by the manager.
  213. * You should normally not have to call this method manually, except if you change the useFullTexture property of a GIRSM, because the manager won't track this change.
  214. * @param disposeGeometryBufferRenderer Defines if the geometry buffer renderer should be disposed and recreated. Default is false.
  215. */
  216. recreateResources(disposeGeometryBufferRenderer = false) {
  217. this._disposePostProcesses(disposeGeometryBufferRenderer);
  218. this._createPostProcesses();
  219. this._setPluginParameters();
  220. }
  221. /**
  222. * Generates the sample texture used by the the global illumination calculation process.
  223. * @param maxSamples The maximum number of samples to generate in the texture. Default value is 2048. The numSamples property of the GIRSM should be less than or equal to this value!
  224. */
  225. generateSampleTexture(maxSamples) {
  226. this._sampleTexture?.dispose();
  227. this._maxSamples = maxSamples;
  228. const data = new Float32Array(this._maxSamples * 4);
  229. for (let i = 0; i < this._maxSamples; i++) {
  230. const xi1 = Math.random();
  231. const xi2 = Math.random();
  232. const x = xi1 * Math.sin(2 * Math.PI * xi2);
  233. const y = xi1 * Math.cos(2 * Math.PI * xi2);
  234. data[i * 4 + 0] = x;
  235. data[i * 4 + 1] = y;
  236. data[i * 4 + 2] = xi1 * xi1;
  237. data[i * 4 + 3] = 1;
  238. }
  239. this._sampleTexture = new RawTexture(data, this._maxSamples, 1, 5, this._scene, false, false, 1, 1);
  240. this._sampleTexture.name = "GIRSMSamples";
  241. }
  242. /**
  243. * Disposes the manager.
  244. */
  245. dispose() {
  246. this._disposePostProcesses(true);
  247. this._debugLayer.texture?.dispose();
  248. this._debugLayer.dispose();
  249. this._scene.onBeforeDrawPhaseObservable.remove(this._drawPhaseObserver);
  250. }
  251. /**
  252. * Creates a new GIRSMManager
  253. * @param scene The scene
  254. * @param outputDimensions The dimensions of the output texture (width and height). Should normally be the same as the output dimensions of the screen.
  255. * @param giTextureDimensions The dimensions of the GI texture (width and height). Try to use the smallest size possible for better performance.
  256. * @param maxSamples The maximum number of samples to generate in the sample texture. Default value is 2048. The numSamples property of the GIRSM should be less than or equal to this value!
  257. * @param giTextureType The texture type used by the GI texture. Default is 11.
  258. */
  259. constructor(scene, outputDimensions, giTextureDimensions = { width: 256, height: 256 }, maxSamples = 2048, giTextureType = 11) {
  260. this._giRSM = [];
  261. this._blurRTT = null;
  262. this._blurPostProcesses = null;
  263. this._blurXPostprocess = null;
  264. this._blurYPostprocess = null;
  265. this._upsamplingXPostprocess = null;
  266. this._upsamplingYPostprocess = null;
  267. this._ppGlobalIllumination = [];
  268. this._firstActivation = true;
  269. this._geomBufferEnabled = false;
  270. this._geomBufferEnablePosition = false;
  271. this._tempMatrix = new Matrix();
  272. this._enable = false;
  273. /**
  274. * Defines if the global illumination calculation is paused or not.
  275. * Use this setting to pause the global illumination calculation when you know that the scene (camera/mesh/light positions) is not changing anymore to save some GPU power.
  276. * The scene will still be rendered with the latest global illumination contribution.
  277. */
  278. this.pause = false;
  279. this._enableBlur = true;
  280. this._useQualityBlur = false;
  281. /**
  282. * Defines the depth threshold used by the bilateral blur post-processes (also used by the upsampling, if enabled).
  283. * You may have to change this value, depending on your scene.
  284. */
  285. this.blurDepthThreshold = 0.05;
  286. /**
  287. * Defines the normal threshold used by the bilateral blur post-processes (also used by the upsampling, if enabled).
  288. * You may have to change this value, depending on your scene.
  289. */
  290. this.blurNormalThreshold = 0.25;
  291. /**
  292. * Defines the kernel size used by the bilateral blur post-processes. Default is 12.
  293. */
  294. this.blurKernel = 12;
  295. this._forceFullSizeBlur = false;
  296. this._useQualityUpsampling = false;
  297. /**
  298. * Defines the kernel size used by the bilateral upsampling post-processes. Default is 6.
  299. */
  300. this.upsamplerKernel = 6;
  301. this._showOnlyGI = false;
  302. this._scene = scene;
  303. this._engine = scene.getEngine();
  304. this._outputDimensions = outputDimensions;
  305. this._giTextureDimensions = giTextureDimensions;
  306. this._giTextureType = giTextureType;
  307. this._materialsWithRenderPlugin = [];
  308. this._maxSamples = maxSamples;
  309. this._debugLayer = new Layer("debug layer", null, this._scene, false);
  310. this._debugLayer.isEnabled = false;
  311. this._counters = [];
  312. this._countersRTW = [];
  313. this.generateSampleTexture(maxSamples);
  314. this._drawPhaseObserver = this._scene.onBeforeDrawPhaseObservable.add(() => {
  315. const currentRenderTarget = this._engine._currentRenderTarget;
  316. let rebindCurrentRenderTarget = false;
  317. if (this._enable) {
  318. if (!this.pause) {
  319. this._scene.postProcessManager.directRender(this._ppGlobalIllumination, this._ppGlobalIllumination[0].inputTexture);
  320. this._engine.unBindFramebuffer(this._ppGlobalIllumination[0].inputTexture, true);
  321. this._engine.setAlphaMode(0);
  322. rebindCurrentRenderTarget = true;
  323. if (this.enableBlur && this._blurPostProcesses) {
  324. this._scene.postProcessManager.directRender(this._blurPostProcesses, this._blurRTT.renderTarget, true);
  325. this._engine.unBindFramebuffer(this._blurRTT.renderTarget, true);
  326. }
  327. }
  328. for (let i = 0; i < this._counters.length; ++i) {
  329. const rtws = this._countersRTW[i];
  330. for (let t = 0; t < rtws.length; ++t) {
  331. if (t === 0) {
  332. this._counters[i].value = this.pause ? 0 : rtws[t].gpuTimeInFrame?.counter.lastSecAverage ?? 0;
  333. }
  334. else if (!this.pause) {
  335. this._counters[i].value += rtws[t].gpuTimeInFrame?.counter.lastSecAverage ?? 0;
  336. }
  337. }
  338. }
  339. if (this._scene.activeCamera) {
  340. this._engine.setViewport(this._scene.activeCamera.viewport);
  341. }
  342. }
  343. if (rebindCurrentRenderTarget && currentRenderTarget) {
  344. this._engine.bindFramebuffer(currentRenderTarget);
  345. }
  346. });
  347. }
  348. _disposePostProcesses(disposeGeometryBufferRenderer = false) {
  349. this._blurRTT?.dispose();
  350. this._blurRTT = null;
  351. this._blurPostProcesses = [];
  352. this._blurXPostprocess?.dispose();
  353. this._blurXPostprocess = null;
  354. this._blurYPostprocess?.dispose();
  355. this._blurYPostprocess = null;
  356. this._upsamplingXPostprocess?.dispose();
  357. this._upsamplingXPostprocess = null;
  358. this._upsamplingYPostprocess?.dispose();
  359. this._upsamplingYPostprocess = null;
  360. for (const ppGlobalIllumination of this._ppGlobalIllumination) {
  361. ppGlobalIllumination.dispose();
  362. }
  363. this._ppGlobalIllumination = [];
  364. if (disposeGeometryBufferRenderer) {
  365. if (this._geomBufferEnabled) {
  366. this._scene.enableGeometryBufferRenderer();
  367. this._scene.geometryBufferRenderer.enablePosition = this._geomBufferEnablePosition;
  368. }
  369. else {
  370. this._scene.disableGeometryBufferRenderer();
  371. }
  372. }
  373. this._counters = [];
  374. this._countersRTW = [];
  375. }
  376. _setPluginParameters() {
  377. if (!this._enable) {
  378. return;
  379. }
  380. this._materialsWithRenderPlugin.forEach((mat) => {
  381. if (mat.pluginManager) {
  382. const plugin = mat.pluginManager.getPlugin(GIRSMRenderPluginMaterial.Name);
  383. plugin.textureGIContrib = this.enableBlur ? this._blurRTT.renderTarget.texture : this._ppGlobalIllumination[0].inputTexture.texture;
  384. plugin.outputTextureWidth = this._outputDimensions.width;
  385. plugin.outputTextureHeight = this._outputDimensions.height;
  386. }
  387. });
  388. }
  389. _createPostProcesses() {
  390. if (!this._enable) {
  391. return;
  392. }
  393. const textureFormat = this._giTextureType === 13 ? 4 : 5;
  394. if (this._firstActivation) {
  395. this._firstActivation = false;
  396. this._geomBufferEnabled = !!this._scene.geometryBufferRenderer;
  397. this._geomBufferEnablePosition = this._scene.geometryBufferRenderer?.enablePosition ?? false;
  398. }
  399. if (!this._geomBufferEnabled) {
  400. this._scene.disableGeometryBufferRenderer();
  401. }
  402. const geometryBufferRenderer = this._scene.enableGeometryBufferRenderer(this._enableBlur ? this._outputDimensions : this._giTextureDimensions, 15, GIRSMManager.GeometryBufferTextureTypesAndFormats);
  403. if (!geometryBufferRenderer) {
  404. throw new Error("Geometry buffer renderer is not supported but is required for GIRSMManager.");
  405. }
  406. geometryBufferRenderer.enablePosition = true;
  407. if (!this._geomBufferEnabled) {
  408. geometryBufferRenderer.generateNormalsInWorldSpace = true;
  409. }
  410. const decodeGeometryBufferNormals = geometryBufferRenderer.normalsAreUnsigned;
  411. const normalsAreInWorldSpace = geometryBufferRenderer.generateNormalsInWorldSpace;
  412. this._counters.push({ name: "Geometry buffer renderer", value: 0 });
  413. this._countersRTW.push([this._scene.geometryBufferRenderer.getGBuffer().renderTarget]);
  414. let defines = "";
  415. if (decodeGeometryBufferNormals) {
  416. defines += "#define DECODE_NORMAL\n";
  417. }
  418. if (!normalsAreInWorldSpace) {
  419. defines += "#define TRANSFORM_NORMAL\n";
  420. }
  421. for (let i = 0; i < this._giRSM.length; ++i) {
  422. const giRSM = this._giRSM[i];
  423. const rsm = giRSM.rsm;
  424. const ppGlobalIllumination = new PostProcess("RSMGlobalIllumination" + i, giRSM.useFullTexture ? "rsmFullGlobalIllumination" : "rsmGlobalIllumination", {
  425. ...this._giTextureDimensions,
  426. uniforms: ["rsmLightMatrix", "rsmInfo", "rsmInfo2", "invView"],
  427. samplers: ["normalSampler", "rsmPositionW", "rsmNormalW", "rsmFlux", "rsmSamples"],
  428. defines,
  429. samplingMode: 2,
  430. engine: this._engine,
  431. textureType: this._giTextureType,
  432. textureFormat,
  433. });
  434. this._ppGlobalIllumination.push(ppGlobalIllumination);
  435. if (i !== 0) {
  436. ppGlobalIllumination.shareOutputWith(this._ppGlobalIllumination[0]);
  437. ppGlobalIllumination.alphaMode = 1;
  438. }
  439. ppGlobalIllumination.autoClear = false;
  440. ppGlobalIllumination.externalTextureSamplerBinding = true;
  441. ppGlobalIllumination.onApplyObservable.add((effect) => {
  442. effect.setTexture("textureSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.POSITION_TEXTURE_TYPE)]);
  443. effect.setTexture("normalSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.NORMAL_TEXTURE_TYPE)]);
  444. effect.setTexture("rsmPositionW", rsm.positionWorldTexture);
  445. effect.setTexture("rsmNormalW", rsm.normalWorldTexture);
  446. effect.setTexture("rsmFlux", rsm.fluxTexture);
  447. effect.setMatrix("rsmLightMatrix", rsm.lightTransformationMatrix);
  448. if (!giRSM.useFullTexture) {
  449. effect.setTexture("rsmSamples", this._sampleTexture);
  450. effect.setFloat4("rsmInfo", giRSM.numSamples, giRSM.radius, giRSM.intensity, giRSM.edgeArtifactCorrection);
  451. effect.setFloat4("rsmInfo2", giRSM.noiseFactor, giRSM.rotateSample ? 1 : 0, rsm.fluxTexture.getInternalTexture().width, rsm.fluxTexture.getInternalTexture().height);
  452. }
  453. else {
  454. effect.setFloat4("rsmInfo", rsm.fluxTexture.getInternalTexture().width, rsm.fluxTexture.getInternalTexture().height, giRSM.intensity, giRSM.edgeArtifactCorrection);
  455. }
  456. if (!normalsAreInWorldSpace) {
  457. this._tempMatrix.copyFrom(this._scene.activeCamera.getViewMatrix());
  458. this._tempMatrix.invert();
  459. effect.setMatrix("invView", this._tempMatrix);
  460. }
  461. });
  462. }
  463. for (const ppGlobalIllumination of this._ppGlobalIllumination) {
  464. if (!ppGlobalIllumination.inputTexture) {
  465. ppGlobalIllumination.resize(this._giTextureDimensions.width, this._giTextureDimensions.height);
  466. }
  467. }
  468. this._counters.push({ name: "GI generation", value: 0 });
  469. this._countersRTW.push([this._ppGlobalIllumination[0].inputTexture]);
  470. if (this._enableBlur) {
  471. const blurTextureSize = this._forceFullSizeBlur ? this._outputDimensions : this._giTextureDimensions;
  472. this._blurRTT = new RenderTargetTexture("GIRSMContribution", this._outputDimensions, this._scene, {
  473. type: this._giTextureType,
  474. format: textureFormat,
  475. generateDepthBuffer: false,
  476. });
  477. this._blurRTT.wrapU = 0;
  478. this._blurRTT.wrapV = 0;
  479. this._blurRTT.updateSamplingMode(1);
  480. this._blurRTT.skipInitialClear = true;
  481. const blurRTWs = [];
  482. this._counters.push({ name: "GI blur", value: 0 });
  483. this._countersRTW.push(blurRTWs);
  484. // Bilateral blur
  485. this._blurXPostprocess = new PostProcess(this._useQualityBlur ? "BilateralBlur" : "BilateralBlurX", this._useQualityBlur ? "bilateralBlurQuality" : "bilateralBlur", {
  486. uniforms: ["filterSize", "blurDir", "depthThreshold", "normalThreshold"],
  487. samplers: ["depthSampler", "normalSampler"],
  488. defines: decodeGeometryBufferNormals ? "#define DECODE_NORMAL" : undefined,
  489. size: blurTextureSize,
  490. samplingMode: 2,
  491. engine: this._engine,
  492. textureType: this._giTextureType,
  493. textureFormat,
  494. });
  495. this._blurXPostprocess.onApplyObservable.add((effect) => {
  496. effect._bindTexture("textureSampler", this._ppGlobalIllumination[0].inputTexture.texture);
  497. effect.setTexture("depthSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.DEPTH_TEXTURE_TYPE)]);
  498. effect.setTexture("normalSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.NORMAL_TEXTURE_TYPE)]);
  499. effect.setInt("filterSize", this.blurKernel);
  500. effect.setFloat2("blurDir", 1 / this._giTextureDimensions.width, this._useQualityBlur ? 1 / this._giTextureDimensions.height : 0);
  501. effect.setFloat("depthThreshold", this.blurDepthThreshold);
  502. effect.setFloat("normalThreshold", this.blurNormalThreshold);
  503. });
  504. this._blurXPostprocess.externalTextureSamplerBinding = true;
  505. this._blurXPostprocess.autoClear = false;
  506. if (!this._useQualityBlur) {
  507. this._blurYPostprocess = new PostProcess("BilateralBlurY", "bilateralBlur", {
  508. uniforms: ["filterSize", "blurDir", "depthThreshold", "normalThreshold"],
  509. samplers: ["depthSampler", "normalSampler"],
  510. defines: decodeGeometryBufferNormals ? "#define DECODE_NORMAL" : undefined,
  511. size: blurTextureSize,
  512. samplingMode: 2,
  513. engine: this._engine,
  514. textureType: this._giTextureType,
  515. textureFormat,
  516. });
  517. this._blurYPostprocess.autoClear = false;
  518. this._blurYPostprocess.onApplyObservable.add((effect) => {
  519. effect.setTexture("depthSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.DEPTH_TEXTURE_TYPE)]);
  520. effect.setTexture("normalSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.NORMAL_TEXTURE_TYPE)]);
  521. effect.setInt("filterSize", this.blurKernel);
  522. effect.setFloat2("blurDir", 0, 1 / this._giTextureDimensions.height);
  523. effect.setFloat("depthThreshold", this.blurDepthThreshold);
  524. effect.setFloat("normalThreshold", this.blurNormalThreshold);
  525. });
  526. this._blurYPostprocess.resize(blurTextureSize.width, blurTextureSize.height);
  527. blurRTWs.push(this._blurYPostprocess.inputTexture);
  528. }
  529. this._blurPostProcesses = [this._blurXPostprocess];
  530. if (this._blurYPostprocess) {
  531. this._blurPostProcesses.push(this._blurYPostprocess);
  532. }
  533. // Bilateral upsampling
  534. const giFullDimensions = this._giTextureDimensions.width >= this._outputDimensions.width && this._giTextureDimensions.height >= this._outputDimensions.height;
  535. if (!giFullDimensions && !this._forceFullSizeBlur) {
  536. const upsamplingRTWs = [];
  537. this._counters.push({ name: "GI upsampling", value: 0 });
  538. this._countersRTW.push(upsamplingRTWs);
  539. this._upsamplingXPostprocess = new PostProcess(this._useQualityUpsampling ? "BilateralUpsampling" : "BilateralUpsamplingX", this._useQualityUpsampling ? "bilateralBlurQuality" : "bilateralBlur", {
  540. uniforms: ["filterSize", "blurDir", "depthThreshold", "normalThreshold"],
  541. samplers: ["depthSampler", "normalSampler"],
  542. defines: decodeGeometryBufferNormals ? "#define DECODE_NORMAL" : undefined,
  543. size: blurTextureSize,
  544. samplingMode: 2,
  545. engine: this._engine,
  546. textureType: this._giTextureType,
  547. textureFormat,
  548. });
  549. this._upsamplingXPostprocess.autoClear = false;
  550. this._upsamplingXPostprocess.onApplyObservable.add((effect) => {
  551. effect.setTexture("depthSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.DEPTH_TEXTURE_TYPE)]);
  552. effect.setTexture("normalSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.NORMAL_TEXTURE_TYPE)]);
  553. effect.setInt("filterSize", this.upsamplerKernel);
  554. effect.setFloat2("blurDir", 1 / this._outputDimensions.width, this._useQualityUpsampling ? 1 / this._outputDimensions.height : 0);
  555. effect.setFloat("depthThreshold", this.blurDepthThreshold);
  556. effect.setFloat("normalThreshold", this.blurNormalThreshold);
  557. });
  558. this._upsamplingXPostprocess.resize(blurTextureSize.width, blurTextureSize.height);
  559. blurRTWs.push(this._upsamplingXPostprocess.inputTexture);
  560. if (!this.useQualityUpsampling) {
  561. this._upsamplingYPostprocess = new PostProcess("BilateralUpsamplingY", "bilateralBlur", {
  562. uniforms: ["filterSize", "blurDir", "depthThreshold", "normalThreshold"],
  563. samplers: ["depthSampler", "normalSampler"],
  564. defines: decodeGeometryBufferNormals ? "#define DECODE_NORMAL" : undefined,
  565. size: this._outputDimensions,
  566. samplingMode: 2,
  567. engine: this._engine,
  568. textureType: this._giTextureType,
  569. textureFormat,
  570. });
  571. this._upsamplingYPostprocess.autoClear = false;
  572. this._upsamplingYPostprocess.onApplyObservable.add((effect) => {
  573. effect.setTexture("depthSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.DEPTH_TEXTURE_TYPE)]);
  574. effect.setTexture("normalSampler", geometryBufferRenderer.getGBuffer().textures[geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.NORMAL_TEXTURE_TYPE)]);
  575. effect.setInt("filterSize", this.upsamplerKernel);
  576. effect.setFloat2("blurDir", 0, 1 / this._outputDimensions.height);
  577. effect.setFloat("depthThreshold", this.blurDepthThreshold);
  578. effect.setFloat("normalThreshold", this.blurNormalThreshold);
  579. });
  580. this._upsamplingYPostprocess.resize(this._outputDimensions.width, this._outputDimensions.height);
  581. upsamplingRTWs.push(this._upsamplingYPostprocess.inputTexture);
  582. }
  583. upsamplingRTWs.push(this._blurRTT.renderTarget);
  584. this._blurPostProcesses.push(this._upsamplingXPostprocess);
  585. if (this._upsamplingYPostprocess) {
  586. this._blurPostProcesses.push(this._upsamplingYPostprocess);
  587. }
  588. }
  589. else {
  590. blurRTWs.push(this._blurRTT.renderTarget);
  591. }
  592. }
  593. this._debugLayer.texture?.dispose();
  594. this._debugLayer.texture = new BaseTexture(this._scene, this._enableBlur ? this._blurRTT.renderTarget.texture : this._ppGlobalIllumination[0].inputTexture.texture);
  595. }
  596. _addGISupportToMaterial(material) {
  597. if (material.pluginManager?.getPlugin(GIRSMRenderPluginMaterial.Name)) {
  598. return;
  599. }
  600. const plugin = new GIRSMRenderPluginMaterial(material);
  601. if (this._enable && this._ppGlobalIllumination.length > 0) {
  602. plugin.textureGIContrib = this._ppGlobalIllumination[0].inputTexture.texture;
  603. plugin.outputTextureWidth = this._outputDimensions.width;
  604. plugin.outputTextureHeight = this._outputDimensions.height;
  605. }
  606. plugin.isEnabled = this._enable;
  607. this._materialsWithRenderPlugin.push(material);
  608. }
  609. }
  610. /**
  611. * Defines the default texture types and formats used by the geometry buffer renderer.
  612. */
  613. GIRSMManager.GeometryBufferTextureTypesAndFormats = {
  614. // eslint-disable-next-line @typescript-eslint/naming-convention
  615. 0: { textureType: 2, textureFormat: 6 },
  616. // eslint-disable-next-line @typescript-eslint/naming-convention
  617. 1: { textureType: 11, textureFormat: 5 },
  618. // eslint-disable-next-line @typescript-eslint/naming-convention
  619. 2: { textureType: 2, textureFormat: 5 }, // position
  620. };
  621. /**
  622. * @internal
  623. */
  624. class MaterialGIRSMRenderDefines extends MaterialDefines {
  625. constructor() {
  626. super(...arguments);
  627. this.RENDER_WITH_GIRSM = false;
  628. this.RSMCREATE_PROJTEXTURE = false;
  629. }
  630. }
  631. /**
  632. * Plugin used to render the global illumination contribution.
  633. */
  634. export class GIRSMRenderPluginMaterial extends MaterialPluginBase {
  635. _markAllSubMeshesAsTexturesDirty() {
  636. this._enable(this._isEnabled);
  637. this._internalMarkAllSubMeshesAsTexturesDirty();
  638. }
  639. constructor(material) {
  640. super(material, GIRSMRenderPluginMaterial.Name, 310, new MaterialGIRSMRenderDefines());
  641. this._isEnabled = false;
  642. /**
  643. * Defines if the plugin is enabled in the material.
  644. */
  645. this.isEnabled = false;
  646. this._internalMarkAllSubMeshesAsTexturesDirty = material._dirtyCallbacks[1];
  647. this._isPBR = material instanceof PBRBaseMaterial;
  648. }
  649. prepareDefines(defines) {
  650. defines.RENDER_WITH_GIRSM = this._isEnabled;
  651. }
  652. getClassName() {
  653. return "GIRSMRenderPluginMaterial";
  654. }
  655. getUniforms() {
  656. return {
  657. ubo: [{ name: "girsmTextureOutputSize", size: 2, type: "vec2" }],
  658. fragment: `#ifdef RENDER_WITH_GIRSM
  659. uniform vec2 girsmTextureOutputSize;
  660. #endif`,
  661. };
  662. }
  663. getSamplers(samplers) {
  664. samplers.push("girsmTextureGIContrib");
  665. }
  666. bindForSubMesh(uniformBuffer) {
  667. if (this._isEnabled) {
  668. uniformBuffer.bindTexture("girsmTextureGIContrib", this.textureGIContrib);
  669. uniformBuffer.updateFloat2("girsmTextureOutputSize", this.outputTextureWidth, this.outputTextureHeight);
  670. }
  671. }
  672. getCustomCode(shaderType) {
  673. const frag = {
  674. // eslint-disable-next-line @typescript-eslint/naming-convention
  675. CUSTOM_FRAGMENT_DEFINITIONS: `
  676. #ifdef RENDER_WITH_GIRSM
  677. uniform sampler2D girsmTextureGIContrib;
  678. vec3 computeIndirect() {
  679. vec2 uv = gl_FragCoord.xy / girsmTextureOutputSize;
  680. return texture2D(girsmTextureGIContrib, uv).rgb;
  681. }
  682. #endif
  683. `,
  684. // eslint-disable-next-line @typescript-eslint/naming-convention
  685. CUSTOM_FRAGMENT_BEFORE_FINALCOLORCOMPOSITION: `
  686. #ifdef RENDER_WITH_GIRSM
  687. finalDiffuse += computeIndirect() * surfaceAlbedo.rgb;
  688. #endif
  689. `,
  690. };
  691. if (!this._isPBR) {
  692. frag["CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR"] = `
  693. #ifdef RENDER_WITH_GIRSM
  694. color.rgb += computeIndirect() * baseColor.rgb;
  695. #endif
  696. `;
  697. }
  698. return shaderType === "vertex" ? null : frag;
  699. }
  700. }
  701. /**
  702. * Defines the name of the plugin.
  703. */
  704. GIRSMRenderPluginMaterial.Name = "GIRSMRender";
  705. __decorate([
  706. serialize()
  707. ], GIRSMRenderPluginMaterial.prototype, "textureGIContrib", void 0);
  708. __decorate([
  709. serialize()
  710. ], GIRSMRenderPluginMaterial.prototype, "outputTextureWidth", void 0);
  711. __decorate([
  712. serialize()
  713. ], GIRSMRenderPluginMaterial.prototype, "outputTextureHeight", void 0);
  714. __decorate([
  715. serialize(),
  716. expandToProperty("_markAllSubMeshesAsTexturesDirty")
  717. ], GIRSMRenderPluginMaterial.prototype, "isEnabled", void 0);
  718. RegisterClass(`BABYLON.GIRSMRenderPluginMaterial`, GIRSMRenderPluginMaterial);
  719. //# sourceMappingURL=giRSMManager.js.map