diff --git a/cocos/core/gfx/base/command-buffer.ts b/cocos/core/gfx/base/command-buffer.ts index cda9840d0e..e402e55e92 100644 --- a/cocos/core/gfx/base/command-buffer.ts +++ b/cocos/core/gfx/base/command-buffer.ts @@ -157,11 +157,14 @@ export abstract class CommandBuffer extends Obj { public abstract bindPipelineState (pipelineState: PipelineState): void; /** - * @en Bind descriptor set. - * @zh 绑定 GFX 描述符集。 + * @en Bind a descriptor set. Note that the corresponding PiplieneState has to be bound first + * before calling this function, or the dynamic offset specified may be invalidated. + * @zh 绑定 GFX 描述符集。注意在调用此函数前,必须先绑定对应的 PipelineState,否则 dynamic offset 可能无效。 + * @param set The target descriptor set index. * @param descriptorSet The descriptor set to be bound. + * @param dynamicOffsets The offset numbers for dynamic bindings. */ - public abstract bindDescriptorSet (set: number, descriptorSets: DescriptorSet, dynamicOffsets?: number[]): void; + public abstract bindDescriptorSet (set: number, descriptorSet: DescriptorSet, dynamicOffsets?: number[]): void; /** * @en Bind input assembler. diff --git a/cocos/core/gfx/webgl/webgl-command-buffer.ts b/cocos/core/gfx/webgl/webgl-command-buffer.ts index 32f874f327..4017ad2007 100644 --- a/cocos/core/gfx/webgl/webgl-command-buffer.ts +++ b/cocos/core/gfx/webgl/webgl-command-buffer.ts @@ -48,28 +48,6 @@ import { WebGLCmd, WebGLCmdBeginRenderPass, WebGLCmdBindStates, WebGLCmdCopyBuff import { GlobalBarrier } from '../base/global-barrier'; import { TextureBarrier } from '../base/texture-barrier'; -export interface IWebGLDepthBias { - constantFactor: number; - clamp: number; - slopeFactor: number; -} - -export interface IWebGLDepthBounds { - minBounds: number; - maxBounds: number; -} - -export interface IWebGLStencilWriteMask { - face: StencilFace; - writeMask: number; -} - -export interface IWebGLStencilCompareMask { - face: StencilFace; - reference: number; - compareMask: number; -} - export class WebGLCommandBuffer extends CommandBuffer { public cmdPackage: WebGLCmdPackage = new WebGLCmdPackage(); protected _webGLAllocator: WebGLCommandAllocator | null = null; @@ -77,7 +55,7 @@ export class WebGLCommandBuffer extends CommandBuffer { protected _curGPUPipelineState: IWebGLGPUPipelineState | null = null; protected _curGPUInputAssembler: IWebGLGPUInputAssembler | null = null; protected _curGPUDescriptorSets: IWebGLGPUDescriptorSet[] = []; - protected _curDynamicOffsets: number[][] = []; + protected _curDynamicOffsets: number[] = Array(8).fill(0); protected _curDynamicStates: DynamicStates = new DynamicStates(); protected _isStateInvalied = false; @@ -90,7 +68,6 @@ export class WebGLCommandBuffer extends CommandBuffer { const setCount = (this._device as WebGLDevice).bindingMappingInfo.bufferOffsets.length; for (let i = 0; i < setCount; i++) { this._curGPUDescriptorSets.push(null!); - this._curDynamicOffsets.push([]); } return true; @@ -165,10 +142,13 @@ export class WebGLCommandBuffer extends CommandBuffer { this._isStateInvalied = true; } if (dynamicOffsets) { - const offsets = this._curDynamicOffsets[set]; - for (let i = 0; i < dynamicOffsets.length; i++) offsets[i] = dynamicOffsets[i]; - offsets.length = dynamicOffsets.length; - this._isStateInvalied = true; + const gpuPipelineLayout = this._curGPUPipelineState?.gpuPipelineLayout; + if (gpuPipelineLayout) { + const offsets = this._curDynamicOffsets; + const idx = gpuPipelineLayout.dynamicOffsetOffsets[set]; + for (let i = 0; i < dynamicOffsets.length; i++) offsets[idx + i] = dynamicOffsets[i]; + this._isStateInvalied = true; + } } } @@ -444,9 +424,7 @@ export class WebGLCommandBuffer extends CommandBuffer { if (bindStatesCmd) { bindStatesCmd.gpuPipelineState = this._curGPUPipelineState; Array.prototype.push.apply(bindStatesCmd.gpuDescriptorSets, this._curGPUDescriptorSets); - for (let i = 0; i < this._curDynamicOffsets.length; i++) { - Array.prototype.push.apply(bindStatesCmd.dynamicOffsets, this._curDynamicOffsets[i]); - } + Array.prototype.push.apply(bindStatesCmd.dynamicOffsets, this._curDynamicOffsets); bindStatesCmd.gpuInputAssembler = this._curGPUInputAssembler; bindStatesCmd.dynamicStates.copy(this._curDynamicStates); diff --git a/cocos/core/gfx/webgl/webgl-commands.ts b/cocos/core/gfx/webgl/webgl-commands.ts index 5023d03787..a293e16d19 100644 --- a/cocos/core/gfx/webgl/webgl-commands.ts +++ b/cocos/core/gfx/webgl/webgl-commands.ts @@ -26,9 +26,6 @@ import { CachedArray } from '../../memop/cached-array'; import { error, errorID } from '../../platform/debug'; import { WebGLCommandAllocator } from './webgl-command-allocator'; -import { - IWebGLDepthBias, IWebGLDepthBounds, IWebGLStencilCompareMask, IWebGLStencilWriteMask, -} from './webgl-command-buffer'; import { WebGLEXT } from './webgl-define'; import { WebGLDevice } from './webgl-device'; import { @@ -36,8 +33,8 @@ import { IWebGLGPUPipelineState, IWebGLGPUShader, IWebGLGPUTexture, IWebGLGPUUniformBlock, IWebGLGPUUniformSamplerTexture, IWebGLGPURenderPass, } from './webgl-gpu-objects'; import { - BufferUsageBit, ClearFlagBit, ClearFlags, ColorMask, CullMode, Format, BufferTextureCopy, Color, Rect, Viewport, - FormatInfos, FormatSize, LoadOp, MemoryUsageBit, SampleCount, ShaderStageFlagBit, StencilFace, + BufferUsageBit, ClearFlagBit, ClearFlags, ColorMask, CullMode, Format, BufferTextureCopy, Color, Rect, + FormatInfos, FormatSize, LoadOp, MemoryUsageBit, SampleCount, ShaderStageFlagBit, TextureFlagBit, TextureType, Type, FormatInfo, DynamicStateFlagBit, BufferSource, DrawInfo, IndirectBuffer, DynamicStates, } from '../base/define'; @@ -522,7 +519,6 @@ export enum WebGLCmd { export abstract class WebGLCmdObject { public cmdType: WebGLCmd; - public refCount = 0; constructor (type: WebGLCmd) { @@ -534,17 +530,11 @@ export abstract class WebGLCmdObject { export class WebGLCmdBeginRenderPass extends WebGLCmdObject { public gpuRenderPass: IWebGLGPURenderPass | null = null; - public gpuFramebuffer: IWebGLGPUFramebuffer | null = null; - public renderArea = new Rect(); - public clearFlag: ClearFlags = ClearFlagBit.NONE; - public clearColors: Color[] = []; - public clearDepth = 1.0; - public clearStencil = 0; constructor () { @@ -589,11 +579,8 @@ export class WebGLCmdDraw extends WebGLCmdObject { export class WebGLCmdUpdateBuffer extends WebGLCmdObject { public gpuBuffer: IWebGLGPUBuffer | null = null; - public buffer: BufferSource | null = null; - public offset = 0; - public size = 0; constructor () { @@ -608,9 +595,7 @@ export class WebGLCmdUpdateBuffer extends WebGLCmdObject { export class WebGLCmdCopyBufferToTexture extends WebGLCmdObject { public gpuTexture: IWebGLGPUTexture | null = null; - public buffers: ArrayBufferView[] = []; - public regions: BufferTextureCopy[] = []; constructor () { @@ -626,15 +611,10 @@ export class WebGLCmdCopyBufferToTexture extends WebGLCmdObject { export class WebGLCmdPackage { public cmds: CachedArray = new CachedArray(1); - public beginRenderPassCmds: CachedArray = new CachedArray(1); - public bindStatesCmds: CachedArray = new CachedArray(1); - public drawCmds: CachedArray = new CachedArray(1); - public updateBufferCmds: CachedArray = new CachedArray(1); - public copyBufferToTextureCmds: CachedArray = new CachedArray(1); public clearCmds (allocator: WebGLCommandAllocator) { diff --git a/cocos/core/gfx/webgl/webgl-gpu-objects.ts b/cocos/core/gfx/webgl/webgl-gpu-objects.ts index d06fea1049..4799b0db09 100644 --- a/cocos/core/gfx/webgl/webgl-gpu-objects.ts +++ b/cocos/core/gfx/webgl/webgl-gpu-objects.ts @@ -185,6 +185,7 @@ export interface IWebGLGPUDescriptorSetLayout { export interface IWebGLGPUPipelineLayout { gpuSetLayouts: IWebGLGPUDescriptorSetLayout[]; dynamicOffsetCount: number; + dynamicOffsetOffsets: number[]; dynamicOffsetIndices: number[][]; } diff --git a/cocos/core/gfx/webgl/webgl-pipeline-layout.ts b/cocos/core/gfx/webgl/webgl-pipeline-layout.ts index 14b38b269a..4e1ca9c230 100644 --- a/cocos/core/gfx/webgl/webgl-pipeline-layout.ts +++ b/cocos/core/gfx/webgl/webgl-pipeline-layout.ts @@ -40,6 +40,7 @@ export class WebGLPipelineLayout extends PipelineLayout { const gpuSetLayouts: IWebGLGPUDescriptorSetLayout[] = []; let dynamicOffsetCount = 0; + const dynamicOffsetOffsets: number[] = []; for (let i = 0; i < this._setLayouts.length; i++) { const setLayout = this._setLayouts[i] as WebGLDescriptorSetLayout; const dynamicBindings = setLayout.gpuDescriptorSetLayout.dynamicBindings; @@ -51,6 +52,7 @@ export class WebGLPipelineLayout extends PipelineLayout { gpuSetLayouts.push(setLayout.gpuDescriptorSetLayout); dynamicOffsetIndices.push(indices); + dynamicOffsetOffsets.push(dynamicOffsetCount); dynamicOffsetCount += dynamicBindings.length; } @@ -58,6 +60,7 @@ export class WebGLPipelineLayout extends PipelineLayout { gpuSetLayouts, dynamicOffsetIndices, dynamicOffsetCount, + dynamicOffsetOffsets, }; return true; diff --git a/cocos/core/gfx/webgl/webgl-primary-command-buffer.ts b/cocos/core/gfx/webgl/webgl-primary-command-buffer.ts index 20258d9930..fc829655f5 100644 --- a/cocos/core/gfx/webgl/webgl-primary-command-buffer.ts +++ b/cocos/core/gfx/webgl/webgl-primary-command-buffer.ts @@ -40,8 +40,6 @@ import { WebGLTexture } from './webgl-texture'; import { RenderPass } from '../base/render-pass'; import { WebGLRenderPass } from './webgl-render-pass'; -const _dynamicOffsets: number[] = []; - export class WebGLPrimaryCommandBuffer extends WebGLCommandBuffer { public beginRenderPass ( renderPass: RenderPass, @@ -134,12 +132,8 @@ export class WebGLPrimaryCommandBuffer extends WebGLCommandBuffer { } protected bindStates () { - _dynamicOffsets.length = 0; - for (let i = 0; i < this._curDynamicOffsets.length; i++) { - Array.prototype.push.apply(_dynamicOffsets, this._curDynamicOffsets[i]); - } WebGLCmdFuncBindStates(this._device as WebGLDevice, this._curGPUPipelineState, this._curGPUInputAssembler, - this._curGPUDescriptorSets, _dynamicOffsets, this._curDynamicStates); + this._curGPUDescriptorSets, this._curDynamicOffsets, this._curDynamicStates); this._isStateInvalied = false; } } diff --git a/cocos/core/gfx/webgl2/webgl2-command-buffer.ts b/cocos/core/gfx/webgl2/webgl2-command-buffer.ts index 861e9905c7..ecf1d00551 100644 --- a/cocos/core/gfx/webgl2/webgl2-command-buffer.ts +++ b/cocos/core/gfx/webgl2/webgl2-command-buffer.ts @@ -60,28 +60,6 @@ import { WebGL2RenderPass } from './webgl2-render-pass'; import { GlobalBarrier } from '../base/global-barrier'; import { TextureBarrier } from '../base/texture-barrier'; -export interface IWebGL2DepthBias { - constantFactor: number; - clamp: number; - slopeFactor: number; -} - -export interface IWebGL2DepthBounds { - minBounds: number; - maxBounds: number; -} - -export interface IWebGL2StencilWriteMask { - face: StencilFace; - writeMask: number; -} - -export interface IWebGL2StencilCompareMask { - face: StencilFace; - reference: number; - compareMask: number; -} - export class WebGL2CommandBuffer extends CommandBuffer { public cmdPackage: WebGL2CmdPackage = new WebGL2CmdPackage(); protected _webGLAllocator: WebGL2CommandAllocator | null = null; @@ -89,7 +67,7 @@ export class WebGL2CommandBuffer extends CommandBuffer { protected _curGPUPipelineState: IWebGL2GPUPipelineState | null = null; protected _curGPUDescriptorSets: IWebGL2GPUDescriptorSet[] = []; protected _curGPUInputAssembler: IWebGL2GPUInputAssembler | null = null; - protected _curDynamicOffsets: number[][] = []; + protected _curDynamicOffsets: number[] = Array(8).fill(0); protected _curDynamicStates: DynamicStates = new DynamicStates(); protected _isStateInvalied = false; @@ -102,7 +80,6 @@ export class WebGL2CommandBuffer extends CommandBuffer { const setCount = (this._device as WebGL2Device).bindingMappingInfo.bufferOffsets.length; for (let i = 0; i < setCount; i++) { this._curGPUDescriptorSets.push(null!); - this._curDynamicOffsets.push([]); } return true; @@ -176,10 +153,13 @@ export class WebGL2CommandBuffer extends CommandBuffer { this._isStateInvalied = true; } if (dynamicOffsets) { - const offsets = this._curDynamicOffsets[set]; - for (let i = 0; i < dynamicOffsets.length; i++) offsets[i] = dynamicOffsets[i]; - offsets.length = dynamicOffsets.length; - this._isStateInvalied = true; + const gpuPipelineLayout = this._curGPUPipelineState?.gpuPipelineLayout; + if (gpuPipelineLayout) { + const offsets = this._curDynamicOffsets; + const idx = gpuPipelineLayout.dynamicOffsetOffsets[set]; + for (let i = 0; i < dynamicOffsets.length; i++) offsets[idx + i] = dynamicOffsets[i]; + this._isStateInvalied = true; + } } } @@ -450,9 +430,7 @@ export class WebGL2CommandBuffer extends CommandBuffer { const bindStatesCmd = this._webGLAllocator!.bindStatesCmdPool.alloc(WebGL2CmdBindStates); bindStatesCmd.gpuPipelineState = this._curGPUPipelineState; Array.prototype.push.apply(bindStatesCmd.gpuDescriptorSets, this._curGPUDescriptorSets); - for (let i = 0; i < this._curDynamicOffsets.length; i++) { - Array.prototype.push.apply(bindStatesCmd.dynamicOffsets, this._curDynamicOffsets[i]); - } + Array.prototype.push.apply(bindStatesCmd.dynamicOffsets, this._curDynamicOffsets); bindStatesCmd.gpuInputAssembler = this._curGPUInputAssembler; bindStatesCmd.dynamicStates = this._curDynamicStates; diff --git a/cocos/core/gfx/webgl2/webgl2-commands.ts b/cocos/core/gfx/webgl2/webgl2-commands.ts index dadddcdf36..8f1f376c9e 100644 --- a/cocos/core/gfx/webgl2/webgl2-commands.ts +++ b/cocos/core/gfx/webgl2/webgl2-commands.ts @@ -27,17 +27,11 @@ import { CachedArray } from '../../memop/cached-array'; import { error, errorID } from '../../platform'; import { BufferUsageBit, ColorMask, CullMode, DynamicStateFlagBit, Filter, Format, TextureType, Type, FormatInfo, - FormatInfos, FormatSize, LoadOp, MemoryUsageBit, SampleCount, ShaderStageFlagBit, StencilFace, TextureFlagBit, - Color, Rect, Viewport, BufferTextureCopy, BufferSource, DrawInfo, IndirectBuffer, UniformBlock, DynamicStates, + FormatInfos, FormatSize, LoadOp, MemoryUsageBit, SampleCount, ShaderStageFlagBit, TextureFlagBit, + Color, Rect, BufferTextureCopy, BufferSource, DrawInfo, IndirectBuffer, UniformBlock, DynamicStates, } from '../base/define'; import { WebGLEXT } from '../webgl/webgl-define'; import { WebGL2CommandAllocator } from './webgl2-command-allocator'; -import { - IWebGL2DepthBias, - IWebGL2DepthBounds, - IWebGL2StencilCompareMask, - IWebGL2StencilWriteMask, -} from './webgl2-command-buffer'; import { WebGL2Device } from './webgl2-device'; import { IWebGL2GPUInputAssembler, @@ -651,7 +645,6 @@ export enum WebGL2Cmd { export abstract class WebGL2CmdObject { public cmdType: WebGL2Cmd; - public refCount = 0; constructor (type: WebGL2Cmd) { @@ -663,15 +656,10 @@ export abstract class WebGL2CmdObject { export class WebGL2CmdBeginRenderPass extends WebGL2CmdObject { public gpuRenderPass: IWebGL2GPURenderPass | null = null; - public gpuFramebuffer: IWebGL2GPUFramebuffer | null = null; - public renderArea = new Rect(); - public clearColors: Color[] = []; - public clearDepth = 1.0; - public clearStencil = 0; constructor () { @@ -716,11 +704,8 @@ export class WebGL2CmdDraw extends WebGL2CmdObject { export class WebGL2CmdUpdateBuffer extends WebGL2CmdObject { public gpuBuffer: IWebGL2GPUBuffer | null = null; - public buffer: BufferSource | null = null; - public offset = 0; - public size = 0; constructor () { @@ -735,9 +720,7 @@ export class WebGL2CmdUpdateBuffer extends WebGL2CmdObject { export class WebGL2CmdCopyBufferToTexture extends WebGL2CmdObject { public gpuTexture: IWebGL2GPUTexture | null = null; - public buffers: ArrayBufferView[] = []; - public regions: BufferTextureCopy[] = []; constructor () { @@ -753,15 +736,10 @@ export class WebGL2CmdCopyBufferToTexture extends WebGL2CmdObject { export class WebGL2CmdPackage { public cmds: CachedArray = new CachedArray(1); - public beginRenderPassCmds: CachedArray = new CachedArray(1); - public bindStatesCmds: CachedArray = new CachedArray(1); - public drawCmds: CachedArray = new CachedArray(1); - public updateBufferCmds: CachedArray = new CachedArray(1); - public copyBufferToTextureCmds: CachedArray = new CachedArray(1); public clearCmds (allocator: WebGL2CommandAllocator) { diff --git a/cocos/core/gfx/webgl2/webgl2-gpu-objects.ts b/cocos/core/gfx/webgl2/webgl2-gpu-objects.ts index f49d86b466..1c35014c10 100644 --- a/cocos/core/gfx/webgl2/webgl2-gpu-objects.ts +++ b/cocos/core/gfx/webgl2/webgl2-gpu-objects.ts @@ -187,6 +187,7 @@ export interface IWebGL2GPUDescriptorSetLayout { export interface IWebGL2GPUPipelineLayout { gpuSetLayouts: IWebGL2GPUDescriptorSetLayout[]; dynamicOffsetCount: number; + dynamicOffsetOffsets: number[]; dynamicOffsetIndices: number[][]; } diff --git a/cocos/core/gfx/webgl2/webgl2-pipeline-layout.ts b/cocos/core/gfx/webgl2/webgl2-pipeline-layout.ts index 4176905b2c..73fa325cf7 100644 --- a/cocos/core/gfx/webgl2/webgl2-pipeline-layout.ts +++ b/cocos/core/gfx/webgl2/webgl2-pipeline-layout.ts @@ -40,6 +40,7 @@ export class WebGL2PipelineLayout extends PipelineLayout { const gpuSetLayouts: IWebGL2GPUDescriptorSetLayout[] = []; let dynamicOffsetCount = 0; + const dynamicOffsetOffsets: number[] = []; for (let i = 0; i < this._setLayouts.length; i++) { const setLayout = this._setLayouts[i] as WebGL2DescriptorSetLayout; const dynamicBindings = setLayout.gpuDescriptorSetLayout.dynamicBindings; @@ -51,6 +52,7 @@ export class WebGL2PipelineLayout extends PipelineLayout { gpuSetLayouts.push(setLayout.gpuDescriptorSetLayout); dynamicOffsetIndices.push(indices); + dynamicOffsetOffsets.push(dynamicOffsetCount); dynamicOffsetCount += dynamicBindings.length; } @@ -58,6 +60,7 @@ export class WebGL2PipelineLayout extends PipelineLayout { gpuSetLayouts, dynamicOffsetIndices, dynamicOffsetCount, + dynamicOffsetOffsets, }; return true; diff --git a/cocos/core/gfx/webgl2/webgl2-primary-command-buffer.ts b/cocos/core/gfx/webgl2/webgl2-primary-command-buffer.ts index 320cb6bb19..084f937000 100644 --- a/cocos/core/gfx/webgl2/webgl2-primary-command-buffer.ts +++ b/cocos/core/gfx/webgl2/webgl2-primary-command-buffer.ts @@ -40,8 +40,6 @@ import { WebGL2Texture } from './webgl2-texture'; import { RenderPass } from '../base/render-pass'; import { WebGL2RenderPass } from './webgl2-render-pass'; -const _dynamicOffsets: number[] = []; - export class WebGL2PrimaryCommandBuffer extends WebGL2CommandBuffer { public beginRenderPass ( renderPass: RenderPass, @@ -134,13 +132,8 @@ export class WebGL2PrimaryCommandBuffer extends WebGL2CommandBuffer { } protected bindStates () { - _dynamicOffsets.length = 0; - for (let i = 0; i < this._curDynamicOffsets.length; i++) { - Array.prototype.push.apply(_dynamicOffsets, this._curDynamicOffsets[i]); - } WebGL2CmdFuncBindStates(this._device as WebGL2Device, this._curGPUPipelineState, this._curGPUInputAssembler, - this._curGPUDescriptorSets, _dynamicOffsets, this._curDynamicStates); - + this._curGPUDescriptorSets, this._curDynamicOffsets, this._curDynamicStates); this._isStateInvalied = false; } }