From f730a810a130e5459fbcf41e133c36c783d7eeae Mon Sep 17 00:00:00 2001 From: Zeqiang Li Date: Tue, 27 Sep 2022 10:33:51 +0800 Subject: [PATCH] wgpu: remove reinit on same object (#12799) --- cocos/core/gfx/webgpu/webgpu-define.ts | 38 ++++++++++++++- .../pipeline/deferred/deferred-pipeline.ts | 4 +- cocos/core/pipeline/instanced-buffer.ts | 4 +- cocos/core/pipeline/render-pipeline.ts | 46 ++++++++++++++----- cocos/core/renderer/core/render-window.ts | 5 +- cocos/core/renderer/scene/submodel.ts | 2 +- native/external-config.json | 4 +- 7 files changed, 83 insertions(+), 20 deletions(-) diff --git a/cocos/core/gfx/webgpu/webgpu-define.ts b/cocos/core/gfx/webgpu/webgpu-define.ts index b10b631264..1924b9caf9 100644 --- a/cocos/core/gfx/webgpu/webgpu-define.ts +++ b/cocos/core/gfx/webgpu/webgpu-define.ts @@ -30,7 +30,7 @@ import { WEBGPU } from 'internal:constants'; import { gfx, webgpuAdapter, glslalgWasmModule, promiseForWebGPUInstantiation } from '../../../webgpu/instantiated'; import { - Texture, CommandBuffer, DescriptorSet, Device, InputAssembler, Buffer, + Texture, CommandBuffer, DescriptorSet, Device, InputAssembler, Buffer, Shader } from './override'; import { DeviceInfo, BufferTextureCopy, ShaderInfo, ShaderStageFlagBit, TextureViewInfo, TextureInfo, DrawInfo, BufferViewInfo, BufferInfo, @@ -558,4 +558,40 @@ WEBGPU && promiseForWebGPUInstantiation.then(() => { const shader = this.createShaderNative(shaderInfo, spvDatas); return shader; }; + + // if property being frequently get in TS, try cache it + // attention: invalid if got this object from a native object, + // eg. inputAssembler.indexBuffer.objectID + function cacheReadOnlyWGPUProperties (type: T, props: string[]) { + const descriptor = { writable: true }; + props.map((prop) => { + return Object.defineProperty(type['prototype'], `_${prop}`, descriptor); + }); + + // trick for emscripten object only, which contains a `name` indicates what type it is. + const typename = type['name'].replace('CCWGPU', ''); + const oldCreate = Device.prototype[`create${typename}`]; + Device.prototype[`create${typename}`] = function (info) { + const res = oldCreate.call(this, info); + for (let prop of props) { + res[`_${prop}`] = res[`${prop}`]; + Object.defineProperty(res, `${prop}`, { + get () { + return this[`_${prop}`]; + } + }); + } + return res; + } + const oldInit = type['prototype']['initialize']; + type['prototype']['initialize'] = function (info) { + oldInit.call(this, info); + for (let prop of props) { + this[`_${prop}`] = this[`${prop}`]; + } + } + }; + + cacheReadOnlyWGPUProperties(Buffer, ['objectID']); + }); diff --git a/cocos/core/pipeline/deferred/deferred-pipeline.ts b/cocos/core/pipeline/deferred/deferred-pipeline.ts index 97b3d304c9..af815c6319 100644 --- a/cocos/core/pipeline/deferred/deferred-pipeline.ts +++ b/cocos/core/pipeline/deferred/deferred-pipeline.ts @@ -316,8 +316,8 @@ export class DeferredPipeline extends RenderPipeline { // Listens when the attachment texture is scaled this.on(PipelineEventType.ATTACHMENT_SCALE_CAHNGED, (val: number) => { data.sampler = val < 1 ? this.globalDSManager.pointSampler : this.globalDSManager.linearSampler; - this.applyFramebufferRatio(data.gbufferFrameBuffer); - this.applyFramebufferRatio(data.outputFrameBuffer); + data.gbufferFrameBuffer = this.newFramebufferByRatio(data.gbufferFrameBuffer); + data.gbufferFrameBuffer = this.newFramebufferByRatio(data.outputFrameBuffer); }); } } diff --git a/cocos/core/pipeline/instanced-buffer.ts b/cocos/core/pipeline/instanced-buffer.ts index ea0b7ae308..6b135eb676 100644 --- a/cocos/core/pipeline/instanced-buffer.ts +++ b/cocos/core/pipeline/instanced-buffer.ts @@ -98,8 +98,8 @@ export class InstancedBuffer { instance.data.set(oldData); instance.vb.resize(newSize); } - if (instance.shader?.objectID !== shader.objectID) { instance.shader = shader; } - if (instance.descriptorSet.objectID !== descriptorSet.objectID) { instance.descriptorSet = descriptorSet; } + instance.shader = shader; + instance.descriptorSet = descriptorSet; instance.data.set(attrs.buffer, instance.stride * instance.count++); this.hasPendingModels = true; return; diff --git a/cocos/core/pipeline/render-pipeline.ts b/cocos/core/pipeline/render-pipeline.ts index 360cce60a3..ec8abc0521 100644 --- a/cocos/core/pipeline/render-pipeline.ts +++ b/cocos/core/pipeline/render-pipeline.ts @@ -49,6 +49,7 @@ import { OS } from '../../../pal/system-info/enum-type'; import { macro } from '../platform/macro'; import { UBOSkinning } from './define'; import { PipelineRuntime } from './custom/pipeline'; +import { murmurhash2_32_gc } from '../utils'; /** * @en Render pipeline information descriptor @@ -95,6 +96,25 @@ export class PipelineInputAssemblerData { quadIA: InputAssembler|null = null; } +function hashFrameBuffer (fbo: Framebuffer) { + let hash = 666; + for (const color of fbo.colorTextures) { + const info = color?.info; + const hashStr = `${info!.type}_${info!.usage}_${info!.format}_${info!.width}_${info!.height}_${info!.flags}_ + ${info!.layerCount}_${info!.levelCount}_${info!.samples}_${info!.depth}_${info!.externalRes}`; + hash = murmurhash2_32_gc(hashStr, hash); + } + + if (fbo.depthStencilTexture) { + const info = fbo.depthStencilTexture.info; + const hashStr = `${info.type}_${info.usage}_${info.format}_${info.width}_${info.height}_${info.flags}_ + ${info.layerCount}_${info.levelCount}_${info.samples}_${info.depth}_${info.externalRes}`; + hash = murmurhash2_32_gc(hashStr, hash); + } + + return hash; +} + /** * @en Render pipeline describes how we handle the rendering process for all render objects in the related render scene root. * It contains some general pipeline configurations, necessary rendering resources and some [[RenderFlow]]s. @@ -257,7 +277,7 @@ export abstract class RenderPipeline extends Asset implements IPipelineEvent, Pi protected _geometryRenderer: GeometryRenderer | null = null; protected declare _pipelineSceneData: PipelineSceneData; protected _pipelineRenderData: PipelineRenderData | null = null; - protected _renderPasses = new Map(); + protected _renderPasses = new Map(); protected _width = 0; protected _height = 0; protected _lastUsedRenderArea: Rect = new Rect(); @@ -311,30 +331,34 @@ export abstract class RenderPipeline extends Asset implements IPipelineEvent, Pi } public getRenderPass (clearFlags: ClearFlags, fbo: Framebuffer): RenderPass { - let renderPass = this._renderPasses.get(clearFlags); + const fbHash = hashFrameBuffer(fbo); + const hash = murmurhash2_32_gc(`${fbHash}_${clearFlags}`, 666); + let renderPass = this._renderPasses.get(hash); if (renderPass) { return renderPass; } renderPass = this.createRenderPass(clearFlags, fbo.colorTextures[0]!.format, fbo.depthStencilTexture!.format); - this._renderPasses.set(clearFlags, renderPass); + this._renderPasses.set(hash, renderPass); return renderPass; } - public applyFramebufferRatio (framebuffer: Framebuffer) { + public newFramebufferByRatio (dyingFramebuffer: Framebuffer) { const sceneData = this.pipelineSceneData; const width = this._width * sceneData.shadingScale; const height = this._height * sceneData.shadingScale; - const colorTexArr: any = framebuffer.colorTextures; + const colorTexArr: any = dyingFramebuffer.colorTextures; for (let i = 0; i < colorTexArr.length; i++) { colorTexArr[i]!.resize(width, height); } - if (framebuffer.depthStencilTexture) { - framebuffer.depthStencilTexture.resize(width, height); + if (dyingFramebuffer.depthStencilTexture) { + dyingFramebuffer.depthStencilTexture.resize(width, height); } - framebuffer.destroy(); - framebuffer.initialize(new FramebufferInfo( - framebuffer.renderPass, + // move + const newFramebuffer = this._device.createFramebuffer(new FramebufferInfo( + dyingFramebuffer.renderPass, colorTexArr, - framebuffer.depthStencilTexture, + dyingFramebuffer.depthStencilTexture, )); + dyingFramebuffer.destroy(); + return newFramebuffer; } /** diff --git a/cocos/core/renderer/core/render-window.ts b/cocos/core/renderer/core/render-window.ts index bd8b7d1872..2f05af1f56 100644 --- a/cocos/core/renderer/core/render-window.ts +++ b/cocos/core/renderer/core/render-window.ts @@ -101,6 +101,7 @@ export class RenderWindow { protected _hasOnScreenAttachments = false; protected _hasOffScreenAttachments = false; protected _framebuffer: Framebuffer | null = null; + protected _device: Device | null = null; /** * @private @@ -122,6 +123,7 @@ export class RenderWindow { this._width = info.width; this._height = info.height; + this._device = device; this._renderPass = device.createRenderPass(info.renderPassInfo); if (info.swapchain) { @@ -178,6 +180,7 @@ export class RenderWindow { } } this._colorTextures.length = 0; + this._device = null; } /** @@ -204,7 +207,7 @@ export class RenderWindow { if (this.framebuffer) { this.framebuffer.destroy(); - this.framebuffer.initialize(new FramebufferInfo( + this._framebuffer = this._device!.createFramebuffer(new FramebufferInfo( this._renderPass!, this._colorTextures, this._depthStencilTexture, diff --git a/cocos/core/renderer/scene/submodel.ts b/cocos/core/renderer/scene/submodel.ts index b4ef6928f4..7bae2d5eb7 100644 --- a/cocos/core/renderer/scene/submodel.ts +++ b/cocos/core/renderer/scene/submodel.ts @@ -115,7 +115,7 @@ export class SubModel { */ set subMesh (subMesh) { this._inputAssembler!.destroy(); - this._inputAssembler!.initialize(subMesh.iaInfo); + this._inputAssembler = this._device!.createInputAssembler(subMesh.iaInfo); if (this._passes![0].batchingScheme === BatchingSchemes.VB_MERGING) { this.subMesh.genFlatBuffers(); } this._subMesh = subMesh; } diff --git a/native/external-config.json b/native/external-config.json index a255896ae8..56168b4f6d 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos-creator", "name": "engine-native-external", - "checkout": "v3.6.1-6" + "checkout": "v3.6.2-1" } -} +} \ No newline at end of file