add pass.update() to render pipeline

This commit is contained in:
jerrywwl
2019-01-14 15:51:12 +08:00
parent bfd4824676
commit 36f1d38e9d
8 changed files with 186 additions and 104 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
import { GFXBuffer, IGFXBufferInfo, GFXBufferSource } from '../buffer';
import { GFXMemoryUsageBit, GFXStatus, GFXBufferUsageBit } from '../define';
import { GFXBuffer, GFXBufferSource, IGFXBufferInfo } from '../buffer';
import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { WebGLGFXDevice } from './webgl-device';
import { WebGLGPUBuffer } from './webgl-gpu-objects';
+64 -44
View File
@@ -1,7 +1,7 @@
import { GFXBindingLayout } from '../binding-layout';
import { GFXBuffer, GFXBufferSource } from '../buffer';
import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer';
import { GFXBufferTextureCopy, GFXStatus, GFXTextureLayout, IGFXColor, IGFXRect, GFXBufferUsageBit } from '../define';
import { GFXBufferTextureCopy, GFXBufferUsageBit, GFXStatus, GFXTextureLayout, IGFXColor, IGFXRect, GFXCommandBufferType } from '../define';
import { GFXDevice } from '../device';
import { GFXFramebuffer } from '../framebuffer';
import { GFXInputAssembler } from '../input-assembler';
@@ -29,6 +29,7 @@ import { WebGLGFXTexture } from './webgl-texture';
export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
public cmdPackage: WebGLCmdPackage = new WebGLCmdPackage();
private _isInRenderPass: boolean = false;
private _curGPUPipelineState: WebGLGPUPipelineState | null = null;
private _curGPUBindingLayout: WebGLGPUBindingLayout | null = null;
private _curGPUInputAssembler: WebGLGPUInputAssembler | null = null;
@@ -68,6 +69,8 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
if (this._isStateInvalied) {
this.bindStates();
}
this._isInRenderPass = false;
}
public beginRenderPass (
@@ -88,9 +91,12 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
this.cmdPackage.cmds.push(WebGLCmd.BEGIN_RENDER_PASS);
}
this._isInRenderPass = true;
}
public endRenderPass () {
this._isInRenderPass = false;
}
public bindPipelineState (pipelineState: GFXPipelineState) {
@@ -119,46 +125,55 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
public draw (inputAssembler: GFXInputAssembler) {
if (this._isStateInvalied) {
this.bindStates();
}
if (this._type === GFXCommandBufferType.PRIMARY && this._isInRenderPass ||
this._type === GFXCommandBufferType.SECONDARY) {
if (this._isStateInvalied) {
this.bindStates();
}
const cmd = ( this._allocator as WebGLGFXCommandAllocator).
drawCmdPool.alloc(WebGLCmdDraw);
if (cmd) {
(inputAssembler as WebGLGFXInputAssembler).extractCmdDraw(cmd);
this.cmdPackage.drawCmds.push(cmd);
const cmd = ( this._allocator as WebGLGFXCommandAllocator).
drawCmdPool.alloc(WebGLCmdDraw);
if (cmd) {
(inputAssembler as WebGLGFXInputAssembler).extractCmdDraw(cmd);
this.cmdPackage.drawCmds.push(cmd);
this.cmdPackage.cmds.push(WebGLCmd.DRAW);
this.cmdPackage.cmds.push(WebGLCmd.DRAW);
}
} else {
console.error('Command \'draw\' must be recorded inside a render pass.');
}
}
public updateBuffer (buffer: GFXBuffer, data: GFXBufferSource, offset?: number, size?: number) {
const gpuBuffer = (buffer as WebGLGFXBuffer).gpuBuffer;
if (gpuBuffer) {
const cmd = (this._allocator as WebGLGFXCommandAllocator).
updateBufferCmdPool.alloc(WebGLCmdUpdateBuffer);
if (cmd) {
if (!this._isInRenderPass) {
const gpuBuffer = (buffer as WebGLGFXBuffer).gpuBuffer;
if (gpuBuffer) {
const cmd = (this._allocator as WebGLGFXCommandAllocator).
updateBufferCmdPool.alloc(WebGLCmdUpdateBuffer);
if (cmd) {
let buffSize;
if (size !== undefined ) {
buffSize = size;
} else if (buffer.usage & GFXBufferUsageBit.INDIRECT) {
buffSize = 0;
} else {
buffSize = (data as ArrayBuffer).byteLength;
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.slice(0);
cmd.offset = (offset !== undefined ? offset : 0);
cmd.size = buffSize;
this.cmdPackage.updateBufferCmds.push(cmd);
this.cmdPackage.cmds.push(WebGLCmd.UPDATE_BUFFER);
}
const buff = data as ArrayBuffer;
cmd.gpuBuffer = gpuBuffer;
cmd.buffer = buff.slice(0);
cmd.offset = (offset !== undefined ? offset : 0);
cmd.size = buffSize;
this.cmdPackage.updateBufferCmds.push(cmd);
this.cmdPackage.cmds.push(WebGLCmd.UPDATE_BUFFER);
}
} else {
console.error('Command \'updateBuffer\' must be recorded outside a render pass.');
}
}
@@ -167,20 +182,25 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
dstTex: GFXTexture,
dstLayout: GFXTextureLayout,
regions: GFXBufferTextureCopy[]) {
const gpuBuffer = ( srcBuff as WebGLGFXBuffer).gpuBuffer;
const gpuTexture = ( dstTex as WebGLGFXTexture).gpuTexture;
if (gpuBuffer && gpuTexture) {
const cmd = (this._allocator as WebGLGFXCommandAllocator).
copyBufferToTextureCmdPool.alloc(WebGLCmdCopyBufferToTexture);
if (cmd) {
cmd.gpuBuffer = gpuBuffer;
cmd.gpuTexture = gpuTexture;
cmd.dstLayout = dstLayout;
cmd.regions = regions;
this.cmdPackage.copyBufferToTextureCmds.push(cmd);
this.cmdPackage.cmds.push(WebGLCmd.COPY_BUFFER_TO_TEXTURE);
if (!this._isInRenderPass) {
const gpuBuffer = ( srcBuff as WebGLGFXBuffer).gpuBuffer;
const gpuTexture = ( dstTex as WebGLGFXTexture).gpuTexture;
if (gpuBuffer && gpuTexture) {
const cmd = (this._allocator as WebGLGFXCommandAllocator).
copyBufferToTextureCmdPool.alloc(WebGLCmdCopyBufferToTexture);
if (cmd) {
cmd.gpuBuffer = gpuBuffer;
cmd.gpuTexture = gpuTexture;
cmd.dstLayout = dstLayout;
cmd.regions = regions;
this.cmdPackage.copyBufferToTextureCmds.push(cmd);
this.cmdPackage.cmds.push(WebGLCmd.COPY_BUFFER_TO_TEXTURE);
}
}
} else {
console.error('Command \'copyBufferToTexture\' must be recorded outside a render pass.');
}
}
+70 -26
View File
@@ -1,8 +1,20 @@
import { GFXBindingLayout, GFXBindingUnit, IGFXBindingLayoutInfo } from '../binding-layout';
import { GFXBuffer, IGFXBufferInfo, GFXBufferSource } from '../buffer';
import { GFXBuffer, GFXBufferSource, IGFXBufferInfo } from '../buffer';
import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from '../command-allocator';
import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer';
import { GFXBindingType, GFXBufferTextureCopy, GFXBufferUsageBit, GFXFilter, GFXFormat, GFXFormatInfos, GFXFormatSize, GFXQueueType, GFXTextureFlagBit, GFXTextureType, GFXTextureViewType } from '../define';
import {
GFXBindingType,
GFXBufferTextureCopy,
GFXBufferUsageBit,
GFXFilter,
GFXFormat,
GFXFormatInfos,
GFXFormatSize,
GFXQueueType,
GFXTextureFlagBit,
GFXTextureType,
GFXTextureViewType,
} from '../define';
import { GFXDevice, IGFXDeviceInfo } from '../device';
import { GFXFramebuffer, IGFXFramebufferInfo } from '../framebuffer';
import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../input-assembler';
@@ -19,9 +31,36 @@ import { WebGLGFXBindingLayout } from './webgl-binding-layout';
import { WebGLGFXBuffer } from './webgl-buffer';
import { WebGLGFXCommandAllocator } from './webgl-command-allocator';
import { WebGLGFXCommandBuffer } from './webgl-command-buffer';
import { WebGLCmdFuncCopyBufferToTexture, WebGLCmdFuncCreateBuffer, WebGLCmdFuncCreateFramebuffer, WebGLCmdFuncCreateInputAssember, WebGLCmdFuncCreateShader, WebGLCmdFuncCreateTexture, WebGLCmdFuncDestroyBuffer, WebGLCmdFuncDestroyFramebuffer, WebGLCmdFuncDestroyShader, WebGLCmdFuncDestroyTexture, WebGLCmdFuncUpdateBuffer } from './webgl-commands';
import {
WebGLCmdFuncCopyBufferToTexture,
WebGLCmdFuncCreateBuffer,
WebGLCmdFuncCreateFramebuffer,
WebGLCmdFuncCreateInputAssember,
WebGLCmdFuncCreateShader,
WebGLCmdFuncCreateTexture,
WebGLCmdFuncDestroyBuffer,
WebGLCmdFuncDestroyFramebuffer,
WebGLCmdFuncDestroyShader,
WebGLCmdFuncDestroyTexture,
WebGLCmdFuncUpdateBuffer,
} from './webgl-commands';
import { WebGLGFXFramebuffer } from './webgl-framebuffer';
import { WebGLGPUBinding, WebGLGPUBindingLayout as WebGLGPUBindingLayout, WebGLGPUBuffer, WebGLGPUFramebuffer, WebGLGPUInputAssembler, WebGLGPUObjectType, WebGLGPUPipelineLayout, WebGLGPUPipelineState, WebGLGPURenderPass, WebGLGPUSampler, WebGLGPUShader, WebGLGPUShaderStage, WebGLGPUTexture, WebGLGPUTextureView } from './webgl-gpu-objects';
import {
WebGLGPUBinding,
WebGLGPUBindingLayout,
WebGLGPUBuffer,
WebGLGPUFramebuffer,
WebGLGPUInputAssembler,
WebGLGPUObjectType,
WebGLGPUPipelineLayout,
WebGLGPUPipelineState,
WebGLGPURenderPass,
WebGLGPUSampler,
WebGLGPUShader,
WebGLGPUShaderStage,
WebGLGPUTexture,
WebGLGPUTextureView,
} from './webgl-gpu-objects';
import { WebGLGFXInputAssembler } from './webgl-input-assembler';
import { WebGLGFXPipelineLayout } from './webgl-pipeline-layout';
import { WebGLGFXPipelineState } from './webgl-pipeline-state';
@@ -475,7 +514,11 @@ export class WebGLGFXDevice extends GFXDevice {
}
}
public copyFramebufferToBuffer (srcFramebuffer: GFXFramebuffer, dstBuffer: ArrayBuffer, regions: GFXBufferTextureCopy[]) {
public copyFramebufferToBuffer (
srcFramebuffer: GFXFramebuffer,
dstBuffer: ArrayBuffer,
regions: GFXBufferTextureCopy[]) {
const gl = this._webGLRC as WebGLRenderingContext;
const gpuFramebuffer = (srcFramebuffer as WebGLGFXFramebuffer).gpuFramebuffer;
@@ -497,7 +540,8 @@ export class WebGLGFXDevice extends GFXDevice {
const memSize = GFXFormatSize(GFXFormat.RGBA8, w, h, 1);
const data = view.subarray(buffOffset, buffOffset + memSize);
gl.readPixels(region.texOffset.x, region.texOffset.y, w, h, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, data);
gl.readPixels(region.texOffset.x, region.texOffset.y, w, h,
WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, data);
}
if (this.stateCache.glFramebuffer !== curFBO) {
@@ -955,37 +999,37 @@ export class WebGLGFXDevice extends GFXDevice {
private initStates (gl: WebGLRenderingContext) {
gl.activeTexture(WebGLRenderingContext.TEXTURE0);
gl.pixelStorei(WebGLRenderingContext.PACK_ALIGNMENT, 1);
gl.pixelStorei(WebGLRenderingContext.UNPACK_ALIGNMENT, 1);
gl.activeTexture(gl.TEXTURE0);
gl.pixelStorei(gl.PACK_ALIGNMENT, 1);
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
gl.bindFramebuffer(WebGLRenderingContext.FRAMEBUFFER, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// rasteriazer state
gl.enable(WebGLRenderingContext.CULL_FACE);
gl.cullFace(WebGLRenderingContext.BACK);
gl.frontFace(WebGLRenderingContext.CCW);
gl.enable(gl.CULL_FACE);
gl.cullFace(gl.BACK);
gl.frontFace(gl.CCW);
gl.polygonOffset(0.0, 0.0);
// depth stencil state
gl.enable(WebGLRenderingContext.DEPTH_TEST);
gl.enable(gl.DEPTH_TEST);
gl.depthMask(true);
gl.depthFunc(WebGLRenderingContext.LESS);
gl.depthFunc(gl.LESS);
gl.stencilFuncSeparate(WebGLRenderingContext.FRONT, WebGLRenderingContext.ALWAYS, 1, 0xffffffff);
gl.stencilOpSeparate(WebGLRenderingContext.FRONT, WebGLRenderingContext.KEEP, WebGLRenderingContext.KEEP, WebGLRenderingContext.KEEP);
gl.stencilMaskSeparate(WebGLRenderingContext.FRONT, 0xffffffff);
gl.stencilFuncSeparate(WebGLRenderingContext.BACK, WebGLRenderingContext.ALWAYS, 1, 0xffffffff);
gl.stencilOpSeparate(WebGLRenderingContext.BACK, WebGLRenderingContext.KEEP, WebGLRenderingContext.KEEP, WebGLRenderingContext.KEEP);
gl.stencilMaskSeparate(WebGLRenderingContext.BACK, 0xffffffff);
gl.stencilFuncSeparate(gl.FRONT, gl.ALWAYS, 1, 0xffffffff);
gl.stencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.KEEP);
gl.stencilMaskSeparate(gl.FRONT, 0xffffffff);
gl.stencilFuncSeparate(gl.BACK, gl.ALWAYS, 1, 0xffffffff);
gl.stencilOpSeparate(gl.BACK, gl.KEEP, gl.KEEP, gl.KEEP);
gl.stencilMaskSeparate(gl.BACK, 0xffffffff);
gl.disable(WebGLRenderingContext.STENCIL_TEST);
gl.disable(gl.STENCIL_TEST);
// blend state
gl.disable(WebGLRenderingContext.SAMPLE_ALPHA_TO_COVERAGE);
gl.disable(WebGLRenderingContext.BLEND);
gl.blendEquationSeparate(WebGLRenderingContext.FUNC_ADD, WebGLRenderingContext.FUNC_ADD);
gl.blendFuncSeparate(WebGLRenderingContext.ONE, WebGLRenderingContext.ZERO, WebGLRenderingContext.ONE, WebGLRenderingContext.ZERO);
gl.disable(gl.SAMPLE_ALPHA_TO_COVERAGE);
gl.disable(gl.BLEND);
gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
gl.blendFuncSeparate(gl.ONE, gl.ZERO, gl.ONE, gl.ZERO);
gl.colorMask(true, true, true, true);
gl.blendColor(0.0, 0.0, 0.0, 0.0);
}
+17 -2
View File
@@ -1,9 +1,24 @@
import { GFXBindingType, GFXBufferUsage, GFXBufferUsageBit, GFXFormat, GFXMemoryUsage, GFXMemoryUsageBit, GFXShaderType, GFXTextureFlagBit, GFXTextureFlags, GFXTextureType, GFXTextureUsage, GFXTextureUsageBit, GFXTextureViewType, GFXType } from '../define';
import { GFXBufferSource } from '../buffer';
import {
GFXBindingType,
GFXBufferUsage,
GFXBufferUsageBit,
GFXFormat,
GFXMemoryUsage,
GFXMemoryUsageBit,
GFXShaderType,
GFXTextureFlagBit,
GFXTextureFlags,
GFXTextureType,
GFXTextureUsage,
GFXTextureUsageBit,
GFXTextureViewType,
GFXType,
} from '../define';
import { IGFXInputAttribute } from '../input-assembler';
import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../pipeline-state';
import { GFXColorAttachment, GFXDepthStencilAttachment } from '../render-pass';
import { GFXUniformBlock, GFXUniformSampler, IGFXShaderMacro } from '../shader';
import { GFXBufferSource } from '../buffer';
export enum WebGLGPUObjectType {
UNKNOWN,
-2
View File
@@ -52,12 +52,10 @@ export class ForwardStage extends RenderStage {
[camera.clearColor], camera.clearDepth, camera.clearStencil);
for (const item of queue.opaques) {
cmdBuff.updateBuffer(item.model.localUBO, item.model.localUniforms.view);
cmdBuff.execute([item.cmdBuff]);
}
for (const item of queue.transparents) {
cmdBuff.updateBuffer(item.model.localUBO, item.model.localUniforms.view);
cmdBuff.execute([item.cmdBuff]);
}
+1 -1
View File
@@ -363,7 +363,7 @@ export abstract class RenderPipeline {
continue;
}
model.updateRenderData();
model.updateUBOs();
// add model pass to render queue
this._queue.add(model, camera);
+4
View File
@@ -54,6 +54,10 @@ export class RenderQueue {
for (let i = 0; i < model.passes.length; ++i) {
const pass = model.passes[i];
// update pass
pass.update();
const cmdBuff = model.commandBuffers[i];
model.node.getWorldPosition();
+28 -27
View File
@@ -39,7 +39,7 @@ export class Model {
private _modelBounds: aabb;
private _material: Material | null;
private _cmdBuffers: GFXCommandBuffer[];
private _localUniforms: UBOLocal | null;
private _uboLocal: UBOLocal;
private _localUBO: GFXBuffer | null;
/**
* Setup a default empty model
@@ -62,12 +62,11 @@ export class Model {
this._castShadow = false;
this._material = null;
this._cmdBuffers = new Array<GFXCommandBuffer>();
this._localUniforms = null;
this._uboLocal = new UBOLocal();
this._localUBO = null;
}
public initialize () {
this._localUniforms = new UBOLocal();
this._localUBO = cc.director.root.device.createBuffer({
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
memUsage: GFXMemoryUsageBit.HOST,
@@ -75,7 +74,7 @@ export class Model {
stride: UBOLocal.SIZE,
});
if (this._localUBO) {
this._localUBO.update(this._localUniforms.view);
this._localUBO.update(this._uboLocal.view);
}
}
@@ -102,22 +101,20 @@ export class Model {
this._node._rot, this._node._scale, this._worldBounds);
}
public updateRenderData () {
public updateUBOs () {
mat4.array(_temp_floatx16, this._node._mat);
this._node._mat.invert(_temp_mat4);
if (this._localUniforms) {
this._localUniforms.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET);
if (this._uboLocal) {
this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET);
}
mat4.array(_temp_floatx16, _temp_mat4);
if (this._localUniforms) {
this._localUniforms.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET);
if (this._uboLocal) {
this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET);
}
/*
if (this._localUBO) {
this._localUBO.update(this._localUniforms.view);
this._localUBO.update(this._uboLocal.view);
}
*/
}
/**
@@ -211,23 +208,27 @@ export class Model {
public recordCommandBuffer (index: number) {
const pass = (this._material as Material).passes[index];
const cmdBufferInfo = {
allocator: cc.director.root.device.commandAllocator,
type: GFXCommandBufferType.SECONDARY,
};
if (this._cmdBuffers[index] == null) {
const cmdBufferInfo = {
allocator: cc.director.root.device.commandAllocator,
type: GFXCommandBufferType.SECONDARY,
};
this._cmdBuffers[index] = cc.director.root.device.createCommandBuffer(cmdBufferInfo);
}
if (this._localUBO) {
pass.bindingLayout.bindBuffer(UBOLocal.BLOCK.binding, this._localUBO);
}
const localUBO = this._localUBO as GFXBuffer;
const subMesh = this._subMeshObject as IRenderingSubmesh;
pass.bindingLayout.bindBuffer(UBOLocal.BLOCK.binding, localUBO);
pass.update();
this._cmdBuffers[index].begin();
this._cmdBuffers[index].bindPipelineState(pass.pipelineState);
this._cmdBuffers[index].bindBindingLayout(pass.bindingLayout);
this._cmdBuffers[index].bindInputAssembler((this._subMeshObject as IRenderingSubmesh).inputAssembler);
this._cmdBuffers[index].draw((this._subMeshObject as IRenderingSubmesh).inputAssembler);
this._cmdBuffers[index].end();
const cmdBuff = this._cmdBuffers[index];
cmdBuff.begin();
cmdBuff.bindPipelineState(pass.pipelineState);
cmdBuff.bindBindingLayout(pass.bindingLayout);
cmdBuff.bindInputAssembler(subMesh.inputAssembler);
cmdBuff.draw(subMesh.inputAssembler);
cmdBuff.end();
}
/**
@@ -246,8 +247,8 @@ export class Model {
return this._cmdBuffers;
}
get localUniforms (): UBOLocal {
return this._localUniforms as UBOLocal;
get uboLocal (): UBOLocal {
return this._uboLocal;
}
get localUBO (): GFXBuffer {