gfx: states store hashes (#9325)

* gfx: states store hashes

* profiler uses fixed viewport
This commit is contained in:
Yun Hsiao Wu
2021-09-16 10:28:27 +08:00
committed by GitHub
parent ccd505ad14
commit 56038db50a
19 changed files with 144 additions and 102 deletions
+22 -9
View File
@@ -121,6 +121,21 @@ export enum Feature {
MULTIPLE_RENDER_TARGETS,
BLEND_MINMAX,
COMPUTE_SHADER,
// This flag indicates whether the device can benefit from subpass-style usages.
// Specifically, this only differs on the GLES backends: the Framebuffer Fetch
// extension is used to simulate input attachments, so the flag is not set when
// the extension is not supported, and you should switch to the fallback branch
// (without the extension requirement) in GLSL shader sources accordingly.
// Everything else can remain the same.
//
// Another caveat when using the Framebuffer Fetch extensions in shaders is that
// for subpasses with exactly 4 inout attachments the output is automatically set
// to the last attachment (taking advantage of 'inout' property), and a separate
// blit operation (if needed) will be added for you afterwards to transfer the
// rendering result to the corrent subpass output texture. This is to ameliorate
// the max number of attachment limit(4) situation for many devices, and shader
// sources inside this kind of subpass must match this behavior.
INPUT_ATTACHMENT_BENEFIT,
COUNT,
}
@@ -608,14 +623,12 @@ export enum CullMode {
export enum DynamicStateFlagBit {
NONE = 0x0,
VIEWPORT = 0x1,
SCISSOR = 0x2,
LINE_WIDTH = 0x4,
DEPTH_BIAS = 0x8,
BLEND_CONSTANTS = 0x10,
DEPTH_BOUNDS = 0x20,
STENCIL_WRITE_MASK = 0x40,
STENCIL_COMPARE_MASK = 0x80,
LINE_WIDTH = 0x1,
DEPTH_BIAS = 0x2,
BLEND_CONSTANTS = 0x4,
DEPTH_BOUNDS = 0x8,
STENCIL_WRITE_MASK = 0x10,
STENCIL_COMPARE_MASK = 0x20,
}
export enum StencilFace {
@@ -955,7 +968,7 @@ export class SwapchainInfo {
constructor (
public windowHandle: HTMLCanvasElement = null!,
public vsyncMode: VsyncMode = VsyncMode.RELAXED,
public vsyncMode: VsyncMode = VsyncMode.ON,
public width: number = 0,
public height: number = 0,
) {}
+4 -1
View File
@@ -37,12 +37,15 @@ import { GFXObject, ObjectType, GlobalBarrierInfo } from '../define';
*/
export class GlobalBarrier extends GFXObject {
get info (): Readonly<GlobalBarrierInfo> { return this._info; }
get hash (): number { return this._hash; }
protected _info: GlobalBarrierInfo = new GlobalBarrierInfo();
protected _hash = 0;
constructor (info: GlobalBarrierInfo) {
constructor (info: GlobalBarrierInfo, hash: number) {
super(ObjectType.GLOBAL_BARRIER);
this._info.copy(info);
this._hash = hash;
}
static computeHash (info: GlobalBarrierInfo) {
+4 -1
View File
@@ -36,12 +36,15 @@ import { GFXObject, ObjectType, SamplerInfo } from '../define';
*/
export class Sampler extends GFXObject {
get info (): Readonly<SamplerInfo> { return this._info; }
get hash (): number { return this._hash; }
protected _info: SamplerInfo = new SamplerInfo();
protected _hash = 0;
constructor (info: SamplerInfo) {
constructor (info: SamplerInfo, hash: number) {
super(ObjectType.SAMPLER);
this._info.copy(info);
this._hash = hash;
}
static computeHash (info: SamplerInfo) {
@@ -37,12 +37,15 @@ import { GFXObject, ObjectType, TextureBarrierInfo } from '../define';
*/
export class TextureBarrier extends GFXObject {
get info (): Readonly<TextureBarrierInfo> { return this._info; }
get hash (): number { return this._hash; }
protected _info: TextureBarrierInfo = new TextureBarrierInfo();
protected _hash = 0;
constructor (info: TextureBarrierInfo) {
constructor (info: TextureBarrierInfo, hash: number) {
super(ObjectType.TEXTURE_BARRIER);
this._info.copy(info);
this._hash = hash;
}
static computeHash (info: TextureBarrierInfo) {
+3 -3
View File
@@ -174,7 +174,7 @@ export class EmptyDevice extends Device {
public getSampler (info: SamplerInfo): Sampler {
const hash = Sampler.computeHash(info);
if (!this._samplers.has(hash)) {
this._samplers.set(hash, new Sampler(info));
this._samplers.set(hash, new Sampler(info, hash));
}
return this._samplers.get(hash)!;
}
@@ -182,7 +182,7 @@ export class EmptyDevice extends Device {
public getGlobalBarrier (info: GlobalBarrierInfo) {
const hash = GlobalBarrier.computeHash(info);
if (!this._globalBarriers.has(hash)) {
this._globalBarriers.set(hash, new GlobalBarrier(info));
this._globalBarriers.set(hash, new GlobalBarrier(info, hash));
}
return this._globalBarriers.get(hash)!;
}
@@ -190,7 +190,7 @@ export class EmptyDevice extends Device {
public getTextureBarrier (info: TextureBarrierInfo) {
const hash = TextureBarrier.computeHash(info);
if (!this._textureBarriers.has(hash)) {
this._textureBarriers.set(hash, new TextureBarrier(info));
this._textureBarriers.set(hash, new TextureBarrier(info, hash));
}
return this._textureBarriers.get(hash)!;
}
+2 -2
View File
@@ -41,8 +41,8 @@ export class WebGLSampler extends Sampler {
private _gpuSampler: IWebGLGPUSampler | null = null;
constructor (info: SamplerInfo) {
super(info);
constructor (info: SamplerInfo, hash: number) {
super(info, hash);
let glMinFilter = 0;
let glMagFilter = 0;
-30
View File
@@ -2384,36 +2384,6 @@ export function WebGLCmdFuncBindStates (
for (let j = 0; j < dsLen; j++) {
const dynamicState = gpuPipelineState.dynamicStates[j];
switch (dynamicState) {
case DynamicStateFlagBit.VIEWPORT: {
const viewport = dynamicStates.viewport;
if (cache.viewport.left !== viewport.left
|| cache.viewport.top !== viewport.top
|| cache.viewport.width !== viewport.width
|| cache.viewport.height !== viewport.height) {
gl.viewport(viewport.left, viewport.top, viewport.width, viewport.height);
cache.viewport.left = viewport.left;
cache.viewport.top = viewport.top;
cache.viewport.width = viewport.width;
cache.viewport.height = viewport.height;
}
break;
}
case DynamicStateFlagBit.SCISSOR: {
const scissor = dynamicStates.scissor;
if (cache.scissorRect.x !== scissor.x
|| cache.scissorRect.y !== scissor.y
|| cache.scissorRect.width !== scissor.width
|| cache.scissorRect.height !== scissor.height) {
gl.scissor(scissor.x, scissor.y, scissor.width, scissor.height);
cache.scissorRect.x = scissor.x;
cache.scissorRect.y = scissor.y;
cache.scissorRect.width = scissor.width;
cache.scissorRect.height = scissor.height;
}
break;
}
case DynamicStateFlagBit.LINE_WIDTH: {
if (cache.rs.lineWidth !== dynamicStates.lineWidth) {
gl.lineWidth(dynamicStates.lineWidth);
+3 -3
View File
@@ -334,7 +334,7 @@ export class WebGLDevice extends Device {
public getSampler (info: SamplerInfo): Sampler {
const hash = Sampler.computeHash(info);
if (!this._samplers.has(hash)) {
this._samplers.set(hash, new WebGLSampler(info));
this._samplers.set(hash, new WebGLSampler(info, hash));
}
return this._samplers.get(hash)!;
}
@@ -342,7 +342,7 @@ export class WebGLDevice extends Device {
public getGlobalBarrier (info: GlobalBarrierInfo) {
const hash = GlobalBarrier.computeHash(info);
if (!this._globalBarriers.has(hash)) {
this._globalBarriers.set(hash, new GlobalBarrier(info));
this._globalBarriers.set(hash, new GlobalBarrier(info, hash));
}
return this._globalBarriers.get(hash)!;
}
@@ -350,7 +350,7 @@ export class WebGLDevice extends Device {
public getTextureBarrier (info: TextureBarrierInfo) {
const hash = TextureBarrier.computeHash(info);
if (!this._textureBarriers.has(hash)) {
this._textureBarriers.set(hash, new TextureBarrier(info));
this._textureBarriers.set(hash, new TextureBarrier(info, hash));
}
return this._textureBarriers.get(hash)!;
}
@@ -25,7 +25,7 @@
import { Buffer } from '../base/buffer';
import { CommandBuffer } from '../base/command-buffer';
import { BufferSource, DrawInfo, BufferTextureCopy, Color, Rect, BufferUsageBit } from '../base/define';
import { BufferSource, DrawInfo, BufferTextureCopy, Color, Rect, BufferUsageBit, Viewport } from '../base/define';
import { Framebuffer } from '../base/framebuffer';
import { InputAssembler } from '../base/input-assembler';
import { Texture } from '../base/texture';
@@ -91,6 +91,38 @@ export class WebGLPrimaryCommandBuffer extends WebGLCommandBuffer {
}
}
public setViewport (viewport: Viewport) {
const { stateCache: cache, gl } = WebGLDeviceManager.instance;
if (cache.viewport.left !== viewport.left
|| cache.viewport.top !== viewport.top
|| cache.viewport.width !== viewport.width
|| cache.viewport.height !== viewport.height) {
gl.viewport(viewport.left, viewport.top, viewport.width, viewport.height);
cache.viewport.left = viewport.left;
cache.viewport.top = viewport.top;
cache.viewport.width = viewport.width;
cache.viewport.height = viewport.height;
}
}
public setScissor (scissor: Rect) {
const { stateCache: cache, gl } = WebGLDeviceManager.instance;
if (cache.scissorRect.x !== scissor.x
|| cache.scissorRect.y !== scissor.y
|| cache.scissorRect.width !== scissor.width
|| cache.scissorRect.height !== scissor.height) {
gl.scissor(scissor.x, scissor.y, scissor.width, scissor.height);
cache.scissorRect.x = scissor.x;
cache.scissorRect.y = scissor.y;
cache.scissorRect.width = scissor.width;
cache.scissorRect.height = scissor.height;
}
}
public updateBuffer (buffer: Buffer, data: BufferSource, size?: number) {
if (!this._isInRenderPass) {
const gpuBuffer = (buffer as WebGLBuffer).gpuBuffer;
@@ -36,8 +36,8 @@ export class WebGL2Sampler extends Sampler {
private _gpuSampler: IWebGL2GPUSampler | null = null;
constructor (info: SamplerInfo) {
super(info);
constructor (info: SamplerInfo, hash: number) {
super(info, hash);
this._gpuSampler = {
glSampler: null,
-30
View File
@@ -2347,36 +2347,6 @@ export function WebGL2CmdFuncBindStates (
for (let k = 0; k < dsLen; k++) {
const dynamicState = gpuPipelineState.dynamicStates[k];
switch (dynamicState) {
case DynamicStateFlagBit.VIEWPORT: {
const viewport = dynamicStates.viewport;
if (cache.viewport.left !== viewport.left
|| cache.viewport.top !== viewport.top
|| cache.viewport.width !== viewport.width
|| cache.viewport.height !== viewport.height) {
gl.viewport(viewport.left, viewport.top, viewport.width, viewport.height);
cache.viewport.left = viewport.left;
cache.viewport.top = viewport.top;
cache.viewport.width = viewport.width;
cache.viewport.height = viewport.height;
}
break;
}
case DynamicStateFlagBit.SCISSOR: {
const scissor = dynamicStates.scissor;
if (cache.scissorRect.x !== scissor.x
|| cache.scissorRect.y !== scissor.y
|| cache.scissorRect.width !== scissor.width
|| cache.scissorRect.height !== scissor.height) {
gl.scissor(scissor.x, scissor.y, scissor.width, scissor.height);
cache.scissorRect.x = scissor.x;
cache.scissorRect.y = scissor.y;
cache.scissorRect.width = scissor.width;
cache.scissorRect.height = scissor.height;
}
break;
}
case DynamicStateFlagBit.LINE_WIDTH: {
if (cache.rs.lineWidth !== dynamicStates.lineWidth) {
gl.lineWidth(dynamicStates.lineWidth);
+3 -3
View File
@@ -322,7 +322,7 @@ export class WebGL2Device extends Device {
public getSampler (info: SamplerInfo): Sampler {
const hash = Sampler.computeHash(info);
if (!this._samplers.has(hash)) {
this._samplers.set(hash, new WebGL2Sampler(info));
this._samplers.set(hash, new WebGL2Sampler(info, hash));
}
return this._samplers.get(hash)!;
}
@@ -330,7 +330,7 @@ export class WebGL2Device extends Device {
public getGlobalBarrier (info: GlobalBarrierInfo) {
const hash = GlobalBarrier.computeHash(info);
if (!this._globalBarriers.has(hash)) {
this._globalBarriers.set(hash, new GlobalBarrier(info));
this._globalBarriers.set(hash, new GlobalBarrier(info, hash));
}
return this._globalBarriers.get(hash)!;
}
@@ -338,7 +338,7 @@ export class WebGL2Device extends Device {
public getTextureBarrier (info: TextureBarrierInfo) {
const hash = TextureBarrier.computeHash(info);
if (!this._textureBarriers.has(hash)) {
this._textureBarriers.set(hash, new TextureBarrier(info));
this._textureBarriers.set(hash, new TextureBarrier(info, hash));
}
return this._textureBarriers.get(hash)!;
}
@@ -25,7 +25,7 @@
import { Buffer } from '../base/buffer';
import { CommandBuffer } from '../base/command-buffer';
import { BufferUsageBit, BufferTextureCopy, Color, Rect, BufferSource, DrawInfo } from '../base/define';
import { BufferUsageBit, BufferTextureCopy, Color, Rect, BufferSource, DrawInfo, Viewport } from '../base/define';
import { Framebuffer } from '../base/framebuffer';
import { InputAssembler } from '../base/input-assembler';
import { Texture } from '../base/texture';
@@ -91,6 +91,38 @@ export class WebGL2PrimaryCommandBuffer extends WebGL2CommandBuffer {
}
}
public setViewport (viewport: Viewport) {
const { stateCache: cache, gl } = WebGL2DeviceManager.instance;
if (cache.viewport.left !== viewport.left
|| cache.viewport.top !== viewport.top
|| cache.viewport.width !== viewport.width
|| cache.viewport.height !== viewport.height) {
gl.viewport(viewport.left, viewport.top, viewport.width, viewport.height);
cache.viewport.left = viewport.left;
cache.viewport.top = viewport.top;
cache.viewport.width = viewport.width;
cache.viewport.height = viewport.height;
}
}
public setScissor (scissor: Rect) {
const { stateCache: cache, gl } = WebGL2DeviceManager.instance;
if (cache.scissorRect.x !== scissor.x
|| cache.scissorRect.y !== scissor.y
|| cache.scissorRect.width !== scissor.width
|| cache.scissorRect.height !== scissor.height) {
gl.scissor(scissor.x, scissor.y, scissor.width, scissor.height);
cache.scissorRect.x = scissor.x;
cache.scissorRect.y = scissor.y;
cache.scissorRect.width = scissor.width;
cache.scissorRect.height = scissor.height;
}
}
public updateBuffer (buffer: Buffer, data: BufferSource, size?: number) {
if (!this._isInRenderPass) {
const gpuBuffer = (buffer as WebGL2Buffer).gpuBuffer;
@@ -40,6 +40,7 @@ import { UIPhase } from '../forward/ui-phase';
import { MainFlow } from './main-flow';
import { RenderQueueDesc } from '../pipeline-serialization';
import { DeferredPipelineSceneData } from './deferred-pipeline-scene-data';
import { renderProfiler } from '../pipeline-funcs';
const colors: Color[] = [new Color(0, 0, 0, 1)];
@@ -131,7 +132,7 @@ export class PostprocessStage extends RenderStage {
cmdBuff.bindDescriptorSet(SetIndex.MATERIAL, pass.descriptorSet);
const inputAssembler = camera.window!.swapchain ? pipeline.quadIAOnscreen : pipeline.quadIAOffscreen;
let pso:PipelineState|null = null;
let pso: PipelineState | null = null;
if (pass != null && shader != null && inputAssembler != null) {
pso = PipelineStateManager.getOrCreatePipelineState(device, pass, shader, renderPass, inputAssembler);
}
@@ -144,6 +145,7 @@ export class PostprocessStage extends RenderStage {
}
this._uiPhase.render(camera, renderPass);
renderProfiler(device, renderPass, cmdBuff, pipeline.profiler, swapchain);
cmdBuff.endRenderPass();
}
+2 -1
View File
@@ -33,7 +33,7 @@ import { SetIndex } from '../define';
import { getPhaseID } from '../pass-phase';
import { renderQueueClearFunc, RenderQueue, convertRenderQueue, renderQueueSortFunc } from '../render-queue';
import { ClearFlagBit, Color, Rect } from '../../gfx';
import { SRGBToLinear } from '../pipeline-funcs';
import { renderProfiler, SRGBToLinear } from '../pipeline-funcs';
import { RenderBatchedQueue } from '../render-batched-queue';
import { RenderInstancedQueue } from '../render-instanced-queue';
import { IRenderStageInfo, RenderStage } from '../render-stage';
@@ -198,6 +198,7 @@ export class ForwardStage extends RenderStage {
this._planarQueue.recordCommandBuffer(device, renderPass, cmdBuff);
this._renderQueues[1].recordCommandBuffer(device, renderPass, cmdBuff);
this._uiPhase.render(camera, renderPass);
renderProfiler(device, renderPass, cmdBuff, pipeline.profiler, swapchain);
cmdBuff.endRenderPass();
}
-11
View File
@@ -27,7 +27,6 @@ import { RenderPass } from '../../gfx';
import { PipelineStateManager } from '../pipeline-state-manager';
import { SetIndex } from '../define';
import { Camera } from '../../renderer/scene/camera';
import { ForwardPipeline } from './forward-pipeline';
import { RenderPipeline } from '../render-pipeline';
import { getPhaseID } from '../pass-phase';
@@ -69,15 +68,5 @@ export class UIPhase {
cmdBuff.draw(inputAssembler!);
}
}
if (pipeline.profiler && pipeline.profiler.enabled && camera.window!.swapchain) {
const { inputAssembler, passes, shaders, descriptorSet } = pipeline.profiler.subModels[0];
const pso = PipelineStateManager.getOrCreatePipelineState(device, passes[0], shaders[0], renderPass, inputAssembler);
cmdBuff.bindPipelineState(pso);
cmdBuff.bindDescriptorSet(SetIndex.MATERIAL, passes[0].descriptorSet);
cmdBuff.bindDescriptorSet(SetIndex.LOCAL, descriptorSet);
cmdBuff.bindInputAssembler(inputAssembler);
cmdBuff.draw(inputAssembler);
}
}
}
+24
View File
@@ -28,7 +28,14 @@
* @module pipeline
*/
import { CommandBuffer, Device, Rect, RenderPass, Swapchain, Viewport } from '../gfx';
import { IVec4Like } from '../math';
import { Model } from '../renderer/scene';
import { PipelineStateManager } from './pipeline-state-manager';
import { SetIndex } from './define';
const profilerViewport = new Viewport();
const profilerScissor = new Rect();
/**
* @en Convert color in SRGB space to linear space
@@ -59,3 +66,20 @@ export function LinearToSRGB (out: IVec4Like, linear: IVec4Like) {
out.y = Math.sqrt(linear.y);
out.z = Math.sqrt(linear.z);
}
export function renderProfiler (device: Device, renderPass: RenderPass, cmdBuff: CommandBuffer, profiler: Model | null, swapchain: Swapchain | null) {
if (profiler && profiler.enabled && swapchain) {
const { inputAssembler, passes, shaders, descriptorSet } = profiler.subModels[0];
profilerViewport.width = profilerScissor.width = swapchain.width;
profilerViewport.height = profilerScissor.height = swapchain.height;
const pso = PipelineStateManager.getOrCreatePipelineState(device, passes[0], shaders[0], renderPass, inputAssembler);
cmdBuff.setViewport(profilerViewport);
cmdBuff.setScissor(profilerScissor);
cmdBuff.bindPipelineState(pso);
cmdBuff.bindDescriptorSet(SetIndex.MATERIAL, passes[0].descriptorSet);
cmdBuff.bindDescriptorSet(SetIndex.LOCAL, descriptorSet);
cmdBuff.bindInputAssembler(inputAssembler);
cmdBuff.draw(inputAssembler);
}
}
@@ -47,7 +47,7 @@ export class PipelineStateManager {
const pipelineLayout = pass.pipelineLayout;
const inputState = new InputState(ia.attributes);
const psoInfo = new PipelineStateInfo(
shader, pass.pipelineLayout, renderPass, inputState,
shader, pipelineLayout, renderPass, inputState,
pass.rasterizerState,
pass.depthStencilState,
pass.blendState,
@@ -1,5 +1,5 @@
{
"ver": "1.4.6",
"ver": "1.4.7",
"importer": "effect",
"imported": true,
"uuid": "9b4d3fa5-89bf-4715-b69a-dbbd1efaf6a5",