From f3ac9b87a69a83992ec33af25e9c7ef4721ae820 Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Wed, 3 Apr 2019 09:42:36 +0800 Subject: [PATCH] fix shading scale --- cocos/gfx/command-buffer.ts | 12 ++- cocos/gfx/webgl/webgl-device.ts | 2 +- cocos/gfx/webgl2/webgl2-command-buffer.ts | 109 ++++++++++++---------- cocos/gfx/webgl2/webgl2-device.ts | 2 +- cocos/pipeline/render-pipeline.ts | 49 +++++----- 5 files changed, 101 insertions(+), 73 deletions(-) diff --git a/cocos/gfx/command-buffer.ts b/cocos/gfx/command-buffer.ts index 4b9b883d2f..966f713c8c 100644 --- a/cocos/gfx/command-buffer.ts +++ b/cocos/gfx/command-buffer.ts @@ -49,13 +49,23 @@ export interface IGFXStencilCompareMask { // tslint:disable: max-line-length export abstract class GFXCommandBuffer extends GFXObject { - public get type (): number { + public get type (): GFXCommandBufferType { return this._type; } + public get numDrawCalls (): number { + return this._numDrawCalls; + } + + public get numTris (): number { + return this._numTris; + } + protected _device: GFXDevice; protected _allocator: GFXCommandAllocator | null = null; protected _type: GFXCommandBufferType = GFXCommandBufferType.PRIMARY; + protected _numDrawCalls: number = 0; + protected _numTris: number = 0; constructor (device: GFXDevice) { super(GFXObjectType.COMMAND_BUFFER); diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index 429b7d251b..2026cd61d2 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -434,7 +434,7 @@ export class WebGLGFXDevice extends GFXDevice { public resize (width: number, height: number) { if (this._width !== width || this._height !== height) { - console.log('RESIZING DEVICE: ' + width + 'x' + height); + console.info('RESIZING DEVICE: ' + width + 'x' + height); this._canvas!.width = width; this._canvas!.height = height; this._width = width; diff --git a/cocos/gfx/webgl2/webgl2-command-buffer.ts b/cocos/gfx/webgl2/webgl2-command-buffer.ts index 74028d5482..6e4cd111db 100644 --- a/cocos/gfx/webgl2/webgl2-command-buffer.ts +++ b/cocos/gfx/webgl2/webgl2-command-buffer.ts @@ -117,6 +117,8 @@ export class WebGL2GFXCommandBuffer extends GFXCommandBuffer { this._curDepthBounds = null; this._curStencilWriteMask = null; this._curStencilCompareMask = null; + this._numDrawCalls = 0; + this._numTris = 0; } public end () { @@ -340,11 +342,25 @@ export class WebGL2GFXCommandBuffer extends GFXCommandBuffer { const cmd = ( this._allocator as WebGL2GFXCommandAllocator). drawCmdPool.alloc(WebGL2CmdDraw); - if (cmd) { - (inputAssembler as WebGL2GFXInputAssembler).extractCmdDraw(cmd); - this.cmdPackage.drawCmds.push(cmd); + (inputAssembler as WebGL2GFXInputAssembler).extractCmdDraw(cmd); + this.cmdPackage.drawCmds.push(cmd); - this.cmdPackage.cmds.push(WebGL2Cmd.DRAW); + this.cmdPackage.cmds.push(WebGL2Cmd.DRAW); + + ++this._numDrawCalls; + if (this._curGPUPipelineState) { + const glPrimitive = this._curGPUPipelineState.glPrimitive; + switch (glPrimitive) { + case WebGL2RenderingContext.TRIANGLES: { + this._numTris += inputAssembler.indexCount / 3 * inputAssembler.instanceCount; + break; + } + case WebGL2RenderingContext.TRIANGLE_STRIP: + case WebGL2RenderingContext.TRIANGLE_FAN: { + this._numTris += (inputAssembler.indexCount - 2) * inputAssembler.instanceCount; + break; + } + } } } else { console.error('Command \'draw\' must be recorded inside a render pass.'); @@ -357,27 +373,24 @@ export class WebGL2GFXCommandBuffer extends GFXCommandBuffer { const gpuBuffer = (buffer as WebGL2GFXBuffer).gpuBuffer; if (gpuBuffer) { const cmd = this._webGLAllocator!.updateBufferCmdPool.alloc(WebGL2CmdUpdateBuffer); - if (cmd) { - - let buffSize; - if (size !== undefined ) { - buffSize = size; - } else if (buffer.usage & GFXBufferUsageBit.INDIRECT) { - buffSize = 0; - } else { - buffSize = (data as ArrayBuffer).byteLength; - } - - const buff = data as ArrayBuffer; - - cmd.gpuBuffer = gpuBuffer; - cmd.buffer = buff; - cmd.offset = (offset !== undefined ? offset : 0); - cmd.size = buffSize; - this.cmdPackage.updateBufferCmds.push(cmd); - - this.cmdPackage.cmds.push(WebGL2Cmd.UPDATE_BUFFER); + let buffSize; + if (size !== undefined ) { + buffSize = size; + } else if (buffer.usage & GFXBufferUsageBit.INDIRECT) { + buffSize = 0; + } else { + buffSize = (data as ArrayBuffer).byteLength; } + + const buff = data as ArrayBuffer; + + cmd.gpuBuffer = gpuBuffer; + cmd.buffer = buff; + cmd.offset = (offset !== undefined ? offset : 0); + cmd.size = buffSize; + this.cmdPackage.updateBufferCmds.push(cmd); + + this.cmdPackage.cmds.push(WebGL2Cmd.UPDATE_BUFFER); } } else { console.error('Command \'updateBuffer\' must be recorded outside a render pass.'); @@ -396,15 +409,13 @@ export class WebGL2GFXCommandBuffer extends GFXCommandBuffer { const gpuTexture = ( dstTex as WebGL2GFXTexture).gpuTexture; if (gpuBuffer && gpuTexture) { const cmd = this._webGLAllocator!.copyBufferToTextureCmdPool.alloc(WebGL2CmdCopyBufferToTexture); - if (cmd) { - cmd.gpuBuffer = gpuBuffer; - cmd.gpuTexture = gpuTexture; - cmd.dstLayout = dstLayout; - cmd.regions = regions; - this.cmdPackage.copyBufferToTextureCmds.push(cmd); + cmd.gpuBuffer = gpuBuffer; + cmd.gpuTexture = gpuTexture; + cmd.dstLayout = dstLayout; + cmd.regions = regions; + this.cmdPackage.copyBufferToTextureCmds.push(cmd); - this.cmdPackage.cmds.push(WebGL2Cmd.COPY_BUFFER_TO_TEXTURE); - } + this.cmdPackage.cmds.push(WebGL2Cmd.COPY_BUFFER_TO_TEXTURE); } } else { console.error('Command \'copyBufferToTexture\' must be recorded outside a render pass.'); @@ -448,6 +459,9 @@ export class WebGL2GFXCommandBuffer extends GFXCommandBuffer { } this.cmdPackage.cmds.concat(webGLCmdBuff.cmdPackage.cmds); + + this._numDrawCalls += webGLCmdBuff._numDrawCalls; + this._numTris += webGLCmdBuff._numTris; } } @@ -457,24 +471,21 @@ export class WebGL2GFXCommandBuffer extends GFXCommandBuffer { private bindStates () { const bindStatesCmd = this._webGLAllocator!.bindStatesCmdPool.alloc(WebGL2CmdBindStates); + bindStatesCmd.gpuPipelineState = this._curGPUPipelineState; + bindStatesCmd.gpuBindingLayout = this._curGPUBindingLayout; + bindStatesCmd.gpuInputAssembler = this._curGPUInputAssembler; + bindStatesCmd.viewport = this._curViewport; + bindStatesCmd.scissor = this._curScissor; + bindStatesCmd.lineWidth = this._curLineWidth; + bindStatesCmd.depthBias = this._curDepthBias; + bindStatesCmd.blendConstants = this._curBlendConstants; + bindStatesCmd.depthBounds = this._curDepthBounds; + bindStatesCmd.stencilWriteMask = this._curStencilWriteMask; + bindStatesCmd.stencilCompareMask = this._curStencilCompareMask; - if (bindStatesCmd) { - bindStatesCmd.gpuPipelineState = this._curGPUPipelineState; - bindStatesCmd.gpuBindingLayout = this._curGPUBindingLayout; - bindStatesCmd.gpuInputAssembler = this._curGPUInputAssembler; - bindStatesCmd.viewport = this._curViewport; - bindStatesCmd.scissor = this._curScissor; - bindStatesCmd.lineWidth = this._curLineWidth; - bindStatesCmd.depthBias = this._curDepthBias; - bindStatesCmd.blendConstants = this._curBlendConstants; - bindStatesCmd.depthBounds = this._curDepthBounds; - bindStatesCmd.stencilWriteMask = this._curStencilWriteMask; - bindStatesCmd.stencilCompareMask = this._curStencilCompareMask; + this.cmdPackage.bindStatesCmds.push(bindStatesCmd); + this.cmdPackage.cmds.push(WebGL2Cmd.BIND_STATES); - this.cmdPackage.bindStatesCmds.push(bindStatesCmd); - this.cmdPackage.cmds.push(WebGL2Cmd.BIND_STATES); - - this._isStateInvalied = false; - } + this._isStateInvalied = false; } } diff --git a/cocos/gfx/webgl2/webgl2-device.ts b/cocos/gfx/webgl2/webgl2-device.ts index e99a67f793..d8b287d905 100644 --- a/cocos/gfx/webgl2/webgl2-device.ts +++ b/cocos/gfx/webgl2/webgl2-device.ts @@ -359,7 +359,7 @@ export class WebGL2GFXDevice extends GFXDevice { public resize (width: number, height: number) { if (this._width !== width || this._height !== height) { - console.log('RESIZING DEVICE: ' + width + 'x' + height); + console.info('RESIZING DEVICE: ' + width + 'x' + height); this._canvas!.width = width; this._canvas!.height = height; this._width = width; diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 31d6ca702e..029a1f01d7 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -178,8 +178,8 @@ export abstract class RenderPipeline { protected _depthStencilTex: GFXTexture | null = null; protected _depthStencilTexView: GFXTextureView | null = null; protected _shadingFBOs: GFXFramebuffer[] = []; - protected _shadingTexWidth: number = 0.0; - protected _shadingTexHeight: number = 0.0; + protected _shadingWidth: number = 0.0; + protected _shadingHeight: number = 0.0; protected _shadingScale: number = 1.0; protected _curIdx: number = 0; protected _prevIdx: number = 1; @@ -214,6 +214,13 @@ export abstract class RenderPipeline { public rebuild () { this.updateMacros(); } public resize (width: number, height: number) { + + if (width * this._shadingScale > this._shadingWidth || + height * this._shadingScale > this._shadingHeight) { + this._shadingScale = Math.min(this._shadingWidth / width, this._shadingHeight / height); + console.info('SHADING_SCALE: ' + this._shadingScale); + } + for (const flow of this._flows) { flow.resize(width, height); } @@ -351,13 +358,13 @@ export abstract class RenderPipeline { // colorFmt = GFXFormat.RGBA16F; this._shadingScale = this._device.devicePixelRatio; - this._shadingTexWidth = this._device.nativeWidth; - this._shadingTexHeight = this._device.nativeHeight; + this._shadingWidth = this._device.nativeWidth; + this._shadingHeight = this._device.nativeHeight; console.info('USE_MSAA: ' + this._useMSAA); console.info('USE_SMAA: ' + this._useSMAA); console.info('CC_USE_HDR: ' + this._isHDR); - console.info('SHADING_SIZE: ' + this._shadingTexWidth + ' x ' + this._shadingTexHeight); + console.info('SHADING_SIZE: ' + this._shadingWidth + ' x ' + this._shadingHeight); console.info('SCREEN_SCALE: ' + this._shadingScale.toFixed(2)); console.info('SHADING_COLOR_FORMAT: ' + GFXFormatInfos[this._colorFmt].name); console.info('SHADING_DEPTH_FORMAT: ' + GFXFormatInfos[this._depthStencilFmt].name); @@ -388,8 +395,8 @@ export abstract class RenderPipeline { type: GFXTextureType.TEX2D, usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, format: this._colorFmt, - width: this._shadingTexWidth, - height: this._shadingTexHeight, + width: this._shadingWidth, + height: this._shadingHeight, }); this._msaaShadingTexView = this._device.createTextureView({ texture : this._msaaShadingTex, @@ -400,8 +407,8 @@ export abstract class RenderPipeline { type : GFXTextureType.TEX2D, usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT | GFXTextureUsageBit.SAMPLED, format : this._depthStencilFmt, - width : this._shadingTexWidth, - height : this._shadingTexHeight, + width : this._shadingWidth, + height : this._shadingHeight, }); this._msaaDepthStencilTexView = this._device.createTextureView({ texture : this._msaaDepthStencilTex, @@ -419,8 +426,8 @@ export abstract class RenderPipeline { type : GFXTextureType.TEX2D, usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT | GFXTextureUsageBit.SAMPLED, format : this._depthStencilFmt, - width : this._shadingTexWidth, - height : this._shadingTexHeight, + width : this._shadingWidth, + height : this._shadingHeight, }); this._depthStencilTexView = this._device.createTextureView({ @@ -434,8 +441,8 @@ export abstract class RenderPipeline { type: GFXTextureType.TEX2D, usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, format: this._colorFmt, - width: this._shadingTexWidth, - height: this._shadingTexHeight, + width: this._shadingWidth, + height: this._shadingHeight, }); this._shadingTexViews[i] = this._device.createTextureView({ @@ -469,8 +476,8 @@ export abstract class RenderPipeline { type: GFXTextureType.TEX2D, usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, format: smaaColorFmt, - width: this._shadingTexWidth, - height: this._shadingTexHeight, + width: this._shadingWidth, + height: this._shadingHeight, }); this._smaaEdgeTexView = this._device.createTextureView({ @@ -488,8 +495,8 @@ export abstract class RenderPipeline { type: GFXTextureType.TEX2D, usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, format: smaaColorFmt, - width: this._shadingTexWidth, - height: this._shadingTexHeight, + width: this._shadingWidth, + height: this._shadingHeight, }); this._smaaBlendTexView = this._device.createTextureView({ @@ -773,14 +780,14 @@ export abstract class RenderPipeline { _vec4Array[3] = 1.0 / _vec4Array[1]; this._defaultUboGlobal.view.set(_vec4Array, UBOGlobal.SCREEN_SIZE_OFFSET); - _vec4Array[0] = camera.width / this._shadingTexWidth * this._shadingScale; - _vec4Array[1] = camera.height / this._shadingTexHeight * this._shadingScale; + _vec4Array[0] = camera.width / this._shadingWidth * this._shadingScale; + _vec4Array[1] = camera.height / this._shadingHeight * this._shadingScale; _vec4Array[2] = 1.0 / _vec4Array[0]; _vec4Array[3] = 1.0 / _vec4Array[1]; this._defaultUboGlobal.view.set(_vec4Array, UBOGlobal.SCREEN_SCALE_OFFSET); - _vec4Array[0] = this._shadingTexWidth; - _vec4Array[1] = this._shadingTexHeight; + _vec4Array[0] = this._shadingWidth; + _vec4Array[1] = this._shadingHeight; _vec4Array[2] = 1.0 / _vec4Array[0]; _vec4Array[3] = 1.0 / _vec4Array[1]; this._defaultUboGlobal.view.set(_vec4Array, UBOGlobal.NATIVE_SIZE_OFFSET);