Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor glTFResource #1831

Merged
merged 8 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/core/src/2d/sprite/SpriteMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ export class SpriteMask extends Renderer {
const lastSprite = this._sprite;
if (lastSprite !== value) {
if (lastSprite) {
lastSprite._addReferCount(-1);
this._addResourceReferCount(lastSprite, -1);
lastSprite._updateFlagManager.removeListener(this._onSpriteChange);
}
this._dirtyUpdateFlag |= SpriteMaskUpdateFlags.All;
if (value) {
value._addReferCount(1);
this._addResourceReferCount(value, 1);
value._updateFlagManager.addListener(this._onSpriteChange);
this.shaderData.setTexture(SpriteMask._textureProperty, value.texture);
} else {
Expand Down Expand Up @@ -231,13 +231,14 @@ export class SpriteMask extends Renderer {
* @inheritdoc
*/
protected override _onDestroy(): void {
super._onDestroy();
const sprite = this._sprite;
if (sprite) {
sprite._addReferCount(-1);
this._addResourceReferCount(sprite, -1);
sprite._updateFlagManager.removeListener(this._onSpriteChange);
}
this._entity = null;

super._onDestroy();

this._sprite = null;
this._verticesData = null;
}
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/2d/sprite/SpriteRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ export class SpriteRenderer extends Renderer {
const lastSprite = this._sprite;
if (lastSprite !== value) {
if (lastSprite) {
lastSprite._addReferCount(-1);
this._addResourceReferCount(lastSprite, -1);
lastSprite._updateFlagManager.removeListener(this._onSpriteChange);
}
this._dirtyUpdateFlag |= SpriteRendererUpdateFlags.All;
if (value) {
value._addReferCount(1);
this._addResourceReferCount(value, 1);
value._updateFlagManager.addListener(this._onSpriteChange);
this.shaderData.setTexture(SpriteRenderer._textureProperty, value.texture);
} else {
Expand Down Expand Up @@ -332,12 +332,14 @@ export class SpriteRenderer extends Renderer {
* @internal
*/
protected override _onDestroy(): void {
super._onDestroy();
const sprite = this._sprite;
if (sprite) {
sprite._addReferCount(-1);
this._addResourceReferCount(sprite, -1);
sprite._updateFlagManager.removeListener(this._onSpriteChange);
}

super._onDestroy();

this._entity = null;
this._color = null;
this._sprite = null;
Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/2d/text/TextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export class TextRenderer extends Renderer {
set font(value: Font) {
const lastFont = this._font;
if (lastFont !== value) {
lastFont && lastFont._addReferCount(-1);
value && value._addReferCount(1);
lastFont && this._addResourceReferCount(lastFont, -1);
value && this._addResourceReferCount(value, 1);
this._font = value;
this._setDirtyFlagTrue(DirtyFlag.Font);
}
Expand Down Expand Up @@ -286,14 +286,19 @@ export class TextRenderer extends Renderer {
_init(): void {
const { engine } = this;
this._font = engine._textDefaultFont;
this._font._addReferCount(1);
this._addResourceReferCount(this._font, 1);
this.setMaterial(engine._spriteDefaultMaterial);
}

/**
* @internal
*/
protected override _onDestroy(): void {
if (this._font) {
this._addResourceReferCount(this._font, -1);
this._font = null;
}

super._onDestroy();
// Clear render data.
const charRenderDatas = this._charRenderDatas;
Expand All @@ -302,10 +307,6 @@ export class TextRenderer extends Renderer {
}
charRenderDatas.length = 0;

if (this._font) {
this._font._addReferCount(-1);
this._font = null;
}
this._subFont && (this._subFont = null);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ export class Camera extends Component {

set renderTarget(value: RenderTarget | null) {
if (this._renderTarget !== value) {
value?._addReferCount(1);
this._renderTarget?._addReferCount(-1);
this._renderTarget && this._addResourceReferCount(this._renderTarget, -1);
value && this._addResourceReferCount(value, 1);
this._renderTarget = value;
this._updatePixelViewport();
}
Expand All @@ -331,7 +331,7 @@ export class Camera extends Component {
this._isInvViewProjDirty = transform.registerWorldChangeFlag();
this._frustumViewChangeFlag = transform.registerWorldChangeFlag();
this._renderPipeline = new BasicRenderPipeline(this);
this.shaderData._addReferCount(1);
this._addResourceReferCount(this.shaderData, 1);
this._updatePixelViewport();
}

Expand Down Expand Up @@ -590,7 +590,7 @@ export class Camera extends Component {
this._renderPipeline?.destroy();
this._isInvViewProjDirty.destroy();
this._isViewMatrixDirty.destroy();
this.shaderData._addReferCount(-1);
this._addResourceReferCount(this.shaderData, -1);

this._entity = null;
this._globalShaderMacro = null;
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/Component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IReferable } from "./asset/IReferable";
import { EngineObject } from "./base";
import { assignmentClone, ignoreClone } from "./clone/CloneManager";
import { Entity } from "./Entity";
Expand Down Expand Up @@ -143,6 +144,10 @@ export class Component extends EngineObject {
}
}

protected _addResourceReferCount(resource: IReferable, count: number): void {
this._entity._isTemplate || resource._addReferCount(count);
}

/**
* @internal
*/
Expand Down
30 changes: 21 additions & 9 deletions packages/core/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ export class Entity extends EngineObject {
_isActive: boolean = true;
/** @internal */
_siblingIndex: number = -1;
/** @internal @todo: temporary solution */
_hookResource: ReferResource;

/** @internal */
_isTemplate: boolean = false;

private _templateResource: ReferResource;
private _parent: Entity = null;
private _activeChangedComponents: Component[];

Expand Down Expand Up @@ -398,14 +400,23 @@ export class Entity extends EngineObject {
return cloneEntity;
}

/**
* @internal
*/
_markAsTemplate(templateResource: ReferResource): void {
this._isTemplate = true;
this._templateResource = templateResource;
}

private _createCloneEntity(srcEntity: Entity): Entity {
const cloneEntity = new Entity(srcEntity._engine, srcEntity.name);

const { _hookResource: hookResource } = this;
if (hookResource) {
cloneEntity._hookResource = hookResource;
hookResource._addReferCount(1);
const templateResource = this._templateResource;
if (templateResource) {
cloneEntity._templateResource = templateResource;
templateResource._addReferCount(1);
}

cloneEntity.layer = srcEntity.layer;
cloneEntity._isActive = srcEntity._isActive;
cloneEntity.transform.localMatrix = srcEntity.transform.localMatrix;
Expand Down Expand Up @@ -443,9 +454,10 @@ export class Entity extends EngineObject {
}

super.destroy();
if (this._hookResource) {
this._hookResource._addReferCount(-1);
this._hookResource = null;

if (this._templateResource) {
this._isTemplate || this._templateResource._addReferCount(-1);
this._templateResource = null;
}

const components = this._components;
Expand Down
21 changes: 11 additions & 10 deletions packages/core/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class Renderer extends Component implements IComponentCustomClone {
const shaderData = this.shaderData;
this._overrideUpdate = this.update !== prototype.update;

shaderData._addReferCount(1);
this._addResourceReferCount(this.shaderData, 1);

this._onTransformChanged = this._onTransformChanged.bind(this);
this._registerEntityTransformListener();
Expand Down Expand Up @@ -267,7 +267,7 @@ export class Renderer extends Component implements IComponentCustomClone {

for (let i = count, n = internalMaterials.length; i < n; i++) {
const internalMaterial = internalMaterials[i];
internalMaterial && internalMaterial._addReferCount(-1);
internalMaterial && this._addResourceReferCount(internalMaterial, -1);
}

internalMaterials.length !== count && (internalMaterials.length = count);
Expand All @@ -278,8 +278,8 @@ export class Renderer extends Component implements IComponentCustomClone {
const material = materials[i];
if (internalMaterial !== material) {
internalMaterials[i] = material;
internalMaterial && internalMaterial._addReferCount(-1);
material && material._addReferCount(1);
internalMaterial && this._addResourceReferCount(internalMaterial, -1);
material && this._addResourceReferCount(material, 1);
}
}
}
Expand Down Expand Up @@ -351,11 +351,12 @@ export class Renderer extends Component implements IComponentCustomClone {
super._onDestroy();
this.entity.transform._updateFlagManager.removeListener(this._onTransformChanged);

this.shaderData._addReferCount(-1);
this._addResourceReferCount(this.shaderData, -1);

const materials = this._materials;
for (let i = 0, n = materials.length; i < n; i++) {
materials[i]?._addReferCount(-1);
const material = materials[i];
material && this._addResourceReferCount(material, -1);
}

this._entity = null;
Expand Down Expand Up @@ -434,8 +435,8 @@ export class Renderer extends Component implements IComponentCustomClone {
private _createInstanceMaterial(material: Material, index: number): Material {
const insMaterial: Material = material.clone();
insMaterial.name = insMaterial.name + "(Instance)";
material._addReferCount(-1);
insMaterial._addReferCount(1);
this._addResourceReferCount(material, -1);
this._addResourceReferCount(insMaterial, 1);
this._materialsInstanced[index] = true;
this._materials[index] = insMaterial;
return insMaterial;
Expand All @@ -452,8 +453,8 @@ export class Renderer extends Component implements IComponentCustomClone {
const materialsInstance = this._materialsInstanced;
index < materialsInstance.length && (materialsInstance[index] = false);

internalMaterial && internalMaterial._addReferCount(-1);
material && material._addReferCount(1);
internalMaterial && this._addResourceReferCount(internalMaterial, -1);
material && this._addResourceReferCount(material, 1);
materials[index] = material;
}
}
Expand Down
50 changes: 45 additions & 5 deletions packages/core/src/asset/ReferResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export abstract class ReferResource extends EngineObject implements IReferable {
isGCIgnored: boolean = false;

private _refCount: number = 0;
private _superResources: ReferResource[] = null;

/**
* Counted by valid references.
Expand All @@ -25,17 +26,56 @@ export abstract class ReferResource extends EngineObject implements IReferable {

/**
* Destroy self.
* @param force - Whether to force the destruction, if it is false, refCount = 0 can be released successfully.
* @returns Whether the release was successful.
* @param force - Whether to force the destruction, if it is false, refCount = 0 can be released successfully
* @returns Whether the release was successful
*/
override destroy(force: boolean = false): boolean {
if (!force && this._refCount !== 0) {
return false;
override destroy(force?: boolean): boolean;

/**
* @internal
*/
override destroy(force: boolean, isGC: boolean): boolean;
cptbtptpbcptdtptp marked this conversation as resolved.
Show resolved Hide resolved

override destroy(force: boolean = false, isGC?: boolean): boolean {
if (!force) {
if (this._refCount !== 0) {
return false;
}

const superResources = this._superResources;
if (superResources?.length) {
if (isGC) {
for (let i = 0, n = superResources.length; i < n; i++) {
if (superResources[i].refCount > 0) {
return false;
}
}
} else {
return false;
}
}
}

super.destroy();
return true;
}

/**
* @internal
*/
_associationSuperResource(superResource: ReferResource): void {
(this._superResources ||= []).push(superResource);
}

/**
* @internal
*/
_disassociationSuperResource(superResource: ReferResource): void {
const superResources = this._superResources;
const index = superResources.indexOf(superResource);
superResources.splice(index, 1);
}

/**
* @internal
*/
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/asset/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,11 @@ export class ResourceManager {
}

private _gc(forceDestroy: boolean): void {
const objects = Utils.objectValues(this._referResourcePool);
for (let i = 0, len = objects.length; i < len; i++) {
if (!objects[i].isGCIgnored || forceDestroy) {
(<ReferResource>objects[i]).destroy(forceDestroy);
const objects = <ReferResource[]>Utils.objectValues(this._referResourcePool);
for (let i = 0, n = objects.length; i < n; i++) {
const object = objects[i];
if (!object.isGCIgnored || forceDestroy) {
object.destroy(forceDestroy, true);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/mesh/MeshRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ export class MeshRenderer extends Renderer {
* @internal
*/
protected override _onDestroy(): void {
super._onDestroy();
const mesh = this._mesh;
if (mesh) {
mesh.destroyed || mesh._addReferCount(-1);
mesh.destroyed || this._addResourceReferCount(mesh, -1);
mesh._updateFlagManager.removeListener(this._onMeshChanged);
this._mesh = null;
}

super._onDestroy();
}

/**
Expand Down Expand Up @@ -161,11 +162,11 @@ export class MeshRenderer extends Renderer {
private _setMesh(mesh: Mesh): void {
const lastMesh = this._mesh;
if (lastMesh) {
lastMesh._addReferCount(-1);
this._addResourceReferCount(lastMesh, -1);
lastMesh._updateFlagManager.removeListener(this._onMeshChanged);
}
if (mesh) {
mesh._addReferCount(1);
this._addResourceReferCount(mesh, 1);
mesh._updateFlagManager.addListener(this._onMeshChanged);
this._dirtyUpdateFlag |= MeshRendererUpdateFlags.All;
}
Expand Down
Loading