diff --git a/cocos/renderer/core/texture-buffer-pool.ts b/cocos/renderer/core/texture-buffer-pool.ts index 143b06d02d..e8922f9084 100644 --- a/cocos/renderer/core/texture-buffer-pool.ts +++ b/cocos/renderer/core/texture-buffer-pool.ts @@ -35,6 +35,13 @@ export interface ITextureBufferHandle { texView: GFXTextureView; } +export interface ITextureBufferPoolInfo { + format: GFXFormat; // target texture format + maxChunks: number; // maximum number of textures to allocate until exception + inOrderFree?: boolean; // will the handles be freed exactly in the order of their allocation? + roundUpFn?: (size: number) => number; // given a target size, how will the actual texture size round up? +} + export class TextureBufferPool { private _device: GFXDevice; @@ -47,6 +54,7 @@ export class TextureBufferPool { private _region1: GFXBufferTextureCopy = new GFXBufferTextureCopy(); private _region2: GFXBufferTextureCopy = new GFXBufferTextureCopy(); private _roundUpFn: ((size: number) => number) | null = null; + private _inOrderFree = false; get formatSize () { return this._formatSize; @@ -56,11 +64,13 @@ export class TextureBufferPool { this._device = device; } - public initialize (format: GFXFormat, maxChunks: number, roundUpFn?: (size: number) => number): boolean { - this._format = format; + public initialize (info: ITextureBufferPoolInfo): boolean { + + this._format = info.format; this._formatSize = GFXFormatInfos[this._format].size; - this._chunks = new Array(maxChunks); - if (roundUpFn) { this._roundUpFn = roundUpFn; } + this._chunks = new Array(info.maxChunks); + this._roundUpFn = info.roundUpFn || null; + this._inOrderFree = info.inOrderFree || false; return true; } @@ -86,7 +96,21 @@ export class TextureBufferPool { let start = chunk.start; if ((start + size) <= chunk.end) { isFound = true; - } else if (start > chunk.end) { + } else if (!this._inOrderFree) { + start = 0; // try to find from head again + const handles = this._handles.filter((h) => h.chunkIdx === i).sort((a, b) => a.start - b.start); + for (let j = 0; j < handles.length; j++) { + const handle = handles[j]; + if ((start + size) <= handle.start) { + isFound = true; + break; + } + start = handle.end; + } + if (!isFound && (start + size) <= chunk.size) { + isFound = true; + } + } else if (start > chunk.end) { // [McDonald 12] Efficient Buffer Management if ((start + size) <= chunk.size) { isFound = true; } else if (size <= chunk.end) { @@ -169,6 +193,7 @@ export class TextureBufferPool { public free (handle: ITextureBufferHandle) { for (let i = 0; i < this._handles.length; ++i) { if (this._handles[i] === handle) { + this._chunks[handle.chunkIdx].end = handle.end; this._handles.splice(i, 1); return; } diff --git a/cocos/renderer/models/joints-texture-utils.ts b/cocos/renderer/models/joints-texture-utils.ts index 93153a2d03..dad1debbb8 100644 --- a/cocos/renderer/models/joints-texture-utils.ts +++ b/cocos/renderer/models/joints-texture-utils.ts @@ -98,7 +98,7 @@ function uploadJointDataDQS (out: Float32Array, base: number, pos: Vec3, rot: Qu } function roundUpTextureSize (targetLength: number) { - return Math.max(1020, Math.ceil(targetLength / 12) * 12); + return Math.max(180, Math.ceil(targetLength / 12) * 12); } const _jointsFormat = { @@ -124,7 +124,12 @@ export class JointsTexturePool { public initialize (maxChunks: number = 16) { const format = _jointsFormat[selectJointsMediumType(this._device)]; const scale = 16 / GFXFormatInfos[format].size; - this._pool.initialize(format, maxChunks * scale, roundUpTextureSize); + this._pool.initialize({ + format, + maxChunks: maxChunks * scale, + inOrderFree: false, + roundUpFn: roundUpTextureSize + }); } public destroy () { diff --git a/cocos/renderer/models/skinning-model.ts b/cocos/renderer/models/skinning-model.ts index 2313096eb7..c9ac454442 100644 --- a/cocos/renderer/models/skinning-model.ts +++ b/cocos/renderer/models/skinning-model.ts @@ -111,7 +111,7 @@ export class SkinningModel extends Model { this._jointsMedium.texture = texture; const { buffer, jointsTextureInfo } = this._jointsMedium; jointsTextureInfo[0] = texture.texture.width; - jointsTextureInfo[1] = this._scene.texturePool.bytesToPixels(texture.start); + jointsTextureInfo[1] = this._scene.texturePool.bytesToPixels(texture.start) + 0.1; // guard against floor() underflow jointsTextureInfo[2] = this.uploadedAnim ? this.uploadedAnim.keys[0].length : 1; jointsTextureInfo[3] = 0; // restore fid if (buffer) { buffer.update(jointsTextureInfo); }