gfx: fewer copies of dynamic offsets (#8741)

This commit is contained in:
Yun Hsiao Wu
2021-06-01 11:25:53 +08:00
committed by GitHub
parent 0a452691c8
commit eccbbba7d0
11 changed files with 38 additions and 126 deletions
+6 -3
View File
@@ -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.
+9 -31
View File
@@ -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);
+2 -22
View File
@@ -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<WebGLCmd> = new CachedArray(1);
public beginRenderPassCmds: CachedArray<WebGLCmdBeginRenderPass> = new CachedArray(1);
public bindStatesCmds: CachedArray<WebGLCmdBindStates> = new CachedArray(1);
public drawCmds: CachedArray<WebGLCmdDraw> = new CachedArray(1);
public updateBufferCmds: CachedArray<WebGLCmdUpdateBuffer> = new CachedArray(1);
public copyBufferToTextureCmds: CachedArray<WebGLCmdCopyBufferToTexture> = new CachedArray(1);
public clearCmds (allocator: WebGLCommandAllocator) {
@@ -185,6 +185,7 @@ export interface IWebGLGPUDescriptorSetLayout {
export interface IWebGLGPUPipelineLayout {
gpuSetLayouts: IWebGLGPUDescriptorSetLayout[];
dynamicOffsetCount: number;
dynamicOffsetOffsets: number[];
dynamicOffsetIndices: number[][];
}
@@ -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;
@@ -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;
}
}
+9 -31
View File
@@ -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;
+2 -24
View File
@@ -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<WebGL2Cmd> = new CachedArray(1);
public beginRenderPassCmds: CachedArray<WebGL2CmdBeginRenderPass> = new CachedArray(1);
public bindStatesCmds: CachedArray<WebGL2CmdBindStates> = new CachedArray(1);
public drawCmds: CachedArray<WebGL2CmdDraw> = new CachedArray(1);
public updateBufferCmds: CachedArray<WebGL2CmdUpdateBuffer> = new CachedArray(1);
public copyBufferToTextureCmds: CachedArray<WebGL2CmdCopyBufferToTexture> = new CachedArray(1);
public clearCmds (allocator: WebGL2CommandAllocator) {
@@ -187,6 +187,7 @@ export interface IWebGL2GPUDescriptorSetLayout {
export interface IWebGL2GPUPipelineLayout {
gpuSetLayouts: IWebGL2GPUDescriptorSetLayout[];
dynamicOffsetCount: number;
dynamicOffsetOffsets: number[];
dynamicOffsetIndices: number[][];
}
@@ -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;
@@ -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;
}
}