diff --git a/cocos/gfx/define.ts b/cocos/gfx/define.ts index 493ce2774f..af9bab90af 100644 --- a/cocos/gfx/define.ts +++ b/cocos/gfx/define.ts @@ -779,6 +779,11 @@ export interface IGFXFormatInfo { readonly isCompressed: boolean; } +export interface IGFXMemoryStatus { + bufferSize: number; + textureSize: number; +} + /** * GFX格式信息数组。 */ diff --git a/cocos/gfx/device.ts b/cocos/gfx/device.ts index 49ed7e0cb8..59854a9977 100644 --- a/cocos/gfx/device.ts +++ b/cocos/gfx/device.ts @@ -7,7 +7,7 @@ import { GFXBindingLayout, IGFXBindingLayoutInfo } from './binding-layout'; import { GFXBuffer, IGFXBufferInfo } from './buffer'; import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from './command-allocator'; import { GFXCommandBuffer, IGFXCommandBufferInfo } from './command-buffer'; -import { GFX_MAX_BUFFER_BINDINGS, GFXBufferTextureCopy, GFXFilter, GFXFormat, IGFXRect } from './define'; +import { GFX_MAX_BUFFER_BINDINGS, GFXBufferTextureCopy, GFXFilter, GFXFormat, IGFXRect, IGFXMemoryStatus } from './define'; import { GFXFramebuffer, IGFXFramebufferInfo } from './framebuffer'; import { GFXInputAssembler, IGFXInputAssemblerInfo } from './input-assembler'; import { GFXPipelineLayout, IGFXPipelineLayoutInfo } from './pipeline-layout'; @@ -290,78 +290,49 @@ export abstract class GFXDevice { return this._numTris; } + /** + * @zh + * 内存状态。 + */ + public get memoryStatus (): IGFXMemoryStatus { + return this._memoryStatus; + } + protected _canvas: HTMLCanvasElement | null = null; - protected _canvas2D: HTMLCanvasElement | null = null; - protected _gfxAPI: GFXAPI = GFXAPI.UNKNOWN; protected _deviceName: string = ''; - protected _renderer: string = ''; - protected _vendor: string = ''; - - /** - * @zh - * 驱动版本。 - */ protected _version: string = ''; - - /** - * @zh - * 特性数组。 - */ protected _features: boolean[] = new Array(GFXFeature.COUNT); - protected _queue: GFXQueue | null = null; - protected _devicePixelRatio: number = 1.0; - protected _width: number = 0; - protected _height: number = 0; - protected _nativeWidth: number = 0; - protected _nativeHeight: number = 0; - protected _mainWindow: GFXWindow | null = null; - protected _cmdAllocator: GFXCommandAllocator | null = null; - protected _maxVertexAttributes: number = 0; - protected _maxVertexUniformVectors: number = 0; - protected _maxFragmentUniformVectors: number = 0; - protected _maxTextureUnits: number = 0; - protected _maxVertexTextureUnits: number = 0; - protected _maxUniformBufferBindings: number = GFX_MAX_BUFFER_BINDINGS; - protected _maxUniformBlockSize: number = 0; - protected _depthBits: number = 0; - protected _stencilBits: number = 0; - protected _colorFmt: GFXFormat = GFXFormat.UNKNOWN; - protected _depthStencilFmt: GFXFormat = GFXFormat.UNKNOWN; - - /** - * @zh - * Shader ID 生成标识。 - */ protected _shaderIdGen: number = 0; - protected _macros: Map = new Map(); - protected _numDrawCalls: number = 0; - protected _numTris: number = 0; + protected _memoryStatus: IGFXMemoryStatus = { + bufferSize: 0, + textureSize: 0, + }; /** * @zh diff --git a/cocos/gfx/webgl/webgl-buffer.ts b/cocos/gfx/webgl/webgl-buffer.ts index 56adafa853..b81a9ab16b 100644 --- a/cocos/gfx/webgl/webgl-buffer.ts +++ b/cocos/gfx/webgl/webgl-buffer.ts @@ -58,6 +58,7 @@ export class WebGLGFXBuffer extends GFXBuffer { WebGLCmdFuncCreateBuffer(this._device as WebGLGFXDevice, this._gpuBuffer); + this._device.memoryStatus.bufferSize += this._size; this._status = GFXStatus.SUCCESS; return true; @@ -66,6 +67,7 @@ export class WebGLGFXBuffer extends GFXBuffer { public destroy () { if (this._gpuBuffer) { WebGLCmdFuncDestroyBuffer(this._device as WebGLGFXDevice, this._gpuBuffer); + this._device.memoryStatus.bufferSize -= this._size; this._gpuBuffer = null; } @@ -73,6 +75,7 @@ export class WebGLGFXBuffer extends GFXBuffer { } public resize (size: number) { + const oldSize = this._size; this._size = size; this._count = this._size / this._stride; @@ -84,9 +87,12 @@ export class WebGLGFXBuffer extends GFXBuffer { if (this._uniformBuffer) { this._gpuBuffer.buffer = this._uniformBuffer; } + this._gpuBuffer.size = this._size; if (this._size > 0) { WebGLCmdFuncResizeBuffer(this._device as WebGLGFXDevice, this._gpuBuffer); + this._device.memoryStatus.bufferSize -= oldSize; + this._device.memoryStatus.bufferSize += this._size; } } } diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index f9e932390c..70a2d5ddcb 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -662,7 +662,7 @@ export class WebGLGFXDevice extends GFXDevice { public blitFramebuffer (src: GFXFramebuffer, dst: GFXFramebuffer, srcRect: IGFXRect, dstRect: IGFXRect, filter: GFXFilter) { } - private initStates (gl: gl) { + private initStates (gl: WebGLRenderingContext) { gl.activeTexture(gl.TEXTURE0); gl.pixelStorei(gl.PACK_ALIGNMENT, 1); diff --git a/cocos/gfx/webgl/webgl-texture.ts b/cocos/gfx/webgl/webgl-texture.ts index 32369a8a3e..0d1d591af5 100644 --- a/cocos/gfx/webgl/webgl-texture.ts +++ b/cocos/gfx/webgl/webgl-texture.ts @@ -130,6 +130,7 @@ export class WebGLGFXTexture extends GFXTexture { WebGLCmdFuncCreateTexture(this._device as WebGLGFXDevice, this._gpuTexture); + this._device.memoryStatus.textureSize += this._size; this._status = GFXStatus.SUCCESS; return true; @@ -138,12 +139,14 @@ export class WebGLGFXTexture extends GFXTexture { public destroy () { if (this._gpuTexture) { WebGLCmdFuncDestroyTexture(this._device as WebGLGFXDevice, this._gpuTexture); + this._device.memoryStatus.textureSize -= this._size; this._gpuTexture = null; } this._status = GFXStatus.UNREADY; } public resize (width: number, height: number) { + const oldSize = this._size; this._width = width; this._height = height; this._size = GFXFormatSurfaceSize(this._format, this.width, this.height, @@ -154,6 +157,8 @@ export class WebGLGFXTexture extends GFXTexture { this._gpuTexture.height = this._height; this._gpuTexture.size = this._size; WebGLCmdFuncResizeTexture(this._device as WebGLGFXDevice, this._gpuTexture); + this._device.memoryStatus.textureSize -= oldSize; + this._device.memoryStatus.textureSize += this._size; } this._status = GFXStatus.UNREADY; } diff --git a/cocos/gfx/webgl2/webgl2-buffer.ts b/cocos/gfx/webgl2/webgl2-buffer.ts index e03357dd4a..cea68f8cd4 100644 --- a/cocos/gfx/webgl2/webgl2-buffer.ts +++ b/cocos/gfx/webgl2/webgl2-buffer.ts @@ -53,6 +53,7 @@ export class WebGL2GFXBuffer extends GFXBuffer { WebGL2CmdFuncCreateBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer); + this._device.memoryStatus.bufferSize += this._size; this._status = GFXStatus.SUCCESS; return true; @@ -61,6 +62,7 @@ export class WebGL2GFXBuffer extends GFXBuffer { public destroy () { if (this._gpuBuffer) { WebGL2CmdFuncDestroyBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer); + this._device.memoryStatus.bufferSize -= this._size; this._gpuBuffer = null; } @@ -68,6 +70,7 @@ export class WebGL2GFXBuffer extends GFXBuffer { } public resize (size: number) { + const oldSize = this._size; this._size = size; this._count = this._size / this._stride; @@ -75,6 +78,8 @@ export class WebGL2GFXBuffer extends GFXBuffer { this._gpuBuffer.size = this._size; if (this._size > 0) { WebGL2CmdFuncResizeBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer); + this._device.memoryStatus.bufferSize -= oldSize; + this._device.memoryStatus.bufferSize += this._size; } } } diff --git a/cocos/gfx/webgl2/webgl2-texture.ts b/cocos/gfx/webgl2/webgl2-texture.ts index 2646371d8c..4c5249bb05 100644 --- a/cocos/gfx/webgl2/webgl2-texture.ts +++ b/cocos/gfx/webgl2/webgl2-texture.ts @@ -129,6 +129,7 @@ export class WebGL2GFXTexture extends GFXTexture { WebGL2CmdFuncCreateTexture(this._device as WebGL2GFXDevice, this._gpuTexture); + this._device.memoryStatus.textureSize += this._size; this._status = GFXStatus.SUCCESS; return true; @@ -137,12 +138,14 @@ export class WebGL2GFXTexture extends GFXTexture { public destroy () { if (this._gpuTexture) { WebGL2CmdFuncDestroyTexture(this._device as WebGL2GFXDevice, this._gpuTexture); + this._device.memoryStatus.textureSize -= this._size; this._gpuTexture = null; } this._status = GFXStatus.UNREADY; } public resize (width: number, height: number) { + const oldSize = this._size; this._width = width; this._height = height; this._size = GFXFormatSurfaceSize(this._format, this.width, this.height, @@ -153,6 +156,8 @@ export class WebGL2GFXTexture extends GFXTexture { this._gpuTexture.height = this._height; this._gpuTexture.size = this._size; WebGL2CmdFuncResizeTexture(this._device as WebGL2GFXDevice, this._gpuTexture); + this._device.memoryStatus.textureSize -= oldSize; + this._device.memoryStatus.textureSize += this._size; } this._status = GFXStatus.UNREADY; }