From a60551ceb91bc0c131ab98bcb2f330b0e8400082 Mon Sep 17 00:00:00 2001 From: Sebastian Sanabria Date: Sat, 25 May 2024 16:44:54 -0600 Subject: [PATCH 1/2] Fix to texture size copy error - The `swapChainTexture` is too big to copy to `texture2d.texture` when the screen size changes, that be fit window or full screen, so the texture size is updated just on those instances of a resize call instead of updating it on every frame on `update` that caused the app to slow down if I had taken that route. --- examples/circleblur/index.js | 5 +++++ examples/videotexture1/index.js | 2 +- src/absulit.points.module.js | 24 +++++++++++++++++++----- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/examples/circleblur/index.js b/examples/circleblur/index.js index 2105b52c..00219b83 100644 --- a/examples/circleblur/index.js +++ b/examples/circleblur/index.js @@ -2,11 +2,16 @@ import vert from './vert.js'; import compute from './compute.js'; import frag from './frag.js'; import RenderPass from 'renderpass'; +import Points from 'points'; const circleblur = { renderPasses: [ new RenderPass(null, null, compute, 800, 800, 1), new RenderPass(vert, frag, null, 8, 1, 1) ], + /** + * + * @param {Points} points + */ init: async points => { points.addSampler('feedbackSampler', null); points.addTexture2d('feedbackTexture', true); diff --git a/examples/videotexture1/index.js b/examples/videotexture1/index.js index 2efed551..9e45fd8b 100644 --- a/examples/videotexture1/index.js +++ b/examples/videotexture1/index.js @@ -5,7 +5,7 @@ */ const options = { - scale: .180, + scale: 1, } import vert from './vert.js'; diff --git a/src/absulit.points.module.js b/src/absulit.points.module.js index 832ef2fc..b53e4871 100644 --- a/src/absulit.points.module.js +++ b/src/absulit.points.module.js @@ -190,6 +190,15 @@ export default class Points { format: 'depth24plus', usage: GPUTextureUsage.RENDER_ATTACHMENT, }); + + // this is to solve an issue that requires the texture to be resized + // if the screen dimensions change, this for a `addTexture2d` with + // `copyCurrentTexture` parameter set to `true`. + this._textures2d.forEach(texture2d => { + if (!texture2d.imageTexture && texture2d.texture) { + this._createTextureBindingToCopy(texture2d); + } + }) } /** @private */ @@ -1023,11 +1032,7 @@ export default class Points { texture2d.texture = cubeTexture; } else { - texture2d.texture = this._device.createTexture({ - size: this._presentationSize, - format: this._presentationFormat, // if 'depth24plus' throws error - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, - }); + this._createTextureBindingToCopy(texture2d); } }); //-------------------------------------------- @@ -1046,6 +1051,15 @@ export default class Points { }); } + /** @private */ + _createTextureBindingToCopy(texture2d){ + texture2d.texture = this._device.createTexture({ + size: this._presentationSize, + format: this._presentationFormat, // if 'depth24plus' throws error + usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, + }); + } + /** @private */ _createComputeBindGroup() { this._renderPasses.forEach((renderPass, index) => { From c466f8ff9dfbf794872acbe22d209d89c02d31d3 Mon Sep 17 00:00:00 2001 From: Sebastian Sanabria Date: Sat, 25 May 2024 19:57:28 -0600 Subject: [PATCH 2/2] Enforcing canvas size by default - The fitWindow call is not consistent when you switch a demo, this because the size is set as the current canvas, then it makes no sense to remove the fitWindow if the canvas size is now a new one, so restoring the value to 800x800 default size enforces that. - Then we don't save the boolean in localstorage and we force its value to false in datgui for the check to go back to unchecked state. --- examples/main.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/main.js b/examples/main.js index 167634a3..64dc7305 100644 --- a/examples/main.js +++ b/examples/main.js @@ -38,11 +38,9 @@ document.addEventListener('fullscreenchange', e => { fullscreenCheck.updateDisplay(); }); -let isFitWindow = (localStorage.getItem('fitwindow-enabled') === 'true') || false; -let isFitWindowData = { 'isFitWindow': isFitWindow }; -gui.add(isFitWindowData, 'isFitWindow').name('Fit Window').onChange(value => { +let isFitWindowData = { 'isFitWindow': false }; +gui.add(isFitWindowData, 'isFitWindow').name('Fit Window').listen().onChange(value => { points.fitWindow = value; - localStorage.setItem('fitwindow-enabled', value); }); const shaderProjects = [ @@ -170,8 +168,11 @@ async function init() { if (animationFrameId) { cancelAnimationFrame(animationFrameId); } - + const canvas = document.getElementById('gl-canvas'); + canvas.width = 800; + canvas.height = 800; points = new Points('gl-canvas'); + isFitWindowData.isFitWindow = false; gui.removeFolder(optionsFolder); optionsFolder = gui.addFolder(FOLDER_NAME); @@ -182,7 +183,7 @@ async function init() { await points.init(renderPasses); let hasVertexAndFragmentShader = renderPasses.every(renderPass => renderPass.hasVertexAndFragmentShader) - hasVertexAndFragmentShader && (points.fitWindow = isFitWindow); + hasVertexAndFragmentShader; update(); }