mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
wgpu: remove reinit on same object (#12799)
This commit is contained in:
@@ -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<T> (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']);
|
||||
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<ClearFlags, RenderPass>();
|
||||
protected _renderPasses = new Map<number, RenderPass>();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
"type": "github",
|
||||
"owner": "cocos-creator",
|
||||
"name": "engine-native-external",
|
||||
"checkout": "v3.6.1-6"
|
||||
"checkout": "v3.6.2-1"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user