From 02c52a77964ef8fcb2a686c005775be70f8108ef Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Wed, 26 Dec 2018 09:43:48 +0800 Subject: [PATCH] mod gfx --- cocos/renderer/gfx/gfx-binding-set-layout.ts | 37 +- cocos/renderer/gfx/gfx-buffer.ts | 21 +- cocos/renderer/gfx/gfx-command-allocator.ts | 2 +- cocos/renderer/gfx/gfx-command-buffer.ts | 15 +- cocos/renderer/gfx/gfx-define.ts | 183 +-- cocos/renderer/gfx/gfx-input-assembler.ts | 115 ++ cocos/renderer/gfx/gfx-input-state.ts | 26 - cocos/renderer/gfx/gfx-pipeline-layout.ts | 4 +- cocos/renderer/gfx/gfx-pipeline-state.ts | 23 +- cocos/renderer/gfx/gfx-render-pass.ts | 16 +- cocos/renderer/gfx/gfx-sampler.ts | 28 +- cocos/renderer/gfx/gfx-shader.ts | 30 +- cocos/renderer/gfx/webgl/webgl-commands.ts | 1172 +++++++++++++++-- .../gfx/webgl/webgl-gfx-binding-set-layout.ts | 55 + cocos/renderer/gfx/webgl/webgl-gfx-buffer.ts | 50 +- .../gfx/webgl/webgl-gfx-command-allocator.ts | 56 +- .../gfx/webgl/webgl-gfx-command-buffer.ts | 136 ++ cocos/renderer/gfx/webgl/webgl-gfx-device.ts | 381 ++++-- .../gfx/webgl/webgl-gfx-framebuffer.ts | 4 + .../gfx/webgl/webgl-gfx-input-assembler.ts | 40 + .../gfx/webgl/webgl-gfx-input-state.ts | 22 - .../gfx/webgl/webgl-gfx-pipeline-layout.ts | 32 + .../gfx/webgl/webgl-gfx-pipeline-state.ts | 45 + cocos/renderer/gfx/webgl/webgl-gfx-queue.ts | 13 +- .../gfx/webgl/webgl-gfx-render-pass.ts | 18 +- cocos/renderer/gfx/webgl/webgl-gfx-sampler.ts | 14 + cocos/renderer/gfx/webgl/webgl-gfx-shader.ts | 1 + cocos/renderer/gfx/webgl/webgl-gpu-objects.ts | 290 ++-- cocos/renderer/gfx/webgl/webgl-state-cache.ts | 21 +- 29 files changed, 2277 insertions(+), 573 deletions(-) create mode 100644 cocos/renderer/gfx/gfx-input-assembler.ts delete mode 100644 cocos/renderer/gfx/gfx-input-state.ts create mode 100644 cocos/renderer/gfx/webgl/webgl-gfx-binding-set-layout.ts create mode 100644 cocos/renderer/gfx/webgl/webgl-gfx-command-buffer.ts create mode 100644 cocos/renderer/gfx/webgl/webgl-gfx-input-assembler.ts delete mode 100644 cocos/renderer/gfx/webgl/webgl-gfx-input-state.ts create mode 100644 cocos/renderer/gfx/webgl/webgl-gfx-pipeline-layout.ts create mode 100644 cocos/renderer/gfx/webgl/webgl-gfx-pipeline-state.ts diff --git a/cocos/renderer/gfx/gfx-binding-set-layout.ts b/cocos/renderer/gfx/gfx-binding-set-layout.ts index 03b4d2512d..32985e531c 100644 --- a/cocos/renderer/gfx/gfx-binding-set-layout.ts +++ b/cocos/renderer/gfx/gfx-binding-set-layout.ts @@ -7,7 +7,6 @@ export const enum GFXBindingType { UNKNOWN, UNIFORM_BUFFER, SAMPLER, - TEXTURE_VIEW, STORAGE_BUFFER, }; @@ -39,6 +38,42 @@ export abstract class GFXBindingSetLayout { public abstract initialize(info : GFXBindingSetLayoutInfo) : boolean; public abstract destroy() : void; + public bindBuffer(binding: number, buffer: GFXBuffer) { + for (let i = 0; i < this._bindingUnits.length; ++i) { + let bindingUnit = this._bindingUnits[i]; + if (bindingUnit.type === GFXBindingType.UNIFORM_BUFFER) { + bindingUnit.buffer = buffer; + return; + } else { + console.error("Setting binding is not GFXBindingType.UNIFORM_BUFFER."); + } + } + } + + public bindSampler(binding: number, sampler: GFXSampler) { + for (let i = 0; i < this._bindingUnits.length; ++i) { + let bindingUnit = this._bindingUnits[i]; + if (bindingUnit.type === GFXBindingType.SAMPLER) { + bindingUnit.sampler = sampler; + return; + } else { + console.error("Setting binding is not GFXBindingType.SAMPLER."); + } + } + } + + public bindTextureView(binding: number, texView: GFXTextureView) { + for (let i = 0; i < this._bindingUnits.length; ++i) { + let bindingUnit = this._bindingUnits[i]; + if (bindingUnit.type === GFXBindingType.SAMPLER) { + bindingUnit.texView = texView; + return; + } else { + console.error("Setting binding is not GFXBindingType.SAMPLER."); + } + } + } + protected _device : GFXDevice; protected _bindingUnits : GFXBindingUnit[] = []; }; diff --git a/cocos/renderer/gfx/gfx-buffer.ts b/cocos/renderer/gfx/gfx-buffer.ts index 9f3e2e87da..4fe393529a 100644 --- a/cocos/renderer/gfx/gfx-buffer.ts +++ b/cocos/renderer/gfx/gfx-buffer.ts @@ -2,14 +2,12 @@ import { GFXDevice } from './gfx-device'; export const enum GFXBufferUsageBit { NONE = 0, - MAP_READ = 0x1, - MAP_WRITE = 0x2, - TRANSFER_SRC = 0x4, - TRANSFER_DST = 0x8, - INDEX = 0x10, - VERTEX = 0x20, - UNIFORM = 0x40, - STORAGE = 0x80, + TRANSFER_SRC = 0x1, + TRANSFER_DST = 0x2, + INDEX = 0x4, + VERTEX = 0x8, + UNIFORM = 0x10, + STORAGE = 0x20, }; export type GFXBufferUsage = GFXBufferUsageBit; @@ -45,7 +43,7 @@ export abstract class GFXBuffer { public abstract initialize(info : GFXBufferInfo) : boolean; public abstract destroy() : void; - public abstract update(buffer : ArrayBuffer, offset : number); + public abstract update(buffer : Buffer, offset : number); public get usage(): GFXBufferUsage { return this._usage; @@ -63,7 +61,7 @@ export abstract class GFXBuffer { return this._stride; } - public get buffer(): ArrayBuffer | null { + public get buffer(): Buffer | null { return this._buffer; } @@ -72,5 +70,6 @@ export abstract class GFXBuffer { protected _memUsage : GFXMemoryUsage = GFXMemoryUsageBit.NONE; protected _size : number = 0; protected _stride : number = 1; - protected _buffer : ArrayBuffer | null = null; + protected _arrayBuffer : ArrayBuffer | null = null; + protected _buffer: Buffer | null = null; }; diff --git a/cocos/renderer/gfx/gfx-command-allocator.ts b/cocos/renderer/gfx/gfx-command-allocator.ts index 12a515ead8..fc4df8554d 100644 --- a/cocos/renderer/gfx/gfx-command-allocator.ts +++ b/cocos/renderer/gfx/gfx-command-allocator.ts @@ -7,7 +7,7 @@ export abstract class GFXCommandAllocator { } public abstract initialize() : boolean; - public abstract destroy() : void; + public abstract destroy(); protected _device : GFXDevice; } \ No newline at end of file diff --git a/cocos/renderer/gfx/gfx-command-buffer.ts b/cocos/renderer/gfx/gfx-command-buffer.ts index db15385888..bb18603cc8 100644 --- a/cocos/renderer/gfx/gfx-command-buffer.ts +++ b/cocos/renderer/gfx/gfx-command-buffer.ts @@ -1,5 +1,9 @@ import { GFXDevice } from './gfx-device'; import { GFXCommandAllocator } from './gfx-command-allocator'; +import { GFXBuffer } from './gfx-buffer'; +import { GFXInputAssembler } from './gfx-input-assembler'; +import { GFXPipelineState } from './gfx-pipeline-state'; +import { GFXBindingSetLayout } from './gfx-binding-set-layout'; export const enum GFXCommandBufferType { PRIMARY, @@ -18,12 +22,21 @@ export abstract class GFXCommandBuffer { } public abstract initialize(info : GFXCommandBufferInfo) : boolean; - public abstract destroy() : void; + public abstract destroy(); + + public abstract begin(); + public abstract end(); + public abstract bindPipelineState(pipelineState : GFXPipelineState); + public abstract bindBindingSetLayout(bindingSetLayout : GFXBindingSetLayout); + public abstract bindInputAssembler(inputAssembler : GFXInputAssembler); + public abstract draw(inputAssembler : GFXInputAssembler); + public abstract updateBuffer(buffer : GFXBuffer, data : ArrayBuffer, offset : number); public get type(): number { return this._type; } protected _device : GFXDevice; + protected _allocator : GFXCommandAllocator | null = null; protected _type : GFXCommandBufferType = GFXCommandBufferType.PRIMARY; }; diff --git a/cocos/renderer/gfx/gfx-define.ts b/cocos/renderer/gfx/gfx-define.ts index 76d20ab9f5..4ed2c79f12 100644 --- a/cocos/renderer/gfx/gfx-define.ts +++ b/cocos/renderer/gfx/gfx-define.ts @@ -182,6 +182,7 @@ export const enum GFXFormat { export class GFXFormatInfo { readonly name : string = "UNKNOWN"; readonly size : number = 0; + readonly count : number = 0; readonly isFloating : boolean = false; readonly hasAlpha : boolean = false; readonly hasDepth : boolean = false; @@ -191,107 +192,107 @@ export class GFXFormatInfo { export const GFXFormatInfos : GFXFormatInfo[] = [ - { name : "UNKNOWN", size : 0, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "UNKNOWN", size : 0, count : 0, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "A8", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "L8", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "LA8", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "A8", size : 1, count : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "L8", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "LA8", size : 1, count : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R8", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R8SN", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R8UI", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R8I", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R16F", size : 2, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R16UI", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R16I", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R32F", size : 4, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R32UI", size : 4, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R32I", size : 4, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R8", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R8SN", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R8UI", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R8I", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R16F", size : 2, count : 1, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R16UI", size : 2, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R16I", size : 2, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R32F", size : 4, count : 1, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R32UI", size : 4, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R32I", size : 4, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG8", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG8SN", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG8UI", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG8I", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG16F", size : 4, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG16UI", size : 4, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG16I", size : 4, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG32F", size : 8, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG32UI", size : 8, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RG32I", size : 8, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG8", size : 2, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG8SN", size : 2, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG8UI", size : 2, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG8I", size : 2, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG16F", size : 4, count : 2, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG16UI", size : 4, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG16I", size : 4, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG32F", size : 8, count : 2, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG32UI", size : 8, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RG32I", size : 8, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB8", size : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "SRGB8", size : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB8SN", size : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB8UI", size : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB8I", size : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB16F", size : 6, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB16UI", size : 6, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB16I", size : 6, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB32F", size : 12, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB32UI", size : 12, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB32I", size : 12, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB8", size : 3, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "SRGB8", size : 3, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB8SN", size : 3, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB8UI", size : 3, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB8I", size : 3, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB16F", size : 6, count : 3, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB16UI", size : 6, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB16I", size : 6, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB32F", size : 12, count : 3, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB32UI", size : 12, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB32I", size : 12, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA8", size : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "SRGB8_A8", size : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA8SN", size : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA8UI", size : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA8I", size : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA16F", size : 8, isFloating : true, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA16UI", size : 8, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA16I", size : 8, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA32F", size : 16, isFloating : true, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA32UI", size : 16, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA32I", size : 16, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA8", size : 4, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "SRGB8_A8", size : 4, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA8SN", size : 4, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA8UI", size : 4, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA8I", size : 4, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA16F", size : 8, count : 4, isFloating : true, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA16UI", size : 8, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA16I", size : 8, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA32F", size : 16, count : 4, isFloating : true, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA32UI", size : 16, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA32I", size : 16, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R5G6B5", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "R11G11B10F", size : 4, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB5A1", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGBA4", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB10A2", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB10A2UI", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "RGB9E5", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R5G6B5", size : 2, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "R11G11B10F", size : 4, count : 3, isFloating : true, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB5A1", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGBA4", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB10A2", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB10A2UI", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, + { name : "RGB9E5", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : false }, - { name : "D16", size : 2, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, - { name : "D24", size : 3, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, - { name : "D24S8", size : 4, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : true, isCompressed : false }, - { name : "D32F", size : 4, isFloating : true, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, - { name : "D32FS8", size : 5, isFloating : true, hasAlpha : false, hasDepth : true, hasStencil : true, isCompressed : false }, + { name : "D16", size : 2, count : 1, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, + { name : "D24", size : 3, count : 1, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, + { name : "D24S8", size : 4, count : 2, isFloating : false, hasAlpha : false, hasDepth : true, hasStencil : true, isCompressed : false }, + { name : "D32F", size : 4, count : 1, isFloating : true, hasAlpha : false, hasDepth : true, hasStencil : false, isCompressed : false }, + { name : "D32FS8", size : 5, count : 2, isFloating : true, hasAlpha : false, hasDepth : true, hasStencil : true, isCompressed : false }, - { name : "BC1", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC1_ALPHA", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC1_SRGB", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC1_SRGB_ALPHA", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC2", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC2_SRGB", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC3", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC3_SRGB", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC4", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC4_SNORM", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC5", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC5_SNORM", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC6H_UF16", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC6H_SF16", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC7", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "BC7_SRGB", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC1", size : 1, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC1_ALPHA", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC1_SRGB", size : 1, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC1_SRGB_ALPHA", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC2", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC2_SRGB", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC3", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC3_SRGB", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC4", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC4_SNORM", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC5", size : 1, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC5_SNORM", size : 1, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC6H_UF16", size : 1, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC6H_SF16", size : 1, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC7", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "BC7_SRGB", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "ETC_RGB8", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "ETC2_RGB8", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "ETC2_SRGB8", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "ETC2_RGB8_A1", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "ETC2_SRGB8_A1", size : 1, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "ETC2_RGBA8", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "ETC2_SRGB8_A8", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "EAC_R11", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "EAC_R11SN", size : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "EAC_RG11", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "EAC_RG11SN", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "ETC_RGB8", size : 1, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "ETC2_RGB8", size : 1, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "ETC2_SRGB8", size : 1, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "ETC2_RGB8_A1", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "ETC2_SRGB8_A1", size : 1, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "ETC2_RGBA8", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "ETC2_SRGB8_A8", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "EAC_R11", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "EAC_R11SN", size : 1, count : 1, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "EAC_RG11", size : 2, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "EAC_RG11SN", size : 2, count : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "PVRTC_RGB2", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "PVRTC_RGBA2", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "PVRTC_RGB4", size : 2, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "PVRTC_RGBA4", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "PVRTC2_2BPP", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, - { name : "PVRTC2_4BPP", size : 2, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "PVRTC_RGB2", size : 2, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "PVRTC_RGBA2", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "PVRTC_RGB4", size : 2, count : 3, isFloating : false, hasAlpha : false, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "PVRTC_RGBA4", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "PVRTC2_2BPP", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, + { name : "PVRTC2_4BPP", size : 2, count : 4, isFloating : false, hasAlpha : true, hasDepth : false, hasStencil : false, isCompressed : true }, ]; export function GFXFormatSize(format : GFXFormat, width : number, height : number, depth : number) : number { diff --git a/cocos/renderer/gfx/gfx-input-assembler.ts b/cocos/renderer/gfx/gfx-input-assembler.ts new file mode 100644 index 0000000000..50aa6dfc6a --- /dev/null +++ b/cocos/renderer/gfx/gfx-input-assembler.ts @@ -0,0 +1,115 @@ +import { GFXDevice } from './gfx-device'; +import { GFXFormat } from './gfx-define'; +import { GFXBuffer } from './gfx-buffer'; +import { GFXShader } from './gfx-shader'; + +export class GFXInputAttribute { + name: string = ""; + format: GFXFormat = GFXFormat.UNKNOWN; + stream: number = 0; + isInstanced: boolean = false; +}; + +export class GFXInputAssemblerInfo { + attributes: GFXInputAttribute[] = []; + shader: GFXShader | null = null; + vertexBuffers: GFXBuffer[] = []; + indexBuffer: GFXBuffer | null = null; + isIndirect: boolean = false; +}; + +export abstract class GFXInputAssembler { + + constructor(device: GFXDevice) { + this._device = device; + } + + public abstract initialize(info: GFXInputAssemblerInfo): boolean; + public abstract destroy(): void; + + public get vertexBuffers(): GFXBuffer[] { + return this._vertexBuffers; + } + + public get indexBuffer(): GFXBuffer | null { + return this._indexBuffer; + } + + public getVertexBuffer(stream: number): GFXBuffer | null { + if (stream < this._vertexBuffers.length) { + return this._vertexBuffers[stream]; + } else { + return null; + } + } + + public get vertexCount(): number { + return this._vertexCount; + } + + public set vertexCount(count : number) { + this._vertexCount = count; + } + + public get firstVertex(): number { + return this._firstVertex; + } + + public set firstVertex(first : number) { + this._firstVertex = first; + } + + public get indexCount(): number { + return this._indexCount; + } + + public set indexCount(count : number) { + this._indexCount = count; + } + + public get firstIndex(): number { + return this._firstIndex; + } + + public set firstIndex(first : number) { + this._firstIndex = first; + } + + public get vertexOffset(): number { + return this._vertexOffset; + } + + public set vertexOffset(offset : number) { + this._vertexOffset = offset; + } + + public get instanceCount(): number { + return this._instanceCount; + } + + public set instanceCount(count : number) { + this._instanceCount = count; + } + + public get firstInstance(): number { + return this._firstInstance; + } + + public set firstInstance(first : number) { + this._firstInstance = first; + } + + protected _device: GFXDevice; + protected _attributes: GFXInputAttribute[] = []; + protected _vertexBuffers: GFXBuffer[] = []; + protected _indexBuffer: GFXBuffer | null = null; + protected _vertexCount: number = 0; + protected _firstVertex: number = 0; + protected _indexCount: number = 0; + protected _firstIndex: number = 0; + protected _vertexOffset: number = 0; + protected _instanceCount: number = 0; + protected _firstInstance: number = 0; + + protected _isIndirect: boolean = false; +}; diff --git a/cocos/renderer/gfx/gfx-input-state.ts b/cocos/renderer/gfx/gfx-input-state.ts deleted file mode 100644 index 2a682f6f75..0000000000 --- a/cocos/renderer/gfx/gfx-input-state.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { GFXDevice } from './gfx-device'; -import { GFX_MAX_VERTEX_ATTRIBUTES, GFXFormat } from './gfx-define'; - -export class GFXInputAttribute { - name : string = ""; - format : GFXFormat = GFXFormat.UNKNOWN; - //offset : number = 0; - isInstanced : boolean = false; -}; - -export class GFXInputStateInfo { - attributes: GFXInputAttribute[] = []; -}; - -export abstract class GFXInputState { - - constructor(device : GFXDevice) { - this._device = device; - } - - public abstract initialize(info : GFXInputStateInfo) : boolean; - public abstract destroy() : void; - - protected _device : GFXDevice; - protected _attributes: GFXInputAttribute[] = []; -}; diff --git a/cocos/renderer/gfx/gfx-pipeline-layout.ts b/cocos/renderer/gfx/gfx-pipeline-layout.ts index 0f3cbb944a..5190403b0a 100644 --- a/cocos/renderer/gfx/gfx-pipeline-layout.ts +++ b/cocos/renderer/gfx/gfx-pipeline-layout.ts @@ -1,5 +1,4 @@ import { GFXDevice } from './gfx-device'; -import { GFXRenderPass } from './gfx-render-pass'; import { GFXBindingSetLayout } from './gfx-binding-set-layout'; import { GFXShaderType } from './gfx-shader'; @@ -24,5 +23,6 @@ export abstract class GFXPipelineLayout { public abstract destroy() : void; protected _device : GFXDevice; - protected _renderPass: GFXRenderPass | null = null; + protected _pushConstantsRanges : GFXPushConstantRange[] = []; + protected _layouts: GFXBindingSetLayout[] = []; }; diff --git a/cocos/renderer/gfx/gfx-pipeline-state.ts b/cocos/renderer/gfx/gfx-pipeline-state.ts index c02ddc9f99..29de3c15ef 100644 --- a/cocos/renderer/gfx/gfx-pipeline-state.ts +++ b/cocos/renderer/gfx/gfx-pipeline-state.ts @@ -2,6 +2,8 @@ import { GFXDevice } from './gfx-device'; import { GFX_MAX_VERTEX_ATTRIBUTES, GFXFormat } from './gfx-define'; import { GFXShader } from './gfx-shader'; import { GFXRenderPass } from './gfx-render-pass'; +import { GFXPipelineLayout } from './gfx-pipeline-layout'; +import { GFXInputAttribute } from './gfx-input-assembler'; export const enum GFXPrimitiveMode { POINT_LIST, @@ -132,7 +134,7 @@ export class GFXDepthStencilState { frontStencilWriteMask : number = 0xffffffff; frontStencilFailOp : GFXStencilOp = GFXStencilOp.KEEP; frontStencilDepthFailOp : GFXStencilOp = GFXStencilOp.KEEP; - frontStencilPassFailOp : GFXStencilOp = GFXStencilOp.KEEP; + frontStencilPassOp : GFXStencilOp = GFXStencilOp.KEEP; frontStencilRef : number = 1; isBackStencilTest : boolean = false; backStencilFunc : GFXComparisonFunc = GFXComparisonFunc.ALWAYS; @@ -140,7 +142,7 @@ export class GFXDepthStencilState { backStencilWriteMask : number = 0xffffffff; backStencilFailOp : GFXStencilOp = GFXStencilOp.KEEP; backStencilDepthFailOp : GFXStencilOp = GFXStencilOp.KEEP; - backStencilPassFailOp : GFXStencilOp = GFXStencilOp.KEEP; + backStencilPassOp : GFXStencilOp = GFXStencilOp.KEEP; backStencilRef : number = 1; public compare(state : GFXDepthStencilState) : boolean { @@ -153,7 +155,7 @@ export class GFXDepthStencilState { (this.frontStencilWriteMask === state.frontStencilWriteMask) && (this.frontStencilFailOp === state.frontStencilFailOp) && (this.frontStencilDepthFailOp === state.frontStencilDepthFailOp) && - (this.frontStencilPassFailOp === state.frontStencilPassFailOp) && + (this.frontStencilPassOp === state.frontStencilPassOp) && (this.frontStencilRef === state.frontStencilRef) && (this.isBackStencilTest === state.isBackStencilTest) && (this.backStencilFunc === state.backStencilFunc) && @@ -161,7 +163,7 @@ export class GFXDepthStencilState { (this.backStencilWriteMask === state.backStencilWriteMask) && (this.backStencilFailOp === state.backStencilFailOp) && (this.backStencilDepthFailOp === state.backStencilDepthFailOp) && - (this.backStencilPassFailOp === state.backStencilPassFailOp) && + (this.backStencilPassOp === state.backStencilPassOp) && (this.backStencilRef === state.backStencilRef); } }; @@ -196,12 +198,18 @@ export class GFXBlendState { targets : GFXBlendTarget[] = [new GFXBlendTarget]; } +export class GFXInputState { + attributes: GFXInputAttribute[] = []; +}; + export class GFXPipelineStateInfo { - primitiveMode : GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; + primitive : GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; shader : GFXShader | null = null; + is : GFXInputState | null = null; rs : GFXRasterizerState | null = null; dss : GFXDepthStencilState | null = null; - bs : GFXBlendState = new GFXBlendState; + bs : GFXBlendState | null = null; + layout : GFXPipelineLayout | null = null; renderPass : GFXRenderPass | null = null; }; @@ -216,8 +224,11 @@ export abstract class GFXPipelineState { protected _device : GFXDevice; protected _shader : GFXShader | null = null; + protected _primitive : GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; + protected _is : GFXInputState | null = null; protected _rs : GFXRasterizerState | null = null; protected _dss : GFXDepthStencilState | null = null; protected _bs : GFXBlendState | null = null; + protected _layout: GFXPipelineLayout | null = null; protected _renderPass: GFXRenderPass | null = null; }; diff --git a/cocos/renderer/gfx/gfx-render-pass.ts b/cocos/renderer/gfx/gfx-render-pass.ts index 03634718e0..0fc5a925cc 100644 --- a/cocos/renderer/gfx/gfx-render-pass.ts +++ b/cocos/renderer/gfx/gfx-render-pass.ts @@ -30,7 +30,7 @@ export const enum GFXTextureLayout PRESENT_SRC, }; -export class ColorAttachmentInfo +export class GFXColorAttachment { format : GFXFormat = GFXFormat.UNKNOWN; loadOp : GFXLoadOp = GFXLoadOp.CLEAR; @@ -40,11 +40,11 @@ export class ColorAttachmentInfo endLayout : GFXTextureLayout = GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL; }; -export class DepthStencilAttachmentInfo +export class GFXDepthStencilAttachment { format : GFXFormat = GFXFormat.UNKNOWN; - loadOp : GFXLoadOp = GFXLoadOp.CLEAR; - storeOp : GFXStoreOp = GFXStoreOp.STORE; + depthLoadOp : GFXLoadOp = GFXLoadOp.CLEAR; + depthStoreOp : GFXStoreOp = GFXStoreOp.STORE; stencilLoadOp : GFXLoadOp = GFXLoadOp.CLEAR; stencilStoreOp : GFXStoreOp = GFXStoreOp.STORE; sampleCount : number = 1; @@ -71,8 +71,8 @@ export class GFXSubPassInfo export class GFXRenderPassInfo { - colorInfos : ColorAttachmentInfo[] = []; - depthStencilInfo : DepthStencilAttachmentInfo | null = null; + colorInfos : GFXColorAttachment[] = []; + depthStencilInfo : GFXDepthStencilAttachment | null = null; subPasses : GFXSubPassInfo[] = []; }; @@ -86,7 +86,7 @@ export abstract class GFXRenderPass { public abstract destroy() : void; protected _device : GFXDevice; - protected _colorInfos : ColorAttachmentInfo[] = []; - protected _depthStencilInfo : DepthStencilAttachmentInfo | null = null; + protected _colorInfos : GFXColorAttachment[] = []; + protected _depthStencilInfo : GFXDepthStencilAttachment | null = null; protected _subPasses : GFXSubPassInfo[] = []; }; diff --git a/cocos/renderer/gfx/gfx-sampler.ts b/cocos/renderer/gfx/gfx-sampler.ts index 5b44c35ad0..4750c78372 100644 --- a/cocos/renderer/gfx/gfx-sampler.ts +++ b/cocos/renderer/gfx/gfx-sampler.ts @@ -1,14 +1,14 @@ import { GFXDevice } from './gfx-device'; import { GFXComparisonFunc } from './gfx-pipeline-state'; -export const enum GFXFilterOp { +export const enum GFXFilter { NONE, POINT, LINEAR, ANISOTROPIC, }; -export const enum GFXAddressMode { +export const enum GFXAddress { WRAP, MIRROR, CLAMP, @@ -17,12 +17,12 @@ export const enum GFXAddressMode { export class GFXSamplerInfo { name : string = ""; - minFilter : GFXFilterOp = GFXFilterOp.LINEAR; - magFilter : GFXFilterOp = GFXFilterOp.LINEAR; - mipFilter : GFXFilterOp = GFXFilterOp.LINEAR; - addressU : GFXAddressMode = GFXAddressMode.WRAP; - addressV : GFXAddressMode = GFXAddressMode.WRAP; - addressW : GFXAddressMode = GFXAddressMode.WRAP; + minFilter : GFXFilter = GFXFilter.LINEAR; + magFilter : GFXFilter = GFXFilter.LINEAR; + mipFilter : GFXFilter = GFXFilter.LINEAR; + addressU : GFXAddress = GFXAddress.WRAP; + addressV : GFXAddress = GFXAddress.WRAP; + addressW : GFXAddress = GFXAddress.WRAP; maxAnisotropy : number = 16; cmpFunc : GFXComparisonFunc = GFXComparisonFunc.NEVER; borderColor : number[] = [0.0, 0.0, 0.0, 0.0]; @@ -57,12 +57,12 @@ export abstract class GFXSampler { protected _device : GFXDevice; protected _name : string = ""; - protected _minFilter : GFXFilterOp = GFXFilterOp.LINEAR; - protected _magFilter : GFXFilterOp = GFXFilterOp.LINEAR; - protected _mipFilter : GFXFilterOp = GFXFilterOp.LINEAR; - protected _addressU : GFXAddressMode = GFXAddressMode.WRAP; - protected _addressV : GFXAddressMode = GFXAddressMode.WRAP; - protected _addressW : GFXAddressMode = GFXAddressMode.WRAP; + protected _minFilter : GFXFilter = GFXFilter.LINEAR; + protected _magFilter : GFXFilter = GFXFilter.LINEAR; + protected _mipFilter : GFXFilter = GFXFilter.LINEAR; + protected _addressU : GFXAddress = GFXAddress.WRAP; + protected _addressV : GFXAddress = GFXAddress.WRAP; + protected _addressW : GFXAddress = GFXAddress.WRAP; protected _maxAnisotropy : number = 16; protected _cmpFunc : GFXComparisonFunc = GFXComparisonFunc.NEVER; protected _borderColor : number[] = [0.0, 0.0, 0.0, 0.0]; diff --git a/cocos/renderer/gfx/gfx-shader.ts b/cocos/renderer/gfx/gfx-shader.ts index 8c400e3c8c..ae7e662ddf 100644 --- a/cocos/renderer/gfx/gfx-shader.ts +++ b/cocos/renderer/gfx/gfx-shader.ts @@ -22,28 +22,17 @@ export class GFXShaderMacro { } }; -export class GFXShaderVar { - binding : number = -1; +export class GFXUniform { name : string = ""; type : GFXType = GFXType.UNKNOWN; - stride : number = 0; - count : number = 0; - size : number = 0; - offset : number = 0; + count : number = 1; }; -export class GFXShaderBlock { +export class GFXUniformBlock { binding : number = -1; name : string = ""; - size : number = 0; - uniforms : GFXShaderVar[] = []; -}; - -export class GFXShaderSampler { - binding : number = -1; - name : string = ""; - type : GFXType = GFXType.UNKNOWN; - units : number[] = []; + //instance : string = ""; + uniforms : GFXUniform[] = []; }; export class GFXShaderStage { @@ -60,6 +49,9 @@ export class GFXShaderInfo { name : string = ""; stages : GFXShaderStage[] = []; bindings : GFXBinding[] = []; + + // blocks are used for being compatible with single uniforms + blocks : GFXUniformBlock[] = []; }; export abstract class GFXShader { @@ -75,9 +67,5 @@ export abstract class GFXShader { protected _name : string = ""; protected _stages : GFXShaderStage[] = []; protected _bindings : GFXBinding[] = []; - - protected _inputs : GFXShaderVar[] = []; - protected _uniforms : GFXShaderVar[] = []; - protected _blocks : GFXShaderBlock[] = []; - protected _samplers : GFXShaderSampler[] = []; + protected _blocks : GFXUniformBlock[] = []; }; diff --git a/cocos/renderer/gfx/webgl/webgl-commands.ts b/cocos/renderer/gfx/webgl/webgl-commands.ts index e68ac7ea18..69a56c6b3c 100644 --- a/cocos/renderer/gfx/webgl/webgl-commands.ts +++ b/cocos/renderer/gfx/webgl/webgl-commands.ts @@ -1,12 +1,17 @@ -import { WebGLGPUBuffer, WebGLGPUTexture, WebGLGPUInputState, WebGLGPUFramebuffer, WebGLGPUShader, WebGLGPUInput, WebGLGPUUniform } from "./webgl-gpu-objects"; +import { WebGLGPUBuffer, WebGLGPUTexture, WebGLGPUInputAssembler, WebGLGPUFramebuffer, WebGLGPUShader, WebGLGPUInput, WebGLAttrib, WebGLGPUPipelineState, WebGLGPUUniformBlock, WebGLGPUBindingSetLayout } from "./webgl-gpu-objects"; import { WebGLGFXDevice } from "./webgl-gfx-device"; import { GFXBufferUsageBit, GFXMemoryUsageBit } from "../gfx-buffer"; import { GFXTextureViewType } from "../gfx-texture-view"; -import { GFXFormatInfos, GFXFormat, WebGLEXT, GFXType } from "../gfx-define"; +import { GFXFormatInfos, GFXFormat, WebGLEXT, GFXType, GFX_MAX_VERTEX_ATTRIBUTES } from "../gfx-define"; import { GFXShaderType } from "../gfx-shader"; +import { GFXLoadOp } from "../gfx-render-pass"; +import { GFXColorMask, GFXCullMode } from "../gfx-pipeline-state"; +import { WebGLGFXCommandAllocator } from "./webgl-gfx-command-allocator"; +import { GFXBindingType } from "../gfx-binding-set-layout"; +import { Buffer } from "buffer"; -function GFXFormatToWebGLType(format : GFXFormat) : GLenum { - switch(format) { +function GFXFormatToWebGLType(format: GFXFormat): GLenum { + switch (format) { case GFXFormat.R8: return WebGLRenderingContext.UNSIGNED_BYTE; case GFXFormat.R8SN: return WebGLRenderingContext.BYTE; case GFXFormat.R8UI: return WebGLRenderingContext.UNSIGNED_INT; @@ -107,8 +112,8 @@ function GFXFormatToWebGLType(format : GFXFormat) : GLenum { } } -function GFXFormatToWebGLInternalFormat(format : GFXFormat) : GLenum { - switch(format) { +function GFXFormatToWebGLInternalFormat(format: GFXFormat): GLenum { + switch (format) { case GFXFormat.A8: return WebGLRenderingContext.ALPHA; case GFXFormat.L8: return WebGLRenderingContext.LUMINANCE; case GFXFormat.LA8: return WebGLRenderingContext.LUMINANCE_ALPHA; @@ -122,7 +127,7 @@ function GFXFormatToWebGLInternalFormat(format : GFXFormat) : GLenum { case GFXFormat.D24S8: return WebGLRenderingContext.DEPTH_STENCIL; case GFXFormat.D32F: return WebGLRenderingContext.DEPTH_COMPONENT; case GFXFormat.D32F_S8: return WebGLRenderingContext.DEPTH_STENCIL; - + case GFXFormat.BC1: return WebGLEXT.COMPRESSED_RGB_S3TC_DXT1_EXT; case GFXFormat.BC1_ALPHA: return WebGLEXT.COMPRESSED_RGBA_S3TC_DXT1_EXT; case GFXFormat.BC1_SRGB: return WebGLEXT.COMPRESSED_SRGB_S3TC_DXT1_EXT; @@ -146,8 +151,8 @@ function GFXFormatToWebGLInternalFormat(format : GFXFormat) : GLenum { } } -function GFXFormatToWebGLFormat(format : GFXFormat) : GLenum { - switch(format) { +function GFXFormatToWebGLFormat(format: GFXFormat): GLenum { + switch (format) { case GFXFormat.A8: return WebGLRenderingContext.ALPHA; case GFXFormat.L8: return WebGLRenderingContext.LUMINANCE; case GFXFormat.LA8: return WebGLRenderingContext.LUMINANCE_ALPHA; @@ -172,7 +177,7 @@ function GFXFormatToWebGLFormat(format : GFXFormat) : GLenum { case GFXFormat.BC3_SRGB: return WebGLEXT.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT; case GFXFormat.ETC_RGB8: return WebGLEXT.COMPRESSED_RGB_ETC1_WEBGL; - + case GFXFormat.PVRTC_RGB2: return WebGLEXT.COMPRESSED_RGB_PVRTC_2BPPV1_IMG; case GFXFormat.PVRTC_RGBA2: return WebGLEXT.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; case GFXFormat.PVRTC_RGB4: return WebGLEXT.COMPRESSED_RGB_PVRTC_4BPPV1_IMG; @@ -185,8 +190,8 @@ function GFXFormatToWebGLFormat(format : GFXFormat) : GLenum { } } -function WebGLTypeToGFXType(glType : GLenum) : GFXType { - switch(glType) { +function WebGLTypeToGFXType(glType: GLenum): GFXType { + switch (glType) { case WebGLRenderingContext.BOOL: return GFXType.BOOL; case WebGLRenderingContext.BOOL_VEC2: return GFXType.BOOL2; case WebGLRenderingContext.BOOL_VEC3: return GFXType.BOOL3; @@ -212,8 +217,8 @@ function WebGLTypeToGFXType(glType : GLenum) : GFXType { } } -function WebGLGetTypeSize(glType : GLenum) : GFXType { - switch(glType) { +function WebGLGetTypeSize(glType: GLenum): GFXType { + switch (glType) { case WebGLRenderingContext.BOOL: return 4; case WebGLRenderingContext.BOOL_VEC2: return 8; case WebGLRenderingContext.BOOL_VEC3: return 12; @@ -239,24 +244,215 @@ function WebGLGetTypeSize(glType : GLenum) : GFXType { } } -export const enum WebGLCmdType { - UNKNOWN, - CREATE_BUFFER, - DESTROY_BUFFER, +function WebGLGetComponentCount(glType: GLenum): GFXType { + switch (glType) { + case WebGLRenderingContext.FLOAT_MAT2: return 2; + case WebGLRenderingContext.FLOAT_MAT3: return 3; + case WebGLRenderingContext.FLOAT_MAT4: return 4; + default: { + console.error("Unsupported GLType, get component count failed."); + return 0; + } + } +} + +const WebGLCmpFuncs: GLenum[] = [ + WebGLRenderingContext.NEVER, + WebGLRenderingContext.LESS, + WebGLRenderingContext.EQUAL, + WebGLRenderingContext.LEQUAL, + WebGLRenderingContext.GREATER, + WebGLRenderingContext.NOTEQUAL, + WebGLRenderingContext.GEQUAL, + WebGLRenderingContext.ALWAYS, +]; + +const WebGLStencilOps: GLenum[] = [ + WebGLRenderingContext.KEEP, + WebGLRenderingContext.ZERO, + WebGLRenderingContext.REPLACE, + WebGLRenderingContext.INCR, + WebGLRenderingContext.DECR, + WebGLRenderingContext.INVERT, + WebGLRenderingContext.INCR_WRAP, + WebGLRenderingContext.DECR_WRAP, +]; + +const WebGLBlendOps: GLenum[] = [ + WebGLRenderingContext.FUNC_ADD, + WebGLRenderingContext.FUNC_SUBTRACT, + WebGLRenderingContext.FUNC_REVERSE_SUBTRACT, + WebGLRenderingContext.FUNC_ADD, + WebGLRenderingContext.FUNC_ADD, +]; + +const WebGLBlendFactors: GLenum[] = [ + WebGLRenderingContext.ZERO, + WebGLRenderingContext.ONE, + WebGLRenderingContext.SRC_ALPHA, + WebGLRenderingContext.DST_ALPHA, + WebGLRenderingContext.ONE_MINUS_SRC_ALPHA, + WebGLRenderingContext.ONE_MINUS_DST_ALPHA, + WebGLRenderingContext.SRC_COLOR, + WebGLRenderingContext.DST_COLOR, + WebGLRenderingContext.SRC_ALPHA_SATURATE, + WebGLRenderingContext.CONSTANT_COLOR, + WebGLRenderingContext.ONE_MINUS_CONSTANT_COLOR, + WebGLRenderingContext.CONSTANT_ALPHA, + WebGLRenderingContext.ONE_MINUS_CONSTANT_ALPHA, +]; + +export const enum WebGLCmd { + BEGIN_RENDER_PASS, + END_RENDER_PASS, + BIND_PIPELINE_STATE, + BIND_INPUT_ASSEMBLER, + BIND_BINDING_SET_LAYOUT, + DRAW, + UPDATE_BUFFER, + COUNT, }; -export class WebGLCmd { - type : WebGLCmdType = WebGLCmdType.UNKNOWN; - obj : any; - isFree : boolean = false; +export class WebGLCmdObject { + cmdType: WebGLCmd; + + constructor(type: WebGLCmd) { + this.cmdType = type; + } }; -export function WebGLCmdFuncCreateBuffer(device : WebGLGFXDevice, gpuBuffer : WebGLGPUBuffer) { +export class WebGLCmdBeginRenderPass extends WebGLCmdObject { - let gl : WebGLRenderingContext = device.gl; + gpuFramebuffer: WebGLGPUFramebuffer | null = null; + viewport: number[] = []; + numClearColors: number = 0; + clearColors: number[] = []; + clearDepth: number = 1.0; + clearStencil: number = 0; + + constructor() { + super(WebGLCmd.BEGIN_RENDER_PASS); + } +} + +export class WebGLCmdBindPipelineState extends WebGLCmdObject { + + gpuPipelineState: WebGLGPUPipelineState | null = null; + + constructor() { + super(WebGLCmd.BIND_PIPELINE_STATE); + } +}; + +export class WebGLCmdBindInputAssembler extends WebGLCmdObject { + + gpuInputAssembler: WebGLGPUInputAssembler | null = null; + + constructor() { + super(WebGLCmd.BIND_INPUT_ASSEMBLER); + } +}; + +export class WebGLCmdBindBindingSetLayout extends WebGLCmdObject { + + gpuBindingSetLayout: WebGLGPUBindingSetLayout | null = null; + + constructor() { + super(WebGLCmd.BIND_BINDING_SET_LAYOUT); + } +}; + +export class WebGLCmdDraw extends WebGLCmdObject { + + vertexCount: number = 0; + firstVertex: number = 0; + + indexCount: number = 0; + firstIndex: number = 0; + vertexOffset: number = 0; + + instanceCount: number = 0; + firstInstance: number = 0; + + constructor() { + super(WebGLCmd.DRAW); + } +}; + +export class WebGLCmdUpdateBuffer extends WebGLCmdObject { + + gpuBuffer: WebGLGPUBuffer | null = null; + data: ArrayBuffer | null = null; + offset: number = 0; + + constructor() { + super(WebGLCmd.UPDATE_BUFFER); + } +}; + +export class WebGLCmdPackage { + cmds: WebGLCmd[] = []; + beginRenderPassCmds: WebGLCmdBeginRenderPass[] = []; + bindPipelineStateCmds: WebGLCmdBindPipelineState[] = []; + bindBindingSetLayoutCmds: WebGLCmdBindBindingSetLayout[] = []; + bindInputAssemblerCmds: WebGLCmdBindInputAssembler[] = []; + drawCmds: WebGLCmdDraw[] = []; + updateBufferCmds: WebGLCmdUpdateBuffer[] = []; + + clearCmds(allocator: WebGLGFXCommandAllocator) { + + if (this.beginRenderPassCmds.length) { + for (let i = 0; i < this.beginRenderPassCmds.length; ++i) { + allocator.beginRenderPassCmdPool.free(this.beginRenderPassCmds[i]); + } + this.beginRenderPassCmds = []; + } + + if (this.bindPipelineStateCmds.length) { + for (let i = 0; i < this.bindPipelineStateCmds.length; ++i) { + allocator.bindPipelineStateCmdPool.free(this.bindPipelineStateCmds[i]); + } + this.bindPipelineStateCmds = []; + } + + if (this.bindBindingSetLayoutCmds.length) { + for (let i = 0; i < this.bindBindingSetLayoutCmds.length; ++i) { + allocator.bindBindingSetLayoutCmdPool.free(this.bindBindingSetLayoutCmds[i]); + } + this.bindBindingSetLayoutCmds = []; + } + + if (this.bindInputAssemblerCmds.length) { + for (let i = 0; i < this.bindInputAssemblerCmds.length; ++i) { + allocator.bindInputAssemblerCmdPool.free(this.bindInputAssemblerCmds[i]); + } + this.bindInputAssemblerCmds = []; + } + + if (this.drawCmds.length) { + for (let i = 0; i < this.drawCmds.length; ++i) { + allocator.drawCmdPool.free(this.drawCmds[i]); + } + this.drawCmds = []; + } + + if (this.updateBufferCmds.length) { + for (let i = 0; i < this.updateBufferCmds.length; ++i) { + allocator.updateBufferCmdPool.free(this.updateBufferCmds[i]); + } + this.updateBufferCmds = []; + } + + this.cmds = []; + } +} + +export function WebGLCmdFuncCreateBuffer(device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer) { + + let gl: WebGLRenderingContext = device.gl; if (gpuBuffer.usage & GFXBufferUsageBit.VERTEX) { - + gpuBuffer.glTarget = WebGLRenderingContext.ARRAY_BUFFER; let glBuffer = gl.createBuffer(); if (glBuffer) { @@ -266,15 +462,15 @@ export function WebGLCmdFuncCreateBuffer(device : WebGLGFXDevice, gpuBuffer : We gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, gpuBuffer.glBuffer); device.stateCache.glArrayBuffer = gpuBuffer.glBuffer; } - - let glUsage : GLenum = gpuBuffer.usage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; + + let glUsage: GLenum = gpuBuffer.usage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, gpuBuffer.size, glUsage); - + gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, null); device.stateCache.glArrayBuffer = null; } } else if (gpuBuffer.usage & GFXBufferUsageBit.INDEX) { - + gpuBuffer.glTarget = WebGLRenderingContext.ELEMENT_ARRAY_BUFFER; let glBuffer = gl.createBuffer(); if (glBuffer) { @@ -284,10 +480,10 @@ export function WebGLCmdFuncCreateBuffer(device : WebGLGFXDevice, gpuBuffer : We gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; } - - let glUsage : GLenum = gpuBuffer.usage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; + + let glUsage: GLenum = gpuBuffer.usage & GFXMemoryUsageBit.DEVICE ? WebGLRenderingContext.STATIC_DRAW : WebGLRenderingContext.DYNAMIC_DRAW; gl.bufferData(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.size, glUsage); - + gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, null); device.stateCache.glElementArrayBuffer = null; } @@ -304,89 +500,84 @@ export function WebGLCmdFuncCreateBuffer(device : WebGLGFXDevice, gpuBuffer : We } } -export function WebGLCmdFuncDestroyBuffer(device : WebGLGFXDevice, gpuBuffer : WebGLGPUBuffer) { - if(gpuBuffer.glBuffer > 0) { +export function WebGLCmdFuncDestroyBuffer(device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer) { + if (gpuBuffer.glBuffer > 0) { device.gl.deleteBuffer(gpuBuffer.glBuffer); gpuBuffer.glBuffer = 0; } } -export function WebGLCmdFuncUpdateBuffer(device : WebGLGFXDevice, gpuBuffer : WebGLGPUBuffer, offset : number, data : ArrayBuffer) { +export function WebGLCmdFuncUpdateBuffer(device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer, offset: number, buffer: Buffer) { - let gl : WebGLRenderingContext = device.gl; + let gl: WebGLRenderingContext = device.gl; switch (gpuBuffer.glTarget) { - case WebGLRenderingContext.ARRAY_BUFFER : { - + case WebGLRenderingContext.ARRAY_BUFFER: { if (device.stateCache.glArrayBuffer !== gpuBuffer.glBuffer) { gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, gpuBuffer.glBuffer); device.stateCache.glArrayBuffer = gpuBuffer.glBuffer; } - - gl.bufferSubData(WebGLRenderingContext.ARRAY_BUFFER, offset, data); - break; } - case WebGLRenderingContext.ELEMENT_ARRAY_BUFFER : { - + case WebGLRenderingContext.ELEMENT_ARRAY_BUFFER: { if (device.stateCache.glElementArrayBuffer !== gpuBuffer.glBuffer) { gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; } - - gl.bufferSubData(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, offset, data); - break; } - default : { + default: { console.error("Unsupported GFXBufferType, update buffer failed."); + return; } } + + gl.bufferSubData(gpuBuffer.glTarget, offset, buffer.buffer); } -export function WebGLCmdFuncCreateInputState(device : WebGLGFXDevice, gpuInputState : WebGLGPUInputState) { +export function WebGLCmdFuncCreateInputState(device: WebGLGFXDevice, gpuInputState: WebGLGPUInputAssembler) { } -export function WebGLCmdFuncDestroyInputState(device : WebGLGFXDevice, gpuInputState : WebGLGPUInputState) { +export function WebGLCmdFuncDestroyInputState(device: WebGLGFXDevice, gpuInputState: WebGLGPUInputAssembler) { /*if(gpuInputState.glVAO > 0) { gpuInputState.glVAO = 0; }*/ } -export function WebGLCmdFuncCreateTexture(device : WebGLGFXDevice, gpuTexture : WebGLGPUTexture) { +export function WebGLCmdFuncCreateTexture(device: WebGLGFXDevice, gpuTexture: WebGLGPUTexture) { - let gl : WebGLRenderingContext = device.gl; + let gl: WebGLRenderingContext = device.gl; gpuTexture.glInternelFmt = GFXFormatToWebGLInternalFormat(gpuTexture.format); gpuTexture.glFormat = GFXFormatToWebGLFormat(gpuTexture.format); gpuTexture.glType = GFXFormatToWebGLType(gpuTexture.format); switch (gpuTexture.viewType) { - case GFXTextureViewType.TV2D : { + case GFXTextureViewType.TV2D: { gpuTexture.viewType = GFXTextureViewType.TV2D; gpuTexture.glTarget = WebGLRenderingContext.TEXTURE_2D; let glTexture = gl.createTexture(); - if(glTexture) { + if (glTexture) { gpuTexture.glTexture = glTexture; if (device.stateCache.glTex2DUnits[device.stateCache.texUnit] !== gpuTexture.glTexture) { gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, gpuTexture.glTexture); device.stateCache.glTex2DUnits[device.stateCache.texUnit] = gpuTexture.glTexture; } - + let w = gpuTexture.width; let h = gpuTexture.height; - + if (!GFXFormatInfos[gpuTexture.format].isCompressed) { - for(let i = 0; i < gpuTexture.mipLevel; ++i) { + for (let i = 0; i < gpuTexture.mipLevel; ++i) { gl.texImage2D(WebGLRenderingContext.TEXTURE_2D, i, gpuTexture.glInternelFmt, w, h, 0, gpuTexture.glFormat, gpuTexture.glType, null); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); } } else { - let view : ArrayBufferView = { buffer : new ArrayBuffer(0), byteLength : 0, byteOffset : 0 }; + let view: ArrayBufferView = { buffer: new ArrayBuffer(0), byteLength: 0, byteOffset: 0 }; for (let i = 0; i < gpuTexture.mipLevel; ++i) { gl.compressedTexImage2D(WebGLRenderingContext.TEXTURE_2D, i, gpuTexture.glInternelFmt, w, h, 0, view); w = Math.max(1, w >> 1); @@ -397,7 +588,7 @@ export function WebGLCmdFuncCreateTexture(device : WebGLGFXDevice, gpuTexture : break; } - case GFXTextureViewType.CUBE : { + case GFXTextureViewType.CUBE: { gpuTexture.viewType = GFXTextureViewType.CUBE; gpuTexture.glTarget = WebGLRenderingContext.TEXTURE_CUBE_MAP; @@ -422,12 +613,12 @@ export function WebGLCmdFuncCreateTexture(device : WebGLGFXDevice, gpuTexture : } } } else { - let view : ArrayBufferView = { buffer : new ArrayBuffer(0), byteLength : 0, byteOffset : 0 }; + let view: ArrayBufferView = { buffer: new ArrayBuffer(0), byteLength: 0, byteOffset: 0 }; for (let f = 0; f < 6; ++f) { let w = gpuTexture.width; let h = gpuTexture.height; - for(let i = 0; i < gpuTexture.mipLevel; ++i) { + for (let i = 0; i < gpuTexture.mipLevel; ++i) { gl.compressedTexImage2D(WebGLRenderingContext.TEXTURE_CUBE_MAP_NEGATIVE_X + f, i, gpuTexture.glInternelFmt, w, h, 0, view); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); @@ -438,7 +629,7 @@ export function WebGLCmdFuncCreateTexture(device : WebGLGFXDevice, gpuTexture : break; } - default : { + default: { console.error("Unsupported GFXTextureType, create texture failed."); gpuTexture.viewType = GFXTextureViewType.TV2D; gpuTexture.glTarget = WebGLRenderingContext.TEXTURE_2D; @@ -446,19 +637,19 @@ export function WebGLCmdFuncCreateTexture(device : WebGLGFXDevice, gpuTexture : } } -export function WebGLCmdFuncDestroyTexture(device : WebGLGFXDevice, gpuTexture : WebGLGPUTexture) { +export function WebGLCmdFuncDestroyTexture(device: WebGLGFXDevice, gpuTexture: WebGLGPUTexture) { if (gpuTexture.glTexture > 0) { device.gl.deleteTexture(gpuTexture.glTexture); gpuTexture.glTexture = 0; } } -export function WebGLCmdFuncCreateFramebuffer(device : WebGLGFXDevice, gpuFramebuffer : WebGLGPUFramebuffer) { +export function WebGLCmdFuncCreateFramebuffer(device: WebGLGFXDevice, gpuFramebuffer: WebGLGPUFramebuffer) { + + if (!gpuFramebuffer.isOffscreen) { + + let gl: WebGLRenderingContext = device.gl; - if(!gpuFramebuffer.isOffscreen) { - - let gl : WebGLRenderingContext = device.gl; - let glFramebuffer = gl.createFramebuffer(); if (glFramebuffer) { gpuFramebuffer.glFramebuffer = glFramebuffer; @@ -467,28 +658,28 @@ export function WebGLCmdFuncCreateFramebuffer(device : WebGLGFXDevice, gpuFrameb gl.bindFramebuffer(WebGLRenderingContext.FRAMEBUFFER, gpuFramebuffer.glFramebuffer); device.stateCache.glFramebuffer = gpuFramebuffer.glFramebuffer; } - + for (let i = 0; i < gpuFramebuffer.gpuColorViews.length; ++i) { - + let gpuColorView = gpuFramebuffer.gpuColorViews[i]; if (gpuColorView) { gl.framebufferTexture2D( - WebGLRenderingContext.FRAMEBUFFER, - WebGLRenderingContext.COLOR_ATTACHMENT0 + i, + WebGLRenderingContext.FRAMEBUFFER, + WebGLRenderingContext.COLOR_ATTACHMENT0 + i, gpuColorView.gpuTexture.glTarget, gpuColorView.gpuTexture.glTexture, gpuColorView.baseLevel); } } - - if(gpuFramebuffer.gpuDepthStencilView) { - - let glAttachment = GFXFormatInfos[gpuFramebuffer.gpuDepthStencilView.format].hasStencil? - WebGLRenderingContext.DEPTH_STENCIL_ATTACHMENT : WebGLRenderingContext.DEPTH_ATTACHMENT; - + + if (gpuFramebuffer.gpuDepthStencilView) { + + let glAttachment = GFXFormatInfos[gpuFramebuffer.gpuDepthStencilView.format].hasStencil ? + WebGLRenderingContext.DEPTH_STENCIL_ATTACHMENT : WebGLRenderingContext.DEPTH_ATTACHMENT; + gl.framebufferTexture2D( - WebGLRenderingContext.FRAMEBUFFER, - glAttachment, + WebGLRenderingContext.FRAMEBUFFER, + glAttachment, gpuFramebuffer.gpuDepthStencilView.gpuTexture.glTarget, gpuFramebuffer.gpuDepthStencilView.gpuTexture.glTexture, gpuFramebuffer.gpuDepthStencilView.baseLevel); @@ -497,20 +688,20 @@ export function WebGLCmdFuncCreateFramebuffer(device : WebGLGFXDevice, gpuFrameb } } -export function WebGLCmdFuncDestroyFramebuffer(device : WebGLGFXDevice, gpuFramebuffer : WebGLGPUFramebuffer) { +export function WebGLCmdFuncDestroyFramebuffer(device: WebGLGFXDevice, gpuFramebuffer: WebGLGPUFramebuffer) { if (gpuFramebuffer.glFramebuffer > 0) { device.gl.deleteFramebuffer(gpuFramebuffer.glFramebuffer); gpuFramebuffer.glFramebuffer = 0; } } -export function WebGLCmdFuncCreateShader(device : WebGLGFXDevice, gpuShader : WebGLGPUShader) { - let gl : WebGLRenderingContext = device.gl; +export function WebGLCmdFuncCreateShader(device: WebGLGFXDevice, gpuShader: WebGLGPUShader) { + let gl: WebGLRenderingContext = device.gl; for (let i = 0; i < gpuShader.gpuStages.length; ++i) { let gpuStage = gpuShader.gpuStages[i]; - let glShaderType : GLenum = 0; + let glShaderType: GLenum = 0; let shaderTypeStr = ""; switch (gpuStage.type) { @@ -526,7 +717,7 @@ export function WebGLCmdFuncCreateShader(device : WebGLGFXDevice, gpuShader : We } default: { console.error("Unsupported GFXShaderType."); - return ; + return; } } @@ -535,7 +726,7 @@ export function WebGLCmdFuncCreateShader(device : WebGLGFXDevice, gpuShader : We gpuStage.glShader = glShader; gl.shaderSource(gpuStage.glShader, gpuStage.source); gl.compileShader(gpuStage.glShader); - + var isSuccess = gl.getShaderParameter(gpuStage.glShader, gl.COMPILE_STATUS); if (!isSuccess) { console.error(shaderTypeStr + " in '" + gpuShader.name + "' compilation failed."); @@ -554,20 +745,20 @@ export function WebGLCmdFuncCreateShader(device : WebGLGFXDevice, gpuShader : We // link program for (let i = 0; i < gpuShader.gpuStages.length; ++i) { let gpuStage = gpuShader.gpuStages[i]; - + gl.attachShader(gpuShader.glProgram, gpuStage.glShader); } } gl.linkProgram(gpuShader.glProgram); var isSuccess = gl.getProgramParameter(gpuShader.glProgram, gl.LINK_STATUS); - if(!isSuccess) { + if (!isSuccess) { console.error("Failed to link shader '" + gpuShader.name + "'."); console.error(gl.getProgramInfoLog(gpuShader.glProgram)); - + for (let i = 0; i < gpuShader.gpuStages.length; ++i) { let gpuStage = gpuShader.gpuStages[i]; - if(gpuStage.glShader > 0) { + if (gpuStage.glShader > 0) { gl.deleteShader(gpuStage.glShader); gpuStage.glShader = 0; } @@ -580,10 +771,10 @@ export function WebGLCmdFuncCreateShader(device : WebGLGFXDevice, gpuShader : We for (let i = 0; i < activeAttribCount; ++i) { let info = gl.getActiveAttrib(gpuShader.glProgram, i); - if(info) { + if (info) { let glLoc = gl.getAttribLocation(gpuShader.glProgram, info.name); - - let varName : string; + + let varName: string; let nameOffset = info.name.search('['); if (nameOffset !== -1) { varName = info.name.substr(0, nameOffset); @@ -595,81 +786,141 @@ export function WebGLCmdFuncCreateShader(device : WebGLGFXDevice, gpuShader : We let stride = WebGLGetTypeSize(info.type); gpuShader.glInputs[i] = { - binding : -1, - name : varName, - type : type, - stride : stride, - count : info.size, - size : stride * info.size, - - glType : info.type, - glLoc : glLoc, + binding: -1, + name: varName, + type: type, + stride: stride, + count: info.size, + size: stride * info.size, + + glType: info.type, + glLoc: glLoc, }; } } + // create uniform blocks + if (gpuShader.blocks.length > 0) { + gpuShader.glBlocks = new Array(gpuShader.blocks.length); + for (let i = 0; i < gpuShader.blocks.length; ++i) { + let block = gpuShader.blocks[i]; + let glBlock = gpuShader.glBlocks[i]; + glBlock.isUniformPackage = true; + glBlock.binding = block.binding; + glBlock.name = block.name; + } + } + // parse uniforms let activeUniformCount = gl.getProgramParameter(gpuShader.glProgram, gl.ACTIVE_UNIFORMS); let unitIdx = 0; - for (let i = 0; i < activeAttribCount; ++i) { + for (let i = 0; i < activeUniformCount; ++i) { let info = gl.getActiveUniform(gpuShader.glProgram, i); - if(info) { + if (info) { let glLoc = gl.getUniformLocation(gpuShader.glProgram, info.name); - if(glLoc) { - let varName : string; + if (glLoc) { + let varName: string; let nameOffset = info.name.search('['); if (nameOffset !== -1) { varName = info.name.substr(0, nameOffset); } else { varName = info.name; } - + let type = WebGLTypeToGFXType(info.type); let isSampler = (type === WebGLRenderingContext.SAMPLER_2D) || - (type === WebGLRenderingContext.SAMPLER_CUBE); + (type === WebGLRenderingContext.SAMPLER_CUBE); - if(!isSampler) { + if (!isSampler) { let stride = WebGLGetTypeSize(info.type); - - gpuShader.glUniforms.push({ - binding : -1, - name : varName, - type : type, - stride : stride, - count : info.size, - size : stride * info.size, - offset : 0, - - glType : info.type, - glLoc : glLoc, - }); + + // build uniform block mapping + for (let b = 0; b < gpuShader.blocks.length; ++b) { + + let block = gpuShader.blocks[b]; + let glBlock = gpuShader.glBlocks[b]; + + for (let u = 0; u < block.uniforms.length; ++u) { + let uniform = block.uniforms[u]; + if (uniform.name === varName) { + let varSize = stride * info.size; + + glBlock.glUniforms.push({ + binding: -1, + name: varName, + type: type, + stride: stride, + count: info.size, + size: varSize, + offset: glBlock.size, + + glType: info.type, + glLoc: glLoc, + bufferView: null, + }); + + glBlock.size += varSize; + + break; + } + } + } // for } else { let units = new Array(info.size); - for(let u = 0; u < info.size; ++u) { + for (let u = 0; u < info.size; ++u) { units[u] = unitIdx + u; } gpuShader.glSamplers.push({ - binding : -1, - name : varName, - type : type, - units : units, - - glType : info.type, - glLoc : glLoc, + binding: -1, + name: varName, + type: type, + units: units, + + glType: info.type, + glLoc: glLoc, }); unitIdx += info.size; } } } + } // for + + for (let b = 0; b < gpuShader.glBlocks.length; ++b) { + + let glBlock = gpuShader.glBlocks[b]; + glBlock.buffer = new ArrayBuffer(glBlock.size); + glBlock.bufferView = new Buffer(glBlock.buffer); + + for (let u = 0; u < glBlock.glUniforms.length; ++u) { + let glUniform = glBlock.glUniforms[u]; + if(glUniform.glLoc >= 0) { + switch (glUniform.glType) { + case WebGLRenderingContext.BOOL: + case WebGLRenderingContext.BOOL_VEC2: + case WebGLRenderingContext.BOOL_VEC3: + case WebGLRenderingContext.BOOL_VEC4: + case WebGLRenderingContext.INT: + case WebGLRenderingContext.INT_VEC3: + case WebGLRenderingContext.INT_VEC2: + case WebGLRenderingContext.INT_VEC4: { + glUniform.bufferView = new Int32Array(glBlock.buffer, glUniform.offset, glUniform.count); + break; + } + default: { + glUniform.bufferView = new Float32Array(glBlock.buffer, glUniform.offset, glUniform.count); + } + } + } + } } } -export function WebGLCmdFuncDestroyShader(device : WebGLGFXDevice, gpuShader : WebGLGPUShader) { +export function WebGLCmdFuncDestroyShader(device: WebGLGFXDevice, gpuShader: WebGLGPUShader) { for (let i = 0; i < gpuShader.gpuStages.length; ++i) { let gpuStage = gpuShader.gpuStages[i]; @@ -679,9 +930,666 @@ export function WebGLCmdFuncDestroyShader(device : WebGLGFXDevice, gpuShader : W gpuStage.glShader = 0; } } - + if (gpuShader.glProgram > 0) { device.gl.deleteProgram(gpuShader.glProgram); gpuShader.glProgram = 0; } -} \ No newline at end of file +} + +export function WebGLCmdFuncCreateInputAssember(device: WebGLGFXDevice, gpuInputAssembler: WebGLGPUInputAssembler) { + + if (gpuInputAssembler.gpuShader) { + let attribCount = gpuInputAssembler.gpuShader.glInputs.length; + gpuInputAssembler.glAttribs = new Array(attribCount); + + let offsets = [0, 0, 0, 0, 0, 0, 0, 0]; + + for (let i = 0; i < attribCount; ++i) { + let glInput = gpuInputAssembler.gpuShader.glInputs[i]; + let glAttrib = gpuInputAssembler.glAttribs[i]; + glAttrib.glLoc = glInput.glLoc; + glAttrib.glType = glInput.glType; + glAttrib.size = glInput.size; + glAttrib.componentCount = WebGLGetComponentCount(glAttrib.glType); + + for (let j = 0; j < gpuInputAssembler.attributes.length; ++j) { + let attrib = gpuInputAssembler.attributes[j]; + if (glInput.name === attrib.name && attrib.stream < gpuInputAssembler.gpuVertexBuffers.length) { + + let gpuBuffer = gpuInputAssembler.gpuVertexBuffers[attrib.stream]; + glAttrib.count = GFXFormatInfos[attrib.format].count; + glAttrib.stride = gpuBuffer.stride; + glAttrib.glBuffer = gpuBuffer.glBuffer; + glAttrib.isInstanced = attrib.isInstanced; + + glAttrib.offset = offsets[attrib.stream]; + offsets[attrib.stream] += glInput.size; + break; + } + } + } + } +} + +export function WebGLCmdFuncDestroyInputAssembler(device: WebGLGFXDevice, gpuInputAssembler: WebGLGPUInputAssembler) { +} + +export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebGLCmdPackage) { + + let gl: WebGLRenderingContext = device.gl; + + let beginRenderPassCmdIdx = 0; + let bindPipelineStateCmdIdx = 0; + let bindBindingSetLayoutCmdIdx = 0; + let bindInputAssemblerCmdIdx = 0; + let drawCmdIdx = 0; + let updateBufferCmdIdx = 0; + + let gpuShader: WebGLGPUShader | null = null; + let gpuInputAssembler: WebGLGPUInputAssembler | null = null; + let glPrimitive = WebGLRenderingContext.TRIANGLES; + + for (let i = 0; i < cmdPackage.cmds.length; ++i) { + switch (cmdPackage.cmds[i]) { + case WebGLCmd.BEGIN_RENDER_PASS: { + let cmd = cmdPackage.beginRenderPassCmds[beginRenderPassCmdIdx++]; + //curGPUFramebuffer = cmd.gpuFramebuffer; + + let clears: GLbitfield = 0; + + if (cmd.gpuFramebuffer) { + if (device.stateCache.glFramebuffer !== cmd.gpuFramebuffer.glFramebuffer) { + gl.bindFramebuffer(WebGLRenderingContext.FRAMEBUFFER, cmd.gpuFramebuffer.glFramebuffer); + device.stateCache.glFramebuffer = cmd.gpuFramebuffer.glFramebuffer; + } + + if (device.stateCache.viewport[0] !== cmd.viewport[0] || + device.stateCache.viewport[1] !== cmd.viewport[1] || + device.stateCache.viewport[2] !== cmd.viewport[2] || + device.stateCache.viewport[3] !== cmd.viewport[3]) { + + gl.viewport(cmd.viewport[0], cmd.viewport[1], cmd.viewport[2], cmd.viewport[3]); + + device.stateCache.viewport[0] = cmd.viewport[0]; + device.stateCache.viewport[1] = cmd.viewport[1]; + device.stateCache.viewport[2] = cmd.viewport[2]; + device.stateCache.viewport[3] = cmd.viewport[3]; + } + + if (device.stateCache.scissorRect[0] !== cmd.viewport[0] || + device.stateCache.scissorRect[1] !== cmd.viewport[1] || + device.stateCache.scissorRect[2] !== cmd.viewport[2] || + device.stateCache.scissorRect[3] !== cmd.viewport[3]) { + + gl.scissor(cmd.viewport[0], cmd.viewport[1], cmd.viewport[2], cmd.viewport[3]); + + device.stateCache.scissorRect[0] = cmd.viewport[0]; + device.stateCache.scissorRect[1] = cmd.viewport[1]; + device.stateCache.scissorRect[2] = cmd.viewport[2]; + device.stateCache.scissorRect[3] = cmd.viewport[3]; + } + + let curGPURenderPass = cmd.gpuFramebuffer.gpuRenderPass; + + let invalidateAttachments: GLenum[] = [0, 0, 0, 0]; + let numInvalidAttach = 0; + + if (curGPURenderPass.colorAttachments.length > 0) { + let colorAttachment = curGPURenderPass.colorAttachments[0]; + + if (colorAttachment.format !== GFXFormat.UNKNOWN) { + switch (colorAttachment.loadOp) { + case GFXLoadOp.LOAD: break; // GL default behaviour + case GFXLoadOp.CLEAR: { + + if (device.stateCache.bs.targets[0].colorWriteMask !== GFXColorMask.ALL) { + gl.colorMask(true, true, true, true); + } + + gl.clearColor(cmd.clearColors[0], cmd.clearColors[1], cmd.clearColors[2], cmd.clearColors[3]); + clears |= WebGLRenderingContext.COLOR_BUFFER_BIT; + + break; + } + case GFXLoadOp.DISCARD: { + // invalidate the framebuffer + invalidateAttachments[numInvalidAttach++] = WebGLRenderingContext.COLOR_ATTACHMENT0; + break; + } + default: ; + } + } + } // if (curGPURenderPass) + + if (curGPURenderPass.depthStencilAttachment) { + + if (curGPURenderPass.depthStencilAttachment.format !== GFXFormat.UNKNOWN) { + switch (curGPURenderPass.depthStencilAttachment.depthLoadOp) { + case GFXLoadOp.LOAD: break; // GL default behaviour + case GFXLoadOp.CLEAR: { + + if (!device.stateCache.dss.isWriteDepth) { + gl.depthMask(true); + } + + gl.clearDepth(cmd.clearDepth); + + clears |= WebGLRenderingContext.DEPTH_BUFFER_BIT; + + break; + } + case GFXLoadOp.DISCARD: { + // invalidate the framebuffer + invalidateAttachments[numInvalidAttach++] = WebGLRenderingContext.DEPTH_ATTACHMENT; + break; + } + default: ; + } + + if (GFXFormatInfos[curGPURenderPass.depthStencilAttachment.format].hasStencil) { + switch (curGPURenderPass.depthStencilAttachment.stencilLoadOp) { + case GFXLoadOp.LOAD: break; // GL default behaviour + case GFXLoadOp.CLEAR: { + + if (!device.stateCache.dss.frontStencilWriteMask) { + gl.stencilMaskSeparate(WebGLRenderingContext.FRONT, 0xFFFFFFFF); + } + + if (!device.stateCache.dss.backStencilWriteMask) { + gl.stencilMaskSeparate(WebGLRenderingContext.BACK, 0xFFFFFFFF); + } + + gl.clearStencil(cmd.clearStencil); + clears |= WebGLRenderingContext.STENCIL_BUFFER_BIT; + + break; + } + case GFXLoadOp.DISCARD: { + // invalidate the framebuffer + invalidateAttachments[numInvalidAttach++] = WebGLRenderingContext.STENCIL_ATTACHMENT; + break; + } + default: ; + } + } + } + } // if (curGPURenderPass.depthStencilAttachment) + + /* + if (numInvalidAttach) { + gl.invalidateFramebuffer(WebGLRenderingContext.FRAMEBUFFER, numInvalidAttach, invalidateAttachments); + } + */ + + if (clears) { + gl.clear(clears); + } + + // restore states + if (clears & WebGLRenderingContext.COLOR_BUFFER_BIT) { + + let colorMask = device.stateCache.bs.targets[0].colorWriteMask; + if (colorMask !== GFXColorMask.ALL) { + let r = (colorMask & GFXColorMask.R) != GFXColorMask.NONE; + let g = (colorMask & GFXColorMask.G) != GFXColorMask.NONE; + let b = (colorMask & GFXColorMask.B) != GFXColorMask.NONE; + let a = (colorMask & GFXColorMask.A) != GFXColorMask.NONE; + gl.colorMask(r, g, b, a); + } + } + + if ((clears & WebGLRenderingContext.DEPTH_BUFFER_BIT) && + !device.stateCache.dss.isWriteDepth) { + gl.depthMask(false); + } + + if (clears & WebGLRenderingContext.STENCIL_BUFFER_BIT) { + if (!device.stateCache.dss.frontStencilWriteMask) { + gl.stencilMaskSeparate(WebGLRenderingContext.FRONT, 0); + } + + if (!device.stateCache.dss.backStencilWriteMask) { + gl.stencilMaskSeparate(WebGLRenderingContext.BACK, 0); + } + } + } // if (curGPURenderPass) + + break; + } + case WebGLCmd.END_RENDER_PASS: { + // WebGL 1.0 doesn't support store operation of attachments. + // GFXStoreOp.Store is the default GL behaviour. + break; + } + case WebGLCmd.BIND_PIPELINE_STATE: { + + let cmd = cmdPackage.bindPipelineStateCmds[bindPipelineStateCmdIdx++]; + if (cmd.gpuPipelineState) { + + glPrimitive = cmd.gpuPipelineState.glPrimitive; + + if (cmd.gpuPipelineState.gpuShader) { + + let glProgram = cmd.gpuPipelineState.gpuShader.glProgram; + if (device.stateCache.glProgram !== glProgram) { + gl.useProgram(glProgram); + device.stateCache.glProgram = glProgram; + } + + gpuShader = cmd.gpuPipelineState.gpuShader; + } + + // rasterizater state + let rs = cmd.gpuPipelineState.rs; + if (rs) { + + if (device.stateCache.rs.cullMode !== rs.cullMode) { + switch (rs.cullMode) { + case GFXCullMode.NONE: { + gl.disable(WebGLRenderingContext.CULL_FACE); + break; + } + case GFXCullMode.FRONT: { + gl.enable(WebGLRenderingContext.CULL_FACE); + gl.cullFace(WebGLRenderingContext.FRONT); + break; + } + case GFXCullMode.BACK: { + gl.enable(WebGLRenderingContext.CULL_FACE); + gl.cullFace(WebGLRenderingContext.BACK); + break; + } + default: ; + } + + device.stateCache.rs.cullMode = rs.cullMode; + } + + if (device.stateCache.rs.isFrontFaceCCW !== rs.isFrontFaceCCW) { + gl.frontFace(rs.isFrontFaceCCW ? WebGLRenderingContext.CCW : WebGLRenderingContext.CW); + device.stateCache.rs.isFrontFaceCCW = rs.isFrontFaceCCW; + } + + if ((device.stateCache.rs.depthBias !== rs.depthBias) || + (device.stateCache.rs.depthBiasFactor !== rs.depthBiasFactor)) { + gl.polygonOffset(rs.depthBiasFactor, rs.depthBias); + device.stateCache.rs.depthBias = rs.depthBias; + device.stateCache.rs.depthBiasFactor = rs.depthBiasFactor; + } + + if (device.stateCache.rs.lineWidth !== rs.lineWidth) { + gl.lineWidth(rs.lineWidth); + device.stateCache.rs.lineWidth = rs.lineWidth; + } + } // rasterizater state + + // depth-stencil state + let dss = cmd.gpuPipelineState.dss; + if (dss) { + + if (device.stateCache.dss.isDepthTest !== dss.isDepthTest) { + if (dss.isDepthTest) { + gl.enable(WebGLRenderingContext.DEPTH_TEST); + } else { + gl.disable(WebGLRenderingContext.DEPTH_TEST); + } + device.stateCache.dss.isDepthTest = dss.isDepthTest; + } + + if (device.stateCache.dss.isWriteDepth !== dss.isWriteDepth) { + gl.depthMask(dss.isWriteDepth); + device.stateCache.dss.isWriteDepth = dss.isWriteDepth; + } + + if (device.stateCache.dss.depthFunc !== dss.depthFunc) { + gl.depthFunc(WebGLCmpFuncs[dss.depthFunc]); + device.stateCache.dss.depthFunc = dss.depthFunc; + } + + // front + if ((device.stateCache.dss.isFrontStencilTest !== dss.isFrontStencilTest) || + (device.stateCache.dss.isBackStencilTest !== dss.isBackStencilTest)) { + if (dss.isFrontStencilTest || dss.isBackStencilTest) { + gl.enable(WebGLRenderingContext.STENCIL_TEST); + } else { + gl.disable(WebGLRenderingContext.STENCIL_TEST); + } + device.stateCache.dss.isFrontStencilTest = dss.isFrontStencilTest; + device.stateCache.dss.isBackStencilTest = dss.isBackStencilTest; + } + + if ((device.stateCache.dss.frontStencilFunc !== dss.frontStencilFunc) || + (device.stateCache.dss.frontStencilRef !== dss.frontStencilRef) || + (device.stateCache.dss.frontStencilReadMask !== dss.frontStencilReadMask)) { + + gl.stencilFuncSeparate( + WebGLRenderingContext.FRONT, + WebGLCmpFuncs[dss.frontStencilFunc], + dss.frontStencilRef, + dss.frontStencilReadMask); + + device.stateCache.dss.frontStencilFunc = dss.frontStencilFunc; + device.stateCache.dss.frontStencilRef = dss.frontStencilRef; + device.stateCache.dss.frontStencilReadMask = dss.frontStencilReadMask; + } + + if ((device.stateCache.dss.frontStencilFailOp !== dss.frontStencilFailOp) || + (device.stateCache.dss.frontStencilDepthFailOp !== dss.frontStencilDepthFailOp) || + (device.stateCache.dss.frontStencilPassOp !== dss.frontStencilPassOp)) { + + gl.stencilOpSeparate( + WebGLRenderingContext.FRONT, + WebGLStencilOps[dss.frontStencilFailOp], + WebGLStencilOps[dss.frontStencilDepthFailOp], + WebGLStencilOps[dss.frontStencilPassOp]); + + device.stateCache.dss.frontStencilFailOp = dss.frontStencilFailOp; + device.stateCache.dss.frontStencilDepthFailOp = dss.frontStencilDepthFailOp; + device.stateCache.dss.frontStencilPassOp = dss.frontStencilPassOp; + } + + if (device.stateCache.dss.frontStencilWriteMask !== dss.frontStencilWriteMask) { + gl.stencilMaskSeparate(WebGLRenderingContext.FRONT, dss.frontStencilWriteMask); + device.stateCache.dss.frontStencilWriteMask = dss.frontStencilWriteMask; + } + + // back + if ((device.stateCache.dss.backStencilFunc !== dss.backStencilFunc) || + (device.stateCache.dss.backStencilRef !== dss.backStencilRef) || + (device.stateCache.dss.backStencilReadMask !== dss.backStencilReadMask)) { + + gl.stencilFuncSeparate( + WebGLRenderingContext.BACK, + WebGLCmpFuncs[dss.backStencilFunc], + dss.backStencilRef, + dss.backStencilReadMask); + + device.stateCache.dss.backStencilFunc = dss.backStencilFunc; + device.stateCache.dss.backStencilRef = dss.backStencilRef; + device.stateCache.dss.backStencilReadMask = dss.backStencilReadMask; + } + + if ((device.stateCache.dss.backStencilFailOp !== dss.backStencilFailOp) || + (device.stateCache.dss.backStencilDepthFailOp !== dss.backStencilDepthFailOp) || + (device.stateCache.dss.backStencilPassOp !== dss.backStencilPassOp)) { + + gl.stencilOpSeparate( + WebGLRenderingContext.BACK, + WebGLStencilOps[dss.backStencilFailOp], + WebGLStencilOps[dss.backStencilDepthFailOp], + WebGLStencilOps[dss.backStencilPassOp]); + + device.stateCache.dss.backStencilFailOp = dss.backStencilFailOp; + device.stateCache.dss.backStencilDepthFailOp = dss.backStencilDepthFailOp; + device.stateCache.dss.backStencilPassOp = dss.backStencilPassOp; + } + + if (device.stateCache.dss.backStencilWriteMask !== dss.backStencilWriteMask) { + gl.stencilMaskSeparate(WebGLRenderingContext.BACK, dss.backStencilWriteMask); + device.stateCache.dss.backStencilWriteMask = dss.backStencilWriteMask; + } + } // depth-stencil state + + // blend state + let bs = cmd.gpuPipelineState.bs; + if (bs) { + + if (device.stateCache.bs.isA2C !== bs.isA2C) { + if (bs.isA2C) { + gl.enable(WebGLRenderingContext.SAMPLE_ALPHA_TO_COVERAGE); + } else { + gl.disable(WebGLRenderingContext.SAMPLE_ALPHA_TO_COVERAGE); + } + device.stateCache.bs.isA2C = bs.isA2C; + } + + if ((device.stateCache.bs.factor[0] !== bs.factor[0]) || + (device.stateCache.bs.factor[1] !== bs.factor[1]) || + (device.stateCache.bs.factor[2] !== bs.factor[2]) || + (device.stateCache.bs.factor[3] !== bs.factor[3])) { + + gl.blendColor(bs.factor[0], bs.factor[1], bs.factor[2], bs.factor[3]); + + device.stateCache.bs.factor[0] = bs.factor[0]; + device.stateCache.bs.factor[1] = bs.factor[1]; + device.stateCache.bs.factor[2] = bs.factor[2]; + device.stateCache.bs.factor[3] = bs.factor[3]; + } + + let target0 = bs.targets[0]; + let catchTarget0 = device.stateCache.bs.targets[0]; + + if (catchTarget0.isBlend !== target0.isBlend) { + if (target0.isBlend) { + gl.enable(WebGLRenderingContext.BLEND); + } else { + gl.disable(WebGLRenderingContext.BLEND); + } + catchTarget0.isBlend = target0.isBlend; + } + + if ((catchTarget0.blendOp !== target0.blendOp) || + (catchTarget0.alphBlendOp !== target0.alphBlendOp)) { + + gl.blendEquationSeparate(WebGLBlendOps[target0.blendOp], WebGLBlendOps[target0.alphBlendOp]); + + catchTarget0.blendOp = target0.blendOp; + catchTarget0.alphBlendOp = target0.alphBlendOp; + } + + if ((catchTarget0.srcBlend !== target0.srcBlend) || + (catchTarget0.dstBlend !== target0.dstBlend) || + (catchTarget0.srcAlphaBlend !== target0.srcAlphaBlend) || + (catchTarget0.dstAlphaBlend !== target0.dstAlphaBlend)) { + + gl.blendFuncSeparate( + WebGLBlendFactors[target0.srcBlend], + WebGLBlendFactors[target0.dstBlend], + WebGLBlendFactors[target0.srcAlphaBlend], + WebGLBlendFactors[target0.dstAlphaBlend]); + + catchTarget0.srcBlend = target0.srcBlend; + catchTarget0.dstBlend = target0.dstBlend; + catchTarget0.srcAlphaBlend = target0.srcAlphaBlend; + catchTarget0.dstAlphaBlend = target0.dstAlphaBlend; + } + + if (catchTarget0.colorWriteMask !== target0.colorWriteMask) { + + gl.colorMask( + (target0.colorWriteMask & GFXColorMask.R) !== GFXColorMask.NONE, + (target0.colorWriteMask & GFXColorMask.G) !== GFXColorMask.NONE, + (target0.colorWriteMask & GFXColorMask.B) !== GFXColorMask.NONE, + (target0.colorWriteMask & GFXColorMask.A) !== GFXColorMask.NONE); + + catchTarget0.colorWriteMask = target0.colorWriteMask; + } + } // blend state + } + + break; + } + case WebGLCmd.BIND_BINDING_SET_LAYOUT: { + + let cmd = cmdPackage.bindBindingSetLayoutCmds[bindBindingSetLayoutCmdIdx++]; + if (cmd.gpuBindingSetLayout && gpuShader) { + for (let n = 0; n < cmd.gpuBindingSetLayout.gpuBinding.length; ++n) { + let gpuBinding = cmd.gpuBindingSetLayout.gpuBinding[n]; + + switch (gpuBinding.type) { + case GFXBindingType.UNIFORM_BUFFER: { + + if (gpuBinding.gpuBuffer && gpuBinding.gpuBuffer.buffer) { + + let glBlock: WebGLGPUUniformBlock | null = null; + + for (let b = 0; b < gpuShader.glBlocks.length; ++b) { + if (gpuShader.glBlocks[b].binding === gpuBinding.binding) { + glBlock = gpuShader.glBlocks[b]; + break; + } + } + + if (glBlock && glBlock.bufferView) { + for (let u = 0; u < glBlock.glUniforms.length; ++u) { + let glUniform = glBlock.glUniforms[u]; + if (glUniform.glLoc >= 0 && + glBlock.bufferView.compare(gpuBinding.gpuBuffer.buffer, glUniform.offset, glUniform.size, glUniform.offset, glUniform.size) != 0) { + + switch (glUniform.glType) { + case WebGLRenderingContext.BOOL: + case WebGLRenderingContext.INT: { + gl.uniform1iv(glUniform.glLoc, glUniform.bufferView); + break; + } + case WebGLRenderingContext.BOOL_VEC2: + case WebGLRenderingContext.INT_VEC2: { + gl.uniform2iv(glUniform.glLoc, glUniform.bufferView); + break; + } + case WebGLRenderingContext.BOOL_VEC3: + case WebGLRenderingContext.INT_VEC3: { + gl.uniform3iv(glUniform.glLoc, glUniform.bufferView); + break; + } + case WebGLRenderingContext.BOOL_VEC4: + case WebGLRenderingContext.INT_VEC4: { + gl.uniform4iv(glUniform.glLoc, glUniform.bufferView); + break; + } + case WebGLRenderingContext.FLOAT: { + gl.uniform1fv(glUniform.glLoc, glUniform.bufferView); + break; + } + case WebGLRenderingContext.FLOAT_VEC2: { + gl.uniform2fv(glUniform.glLoc, glUniform.bufferView); + break; + } + case WebGLRenderingContext.FLOAT_VEC3: { + gl.uniform3fv(glUniform.glLoc, glUniform.bufferView); + break; + } + case WebGLRenderingContext.FLOAT_VEC4: { + gl.uniform4fv(glUniform.glLoc, glUniform.bufferView); + break; + } + case WebGLRenderingContext.FLOAT_MAT2: { + gl.uniformMatrix2fv(glUniform.glLoc, false, glUniform.bufferView); + break; + } + case WebGLRenderingContext.FLOAT_MAT3: { + gl.uniformMatrix2fv(glUniform.glLoc, false, glUniform.bufferView); + break; + } + case WebGLRenderingContext.FLOAT_MAT4: { + gl.uniformMatrix2fv(glUniform.glLoc, false, glUniform.bufferView); + break; + } + default: ; + } + } // if + } + } + } // if + break; + } + } + } + } + + break; + } + case WebGLCmd.BIND_INPUT_ASSEMBLER: { + let cmd = cmdPackage.bindInputAssemblerCmds[bindInputAssemblerCmdIdx++]; + gpuInputAssembler = cmd.gpuInputAssembler; + break; + } + case WebGLCmd.DRAW: { + if (gpuInputAssembler) { + let cmd = cmdPackage.drawCmds[drawCmdIdx++]; + + for (let a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { + device.stateCache.glCurrentAttribLocs[a] = false; + } + + for (let a = 0; a < gpuInputAssembler.glAttribs.length; ++a) { + let glAttrib = gpuInputAssembler.glAttribs[a]; + + if (device.stateCache.glArrayBuffer !== glAttrib.glBuffer) { + gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, glAttrib.glBuffer); + device.stateCache.glArrayBuffer = glAttrib.glBuffer; + } + + for (let c = 0; c < glAttrib.componentCount; ++c) { + let glLoc = glAttrib.glLoc + c; + let offset = glAttrib.offset + glAttrib.size * c; + + if (!device.stateCache.glEnabledAttribLocs[a]) { + gl.enableVertexAttribArray(glLoc); + device.stateCache.glEnabledAttribLocs[a] = true; + } + device.stateCache.glCurrentAttribLocs[a] = true; + + gl.vertexAttribPointer(glLoc, glAttrib.count, glAttrib.glType, false, glAttrib.stride, offset); + } + } + + for (let a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { + if (device.stateCache.glEnabledAttribLocs[a] != device.stateCache.glCurrentAttribLocs[a]) { + gl.disableVertexAttribArray(a); + device.stateCache.glEnabledAttribLocs[a] = false; + } + } + + if (gpuInputAssembler.gpuIndexBuffer && cmd.indexCount > 0) { + let gpuBuffer = gpuInputAssembler.gpuIndexBuffer; + + if (device.stateCache.glElementArrayBuffer !== gpuBuffer.glBuffer) { + gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); + device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; + } + + let offset = cmd.firstIndex * gpuBuffer.stride; + gl.drawElements(glPrimitive, cmd.indexCount, gpuInputAssembler.glIndexType, offset); + } else { + gl.drawArrays(glPrimitive, cmd.firstVertex, cmd.vertexCount); + } + } + break; + } + case WebGLCmd.UPDATE_BUFFER: { + + let cmd = cmdPackage.updateBufferCmds[updateBufferCmdIdx++]; + if (cmd.gpuBuffer) { + switch (cmd.gpuBuffer.glTarget) { + case WebGLRenderingContext.ARRAY_BUFFER: { + if (device.stateCache.glArrayBuffer !== cmd.gpuBuffer.glBuffer) { + gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, cmd.gpuBuffer.glBuffer); + device.stateCache.glArrayBuffer = cmd.gpuBuffer.glBuffer; + } + break; + } + case WebGLRenderingContext.ELEMENT_ARRAY_BUFFER: { + if (device.stateCache.glElementArrayBuffer !== cmd.gpuBuffer.glBuffer) { + gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, cmd.gpuBuffer.glBuffer); + device.stateCache.glElementArrayBuffer = cmd.gpuBuffer.glBuffer; + } + break; + } + default: { + console.error("Unsupported GFXBufferType, update buffer failed."); + continue; + } + } + + if (cmd.data) { + gl.bufferSubData(cmd.gpuBuffer.glTarget, cmd.offset, cmd.data); + } + } + + break; + } + } + } +} diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-binding-set-layout.ts b/cocos/renderer/gfx/webgl/webgl-gfx-binding-set-layout.ts new file mode 100644 index 0000000000..f9667355ff --- /dev/null +++ b/cocos/renderer/gfx/webgl/webgl-gfx-binding-set-layout.ts @@ -0,0 +1,55 @@ +import { GFXDevice } from '../gfx-device'; +import { WebGLGFXDevice } from './webgl-gfx-device'; +import { WebGLGPUBindingSetLayout } from './webgl-gpu-objects'; +import { GFXBindingSetLayout, GFXBindingSetLayoutInfo, GFXBindingType } from '../gfx-binding-set-layout'; +import { GFXBuffer } from '../gfx-buffer'; +import { GFXSampler } from '../gfx-sampler'; +import { GFXTextureView } from '../gfx-texture-view'; + +export class WebGLGFXBindingSetLayout extends GFXBindingSetLayout { + + constructor(device: GFXDevice) { + super(device); + } + + public initialize(info: GFXBindingSetLayoutInfo): boolean { + + for (let i = 0; i < info.bindings.length; ++i) { + let binding = info.bindings[i]; + + this._bindingUnits.push({ + binding: binding.binding, + type: binding.type, + name: binding.name, + buffer: null, + texView: null, + sampler: null, + }); + } + + this._gpuBindingSetLayout = this.webGLDevice.emitCmdCreateGPUBindingSetLayout(info); + + return true; + } + + public destroy() { + if (this._gpuBindingSetLayout) { + this.webGLDevice.emitCmdDestroyGPUBindingSetLayout(this._gpuBindingSetLayout); + this._gpuBindingSetLayout = null; + } + } + + public update() { + + } + + public get webGLDevice(): WebGLGFXDevice { + return this._device; + } + + public get gpuBindingSetLayout(): WebGLGPUBindingSetLayout | null { + return this._gpuBindingSetLayout; + } + + private _gpuBindingSetLayout: WebGLGPUBindingSetLayout | null = null; +}; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-buffer.ts b/cocos/renderer/gfx/webgl/webgl-gfx-buffer.ts index d3ad89de4f..afd0528b18 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-buffer.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-buffer.ts @@ -5,11 +5,11 @@ import { WebGLGFXDevice } from './webgl-gfx-device'; export class WebGLGFXBuffer extends GFXBuffer { - constructor(device : GFXDevice) { + constructor(device: GFXDevice) { super(device); } - public initialize(info : GFXBufferInfo) : boolean { + public initialize(info: GFXBufferInfo): boolean { this._usage = info.usage; this._memUsage = info.memUsage; @@ -17,46 +17,48 @@ export class WebGLGFXBuffer extends GFXBuffer { this._stride = info.stride; if (this._memUsage & GFXMemoryUsageBit.HOST) { - this._buffer = new ArrayBuffer(this._size); + this._arrayBuffer = new ArrayBuffer(this._size); + this._buffer = new Buffer(this._arrayBuffer); } this._isSimulate = (this._usage & GFXBufferUsageBit.TRANSFER_SRC) != GFXBufferUsageBit.NONE; - if (!this._isSimulate) { - this._gpuBuffer = this.webGLDevice.emitCmdCreateGPUBuffer(info); + if (!this._isSimulate || this._usage & GFXBufferUsageBit.UNIFORM) { + this._gpuBuffer = this.webGLDevice.emitCmdCreateGPUBuffer(info, this._buffer); } return true; } public destroy() { - if(this._gpuBuffer) { + if (this._gpuBuffer) { this.webGLDevice.emitCmdDestroyGPUBuffer(this._gpuBuffer); - this._buffer = null; + this._gpuBuffer = null; + } + + this._arrayBuffer = null; + this._buffer = null; + } + + public update(buffer: Buffer, offset: number) { + + if (this._buffer) { + this._buffer.set(buffer, offset); + } + + if (!this._isSimulate && this._buffer) { + this.webGLDevice.emitCmdUpdateGPUBuffer(this._gpuBuffer, offset, this._buffer); } } - public update(buffer : ArrayBuffer, offset : number) { - - if(this._buffer) { - let srcView : Uint8Array = new Uint8Array(buffer); - let dstView : Uint8Array = new Uint8Array(this._buffer, offset); - dstView.set(srcView); - } - - if(!this._isSimulate) { - this.webGLDevice.emitCmdUpdateGPUBuffer(this._gpuBuffer, offset, this._buffer); - } - } - - public get webGLDevice() : WebGLGFXDevice { + public get webGLDevice(): WebGLGFXDevice { return this._device; } - public get gpuBuffer() : WebGLGPUBuffer | null { + public get gpuBuffer(): WebGLGPUBuffer | null { return this._gpuBuffer; } - private _gpuBuffer : WebGLGPUBuffer | null = null; - private _isSimulate : boolean = false; + private _gpuBuffer: WebGLGPUBuffer | null = null; + private _isSimulate: boolean = false; }; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-command-allocator.ts b/cocos/renderer/gfx/webgl/webgl-gfx-command-allocator.ts index 94499b104b..615627a5a1 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-command-allocator.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-command-allocator.ts @@ -1,19 +1,65 @@ import { GFXDevice } from '../gfx-device'; import { GFXCommandAllocator } from '../gfx-command-allocator'; +import { WebGLCmdUpdateBuffer, WebGLCmdBeginRenderPass, WebGLCmdBindInputAssembler, WebGLCmdDraw, WebGLCmdBindPipelineState, WebGLCmdBindBindingSetLayout } from './webgl-commands'; export class WebGLGFXCommandPool { - constructor(size: number) { - this._commands = new Array(size); + constructor(count: number, increase : number) { + this._frees = new Array(count); + this._freeIdx = count - 1; + this._increase = increase; } - private _commands : T[]; + alloc(clazz: new() => T) : T | null { + + if (this._freeIdx <= 0) { + let size = this._frees.length + this._increase; + let temp = this._frees; + this._frees = new Array(size); + + for(let i = 0; i < temp.length; ++i) { + this._frees[i] = temp[i]; + } + + for(let i = temp.length + 1; i < size; ++i) { + this._frees[i] = new clazz(); + } + + this._freeIdx += (size - temp.length); + } + + let item = this._frees[this._freeIdx]; + this._frees[this._freeIdx--] = null; + + return item; + } + + free(cmd : T) { + this._frees[++this._freeIdx] = cmd; + } + + private _frees : (T|null)[]; + private _freeIdx : number = 0; + private _increase : number = 0; }; -export class GFXWebGLCommandAllocator extends GFXCommandAllocator { +export class WebGLGFXCommandAllocator extends GFXCommandAllocator { + + beginRenderPassCmdPool : WebGLGFXCommandPool; + bindPipelineStateCmdPool : WebGLGFXCommandPool; + bindBindingSetLayoutCmdPool : WebGLGFXCommandPool; + bindInputAssemblerCmdPool : WebGLGFXCommandPool; + drawCmdPool : WebGLGFXCommandPool; + updateBufferCmdPool : WebGLGFXCommandPool; constructor(device : GFXDevice) { super(device); + this.beginRenderPassCmdPool = new WebGLGFXCommandPool(64, 32); + this.bindPipelineStateCmdPool = new WebGLGFXCommandPool(64, 32); + this.bindBindingSetLayoutCmdPool = new WebGLGFXCommandPool(64, 32); + this.bindInputAssemblerCmdPool = new WebGLGFXCommandPool(64, 32); + this.drawCmdPool = new WebGLGFXCommandPool(64, 32); + this.updateBufferCmdPool = new WebGLGFXCommandPool(64, 32); } public initialize() : boolean { @@ -21,7 +67,7 @@ export class GFXWebGLCommandAllocator extends GFXCommandAllocator { return true; } - public destroy() : void { + public destroy() { } }; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-command-buffer.ts b/cocos/renderer/gfx/webgl/webgl-gfx-command-buffer.ts new file mode 100644 index 0000000000..abd0edb1a1 --- /dev/null +++ b/cocos/renderer/gfx/webgl/webgl-gfx-command-buffer.ts @@ -0,0 +1,136 @@ +import { GFXDevice } from '../gfx-device'; +import { GFXCommandBuffer, GFXCommandBufferInfo } from '../gfx-command-buffer'; +import { GFXBuffer } from '../gfx-buffer'; +import { WebGLCmdUpdateBuffer, WebGLCmd, WebGLCmdBeginRenderPass, WebGLCmdPackage, WebGLCmdBindInputAssembler, WebGLCmdBindPipelineState, WebGLCmdDraw } from './webgl-commands'; +import { WebGLGFXCommandAllocator } from './webgl-gfx-command-allocator'; +import { WebGLGFXDevice } from './webgl-gfx-device'; +import { WebGLGFXBuffer } from './webgl-gfx-buffer'; +import { GFXFramebuffer } from '../gfx-framebuffer'; +import { WebGLGFXFramebuffer } from './webgl-gfx-framebuffer'; +import { GFXInputAssembler } from '../gfx-input-assembler'; +import { WebGLGFXInputAssembler } from './webgl-gfx-input-assembler'; +import { GFXPipelineState } from '../gfx-pipeline-state'; +import { WebGLGFXPipelineLayout } from './webgl-gfx-pipeline-layout'; +import { WebGLGFXPipelineState } from './webgl-gfx-pipeline-state'; +import { GFXBindingSetLayout } from '../gfx-binding-set-layout'; + +export class WebGLGFXCommandBuffer extends GFXCommandBuffer { + + cmdPackage : WebGLCmdPackage = new WebGLCmdPackage; + + constructor(device : GFXDevice) { + super(device); + } + + public initialize(info : GFXCommandBufferInfo) : boolean { + + if(!info.allocator) { + return false; + } + + this._allocator = info.allocator; + this._type = info.type; + + return true; + } + + public destroy() { + if(this._allocator) { + this.cmdPackage.clearCmds(this._allocator); + } + } + + public begin() { + if(this._allocator) { + this.cmdPackage.clearCmds(this._allocator); + } + } + + public end() { + + } + + public beginRenderPass(framebuffer : GFXFramebuffer, viewport : number[], numClearColors : number, clearColors : number[], clearStencil : number) { + + let cmd = (this._allocator).beginRenderPassCmdPool.alloc(WebGLCmdBeginRenderPass); + if (cmd) { + cmd.gpuFramebuffer = (framebuffer).gpuFramebuffer; + cmd.viewport = viewport.slice(); + cmd.numClearColors = numClearColors; + cmd.clearColors = clearColors.slice(); + cmd.clearStencil = clearStencil; + this.cmdPackage.beginRenderPassCmds.push(cmd); + + this.cmdPackage.cmds.push(WebGLCmd.BEGIN_RENDER_PASS); + } + } + + public endRenderPass() { + + } + + public bindPipelineState(pipelineState : GFXPipelineState) { + + let cmd = (this._allocator).bindPipelineStateCmdPool.alloc(WebGLCmdBindPipelineState); + if (cmd) { + cmd.gpuPipelineState = (pipelineState).gpuPipelineState; + this.cmdPackage.bindPipelineStateCmds.push(cmd); + + this.cmdPackage.cmds.push(WebGLCmd.BIND_PIPELINE_STATE); + } + } + + public bindBindingSetLayout(bindingSetLayout : GFXBindingSetLayout) { + + // TODO + + } + + public bindInputAssembler(inputAssembler : GFXInputAssembler) { + + let cmd = (this._allocator).bindInputAssemblerCmdPool.alloc(WebGLCmdBindInputAssembler); + if (cmd) { + cmd.gpuInputAssembler = (inputAssembler).gpuInputAssembler; + this.cmdPackage.bindInputAssemblerCmds.push(cmd); + + this.cmdPackage.cmds.push(WebGLCmd.BIND_INPUT_ASSEMBLER); + } + } + + public draw(inputAssembler : GFXInputAssembler) { + let cmd = (this._allocator).drawCmdPool.alloc(WebGLCmdDraw); + if (cmd) { + cmd.vertexCount = inputAssembler.vertexCount; + cmd.firstVertex = inputAssembler.firstVertex; + cmd.indexCount = inputAssembler.indexCount; + cmd.firstIndex = inputAssembler.firstIndex; + cmd.vertexOffset = inputAssembler.vertexOffset; + cmd.instanceCount = inputAssembler.instanceCount; + cmd.firstInstance = inputAssembler.firstInstance; + + this.cmdPackage.drawCmds.push(cmd); + + this.cmdPackage.cmds.push(WebGLCmd.DRAW); + } + } + + public updateBuffer(buffer : GFXBuffer, data : ArrayBuffer, offset : number) { + + let gpuBuffer = (buffer).gpuBuffer; + if(gpuBuffer) { + let cmd = (this._allocator).updateBufferCmdPool.alloc(WebGLCmdUpdateBuffer); + if (cmd) { + cmd.gpuBuffer = gpuBuffer; + cmd.data = data.slice(0); + cmd.offset = offset; + this.cmdPackage.updateBufferCmds.push(cmd); + + this.cmdPackage.cmds.push(WebGLCmd.UPDATE_BUFFER); + } + } + } + + public get webGLDevice() : WebGLGFXDevice { + return this._device; + } +}; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-device.ts b/cocos/renderer/gfx/webgl/webgl-gfx-device.ts index 12b6066f8b..709b41c2e5 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-device.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-device.ts @@ -3,8 +3,8 @@ import { GFXBuffer, GFXBufferInfo } from '../gfx-buffer'; import { WebGLGFXBuffer } from './webgl-gfx-buffer'; import { WebGLGFXQueue } from './webgl-gfx-queue'; import { WebGLStateCache } from './webgl-state-cache'; -import { WebGLGPUBuffer, WebGLGPUObjectType, WebGLGPUTexture, WebGLGPURenderPass, WebGLGPUFramebuffer, WebGLGPUTextureView, WebGLGPUShader, WebGLGPUShaderStage } from './webgl-gpu-objects'; -import { WebGLCmdFuncUpdateBuffer, WebGLCmdFuncDestroyBuffer, WebGLCmdFuncCreateBuffer, WebGLCmdFuncDestroyTexture, WebGLCmdFuncCreateTexture, WebGLCmdFuncCreateFramebuffer, WebGLCmdFuncDestroyFramebuffer, WebGLCmdFuncCreateShader, WebGLCmdFuncDestroyShader } from './webgl-commands'; +import { WebGLGPUBuffer, WebGLGPUObjectType, WebGLGPUTexture, WebGLGPURenderPass, WebGLGPUFramebuffer, WebGLGPUTextureView, WebGLGPUShader, WebGLGPUShaderStage, WebGLGPUSampler, WebGLGPUInputAssembler, WebGLGPUPipelineState, WebGLGPUPipelineLayout, WebGLGPUBindingSetLayout, WebGLGPUBinding } from './webgl-gpu-objects'; +import { WebGLCmdFuncUpdateBuffer, WebGLCmdFuncDestroyBuffer, WebGLCmdFuncCreateBuffer, WebGLCmdFuncDestroyTexture, WebGLCmdFuncCreateTexture, WebGLCmdFuncCreateFramebuffer, WebGLCmdFuncDestroyFramebuffer, WebGLCmdFuncCreateShader, WebGLCmdFuncDestroyShader, WebGLCmdFuncCreateInputAssember, WebGLCmdFuncDestroyInputAssembler } from './webgl-commands'; import { GFXTextureInfo, GFXTextureType, GFXTextureFlagBit } from '../gfx-texture'; import { GFXTextureViewType, GFXTextureViewInfo } from '../gfx-texture-view'; import { GFXRenderPassInfo } from '../gfx-render-pass'; @@ -13,22 +13,50 @@ import { WebGLGFXRenderPass } from './webgl-gfx-render-pass'; import { WebGLGFXTextureView } from './webgl-gfx-texture-view'; import { WebGLGFXTexture } from './webgl-gfx-texture'; import { GFXShaderInfo } from '../gfx-shader'; +import { GFXSamplerInfo } from '../gfx-sampler'; +import { WebGLGFXCommandAllocator } from './webgl-gfx-command-allocator'; +import { GFXInputAssemblerInfo } from '../gfx-input-assembler'; +import { WebGLGFXShader } from './webgl-gfx-shader'; +import { GFXCommandAllocator } from '../gfx-command-allocator'; +import { GFXPipelineStateInfo } from '../gfx-pipeline-state'; +import { WebGLGFXPipelineLayout } from './webgl-gfx-pipeline-layout'; +import { GFXBindingSetLayoutInfo } from '../gfx-binding-set-layout'; + +const WebGLPrimitives: GLenum[] = [ + WebGLRenderingContext.POINTS, + WebGLRenderingContext.LINES, + WebGLRenderingContext.LINE_STRIP, + WebGLRenderingContext.LINE_LOOP, + WebGLRenderingContext.TRIANGLES, + WebGLRenderingContext.TRIANGLE_STRIP, + WebGLRenderingContext.TRIANGLE_FAN, + WebGLRenderingContext.NONE, + WebGLRenderingContext.NONE, + WebGLRenderingContext.NONE, + WebGLRenderingContext.NONE, + WebGLRenderingContext.NONE, + WebGLRenderingContext.NONE, + WebGLRenderingContext.NONE, +]; export class WebGLGFXDevice extends GFXDevice { - - stateCache : WebGLStateCache = new WebGLStateCache; - constructor(canvasEL : HTMLElement) { - + stateCache: WebGLStateCache = new WebGLStateCache; + webGLCmdAllocator: WebGLGFXCommandAllocator; + + constructor(canvasEL: HTMLElement) { + super(); + this.webGLCmdAllocator = new WebGLGFXCommandAllocator(this); + try { this._webGLRC = (canvasEL).getContext('webgl'); } catch (err) { console.error(err); return; } - + // No errors are thrown using try catch // Tested through ios baidu browser 4.14.1 if (!this._webGLRC) { @@ -36,24 +64,28 @@ export class WebGLGFXDevice extends GFXDevice { } } - public initialize(info : GFXDeviceInfo) : boolean { + public initialize(info: GFXDeviceInfo): boolean { return true; } - public destroy() : void { + public destroy(): void { } - public createBuffer() : GFXBuffer { + public createBuffer(): GFXBuffer { return new WebGLGFXBuffer(this); } - public getDeviceName() : string { + public createCommandAllocator(): GFXCommandAllocator { + return new WebGLGFXCommandAllocator(this); + } + + public getDeviceName(): string { return "WebGLDevice"; } - public get gl() : WebGLRenderingContext { + public get gl(): WebGLRenderingContext { return this._webGLRC; } @@ -61,18 +93,25 @@ export class WebGLGFXDevice extends GFXDevice { return this._queue; } - public emitCmdCreateGPUBuffer(info : GFXBufferInfo) : WebGLGPUBuffer { + public emitCmdCreateGPUBuffer(info: GFXBufferInfo, buffer : Buffer | null): WebGLGPUBuffer | null { - let gpuBuffer : WebGLGPUBuffer = { - objType : WebGLGPUObjectType.BUFFER, - usage : info.usage, - memUsage : info.memUsage, - size : info.size, - stride : info.stride, - glTarget : 0, - glBuffer : 0, + let gpuBuffer: WebGLGPUBuffer = { + objType: WebGLGPUObjectType.BUFFER, + usage: info.usage, + memUsage: info.memUsage, + size: info.size, + stride: info.stride, + arrayBuffer: null, + buffer: null, + glTarget: 0, + glBuffer: 0, }; + if(buffer) { + gpuBuffer.arrayBuffer = buffer.buffer; + gpuBuffer.buffer = buffer; + } + // TODO: Async // let cmd : WebGLCmd = { type : WebGLCmdType.CREATE_BUFFER, gpuObj : gpuBuffer, isFree : false }; @@ -81,7 +120,7 @@ export class WebGLGFXDevice extends GFXDevice { return gpuBuffer; } - public emitCmdDestroyGPUBuffer(gpuBuffer : WebGLGPUBuffer) { + public emitCmdDestroyGPUBuffer(gpuBuffer: WebGLGPUBuffer) { // TODO: Async // let cmd : WebGLCmd = { type : WebGLCmdType.DESTROY_BUFFER, gpuObj : gpuBuffer, isFree : false }; @@ -89,25 +128,25 @@ export class WebGLGFXDevice extends GFXDevice { WebGLCmdFuncDestroyBuffer(this, gpuBuffer); } - public emitCmdUpdateGPUBuffer(gpuBuffer : WebGLGPUBuffer, offset : number, data : ArrayBuffer) { + public emitCmdUpdateGPUBuffer(gpuBuffer: WebGLGPUBuffer, offset: number, buffer: Buffer) { // TODO: Async - WebGLCmdFuncUpdateBuffer(this, gpuBuffer, offset, data); + WebGLCmdFuncUpdateBuffer(this, gpuBuffer, offset, buffer); } - public emitCmdCreateGPUTexture(info : GFXTextureInfo) : WebGLGPUTexture { + public emitCmdCreateGPUTexture(info: GFXTextureInfo): WebGLGPUTexture { + + let viewType: GFXTextureViewType; - let viewType : GFXTextureViewType; - switch (info.type) { - case GFXTextureType.TEX1D : { - viewType = info.arrayLayer <= 1? GFXTextureViewType.TV1D : GFXTextureViewType.TV1D_ARRAY; + case GFXTextureType.TEX1D: { + viewType = info.arrayLayer <= 1 ? GFXTextureViewType.TV1D : GFXTextureViewType.TV1D_ARRAY; break; } - case GFXTextureType.TEX2D : { - - if(info.arrayLayer <= 1) { + case GFXTextureType.TEX2D: { + + if (info.arrayLayer <= 1) { viewType = GFXTextureViewType.TV2D; - } else if(info.arrayLayer === 6 && (info.flags & GFXTextureFlagBit.CUBEMAP)) { + } else if (info.arrayLayer === 6 && (info.flags & GFXTextureFlagBit.CUBEMAP)) { viewType = GFXTextureViewType.CUBE; } else { viewType = GFXTextureViewType.TV2D_ARRAY; @@ -115,7 +154,7 @@ export class WebGLGFXDevice extends GFXDevice { break; } - case GFXTextureType.TEX3D : { + case GFXTextureType.TEX3D: { viewType = GFXTextureViewType.TV3D; break; } @@ -124,25 +163,25 @@ export class WebGLGFXDevice extends GFXDevice { } } - let gpuTexture : WebGLGPUTexture = { - objType : WebGLGPUObjectType.TEXTURE, - type : info.type, - viewType : viewType, - format : info.format, - usage : info.usage, - width : info.width, - height : info.height, - depth : info.depth, - arrayLayer : info.arrayLayer, - mipLevel : info.mipLevel, - flags : info.flags, + let gpuTexture: WebGLGPUTexture = { + objType: WebGLGPUObjectType.TEXTURE, + type: info.type, + viewType: viewType, + format: info.format, + usage: info.usage, + width: info.width, + height: info.height, + depth: info.depth, + arrayLayer: info.arrayLayer, + mipLevel: info.mipLevel, + flags: info.flags, - glTarget : 0, - glInternelFmt : 0, - glFormat : 0, - glType : 0, - glUsage : 0, - glTexture : 0, + glTarget: 0, + glInternelFmt: 0, + glFormat: 0, + glType: 0, + glUsage: 0, + glTexture: 0, }; // TODO: Async @@ -152,43 +191,46 @@ export class WebGLGFXDevice extends GFXDevice { return gpuTexture; } - public emitCmdDestroyGPUTexture(gpuTexture : WebGLGPUTexture) { + public emitCmdDestroyGPUTexture(gpuTexture: WebGLGPUTexture) { // TODO: Async // let cmd : WebGLCmd = { type : WebGLCmdType.DESTROY_BUFFER, gpuObj : gpuBuffer, isFree : false }; WebGLCmdFuncDestroyTexture(this, gpuTexture); } - public emitCmdCreateGPUTextureView(info : GFXTextureViewInfo) : WebGLGPUTextureView { + public emitCmdCreateGPUTextureView(info: GFXTextureViewInfo): WebGLGPUTextureView { let webGLTexture = info.texture; - let gpuTextureView : WebGLGPUTextureView = { - objType : WebGLGPUObjectType.TEXTURE_VIEW, - gpuTexture : webGLTexture.gpuTexture, - type : info.type, - format : info.format, - baseLevel : info.baseLevel, - levelCount : info.levelCount, + let gpuTextureView: WebGLGPUTextureView = { + objType: WebGLGPUObjectType.TEXTURE_VIEW, + gpuTexture: webGLTexture.gpuTexture, + type: info.type, + format: info.format, + baseLevel: info.baseLevel, + levelCount: info.levelCount, }; return gpuTextureView; } - public emitCmdCreateGPURenderPass(info : GFXRenderPassInfo) : WebGLGPURenderPass { - let gpuRenderPass : WebGLGPURenderPass = { - objType : WebGLGPUObjectType.RENDER_PASS, - colorInfos : info.colorInfos, - depthStencilInfo : info.depthStencilInfo, + public emitCmdCreateGPURenderPass(info: GFXRenderPassInfo): WebGLGPURenderPass { + let gpuRenderPass: WebGLGPURenderPass = { + objType: WebGLGPUObjectType.RENDER_PASS, + colorAttachments: info.colorInfos, + depthStencilAttachment: info.depthStencilInfo, } return gpuRenderPass; } - public emitCmdCreateGPUFramebuffer(info : GFXFramebufferInfo) : WebGLGPUFramebuffer { - + public emitCmdDestroyGPURenderPass(renderPass: WebGLGPURenderPass) { + } + + public emitCmdCreateGPUFramebuffer(info: GFXFramebufferInfo): WebGLGPUFramebuffer { + let renderPass = info.renderPass; - - let gpuColorViews : (WebGLGPUTextureView | null)[] = new Array(); + + let gpuColorViews: (WebGLGPUTextureView | null)[] = new Array(); for (let i = 0; i < info.colorViews.length; ++i) { let webGLColorView = info.colorViews[i]; @@ -197,13 +239,13 @@ export class WebGLGFXDevice extends GFXDevice { let webGLDSView = info.depthStencilView; - let gpuFramebuffer : WebGLGPUFramebuffer = { - objType : WebGLGPUObjectType.FRAMEBUFFER, - gpuRenderPass : renderPass.gpuRenderPass, - gpuColorViews : gpuColorViews, - gpuDepthStencilView : webGLDSView.gpuTextureView, - isOffscreen : info.isOffscreen, - glFramebuffer : 0, + let gpuFramebuffer: WebGLGPUFramebuffer = { + objType: WebGLGPUObjectType.FRAMEBUFFER, + gpuRenderPass: renderPass.gpuRenderPass, + gpuColorViews: gpuColorViews, + gpuDepthStencilView: webGLDSView.gpuTextureView, + isOffscreen: info.isOffscreen, + glFramebuffer: 0, }; // TODO: Async @@ -212,37 +254,62 @@ export class WebGLGFXDevice extends GFXDevice { return gpuFramebuffer; } - public emitCmdDestroyGPUFramebuffer(gpuFramebuffer : WebGLGPUFramebuffer) { + public emitCmdDestroyGPUFramebuffer(gpuFramebuffer: WebGLGPUFramebuffer) { // TODO: Async WebGLCmdFuncDestroyFramebuffer(this, gpuFramebuffer); } - public emitCmdCreateGPUShader(info : GFXShaderInfo) : WebGLGPUShader { + public emitCmdCreateGPUSampler(info: GFXSamplerInfo): WebGLGPUSampler { + let gpuSampler: WebGLGPUSampler = { + objType: WebGLGPUObjectType.FRAMEBUFFER, + minFilter: info.minFilter, + magFilter: info.magFilter, + mipFilter: info.minFilter, + addressU: info.addressU, + addressV: info.addressV, + addressW: info.addressW, + maxAnisotropy: info.maxAnisotropy, + cmpFunc: info.cmpFunc, + borderColor: info.borderColor, + minLOD: info.minLOD, + maxLOD: info.maxLOD, + mipLODBias: info.mipLODBias, + }; + + return gpuSampler; + } + + public emitCmdDestroyGPUSampler(gpuSampler: WebGLGPUSampler) { + // TODO: Async + } + + public emitCmdCreateGPUShader(info: GFXShaderInfo): WebGLGPUShader { let gpuStages: WebGLGPUShaderStage[] = []; - for(let i = 0; i < info.stages.length; ++i) { + for (let i = 0; i < info.stages.length; ++i) { let stage = info.stages[i]; gpuStages.push({ - type : stage.type, - source : stage.source, - macros : stage.macros, - glShader : 0, + type: stage.type, + source: stage.source, + macros: stage.macros, + glShader: 0, }); } - let gpuShader : WebGLGPUShader = { - objType : WebGLGPUObjectType.SHADER, - name : info.name, - bindings : info.bindings, + let gpuShader: WebGLGPUShader = { + objType: WebGLGPUObjectType.SHADER, + name: info.name, + bindings: info.bindings, + blocks: info.blocks, - gpuStages : gpuStages, - glProgram : 0, - glInputs : [], - glUniforms : [], - glBlocks : [], - glSamplers : [], + gpuStages: gpuStages, + glProgram: 0, + glInputs: [], + glUniforms: [], + glBlocks: [], + glSamplers: [], }; // TODO: Async @@ -251,11 +318,131 @@ export class WebGLGFXDevice extends GFXDevice { return gpuShader; } - public emitCmdDestroyGPUShader(gpuShader : WebGLGPUShader) { - + public emitCmdDestroyGPUShader(gpuShader: WebGLGPUShader) { // TODO: Async WebGLCmdFuncDestroyShader(this, gpuShader); } - private _webGLRC : WebGLRenderingContext | null = null; + public emitCmdCreateGPUPipelineState(info: GFXPipelineStateInfo): WebGLGPUPipelineState { + + let gpuShader: WebGLGPUShader | null = null; + if (info.shader) { + gpuShader = (info.shader).gpuShader; + } + + let gpuLayout: WebGLGPUPipelineLayout | null = null; + if (info.layout) { + gpuLayout = (info.layout).gpuPipelineLayout; + } + + let gpuRenderPass: WebGLGPURenderPass | null = null; + if (info.renderPass) { + gpuRenderPass = (info.renderPass).gpuRenderPass; + } + + let gpuPipelineState: WebGLGPUPipelineState = { + objType: WebGLGPUObjectType.PIPELINE_STATE, + glPrimitive: WebGLPrimitives[info.primitive], + gpuShader: gpuShader, + rs: info.rs, + dss: info.dss, + bs: info.bs, + gpuLayout: gpuLayout, + gpuRenderPass: gpuRenderPass, + }; + + return gpuPipelineState; + } + + public emitCmdDestroyGPUPipelineState(gpuPipelineState: WebGLGPUPipelineState) { + // TODO: Async + } + + public emitCmdCreateGPUBindingSetLayout(info: GFXBindingSetLayoutInfo): WebGLGPUBindingSetLayout { + + let gpuBindings: WebGLGPUBinding[] = new Array(info.bindings.length); + for (let i = 0; i < info.bindings.length; ++i) { + let binding = info.bindings[i]; + let gpuBinding = gpuBindings[i]; + gpuBinding.binding = binding.binding; + gpuBinding.type = binding.type; + gpuBinding.name = binding.name; + } + + let gpuBindingSetLayout: WebGLGPUBindingSetLayout = { + objType: WebGLGPUObjectType.BINDING_SET_LAYOUT, + gpuBinding: gpuBindings, + }; + + return gpuBindingSetLayout; + } + + public emitCmdDestroyGPUBindingSetLayout(gpuBindingSetLayout: WebGLGPUBindingSetLayout) { + // TODO: Async + } + + public emitCmdUpdateGPUBindingSetLayout(gpuBindingSetLayout: WebGLGPUBindingSetLayout) { + // TODO: Async + + + } + + public emitCmdCreateGPUInputAssembler(info: GFXInputAssemblerInfo): WebGLGPUInputAssembler { + + let gpuShader: WebGLGPUShader | null = null; + + if (info.shader) { + gpuShader = (info.shader).gpuShader; + } + + let gpuVertexBuffers: WebGLGPUBuffer[] = new Array(info.vertexBuffers.length); + + for (let i = 0; i < info.vertexBuffers.length; ++i) { + let vb = info.vertexBuffers[i]; + if (vb.gpuBuffer) { + gpuVertexBuffers[i] = vb.gpuBuffer; + } + } + + let gpuIndexBuffer: WebGLGPUBuffer | null = null; + + let glIndexType = 0; + if (info.indexBuffer) { + gpuIndexBuffer = (info.indexBuffer).gpuBuffer; + if (gpuIndexBuffer) { + switch (gpuIndexBuffer.stride) { + case 1: glIndexType = WebGLRenderingContext.UNSIGNED_BYTE; break; + case 2: glIndexType = WebGLRenderingContext.UNSIGNED_SHORT; break; + case 4: glIndexType = WebGLRenderingContext.UNSIGNED_INT; break; + default: { + console.error("Error index buffer stride."); + } + } + } + } + + let gpuInputAssembler: WebGLGPUInputAssembler = { + objType: WebGLGPUObjectType.INPUT_ASSEMBLER, + attributes: info.attributes, + gpuShader: gpuShader, + gpuVertexBuffers: gpuVertexBuffers, + gpuIndexBuffer: gpuIndexBuffer, + + glAttribs: [], + glIndexType: glIndexType, + glVAO: 0, + }; + + WebGLCmdFuncCreateInputAssember(this, gpuInputAssembler); + + // TODO: Async + return gpuInputAssembler; + } + + public emitCmdDestroyGPUInputAssembler(gpuInputAssembler: WebGLGPUInputAssembler) { + // TODO: Async + // WebGLCmdFuncDestroyInputAssembler(this, gpuInputAssembler); + } + + private _webGLRC: WebGLRenderingContext | null = null; }; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-framebuffer.ts b/cocos/renderer/gfx/webgl/webgl-gfx-framebuffer.ts index d5a789727d..ce5a68e4f9 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-framebuffer.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-framebuffer.ts @@ -33,5 +33,9 @@ export class WebGLGFXFramebuffer extends GFXFramebuffer { return this._device; } + public get gpuFramebuffer() : WebGLGPUFramebuffer | null { + return this._gpuFramebuffer; + } + private _gpuFramebuffer : WebGLGPUFramebuffer | null = null; }; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-input-assembler.ts b/cocos/renderer/gfx/webgl/webgl-gfx-input-assembler.ts new file mode 100644 index 0000000000..70d16886d8 --- /dev/null +++ b/cocos/renderer/gfx/webgl/webgl-gfx-input-assembler.ts @@ -0,0 +1,40 @@ +import { GFXDevice } from '../gfx-device'; +import { GFXInputAssembler, GFXInputAssemblerInfo } from '../gfx-input-assembler'; +import { WebGLGFXDevice } from './webgl-gfx-device'; +import { WebGLGPUInputAssembler } from './webgl-gpu-objects'; + +export class WebGLGFXInputAssembler extends GFXInputAssembler { + + constructor(device : GFXDevice) { + super(device); + } + + public initialize(info : GFXInputAssemblerInfo) : boolean { + + this._attributes = info.attributes; + this._vertexBuffers = info.vertexBuffers; + this._indexBuffer = info.indexBuffer; + this._isIndirect = info.isIndirect; + + this._gpuInputAssembler = this.webGLDevice.emitCmdCreateGPUInputAssembler(info); + + return true; + } + + public destroy() { + if(this._gpuInputAssembler) { + this.webGLDevice.emitCmdDestroyGPUInputAssembler(this._gpuInputAssembler); + this._gpuInputAssembler = null; + } + } + + public get webGLDevice() : WebGLGFXDevice { + return this._device; + } + + public get gpuInputAssembler() : WebGLGPUInputAssembler | null { + return this._gpuInputAssembler; + } + + private _gpuInputAssembler : WebGLGPUInputAssembler | null = null; +}; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-input-state.ts b/cocos/renderer/gfx/webgl/webgl-gfx-input-state.ts deleted file mode 100644 index 5b41266d2b..0000000000 --- a/cocos/renderer/gfx/webgl/webgl-gfx-input-state.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { GFXDevice } from '../gfx-device'; -import { WebGLGPUTexture } from './webgl-gpu-objects'; -import { WebGLGFXDevice } from './webgl-gfx-device'; -import { GFXFormatSurfaceSize } from '../gfx-define'; -import { GFXInputState, GFXInputStateInfo } from '../gfx-input-state'; - -export class WebGLGFXInputState extends GFXInputState { - - constructor(device : GFXDevice) { - super(device); - } - - public initialize(info : GFXInputStateInfo) : boolean { - - this._attributes = info.attributes; - - return true; - } - - public destroy() { - } -}; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-pipeline-layout.ts b/cocos/renderer/gfx/webgl/webgl-gfx-pipeline-layout.ts new file mode 100644 index 0000000000..3a58074933 --- /dev/null +++ b/cocos/renderer/gfx/webgl/webgl-gfx-pipeline-layout.ts @@ -0,0 +1,32 @@ +import { GFXDevice } from '../gfx-device'; +import { GFXPipelineLayout, GFXPipelineLayoutInfo } from '../gfx-pipeline-layout'; +import { WebGLGFXDevice } from './webgl-gfx-device'; +import { WebGLGPUPipelineLayout } from './webgl-gpu-objects'; + +export class WebGLGFXPipelineLayout extends GFXPipelineLayout { + + constructor(device: GFXDevice) { + super(device); + } + + public initialize(info: GFXPipelineLayoutInfo): boolean { + + this._pushConstantsRanges = info.pushConstantsRanges; + this._layouts = info.layouts; + + return true; + } + + public destroy() { + } + + public get webGLDevice(): WebGLGFXDevice { + return this._device; + } + + public get gpuPipelineLayout(): WebGLGPUPipelineLayout | null { + return this._gpuPipelineLayout; + } + + private _gpuPipelineLayout: WebGLGPUPipelineLayout | null = null; +}; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-pipeline-state.ts b/cocos/renderer/gfx/webgl/webgl-gfx-pipeline-state.ts new file mode 100644 index 0000000000..e64dfcc6ec --- /dev/null +++ b/cocos/renderer/gfx/webgl/webgl-gfx-pipeline-state.ts @@ -0,0 +1,45 @@ +import { GFXDevice } from '../gfx-device'; +import { GFXPipelineState, GFXPipelineStateInfo } from '../gfx-pipeline-state'; +import { WebGLGFXDevice } from './webgl-gfx-device'; +import { WebGLGPUPipelineState } from './webgl-gpu-objects'; + +export class WebGLGFXPipelineState extends GFXPipelineState { + + constructor(device : GFXDevice) { + super(device); + } + + public initialize(info : GFXPipelineStateInfo) : boolean { + + this._primitive = info.primitive; + this._shader = info.shader; + this._is = info.is; + this._rs = info.rs; + this._dss = info.dss; + this._bs = info.bs; + this._layout = info.layout; + this._renderPass = info.renderPass; + + this._gpuPipelineState = this.webGLDevice.emitCmdCreateGPUPipelineState(info); + + return true; + } + + public destroy() { + if(this._gpuPipelineState) + { + this.webGLDevice.emitCmdDestroyGPUPipelineState(this._gpuPipelineState); + this._gpuPipelineState = null; + } + } + + public get webGLDevice() : WebGLGFXDevice { + return this._device; + } + + public get gpuPipelineState() : WebGLGPUPipelineState | null { + return this._gpuPipelineState; + } + + private _gpuPipelineState : WebGLGPUPipelineState | null = null; +}; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-queue.ts b/cocos/renderer/gfx/webgl/webgl-gfx-queue.ts index 1007da7839..30900e34a0 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-queue.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-queue.ts @@ -1,6 +1,9 @@ import { GFXDevice } from '../gfx-device'; import { GFXQueueInfo, GFXQueue } from '../gfx-queue'; import { GFXCommandBuffer } from '../gfx-command-buffer'; +import { WebGLGFXCommandBuffer } from './webgl-gfx-command-buffer'; +import { WebGLCmdFuncExecuteCmds } from './webgl-commands'; +import { WebGLGFXDevice } from './webgl-gfx-device'; export class WebGLGFXQueue extends GFXQueue { @@ -19,10 +22,14 @@ export class WebGLGFXQueue extends GFXQueue { } - public submit(cmdBuffs: GFXCommandBuffer[], fence) { + public submit(cmdBuffs: GFXCommandBuffer[], fence : null = null) { - for(let cb in cmdBuffs) { - + // TODO: Async + if(!this._isAsync) { + for(let i = 0; i < cmdBuffs.length; ++i) { + let cmdBuff = cmdBuffs[i]; + WebGLCmdFuncExecuteCmds(this._device, cmdBuff.cmdPackage); + } } } diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-render-pass.ts b/cocos/renderer/gfx/webgl/webgl-gfx-render-pass.ts index 0d64ae4606..ed3c095443 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-render-pass.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-render-pass.ts @@ -7,28 +7,36 @@ import { GFXRenderPass, GFXRenderPassInfo } from '../gfx-render-pass'; export class WebGLGFXRenderPass extends GFXRenderPass { - constructor(device : GFXDevice) { + constructor(device: GFXDevice) { super(device); } - public initialize(info : GFXRenderPassInfo) : boolean { + public initialize(info: GFXRenderPassInfo): boolean { this._colorInfos = info.colorInfos; this._depthStencilInfo = info.depthStencilInfo; this._subPasses = info.subPasses; - this._gpuRenderPass = (this._device).emitCmdCreateGPURenderPass(info); + this._gpuRenderPass = this.webGLDevice.emitCmdCreateGPURenderPass(info); return true; } public destroy() { - this._gpuRenderPass = null; + + if (this._gpuRenderPass) { + this.webGLDevice.emitCmdDestroyGPURenderPass(this._gpuRenderPass); + this._gpuRenderPass = null; + } + } + + public get webGLDevice(): WebGLGFXDevice { + return this._device; } public get gpuRenderPass(): WebGLGPURenderPass { return this._gpuRenderPass; } - private _gpuRenderPass : WebGLGPURenderPass | null = null; + private _gpuRenderPass: WebGLGPURenderPass | null = null; }; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-sampler.ts b/cocos/renderer/gfx/webgl/webgl-gfx-sampler.ts index 28de03d116..cd76700db3 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-sampler.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-sampler.ts @@ -1,5 +1,7 @@ import { GFXDevice } from '../gfx-device'; import { GFXSampler, GFXSamplerInfo } from '../gfx-sampler'; +import { WebGLGFXDevice } from './webgl-gfx-device'; +import { WebGLGPUSampler } from './webgl-gpu-objects'; export class WebGLGFXSampler extends GFXSampler { @@ -22,9 +24,21 @@ export class WebGLGFXSampler extends GFXSampler { this._maxLOD = info.maxLOD; this._mipLODBias = info.mipLODBias; + this._gpuSampler = this.webGLDevice.emitCmdCreateGPUSampler(info); + return true; } public destroy() { + if(this._gpuSampler) { + this.webGLDevice.emitCmdDestroyGPUSampler(this._gpuSampler); + this._gpuSampler = null; + } } + + public get webGLDevice() : WebGLGFXDevice { + return this._device; + } + + private _gpuSampler : WebGLGPUSampler | null = null; }; diff --git a/cocos/renderer/gfx/webgl/webgl-gfx-shader.ts b/cocos/renderer/gfx/webgl/webgl-gfx-shader.ts index af0631eda8..1cf1045ec7 100644 --- a/cocos/renderer/gfx/webgl/webgl-gfx-shader.ts +++ b/cocos/renderer/gfx/webgl/webgl-gfx-shader.ts @@ -14,6 +14,7 @@ export class WebGLGFXShader extends GFXShader { this._name = info.name; this._stages = info.stages; this._bindings = info.bindings; + this._blocks = info.blocks; this._gpuShader = this.webGLDevice.emitCmdCreateGPUShader(info); diff --git a/cocos/renderer/gfx/webgl/webgl-gpu-objects.ts b/cocos/renderer/gfx/webgl/webgl-gpu-objects.ts index 23ad99a8ff..619df2413a 100644 --- a/cocos/renderer/gfx/webgl/webgl-gpu-objects.ts +++ b/cocos/renderer/gfx/webgl/webgl-gpu-objects.ts @@ -1,11 +1,13 @@ import { GFXBufferUsage, GFXBufferUsageBit, GFXMemoryUsage, GFXMemoryUsageBit } from "../gfx-buffer"; import { GFXTextureType, GFXTextureUsage, GFXTextureUsageBit, GFXTextureFlags, GFXTextureFlagBit } from "../gfx-texture"; import { GFXTextureViewType } from "../gfx-texture-view"; -import { GFXFormat, GFX_MAX_VERTEX_ATTRIBUTES, GFXType } from "../gfx-define"; -import { GFXInputAttribute } from "../gfx-input-state"; -import { ColorAttachmentInfo, DepthStencilAttachmentInfo } from "../gfx-render-pass"; -import { GFXShaderType, GFXShaderMacro } from "../gfx-shader"; -import { GFXBinding } from "../gfx-binding-set-layout"; +import { GFXFormat, GFXType } from "../gfx-define"; +import { GFXColorAttachment, GFXDepthStencilAttachment } from "../gfx-render-pass"; +import { GFXShaderType, GFXShaderMacro, GFXUniformBlock } from "../gfx-shader"; +import { GFXBinding, GFXBindingType } from "../gfx-binding-set-layout"; +import { GFXFilter, GFXAddress } from "../gfx-sampler"; +import { GFXComparisonFunc, GFXRasterizerState, GFXDepthStencilState, GFXBlendState } from "../gfx-pipeline-state"; +import { GFXInputAttribute } from "../gfx-input-assembler"; export const enum WebGLGPUObjectType { UNKNOWN, @@ -14,57 +16,56 @@ export const enum WebGLGPUObjectType { TEXTURE_VIEW, RENDER_PASS, FRAMEBUFFER, + SAMPLER, SHADER, - INPUT_STATE, + PIPELINE_STATE, + BINDING_SET_LAYOUT, + INPUT_ASSEMBLER, + COMMAND_BUFFER, }; export class WebGLGPUObject { - objType : WebGLGPUObjectType; + objType: WebGLGPUObjectType; - constructor(type : WebGLGPUObjectType) { + constructor(type: WebGLGPUObjectType) { this.objType = type; - } -}; - -export class WebGLGPUBuffer extends WebGLGPUObject { - usage : GFXBufferUsage = GFXBufferUsageBit.NONE; - memUsage : GFXMemoryUsage = GFXMemoryUsageBit.NONE; - size : number = 0; - stride : number = 0; - - glTarget : GLenum = 0; - glBuffer : WebGLBuffer = 0; - - constructor() { - super(WebGLGPUObjectType.BUFFER); } }; -export class WebGLGPUInputState extends WebGLGPUObject { - attributes : GFXInputAttribute[] = []; - vertexBuffers : (WebGLGPUBuffer | null)[] = Array(GFX_MAX_VERTEX_ATTRIBUTES); - indexBuffer : WebGLGPUBuffer | null = null; - glVAO : WebGLVertexArrayObjectOES = 0; +export class WebGLGPUBuffer extends WebGLGPUObject { + usage: GFXBufferUsage = GFXBufferUsageBit.NONE; + memUsage: GFXMemoryUsage = GFXMemoryUsageBit.NONE; + size: number = 0; + stride: number = 0; + + glTarget: GLenum = 0; + glBuffer: WebGLBuffer = 0; + arrayBuffer: ArrayBuffer | null = null; + buffer: Buffer | null = null; + + constructor() { + super(WebGLGPUObjectType.BUFFER); + } }; export class WebGLGPUTexture extends WebGLGPUObject { - type : GFXTextureType = GFXTextureType.TEX2D; - viewType : GFXTextureViewType = GFXTextureViewType.TV2D; - format : GFXFormat = GFXFormat.UNKNOWN; - usage : GFXTextureUsage = GFXTextureUsageBit.NONE; - width : number = 0; - height : number = 0; - depth : number = 0; - arrayLayer : number = 0; - mipLevel : number = 0; - flags : GFXTextureFlags = GFXTextureFlagBit.NONE; + type: GFXTextureType = GFXTextureType.TEX2D; + viewType: GFXTextureViewType = GFXTextureViewType.TV2D; + format: GFXFormat = GFXFormat.UNKNOWN; + usage: GFXTextureUsage = GFXTextureUsageBit.NONE; + width: number = 0; + height: number = 0; + depth: number = 0; + arrayLayer: number = 0; + mipLevel: number = 0; + flags: GFXTextureFlags = GFXTextureFlagBit.NONE; - glTarget : GLenum = 0; - glInternelFmt : GLenum = 0; - glFormat : GLenum = 0; - glType : GLenum = 0; - glUsage : GLenum = 0; - glTexture : WebGLTexture = 0; + glTarget: GLenum = 0; + glInternelFmt: GLenum = 0; + glFormat: GLenum = 0; + glType: GLenum = 0; + glUsage: GLenum = 0; + glTexture: WebGLTexture = 0; constructor() { super(WebGLGPUObjectType.TEXTURE); @@ -73,22 +74,22 @@ export class WebGLGPUTexture extends WebGLGPUObject { export class WebGLGPUTextureView extends WebGLGPUObject { - gpuTexture : WebGLGPUTexture; - type : GFXTextureViewType = GFXTextureViewType.TV2D; - format : GFXFormat = GFXFormat.UNKNOWN; - baseLevel : number = 0; - levelCount : number = 1; + gpuTexture: WebGLGPUTexture; + type: GFXTextureViewType = GFXTextureViewType.TV2D; + format: GFXFormat = GFXFormat.UNKNOWN; + baseLevel: number = 0; + levelCount: number = 1; - constructor(texture : WebGLGPUTexture) { + constructor(texture: WebGLGPUTexture) { super(WebGLGPUObjectType.TEXTURE_VIEW); this.gpuTexture = texture; } } export class WebGLGPURenderPass extends WebGLGPUObject { - - colorInfos : ColorAttachmentInfo[] = []; - depthStencilInfo : DepthStencilAttachmentInfo | null = null; + + colorAttachments: GFXColorAttachment[] = []; + depthStencilAttachment: GFXDepthStencilAttachment | null = null; constructor() { super(WebGLGPUObjectType.RENDER_PASS); @@ -96,15 +97,15 @@ export class WebGLGPURenderPass extends WebGLGPUObject { }; export class WebGLGPUFramebuffer extends WebGLGPUObject { - - gpuRenderPass : WebGLGPURenderPass; - gpuColorViews : (WebGLGPUTextureView | null)[] = []; - gpuDepthStencilView : WebGLGPUTextureView | null = null; - isOffscreen : boolean = false; - glFramebuffer : WebGLFramebuffer = 0; + gpuRenderPass: WebGLGPURenderPass; + gpuColorViews: (WebGLGPUTextureView | null)[] = []; + gpuDepthStencilView: WebGLGPUTextureView | null = null; + isOffscreen: boolean = false; - constructor(gpuRenderPass : WebGLGPURenderPass) { + glFramebuffer: WebGLFramebuffer = 0; + + constructor(gpuRenderPass: WebGLGPURenderPass) { super(WebGLGPUObjectType.FRAMEBUFFER); this.gpuRenderPass = gpuRenderPass; @@ -112,70 +113,155 @@ export class WebGLGPUFramebuffer extends WebGLGPUObject { }; export class WebGLGPUSampler extends WebGLGPUObject { - + minFilter: GFXFilter = GFXFilter.LINEAR; + magFilter: GFXFilter = GFXFilter.LINEAR; + mipFilter: GFXFilter = GFXFilter.LINEAR; + addressU: GFXAddress = GFXAddress.WRAP; + addressV: GFXAddress = GFXAddress.WRAP; + addressW: GFXAddress = GFXAddress.WRAP; + maxAnisotropy: number = 16; + cmpFunc: GFXComparisonFunc = GFXComparisonFunc.NEVER; + borderColor: number[] = [0.0, 0.0, 0.0, 0.0]; + minLOD: number = 0; + maxLOD: number = 1000; + mipLODBias: number = 0.0; + + constructor() { + super(WebGLGPUObjectType.SAMPLER); + } }; export class WebGLGPUInput { - binding : number = -1; - name : string = ""; - type : GFXType = GFXType.UNKNOWN; - stride : number = 0; - count : number = 0; - size : number = 0; - - glType : GLenum = 0; - glLoc : GLint = 0; + binding: number = -1; + name: string = ""; + type: GFXType = GFXType.UNKNOWN; + stride: number = 0; + count: number = 0; + size: number = 0; + + glType: GLenum = 0; + glLoc: GLint = 0; }; export class WebGLGPUUniform { - binding : number = -1; - name : string = ""; - type : GFXType = GFXType.UNKNOWN; - stride : number = 0; - count : number = 0; - size : number = 0; - offset : number = 0; - - glType : GLenum = 0; - glLoc : WebGLUniformLocation = -1; + binding: number = -1; + name: string = ""; + type: GFXType = GFXType.UNKNOWN; + stride: number = 0; + count: number = 0; + size: number = 0; + offset: number = 0; + + glType: GLenum = 0; + glLoc: WebGLUniformLocation = -1; + bufferView: Int32Array | Float32Array | null = null; }; export class WebGLGPUUniformBlock { - binding : number = -1; - name : string = ""; - size : number = 0; - glUniforms : WebGLGPUUniform[] = []; + binding: number = -1; + name: string = ""; + size: number = 0; + glUniforms: WebGLGPUUniform[] = []; + + isUniformPackage: boolean = false; // Is a single uniform package? + buffer: ArrayBuffer | null = null; // for cache + bufferView: Buffer | null = null; } export class WebGLGPUUniformSampler { - binding : number = -1; - name : string = ""; - type : GFXType = GFXType.UNKNOWN; - units : number[] = []; + binding: number = -1; + name: string = ""; + type: GFXType = GFXType.UNKNOWN; + units: number[] = []; - glType : GLenum = 0; - glLoc : WebGLUniformLocation = -1; + glType: GLenum = 0; + glLoc: WebGLUniformLocation = -1; } export class WebGLGPUShaderStage { - type : GFXShaderType = GFXShaderType.VERTEX; - source : string = ""; - macros : GFXShaderMacro[] = []; - glShader : WebGLShader = 0; + type: GFXShaderType = GFXShaderType.VERTEX; + source: string = ""; + macros: GFXShaderMacro[] = []; + glShader: WebGLShader = 0; }; export class WebGLGPUShader extends WebGLGPUObject { - name : string = ""; - bindings : GFXBinding[] = []; + name: string = ""; + bindings: GFXBinding[] = []; + blocks: GFXUniformBlock[] = []; - gpuStages : WebGLGPUShaderStage[] = []; - glProgram : WebGLProgram = 0; - glInputs : WebGLGPUInput[] = []; - glUniforms : WebGLGPUUniform[] = []; - glBlocks : WebGLGPUUniformBlock[] = []; - glSamplers : WebGLGPUUniformSampler[] = []; + gpuStages: WebGLGPUShaderStage[] = []; + glProgram: WebGLProgram = 0; + glInputs: WebGLGPUInput[] = []; + glUniforms: WebGLGPUUniform[] = []; + glBlocks: WebGLGPUUniformBlock[] = []; + glSamplers: WebGLGPUUniformSampler[] = []; constructor() { super(WebGLGPUObjectType.SHADER); - } + } +}; + +export class WebGLGPUPipelineLayout extends WebGLGPUObject { + +}; + +export class WebGLGPUPipelineState extends WebGLGPUObject { + + glPrimitive: GLenum = WebGLRenderingContext.TRIANGLES; + gpuShader: WebGLGPUShader | null = null; + rs: GFXRasterizerState | null = null; + dss: GFXDepthStencilState | null = null; + bs: GFXBlendState | null = null; + gpuLayout: WebGLGPUPipelineLayout | null = null; + gpuRenderPass: WebGLGPURenderPass | null = null; + + constructor() { + super(WebGLGPUObjectType.PIPELINE_STATE); + } +}; + +export class WebGLGPUBinding { + binding: number = 0; + type: GFXBindingType = GFXBindingType.UNKNOWN; + name: string = ""; + gpuBuffer: WebGLGPUBuffer | null = null; + gpuTexView: WebGLGPUTextureView | null = null; + gpuSampler: WebGLGPUSampler | null = null; +}; + +export class WebGLGPUBindingSetLayout extends WebGLGPUObject { + + gpuBinding: WebGLGPUBinding[] = []; + + constructor() { + super(WebGLGPUObjectType.BINDING_SET_LAYOUT); + } +}; + +export class WebGLAttrib { + glBuffer: WebGLBuffer = 0; + glLoc: number = 0; + glType: GLenum = 0; + size: number = 0; + count: number = 0; + stride: number = 0; + componentCount: number = 1; + isInstanced: boolean = false; + offset: number = 0; +}; + +export class WebGLGPUInputAssembler extends WebGLGPUObject { + attributes: GFXInputAttribute[] = []; + gpuShader: WebGLGPUShader | null = null; + gpuVertexBuffers: WebGLGPUBuffer[] = []; + gpuIndexBuffer: WebGLGPUBuffer | null = null; + + glAttribs: WebGLAttrib[] = []; + glIndexType: GLenum = 0; + glVAO: WebGLVertexArrayObjectOES = 0; + + constructor() { + super(WebGLGPUObjectType.INPUT_ASSEMBLER); + } }; diff --git a/cocos/renderer/gfx/webgl/webgl-state-cache.ts b/cocos/renderer/gfx/webgl/webgl-state-cache.ts index 0770c8a9c5..611800b748 100644 --- a/cocos/renderer/gfx/webgl/webgl-state-cache.ts +++ b/cocos/renderer/gfx/webgl/webgl-state-cache.ts @@ -1,4 +1,5 @@ -import { GFX_MAX_TEXTURE_UNITS } from "../gfx-define"; +import { GFX_MAX_TEXTURE_UNITS, GFX_MAX_VERTEX_ATTRIBUTES } from "../gfx-define"; +import { GFXRasterizerState, GFXDepthStencilState, GFXBlendState } from "../gfx-pipeline-state"; export class WebGLStateCache { glArrayBuffer : WebGLBuffer | null = null; @@ -7,14 +8,32 @@ export class WebGLStateCache { glTex2DUnits : (WebGLTexture | null)[]; glTexCubeUnits : (WebGLTexture | null)[]; glFramebuffer : WebGLFramebuffer | null = null; + viewport : number[] = [0.0, 0.0, 0.0, 0.0]; + scissorRect : number[] = [0.0, 0.0, 0.0, 0.0]; + rs : GFXRasterizerState; + dss : GFXDepthStencilState; + bs : GFXBlendState; + glProgram : WebGLProgram | null = null; + glEnabledAttribLocs : boolean[]; + glCurrentAttribLocs : boolean[]; constructor() { this.glTex2DUnits = new Array(GFX_MAX_TEXTURE_UNITS); this.glTexCubeUnits = new Array(GFX_MAX_TEXTURE_UNITS); + this.rs = new GFXRasterizerState; + this.dss = new GFXDepthStencilState; + this.bs = new GFXBlendState; + this.glEnabledAttribLocs = new Array(GFX_MAX_VERTEX_ATTRIBUTES); + this.glCurrentAttribLocs = new Array(GFX_MAX_VERTEX_ATTRIBUTES); for(let i = 0; i < GFX_MAX_TEXTURE_UNITS; ++i) { this.glTex2DUnits[i] = null; this.glTexCubeUnits[i] = null; } + + for(let i = 0; i < GFX_MAX_VERTEX_ATTRIBUTES; ++i) { + this.glEnabledAttribLocs[i] = false; + this.glCurrentAttribLocs[i] = false; + } } };