add webgl2 module

This commit is contained in:
jerrywwl
2019-02-12 11:17:06 +08:00
parent d81ff6fba6
commit 64ce38e24d
31 changed files with 6395 additions and 45 deletions
+6 -1
View File
@@ -767,8 +767,13 @@ var game = {
{
// todo, adjust opts here
let gl2 = localCanvas.getContext('webgl2');
if (!gl2) {
this._gfxDevice = new cc.WebGLGFXDevice;
} else {
this._gfxDevice = new cc.WebGL2GFXDevice;
}
this._gfxDevice = new cc.WebGLGFXDevice;
this._gfxDevice.initialize({
canvasElm : localCanvas,
debug: true,
+22
View File
@@ -23,6 +23,28 @@ export interface IGFXCommandBufferInfo {
type: GFXCommandBufferType;
}
export interface IGFXDepthBias {
constantFactor: number;
clamp: number;
slopeFactor: number;
}
export interface IGFXDepthBounds {
minBounds: number;
maxBounds: number;
}
export interface IGFXStencilWriteMask {
face: GFXStencilFace;
writeMask: number;
}
export interface IGFXStencilCompareMask {
face: GFXStencilFace;
reference: number;
compareMask: number;
}
// tslint:disable: max-line-length
export abstract class GFXCommandBuffer extends GFXObject {
+2
View File
@@ -13,6 +13,7 @@ import { GFXShader } from './shader';
import { GFXTexture } from './texture';
import { GFXTextureView } from './texture-view';
import { WebGLGFXDevice } from './webgl/webgl-device';
import { WebGL2GFXDevice } from './webgl2/webgl2-device';
cc.GFXDevice = GFXDevice;
cc.GFXBuffer = GFXBuffer;
@@ -28,5 +29,6 @@ cc.GFXPipelineState = GFXPipelineState;
cc.GFXCommandBuffer = GFXCommandBuffer;
cc.GFXQueue = GFXQueue;
cc.WebGLGFXDevice = WebGLGFXDevice;
cc.WebGL2GFXDevice = WebGL2GFXDevice;
Object.assign(cc, GFXDefines);
+8 -8
View File
@@ -36,23 +36,23 @@ import { WebGLGFXInputAssembler } from './webgl-input-assembler';
import { WebGLGFXPipelineState } from './webgl-pipeline-state';
import { WebGLGFXTexture } from './webgl-texture';
export interface IGFXDepthBias {
export interface IWebGLDepthBias {
constantFactor: number;
clamp: number;
slopeFactor: number;
}
export interface IGFXDepthBounds {
export interface IWebGLDepthBounds {
minBounds: number;
maxBounds: number;
}
export interface IGFXStencilWriteMask {
export interface IWebGLStencilWriteMask {
face: GFXStencilFace;
writeMask: number;
}
export interface IGFXStencilCompareMask {
export interface IWebGLStencilCompareMask {
face: GFXStencilFace;
reference: number;
compareMask: number;
@@ -69,11 +69,11 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
private _curViewport: IGFXViewport | null = null;
private _curScissor: IGFXRect | null = null;
private _curLineWidth: number | null = null;
private _curDepthBias: IGFXDepthBias | null = null;
private _curDepthBias: IWebGLDepthBias | null = null;
private _curBlendConstants: number[] = [];
private _curDepthBounds: IGFXDepthBounds | null = null;
private _curStencilWriteMask: IGFXStencilWriteMask | null = null;
private _curStencilCompareMask: IGFXStencilCompareMask | null = null;
private _curDepthBounds: IWebGLDepthBounds | null = null;
private _curStencilWriteMask: IWebGLStencilWriteMask | null = null;
private _curStencilCompareMask: IWebGLStencilCompareMask | null = null;
private _isStateInvalied: boolean = false;
constructor (device: GFXDevice) {
+21 -12
View File
@@ -25,7 +25,12 @@ import {
WebGLEXT,
} from '../define';
import { WebGLGFXCommandAllocator } from './webgl-command-allocator';
import { IGFXDepthBias, IGFXDepthBounds, IGFXStencilCompareMask, IGFXStencilWriteMask } from './webgl-command-buffer';
import {
IWebGLDepthBias,
IWebGLDepthBounds,
IWebGLStencilCompareMask,
IWebGLStencilWriteMask,
} from './webgl-command-buffer';
import { WebGLGFXDevice } from './webgl-device';
import { IWebGLGPUUniform, WebGLAttrib, WebGLGPUBindingLayout, WebGLGPUBuffer,
WebGLGPUFramebuffer, WebGLGPUInput, WebGLGPUInputAssembler, WebGLGPUPipelineState,
@@ -418,11 +423,11 @@ export class WebGLCmdBindStates extends WebGLCmdObject {
public viewport: IGFXViewport | null = null;
public scissor: IGFXRect | null = null;
public lineWidth: number | null = null;
public depthBias: IGFXDepthBias | null = null;
public depthBias: IWebGLDepthBias | null = null;
public blendConstants: number[] | null = null;
public depthBounds: IGFXDepthBounds | null = null;
public stencilWriteMask: IGFXStencilWriteMask | null = null;
public stencilCompareMask: IGFXStencilCompareMask | null = null;
public depthBounds: IWebGLDepthBounds | null = null;
public stencilWriteMask: IWebGLStencilWriteMask | null = null;
public stencilCompareMask: IWebGLStencilCompareMask | null = null;
constructor () {
super(WebGLCmd.BIND_STATES);
@@ -1901,12 +1906,12 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web
}
}
} else {
console.error("Not found texture view on binding unit " + gpuBinding.binding);
console.error('Not found texture view on binding unit ' + gpuBinding.binding);
}
}
} // if
} else {
console.error("Not found sampler or texture view on binding unit " + gpuBinding.binding);
console.error('Not found sampler or texture view on binding unit ' + gpuBinding.binding);
}
break;
@@ -2191,7 +2196,11 @@ export function WebGLCmdFuncCopyBufferToTexture (
const buff = buffer as ArrayBuffer;
const fmtInfo: IGFXFormatInfo = GFXFormatInfos[gpuTexture.format];
let m = 0, w = 1, h = 1, f = 0, buffOffset = 0;
let m = 0;
let w = 1;
let h = 1;
let f = 0;
let buffOffset = 0;
switch (gpuTexture.glTarget) {
case WebGLRenderingContext.TEXTURE_2D: {
@@ -2258,12 +2267,12 @@ export function WebGLCmdFuncCopyBufferToTexture (
if (!isCompressed) {
for (const region of regions) {
buffOffset = region.buffOffset + region.buffTexHeight * region.buffStride;
let fcount = region.texSubres.baseArrayLayer + region.texSubres.layerCount;
const fcount = region.texSubres.baseArrayLayer + region.texSubres.layerCount;
for (f = region.texSubres.baseArrayLayer; f < fcount; ++f) {
w = region.texExtent.width;
h = region.texExtent.height;
let mcount = region.texSubres.baseMipLevel + region.texSubres.levelCount;
const mcount = region.texSubres.baseMipLevel + region.texSubres.levelCount;
for (m = region.texSubres.baseMipLevel; m < mcount; ++m) {
const memSize = GFXFormatSize(gpuTexture.format, w, h, 1);
const data = buff.slice(buffOffset, buffOffset + memSize);
@@ -2282,12 +2291,12 @@ export function WebGLCmdFuncCopyBufferToTexture (
} else {
for (const region of regions) {
buffOffset = region.buffOffset + region.buffTexHeight * region.buffStride;
let fcount = region.texSubres.baseArrayLayer + region.texSubres.layerCount;
const fcount = region.texSubres.baseArrayLayer + region.texSubres.layerCount;
for (f = region.texSubres.baseArrayLayer; f < fcount; ++f) {
w = region.texExtent.width;
h = region.texExtent.height;
let mcount = region.texSubres.baseMipLevel + region.texSubres.levelCount;
const mcount = region.texSubres.baseMipLevel + region.texSubres.levelCount;
for (m = region.texSubres.baseMipLevel; m < mcount; ++m) {
const memSize = GFXFormatSize(gpuTexture.format, w, h, 1);
const data = buff.slice(buffOffset, buffOffset + memSize);
+1 -1
View File
@@ -491,7 +491,7 @@ export class WebGLGFXDevice extends GFXDevice {
dstBuffer: ArrayBuffer,
regions: GFXBufferTextureCopy[]) {
const gl = this._webGLRC as WebGLRenderingContext;
const gl = this._webGLRC!;
const gpuFramebuffer = (srcFramebuffer as WebGLGFXFramebuffer).gpuFramebuffer;
const curFBO = this.stateCache.glFramebuffer;
-4
View File
@@ -6,10 +6,6 @@ import { WebGLGPURenderPass } from './webgl-gpu-objects';
export class WebGLGFXRenderPass extends GFXRenderPass {
public get webGLDevice (): WebGLGFXDevice {
return this._device as WebGLGFXDevice;
}
public get gpuRenderPass (): WebGLGPURenderPass {
return this._gpuRenderPass!;
}
-5
View File
@@ -1,7 +1,6 @@
import { GFXFilter, GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXSampler, GFXSamplerState, IGFXSamplerInfo } from '../sampler';
import { WebGLGFXDevice } from './webgl-device';
import { WebGLGPUSampler } from './webgl-gpu-objects';
const WebGLWraps: GLenum[] = [
@@ -13,10 +12,6 @@ const WebGLWraps: GLenum[] = [
export class WebGLGFXSampler extends GFXSampler {
public get webGLDevice (): WebGLGFXDevice {
return this._device as WebGLGFXDevice;
}
public get gpuSampler (): WebGLGPUSampler {
return this._gpuSampler!;
}
-4
View File
@@ -7,10 +7,6 @@ import { WebGLGFXTexture } from './webgl-texture';
export class WebGLGFXTextureView extends GFXTextureView {
public get webGLDevice (): WebGLGFXDevice {
return this._device as WebGLGFXDevice;
}
public get gpuTextureView (): WebGLGPUTextureView {
return this._gpuTextureView as WebGLGPUTextureView;
}
-4
View File
@@ -7,10 +7,6 @@ import { WebGLGPUTexture } from './webgl-gpu-objects';
export class WebGLGFXTexture extends GFXTexture {
public get webGLDevice (): WebGLGFXDevice {
return this._device as WebGLGFXDevice;
}
public get gpuTexture (): WebGLGPUTexture {
return this._gpuTexture!;
}
-6
View File
@@ -1,7 +1,6 @@
import { GFXLoadOp, GFXStatus, GFXStoreOp, GFXTextureLayout } from '../define';
import { GFXDevice } from '../device';
import { GFXWindow, IGFXWindowInfo } from '../window';
import { WebGLGFXDevice } from './webgl-device';
export class WebGLGFXWindow extends GFXWindow {
@@ -176,9 +175,4 @@ export class WebGLGFXWindow extends GFXWindow {
this._width = width;
this._height = height;
}
public get webGLDevice (): WebGLGFXDevice {
return this._device as WebGLGFXDevice;
}
}
+1541
View File
File diff suppressed because it is too large Load Diff
+91
View File
@@ -0,0 +1,91 @@
import { GFXBindingLayout, GFXBindingUnit, IGFXBindingLayoutInfo } from '../binding-layout';
import { GFXBindingType, GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { WebGL2GFXBuffer } from './webgl2-buffer';
import { WebGL2GPUBinding, WebGL2GPUBindingLayout } from './webgl2-gpu-objects';
import { WebGL2GFXSampler } from './webgl2-sampler';
import { WebGL2GFXTextureView } from './webgl2-texture-view';
export class WebGL2GFXBindingLayout extends GFXBindingLayout {
public get gpuBindingLayout (): WebGL2GPUBindingLayout {
return this._gpuBindingLayout as WebGL2GPUBindingLayout;
}
private _gpuBindingLayout: WebGL2GPUBindingLayout | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXBindingLayoutInfo): boolean {
this._bindingUnits = new Array<GFXBindingUnit>(info.bindings.length);
for (let i = 0; i < info.bindings.length; ++i) {
const binding = info.bindings[i];
this._bindingUnits[i] = {
binding: binding.binding,
type: binding.type,
name: binding.name,
buffer: null,
texView: null,
sampler: null,
};
}
this._gpuBindingLayout = {
gpuBindings: new Array<WebGL2GPUBinding>(info.bindings.length),
};
for (let i = 0; i < info.bindings.length; ++i) {
const binding = info.bindings[i];
this._gpuBindingLayout.gpuBindings[i] = {
binding: binding.binding,
type: binding.type,
name: binding.name,
gpuBuffer: null,
gpuTexView: null,
gpuSampler: null,
};
}
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._gpuBindingLayout = null;
this._status = GFXStatus.UNREADY;
}
public update () {
if (this._isDirty && this._gpuBindingLayout) {
for (let i = 0; i < this._bindingUnits.length; ++i) {
const bindingUnit = this._bindingUnits[i];
switch (bindingUnit.type) {
case GFXBindingType.UNIFORM_BUFFER: {
if (bindingUnit.buffer) {
this._gpuBindingLayout.gpuBindings[i].gpuBuffer =
(bindingUnit.buffer as WebGL2GFXBuffer).gpuBuffer;
}
break;
}
case GFXBindingType.SAMPLER: {
if (bindingUnit.texView) {
this._gpuBindingLayout.gpuBindings[i].gpuTexView =
(bindingUnit.texView as WebGL2GFXTextureView).gpuTextureView;
}
if (bindingUnit.sampler) {
this._gpuBindingLayout.gpuBindings[i].gpuSampler =
(bindingUnit.sampler as WebGL2GFXSampler).gpuSampler;
}
break;
}
default:
}
}
this._isDirty = false;
}
}
}
+119
View File
@@ -0,0 +1,119 @@
import { GFXBuffer, GFXBufferSource, IGFXBufferInfo, IGFXIndirectBuffer } from '../buffer';
import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXStatus } from '../define';
import { GFXDevice } from '../device';
import {
WebGL2CmdFuncCreateBuffer,
WebGL2CmdFuncDestroyBuffer,
WebGL2CmdFuncResizeBuffer,
WebGL2CmdFuncUpdateBuffer,
} from './webgl2-commands';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GPUBuffer } from './webgl2-gpu-objects';
export class WebGL2GFXBuffer extends GFXBuffer {
public get gpuBuffer (): WebGL2GPUBuffer {
return this._gpuBuffer!;
}
private _gpuBuffer: WebGL2GPUBuffer | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXBufferInfo): boolean {
this._usage = info.usage;
this._memUsage = info.memUsage;
this._size = info.size;
this._stride = Math.max(info.stride || 1, 1);
this._count = this._size / this._stride;
if (this._memUsage & GFXMemoryUsageBit.HOST) {
if (this._usage & GFXBufferUsageBit.INDIRECT) {
this._buffer = { drawInfos: [] };
} else {
this._buffer = new ArrayBuffer(this._size);
}
}
this._gpuBuffer = {
usage: info.usage,
memUsage: info.memUsage,
size: info.size,
stride: info.stride ? info.stride : 1,
buffer: null,
vf32: null,
uniforms: [],
indirects: [],
glTarget: 0,
glBuffer: 0,
};
if (this._buffer) {
if (info.usage & GFXBufferUsageBit.INDIRECT) {
this._gpuBuffer.indirects = (this._buffer as IGFXIndirectBuffer).drawInfos;
} else {
this._gpuBuffer.buffer = this._buffer as ArrayBuffer;
}
}
WebGL2CmdFuncCreateBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer);
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
if (this._gpuBuffer) {
WebGL2CmdFuncDestroyBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer);
this._gpuBuffer = null;
}
this._buffer = null;
this._status = GFXStatus.UNREADY;
}
public resize (size: number) {
this._size = size;
this._count = this._size / this._stride;
if (this._memUsage & GFXMemoryUsageBit.HOST) {
if ((this._usage & GFXBufferUsageBit.INDIRECT) === GFXBufferUsageBit.NONE) {
this._buffer = new ArrayBuffer(this._size);
}
}
if (this._gpuBuffer) {
if (this._buffer) {
if ((this._usage & GFXBufferUsageBit.INDIRECT) === GFXBufferUsageBit.NONE) {
this._gpuBuffer.buffer = this._buffer as ArrayBuffer;
this._gpuBuffer.size = this._gpuBuffer.buffer.byteLength;
}
}
WebGL2CmdFuncResizeBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer);
}
}
public update (buffer: GFXBufferSource, offset?: number, size?: number) {
let buffSize;
if (size !== undefined ) {
buffSize = size;
} else if (this._usage & GFXBufferUsageBit.INDIRECT) {
buffSize = 0;
} else {
buffSize = (buffer as ArrayBuffer).byteLength;
}
WebGL2CmdFuncUpdateBuffer(
this._device as WebGL2GFXDevice,
this._gpuBuffer!,
buffer,
offset || 0,
buffSize);
}
}
@@ -0,0 +1,148 @@
import { CachedArray } from '../../core/memop/cached-array';
import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from '../command-allocator';
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import {
WebGL2CmdBeginRenderPass,
WebGL2CmdBindStates,
WebGL2CmdCopyBufferToTexture,
WebGL2CmdDraw,
WebGL2CmdObject,
WebGL2CmdPackage,
WebGL2CmdUpdateBuffer,
} from './webgl2-commands';
export class WebGL2GFXCommandPool<T extends WebGL2CmdObject> {
private _frees: Array<T|null>;
private _freeIdx: number = 0;
private _freeCmds: CachedArray<T>;
constructor (clazz: new() => T, count: number) {
this._frees = new Array(count);
this._freeCmds = new CachedArray(count);
for (let i = 0; i < count; ++i) {
this._frees[i] = new clazz();
}
this._freeIdx = count - 1;
}
public alloc (clazz: new() => T): T {
return new clazz();
}
/*
public alloc (clazz: new() => T): T {
if (this._freeIdx < 0) {
const size = this._frees.length * 2;
const temp = this._frees;
this._frees = new Array<T>(size);
const increase = size - temp.length;
for (let i = 0; i < increase; ++i) {
this._frees[i] = new clazz();
}
for (let i = increase, j = 0; i < size; ++i, ++j) {
this._frees[i] = temp[j];
}
this._freeIdx += increase;
}
const cmd = this._frees[this._freeIdx]!;
this._frees[this._freeIdx--] = null;
++cmd.refCount;
return cmd;
}
*/
public free (cmd: T) {
if (--cmd.refCount === 0) {
this._freeCmds.push(cmd);
}
}
public freeCmds (cmds: CachedArray<T>) {
return ;
for (let i = 0; i < cmds.length; ++i) {
if (--cmds.array[i].refCount === 0) {
this._freeCmds.push(cmds.array[i]);
}
}
}
public release () {
for (let i = 0; i < this._freeCmds.length; ++i) {
const cmd = this._freeCmds.array[i];
cmd.clear();
this._frees[++this._freeIdx] = cmd;
}
this._freeCmds.clear();
}
}
export class WebGL2GFXCommandAllocator extends GFXCommandAllocator {
public beginRenderPassCmdPool: WebGL2GFXCommandPool<WebGL2CmdBeginRenderPass>;
public bindStatesCmdPool: WebGL2GFXCommandPool<WebGL2CmdBindStates>;
public drawCmdPool: WebGL2GFXCommandPool<WebGL2CmdDraw>;
public updateBufferCmdPool: WebGL2GFXCommandPool<WebGL2CmdUpdateBuffer>;
public copyBufferToTextureCmdPool: WebGL2GFXCommandPool<WebGL2CmdCopyBufferToTexture>;
constructor (device: GFXDevice) {
super(device);
this.beginRenderPassCmdPool = new WebGL2GFXCommandPool(WebGL2CmdBeginRenderPass, 16);
this.bindStatesCmdPool = new WebGL2GFXCommandPool(WebGL2CmdBindStates, 16);
this.drawCmdPool = new WebGL2GFXCommandPool(WebGL2CmdDraw, 16);
this.updateBufferCmdPool = new WebGL2GFXCommandPool(WebGL2CmdUpdateBuffer, 16);
this.copyBufferToTextureCmdPool = new WebGL2GFXCommandPool(WebGL2CmdCopyBufferToTexture, 16);
}
public initialize (info: IGFXCommandAllocatorInfo): boolean {
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._status = GFXStatus.UNREADY;
}
public clearCmds (cmdPackage: WebGL2CmdPackage) {
if (cmdPackage.beginRenderPassCmds.length) {
this.beginRenderPassCmdPool.freeCmds(cmdPackage.beginRenderPassCmds);
cmdPackage.beginRenderPassCmds.clear();
}
if (cmdPackage.bindStatesCmds.length) {
this.bindStatesCmdPool.freeCmds(cmdPackage.bindStatesCmds);
cmdPackage.bindStatesCmds.clear();
}
if (cmdPackage.drawCmds.length) {
this.drawCmdPool.freeCmds(cmdPackage.drawCmds);
cmdPackage.drawCmds.clear();
}
if (cmdPackage.updateBufferCmds.length) {
this.updateBufferCmdPool.freeCmds(cmdPackage.updateBufferCmds);
cmdPackage.updateBufferCmds.clear();
}
if (cmdPackage.copyBufferToTextureCmds.length) {
this.copyBufferToTextureCmdPool.freeCmds(cmdPackage.copyBufferToTextureCmds);
cmdPackage.copyBufferToTextureCmds.clear();
}
cmdPackage.cmds.clear();
}
public releaseCmds () {
this.beginRenderPassCmdPool.release();
this.bindStatesCmdPool.release();
this.drawCmdPool.release();
this.updateBufferCmdPool.release();
this.copyBufferToTextureCmdPool.release();
}
}
+475
View File
@@ -0,0 +1,475 @@
import { GFXBindingLayout } from '../binding-layout';
import { GFXBuffer, GFXBufferSource } from '../buffer';
import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer';
import {
GFXBufferTextureCopy,
GFXBufferUsageBit,
GFXCommandBufferType,
GFXStatus,
GFXStencilFace,
GFXTextureLayout,
IGFXColor,
IGFXRect,
IGFXViewport,
} from '../define';
import { GFXDevice } from '../device';
import { GFXFramebuffer } from '../framebuffer';
import { GFXInputAssembler } from '../input-assembler';
import { GFXPipelineState } from '../pipeline-state';
import { GFXTexture } from '../texture';
import { WebGL2GFXBindingLayout } from './webgl2-binding-layout';
import { WebGL2GFXBuffer } from './webgl2-buffer';
import { WebGL2GFXCommandAllocator } from './webgl2-command-allocator';
import {
WebGL2Cmd,
WebGL2CmdBeginRenderPass,
WebGL2CmdBindStates,
WebGL2CmdCopyBufferToTexture,
WebGL2CmdDraw,
WebGL2CmdPackage,
WebGL2CmdUpdateBuffer,
} from './webgl2-commands';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GFXFramebuffer } from './webgl2-framebuffer';
import { WebGL2GPUBindingLayout, WebGL2GPUInputAssembler, WebGL2GPUPipelineState } from './webgl2-gpu-objects';
import { WebGL2GFXInputAssembler } from './webgl2-input-assembler';
import { WebGL2GFXPipelineState } from './webgl2-pipeline-state';
import { WebGL2GFXTexture } from './webgl2-texture';
export interface IWebGL2DepthBias {
constantFactor: number;
clamp: number;
slopeFactor: number;
}
export interface IWebGL2DepthBounds {
minBounds: number;
maxBounds: number;
}
export interface IWebGL2StencilWriteMask {
face: GFXStencilFace;
writeMask: number;
}
export interface IWebGL2StencilCompareMask {
face: GFXStencilFace;
reference: number;
compareMask: number;
}
export class WebGL2GFXCommandBuffer extends GFXCommandBuffer {
public cmdPackage: WebGL2CmdPackage = new WebGL2CmdPackage();
private _webGLAllocator: WebGL2GFXCommandAllocator | null = null;
private _isInRenderPass: boolean = false;
private _curGPUPipelineState: WebGL2GPUPipelineState | null = null;
private _curGPUBindingLayout: WebGL2GPUBindingLayout | null = null;
private _curGPUInputAssembler: WebGL2GPUInputAssembler | null = null;
private _curViewport: IGFXViewport | null = null;
private _curScissor: IGFXRect | null = null;
private _curLineWidth: number | null = null;
private _curDepthBias: IWebGL2DepthBias | null = null;
private _curBlendConstants: number[] = [];
private _curDepthBounds: IWebGL2DepthBounds | null = null;
private _curStencilWriteMask: IWebGL2StencilWriteMask | null = null;
private _curStencilCompareMask: IWebGL2StencilCompareMask | null = null;
private _isStateInvalied: boolean = false;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXCommandBufferInfo): boolean {
if (!info.allocator) {
return false;
}
this._allocator = info.allocator;
this._webGLAllocator = this._allocator as WebGL2GFXCommandAllocator;
this._type = info.type;
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
if (this._webGLAllocator) {
this._webGLAllocator.clearCmds(this.cmdPackage);
this._allocator = null;
this._webGLAllocator = null;
}
this._status = GFXStatus.UNREADY;
}
public begin () {
this._webGLAllocator!.clearCmds(this.cmdPackage);
this._curGPUPipelineState = null;
this._curGPUBindingLayout = null;
this._curGPUInputAssembler = null;
this._curViewport = null;
this._curScissor = null;
this._curLineWidth = null;
this._curDepthBias = null;
this._curBlendConstants = [];
this._curDepthBounds = null;
this._curStencilWriteMask = null;
this._curStencilCompareMask = null;
}
public end () {
if (this._isStateInvalied) {
this.bindStates();
}
this._isInRenderPass = false;
}
public beginRenderPass (
framebuffer: GFXFramebuffer,
renderArea: IGFXRect,
clearColors: IGFXColor[],
clearDepth: number,
clearStencil: number) {
const cmd = this._webGLAllocator!.beginRenderPassCmdPool.alloc(WebGL2CmdBeginRenderPass);
cmd.gpuFramebuffer = ( framebuffer as WebGL2GFXFramebuffer).gpuFramebuffer;
cmd.renderArea = renderArea;
cmd.clearColors = clearColors.slice();
cmd.clearDepth = clearDepth;
cmd.clearStencil = clearStencil;
this.cmdPackage.beginRenderPassCmds.push(cmd);
this.cmdPackage.cmds.push(WebGL2Cmd.BEGIN_RENDER_PASS);
this._isInRenderPass = true;
}
public endRenderPass () {
this._isInRenderPass = false;
}
public bindPipelineState (pipelineState: GFXPipelineState) {
const gpuPipelineState = (pipelineState as WebGL2GFXPipelineState).gpuPipelineState;
this._curGPUPipelineState = gpuPipelineState;
this._isStateInvalied = true;
}
public bindBindingLayout (bindingLayout: GFXBindingLayout) {
const gpuBindingLayout = (bindingLayout as WebGL2GFXBindingLayout).gpuBindingLayout;
this._curGPUBindingLayout = gpuBindingLayout;
this._isStateInvalied = true;
}
public bindInputAssembler (inputAssembler: GFXInputAssembler) {
const gpuInputAssembler = (inputAssembler as WebGL2GFXInputAssembler).gpuInputAssembler;
this._curGPUInputAssembler = gpuInputAssembler;
this._isStateInvalied = true;
}
public setViewport (viewport: IGFXViewport) {
if (!this._curViewport) {
this._curViewport = {
left: viewport.left,
top: viewport.top,
width: viewport.width,
height: viewport.height,
minDepth: viewport.minDepth,
maxDepth: viewport.maxDepth,
};
} else {
if (this._curViewport.left !== viewport.left ||
this._curViewport.top !== viewport.top ||
this._curViewport.width !== viewport.width ||
this._curViewport.height !== viewport.height ||
this._curViewport.minDepth !== viewport.minDepth ||
this._curViewport.maxDepth !== viewport.maxDepth) {
this._curViewport.left = viewport.left;
this._curViewport.top = viewport.top;
this._curViewport.width = viewport.width;
this._curViewport.height = viewport.height;
this._curViewport.minDepth = viewport.minDepth;
this._curViewport.maxDepth = viewport.maxDepth;
this._isStateInvalied = true;
}
}
if (this._curViewport !== viewport) {
this._curViewport = viewport;
this._isStateInvalied = true;
}
}
public setScissor (scissor: IGFXRect) {
if (!this._curScissor) {
this._curScissor = {
x: scissor.x,
y: scissor.y,
width: scissor.width,
height: scissor.height,
};
} else {
if (this._curScissor.x !== scissor.x ||
this._curScissor.y !== scissor.y ||
this._curScissor.width !== scissor.width ||
this._curScissor.height !== scissor.height) {
this._curScissor.x = scissor.x;
this._curScissor.y = scissor.y;
this._curScissor.width = scissor.width;
this._curScissor.height = scissor.height;
this._isStateInvalied = true;
}
}
}
public setLineWidth (lineWidth: number) {
if (this._curLineWidth !== lineWidth) {
this._curLineWidth = lineWidth;
this._isStateInvalied = true;
}
}
public setDepthBias (depthBiasConstantFacotr: number, depthBiasClamp: number, depthBiasSlopeFactor: number) {
if (!this._curDepthBias) {
this._curDepthBias = {
constantFactor: depthBiasConstantFacotr,
clamp: depthBiasClamp,
slopeFactor: depthBiasSlopeFactor,
};
this._isStateInvalied = true;
} else {
if (this._curDepthBias.constantFactor !== depthBiasConstantFacotr ||
this._curDepthBias.clamp !== depthBiasClamp ||
this._curDepthBias.slopeFactor !== depthBiasSlopeFactor) {
this._curDepthBias.constantFactor = depthBiasConstantFacotr;
this._curDepthBias.clamp = depthBiasClamp;
this._curDepthBias.slopeFactor = depthBiasSlopeFactor;
this._isStateInvalied = true;
}
}
}
public setBlendConstants (blendConstants: number[]) {
if (!this._curBlendConstants &&
blendConstants.length === 4) {
this._curBlendConstants = [blendConstants[0], blendConstants[1], blendConstants[2], blendConstants[3]];
this._isStateInvalied = true;
} else {
if (blendConstants.length === 4 && (
this._curBlendConstants[0] !== blendConstants[0] ||
this._curBlendConstants[1] !== blendConstants[1] ||
this._curBlendConstants[2] !== blendConstants[2] ||
this._curBlendConstants[3] !== blendConstants[3])) {
this._curBlendConstants = [blendConstants[0], blendConstants[1], blendConstants[2], blendConstants[3]];
this._isStateInvalied = true;
}
}
}
public setDepthBound (minDepthBounds: number, maxDepthBounds: number) {
if (!this._curDepthBounds) {
this._curDepthBounds = {
minBounds: minDepthBounds,
maxBounds: maxDepthBounds,
};
this._isStateInvalied = true;
} else {
if (this._curDepthBounds.minBounds !== minDepthBounds ||
this._curDepthBounds.maxBounds !== maxDepthBounds) {
this._curDepthBounds = {
minBounds: minDepthBounds,
maxBounds: maxDepthBounds,
};
this._isStateInvalied = true;
}
}
}
public setStencilWirteMask (face: GFXStencilFace, writeMask: number) {
if (!this._curStencilWriteMask) {
this._curStencilWriteMask = {
face,
writeMask,
};
this._isStateInvalied = true;
} else {
if (this._curStencilWriteMask.face !== face ||
this._curStencilWriteMask.writeMask !== writeMask) {
this._curStencilWriteMask.face = face;
this._curStencilWriteMask.writeMask = writeMask;
this._isStateInvalied = true;
}
}
}
public setStencilCompareMask (face: GFXStencilFace, reference: number, compareMask: number) {
if (!this._curStencilCompareMask) {
this._curStencilCompareMask = {
face,
reference,
compareMask,
};
this._isStateInvalied = true;
} else {
if (this._curStencilCompareMask.face !== face ||
this._curStencilCompareMask.reference !== reference ||
this._curStencilCompareMask.compareMask !== compareMask) {
this._curStencilCompareMask.face = face;
this._curStencilCompareMask.reference = reference;
this._curStencilCompareMask.compareMask = compareMask;
this._isStateInvalied = true;
}
}
}
public draw (inputAssembler: GFXInputAssembler) {
if (this._type === GFXCommandBufferType.PRIMARY && this._isInRenderPass ||
this._type === GFXCommandBufferType.SECONDARY) {
if (this._isStateInvalied) {
this.bindStates();
}
const cmd = ( this._allocator as WebGL2GFXCommandAllocator).
drawCmdPool.alloc(WebGL2CmdDraw);
if (cmd) {
(inputAssembler as WebGL2GFXInputAssembler).extractCmdDraw(cmd);
this.cmdPackage.drawCmds.push(cmd);
this.cmdPackage.cmds.push(WebGL2Cmd.DRAW);
}
} else {
console.error('Command \'draw\' must be recorded inside a render pass.');
}
}
public updateBuffer (buffer: GFXBuffer, data: GFXBufferSource, offset?: number, size?: number) {
if (this._type === GFXCommandBufferType.PRIMARY && !this._isInRenderPass ||
this._type === GFXCommandBufferType.SECONDARY) {
const gpuBuffer = (buffer as WebGL2GFXBuffer).gpuBuffer;
if (gpuBuffer) {
const cmd = this._webGLAllocator!.updateBufferCmdPool.alloc(WebGL2CmdUpdateBuffer);
if (cmd) {
let buffSize;
if (size !== undefined ) {
buffSize = size;
} else if (buffer.usage & GFXBufferUsageBit.INDIRECT) {
buffSize = 0;
} else {
buffSize = (data as ArrayBuffer).byteLength;
}
const buff = data as ArrayBuffer;
cmd.gpuBuffer = gpuBuffer;
cmd.buffer = buff;
cmd.offset = (offset !== undefined ? offset : 0);
cmd.size = buffSize;
this.cmdPackage.updateBufferCmds.push(cmd);
this.cmdPackage.cmds.push(WebGL2Cmd.UPDATE_BUFFER);
}
}
} else {
console.error('Command \'updateBuffer\' must be recorded outside a render pass.');
}
}
public copyBufferToTexture (
srcBuff: GFXBuffer,
dstTex: GFXTexture,
dstLayout: GFXTextureLayout,
regions: GFXBufferTextureCopy[]) {
if (this._type === GFXCommandBufferType.PRIMARY && !this._isInRenderPass ||
this._type === GFXCommandBufferType.SECONDARY) {
const gpuBuffer = ( srcBuff as WebGL2GFXBuffer).gpuBuffer;
const gpuTexture = ( dstTex as WebGL2GFXTexture).gpuTexture;
if (gpuBuffer && gpuTexture) {
const cmd = this._webGLAllocator!.copyBufferToTextureCmdPool.alloc(WebGL2CmdCopyBufferToTexture);
if (cmd) {
cmd.gpuBuffer = gpuBuffer;
cmd.gpuTexture = gpuTexture;
cmd.dstLayout = dstLayout;
cmd.regions = regions;
this.cmdPackage.copyBufferToTextureCmds.push(cmd);
this.cmdPackage.cmds.push(WebGL2Cmd.COPY_BUFFER_TO_TEXTURE);
}
}
} else {
console.error('Command \'copyBufferToTexture\' must be recorded outside a render pass.');
}
}
// tslint:disable: max-line-length
public execute (cmdBuffs: GFXCommandBuffer[], count: number) {
for (let i = 0; i < count; ++i) {
const webGLCmdBuff = cmdBuffs[i] as WebGL2GFXCommandBuffer;
for (let c = 0; c < webGLCmdBuff.cmdPackage.beginRenderPassCmds.length; ++c) {
const cmd = webGLCmdBuff.cmdPackage.beginRenderPassCmds.array[c];
++cmd.refCount;
this.cmdPackage.beginRenderPassCmds.push(cmd);
}
for (let c = 0; c < webGLCmdBuff.cmdPackage.bindStatesCmds.length; ++c) {
const cmd = webGLCmdBuff.cmdPackage.bindStatesCmds.array[c];
++cmd.refCount;
this.cmdPackage.bindStatesCmds.push(cmd);
}
for (let c = 0; c < webGLCmdBuff.cmdPackage.drawCmds.length; ++c) {
const cmd = webGLCmdBuff.cmdPackage.drawCmds.array[c];
++cmd.refCount;
this.cmdPackage.drawCmds.push(cmd);
}
for (let c = 0; c < webGLCmdBuff.cmdPackage.updateBufferCmds.length; ++c) {
const cmd = webGLCmdBuff.cmdPackage.updateBufferCmds.array[c];
++cmd.refCount;
this.cmdPackage.updateBufferCmds.push(cmd);
}
for (let c = 0; c < webGLCmdBuff.cmdPackage.copyBufferToTextureCmds.length; ++c) {
const cmd = webGLCmdBuff.cmdPackage.copyBufferToTextureCmds.array[c];
++cmd.refCount;
this.cmdPackage.copyBufferToTextureCmds.push(cmd);
}
this.cmdPackage.cmds.concat(webGLCmdBuff.cmdPackage.cmds);
}
}
public get webGLDevice (): WebGL2GFXDevice {
return this._device as WebGL2GFXDevice;
}
private bindStates () {
const bindStatesCmd = this._webGLAllocator!.bindStatesCmdPool.alloc(WebGL2CmdBindStates);
if (bindStatesCmd) {
bindStatesCmd.gpuPipelineState = this._curGPUPipelineState;
bindStatesCmd.gpuBindingLayout = this._curGPUBindingLayout;
bindStatesCmd.gpuInputAssembler = this._curGPUInputAssembler;
bindStatesCmd.viewport = this._curViewport;
bindStatesCmd.scissor = this._curScissor;
bindStatesCmd.lineWidth = this._curLineWidth;
bindStatesCmd.depthBias = this._curDepthBias;
bindStatesCmd.blendConstants = this._curBlendConstants;
bindStatesCmd.depthBounds = this._curDepthBounds;
bindStatesCmd.stencilWriteMask = this._curStencilWriteMask;
bindStatesCmd.stencilCompareMask = this._curStencilCompareMask;
this.cmdPackage.bindStatesCmds.push(bindStatesCmd);
this.cmdPackage.cmds.push(WebGL2Cmd.BIND_STATES);
this._isStateInvalied = false;
}
}
}
File diff suppressed because it is too large Load Diff
+469
View File
@@ -0,0 +1,469 @@
import { GFXBindingLayout, IGFXBindingLayoutInfo } from '../binding-layout';
import { GFXBuffer, IGFXBufferInfo } from '../buffer';
import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from '../command-allocator';
import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer';
import { GFXBufferTextureCopy, GFXFormat, GFXFormatInfos, GFXFormatSize, GFXQueueType } from '../define';
import { GFXDevice, GFXFeature, IGFXDeviceInfo } from '../device';
import { GFXFramebuffer, IGFXFramebufferInfo } from '../framebuffer';
import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../input-assembler';
import { GFXPipelineLayout, IGFXPipelineLayoutInfo } from '../pipeline-layout';
import { GFXPipelineState, IGFXPipelineStateInfo } from '../pipeline-state';
import { GFXQueue, IGFXQueueInfo } from '../queue';
import { GFXRenderPass, IGFXRenderPassInfo } from '../render-pass';
import { GFXSampler, IGFXSamplerInfo } from '../sampler';
import { GFXShader, IGFXShaderInfo } from '../shader';
import { GFXTexture, IGFXTextureInfo } from '../texture';
import { GFXTextureView, IGFXTextureViewInfo } from '../texture-view';
import { GFXWindow, IGFXWindowInfo } from '../window';
import { WebGL2GFXBindingLayout } from './webgl2-binding-layout';
import { WebGL2GFXBuffer } from './webgl2-buffer';
import { WebGL2GFXCommandAllocator } from './webgl2-command-allocator';
import { WebGL2GFXCommandBuffer } from './webgl2-command-buffer';
import { WebGL2CmdFuncCopyBufferToTexture } from './webgl2-commands';
import { WebGL2GFXFramebuffer } from './webgl2-framebuffer';
import { WebGL2GFXInputAssembler } from './webgl2-input-assembler';
import { WebGL2GFXPipelineLayout } from './webgl2-pipeline-layout';
import { WebGL2GFXPipelineState } from './webgl2-pipeline-state';
import { WebGL2GFXQueue } from './webgl2-queue';
import { WebGL2GFXRenderPass } from './webgl2-render-pass';
import { WebGL2GFXSampler } from './webgl2-sampler';
import { WebGL2GFXShader } from './webgl2-shader';
import { WebGL2StateCache } from './webgl2-state-cache';
import { WebGL2GFXTexture } from './webgl2-texture';
import { WebGL2GFXTextureView } from './webgl2-texture-view';
import { WebGL2GFXWindow } from './webgl2-window';
export class WebGL2GFXDevice extends GFXDevice {
public get gl (): WebGL2RenderingContext {
return this._webGL2RC as WebGL2RenderingContext;
}
public get isAntialias (): boolean {
return this._isAntialias;
}
public get isPremultipliedAlpha (): boolean {
return this._isPremultipliedAlpha;
}
public get EXT_texture_filter_anisotropic (): EXT_texture_filter_anisotropic | null {
return this._EXT_texture_filter_anisotropic;
}
public get OES_texture_float_linear (): OES_texture_float_linear | null {
return this._OES_texture_float_linear;
}
public get EXT_color_buffer_float (): EXT_color_buffer_float | null {
return this._EXT_color_buffer_float;
}
public get EXT_disjoint_timer_query_webgl2 (): EXT_disjoint_timer_query_webgl2 | null {
return this._EXT_disjoint_timer_query_webgl2;
}
public get WEBGL_compressed_texture_s3tc (): WEBGL_compressed_texture_s3tc | null {
return this._WEBGL_compressed_texture_s3tc;
}
public get WEBGL_compressed_texture_s3tc_srgb (): WEBGL_compressed_texture_s3tc_srgb | null {
return this._WEBGL_compressed_texture_s3tc_srgb;
}
public stateCache: WebGL2StateCache = new WebGL2StateCache();
private _webGL2RC: WebGL2RenderingContext | null = null;
private _isAntialias: boolean = true;
private _isPremultipliedAlpha: boolean = true;
private _extensions: string[] | null = null;
private _EXT_texture_filter_anisotropic: EXT_texture_filter_anisotropic | null = null;
private _OES_texture_float_linear: OES_texture_float_linear | null = null;
private _EXT_color_buffer_float: EXT_color_buffer_float | null = null;
private _EXT_disjoint_timer_query_webgl2: EXT_disjoint_timer_query_webgl2 | null = null;
private _WEBGL_compressed_texture_s3tc: WEBGL_compressed_texture_s3tc | null = null;
private _WEBGL_compressed_texture_s3tc_srgb: WEBGL_compressed_texture_s3tc_srgb | null = null;
private _WEBGL_debug_renderer_info: WEBGL_debug_renderer_info | null = null;
private _WEBGL_debug_shaders: WEBGL_debug_shaders | null = null;
private _WEBGL_lose_context: WEBGL_lose_context | null = null;
constructor () {
super();
}
public initialize (info: IGFXDeviceInfo): boolean {
this._canvas = info.canvasElm as HTMLCanvasElement;
this._isAntialias = info.isAntialias !== undefined ? info.isAntialias : true;
this._isPremultipliedAlpha = info.isPremultipliedAlpha !== undefined ? info.isPremultipliedAlpha : true;
try {
const webGLCtxAttribs: WebGLContextAttributes = {
alpha: true,
antialias: this._isAntialias,
depth: true,
stencil: true,
premultipliedAlpha: this._isPremultipliedAlpha,
preserveDrawingBuffer: false,
powerPreference: 'default',
failIfMajorPerformanceCaveat: false,
};
this._webGL2RC = this._canvas.getContext('webgl2', webGLCtxAttribs);
/*
if (this._webGLRC && info.debug) {
this._webGLRC = WebGLDeveloperTools.makeDebugContext(this._webGLRC, this._onWebGLError.bind(this));
}
*/
} catch (err) {
console.error(err);
return false;
}
// No errors are thrown using try catch
// Tested through ios baidu browser 4.14.1
if (!this._webGL2RC) {
console.error('This device does not support WebGL.');
return false;
}
this._canvas2D = document.createElement('canvas');
console.info('WebGL2 device initialized.');
this._deviceName = 'WebGL2';
const gl = this._webGL2RC;
this._WEBGL_debug_renderer_info = gl.getExtension('WEBGL_debug_renderer_info');
if (this._WEBGL_debug_renderer_info) {
this._renderer = gl.getParameter(this._WEBGL_debug_renderer_info.UNMASKED_RENDERER_WEBGL);
this._vendor = gl.getParameter(this._WEBGL_debug_renderer_info.UNMASKED_VENDOR_WEBGL);
} else {
this._renderer = gl.getParameter(WebGLRenderingContext.RENDERER);
this._vendor = gl.getParameter(WebGLRenderingContext.VENDOR);
}
this._version = gl.getParameter(WebGLRenderingContext.VERSION);
this._maxVertexAttributes = gl.getParameter(WebGLRenderingContext.MAX_VERTEX_ATTRIBS);
this._maxVertexUniformVectors = gl.getParameter(WebGLRenderingContext.MAX_VERTEX_UNIFORM_VECTORS);
this._maxFragmentUniformVectors = gl.getParameter(WebGLRenderingContext.MAX_FRAGMENT_UNIFORM_VECTORS);
this._maxTextureUnits = gl.getParameter(WebGLRenderingContext.MAX_TEXTURE_IMAGE_UNITS);
this._maxVertexTextureUnits = gl.getParameter(WebGLRenderingContext.MAX_VERTEX_TEXTURE_IMAGE_UNITS);
this._depthBits = gl.getParameter(WebGLRenderingContext.DEPTH_BITS);
this._stencilBits = gl.getParameter(WebGLRenderingContext.STENCIL_BITS);
this._width = this._canvas.width;
this._height = this._canvas.height;
this._colorFmt = GFXFormat.RGBA8;
if (this._depthBits === 32) {
if (this._stencilBits === 8) {
this._depthStencilFmt = GFXFormat.D32F_S8;
} else {
this._depthStencilFmt = GFXFormat.D32F;
}
} else if (this._depthBits === 24) {
if (this._stencilBits === 8) {
this._depthStencilFmt = GFXFormat.D24S8;
} else {
this._depthStencilFmt = GFXFormat.D24;
}
} else {
if (this._stencilBits === 8) {
this._depthStencilFmt = GFXFormat.D16S8;
} else {
this._depthStencilFmt = GFXFormat.D16;
}
}
console.info('RENDERER: ' + this._renderer);
console.info('VENDOR: ' + this._vendor);
console.info('VERSION: ' + this._version);
console.info('SCREEN_SIZE: ' + this._width + ' x ' + this._height);
console.info('COLOR_FORMAT: ' + GFXFormatInfos[this._colorFmt].name);
console.info('DEPTH_STENCIL_FORMAT: ' + GFXFormatInfos[this._depthStencilFmt].name);
console.info('MAX_VERTEX_ATTRIBS: ' + this._maxVertexAttributes);
console.info('MAX_VERTEX_UNIFORM_VECTORS: ' + this._maxVertexUniformVectors);
console.info('MAX_FRAGMENT_UNIFORM_VECTORS: ' + this._maxFragmentUniformVectors);
console.info('MAX_TEXTURE_IMAGE_UNITS: ' + this._maxTextureUnits);
console.info('MAX_VERTEX_TEXTURE_IMAGE_UNITS: ' + this._maxVertexTextureUnits);
console.info('DEPTH_BITS: ' + this._depthBits);
console.info('STENCIL_BITS: ' + this._stencilBits);
this._extensions = gl.getSupportedExtensions();
/*
if (this._extensions) {
for (let i = 0; i < this._extensions.length; ++i) {
console.info(this._extensions[i]);
}
}
*/
this._EXT_texture_filter_anisotropic = gl.getExtension('EXT_texture_filter_anisotropic');
this._EXT_color_buffer_float = gl.getExtension('EXT_color_buffer_float');
this._EXT_disjoint_timer_query_webgl2 = gl.getExtension('EXT_disjoint_timer_query_webgl2');
this._OES_texture_float_linear = gl.getExtension('OES_texture_float_linear');
this._WEBGL_compressed_texture_s3tc = gl.getExtension('WEBGL_compressed_texture_s3tc');
this._WEBGL_compressed_texture_s3tc_srgb = gl.getExtension('WEBGL_compressed_texture_s3tc_srgb');
this._WEBGL_debug_shaders = gl.getExtension('WEBGL_debug_shaders');
this._WEBGL_lose_context = gl.getExtension('WEBGL_lose_context');
this._features.fill(false);
this._features[GFXFeature.TEXTURE_FLOAT] = true;
this._features[GFXFeature.TEXTURE_HALF_FLOAT] = true;
// init states
this.initStates(gl);
// create queue
this._queue = this.createQueue({ type: GFXQueueType.GRAPHICS });
// create primary window
this._mainWindow = this.createWindow({
title: this._webGL2RC.canvas.title,
left: this._webGL2RC.canvas.offsetLeft,
top: this._webGL2RC.canvas.offsetTop,
width: this._webGL2RC.drawingBufferWidth,
height: this._webGL2RC.drawingBufferHeight,
colorFmt: this._colorFmt,
depthStencilFmt: this._depthStencilFmt,
});
this._cmdAllocator = this.createCommandAllocator({});
return true;
}
public destroy (): void {
if (this._mainWindow) {
this._mainWindow.destroy();
this._mainWindow = null;
}
if (this._cmdAllocator) {
this._cmdAllocator.destroy();
this._cmdAllocator = null;
}
if (this._queue) {
this._queue.destroy();
this._queue = null;
}
this._webGL2RC = null;
}
public resize (width: number, height: number) {
this._canvas!.width = width;
this._canvas!.height = height;
this._width = width;
this._height = height;
}
public createBuffer (info: IGFXBufferInfo): GFXBuffer {
const buffer = new WebGL2GFXBuffer(this);
buffer.initialize(info);
return buffer;
}
public createTexture (info: IGFXTextureInfo): GFXTexture {
const texture = new WebGL2GFXTexture(this);
texture.initialize(info);
return texture;
}
public createTextureView (info: IGFXTextureViewInfo): GFXTextureView {
const texView = new WebGL2GFXTextureView(this);
texView.initialize(info);
return texView;
}
public createSampler (info: IGFXSamplerInfo): GFXSampler {
const sampler = new WebGL2GFXSampler(this);
sampler.initialize(info);
return sampler;
}
public createBindingLayout (info: IGFXBindingLayoutInfo): GFXBindingLayout {
const bindingLayout = new WebGL2GFXBindingLayout(this);
bindingLayout.initialize(info);
return bindingLayout;
}
public createShader (info: IGFXShaderInfo): GFXShader {
const shader = new WebGL2GFXShader(this);
shader.initialize(info);
return shader;
}
public createInputAssembler (info: IGFXInputAssemblerInfo): GFXInputAssembler {
const inputAssembler = new WebGL2GFXInputAssembler(this);
inputAssembler.initialize(info);
return inputAssembler;
}
public createRenderPass (info: IGFXRenderPassInfo): GFXRenderPass {
const renderPass = new WebGL2GFXRenderPass(this);
renderPass.initialize(info);
return renderPass;
}
public createFramebuffer (info: IGFXFramebufferInfo): GFXFramebuffer {
const framebuffer = new WebGL2GFXFramebuffer(this);
framebuffer.initialize(info);
return framebuffer;
}
public createPipelineLayout (info: IGFXPipelineLayoutInfo): GFXPipelineLayout {
const pipelineLayout = new WebGL2GFXPipelineLayout(this);
pipelineLayout.initialize(info);
return pipelineLayout;
}
public createPipelineState (info: IGFXPipelineStateInfo): GFXPipelineState {
const pipelineState = new WebGL2GFXPipelineState(this);
pipelineState.initialize(info);
return pipelineState;
}
public createCommandAllocator (info: IGFXCommandAllocatorInfo): GFXCommandAllocator {
const cmdAllocator = new WebGL2GFXCommandAllocator(this);
cmdAllocator.initialize(info);
return cmdAllocator;
}
public createCommandBuffer (info: IGFXCommandBufferInfo): GFXCommandBuffer {
const cmdBuff = new WebGL2GFXCommandBuffer(this);
cmdBuff.initialize(info);
return cmdBuff;
}
public createQueue (info: IGFXQueueInfo): GFXQueue {
const queue = new WebGL2GFXQueue(this);
queue.initialize(info);
return queue;
}
public createWindow (info: IGFXWindowInfo): GFXWindow {
const window = new WebGL2GFXWindow(this);
window.initialize(info);
return window;
}
public present () {
(this._cmdAllocator as WebGL2GFXCommandAllocator).releaseCmds();
}
public copyBufferToTexture (buffer: ArrayBuffer, texture: GFXTexture, regions: GFXBufferTextureCopy[]) {
WebGL2CmdFuncCopyBufferToTexture(
this,
buffer,
(texture as WebGL2GFXTexture).gpuTexture,
regions);
}
public copyImageSourceToTexture (
source: CanvasImageSource[],
texture: GFXTexture,
regions: GFXBufferTextureCopy[]) {
if (!this._canvas2D) { return; }
const context = this._canvas2D.getContext('2d');
if (!context) { return; }
const data: Uint8ClampedArray[] = [];
for (let i = 0; i < source.length; i++) {
const level = regions[i].texSubres.baseMipLevel;
this._canvas2D.width = texture.width >> level;
this._canvas2D.height = texture.height >> level;
context.drawImage(source[i], 0, 0);
data.push(context.getImageData(0, 0, this._canvas2D.width, this._canvas2D.height).data);
}
const buffer = new Uint8ClampedArray(data.reduce((acc, cur) => acc + cur.length, 0));
data.reduce((acc, cur) => { buffer.set(cur, acc); return acc + cur.length; }, 0);
WebGL2CmdFuncCopyBufferToTexture(
this,
buffer,
(texture as WebGL2GFXTexture).gpuTexture,
regions);
}
public copyFramebufferToBuffer (
srcFramebuffer: GFXFramebuffer,
dstBuffer: ArrayBuffer,
regions: GFXBufferTextureCopy[]) {
const gl = this._webGL2RC!;
const gpuFramebuffer = (srcFramebuffer as WebGL2GFXFramebuffer).gpuFramebuffer;
const curFBO = this.stateCache.glFramebuffer;
if (this.stateCache.glFramebuffer !== gpuFramebuffer.glFramebuffer) {
gl.bindFramebuffer(WebGLRenderingContext.FRAMEBUFFER, gpuFramebuffer.glFramebuffer);
this.stateCache.glFramebuffer = gpuFramebuffer.glFramebuffer;
}
const view = new Uint8Array(dstBuffer);
for (const region of regions) {
const buffOffset = region.buffOffset + region.buffTexHeight * region.buffStride;
const w = region.texExtent.width;
const h = region.texExtent.height;
const memSize = GFXFormatSize(GFXFormat.RGBA8, w, h, 1);
const data = view.subarray(buffOffset, buffOffset + memSize);
gl.readPixels(region.texOffset.x, region.texOffset.y, w, h,
WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, data);
}
if (this.stateCache.glFramebuffer !== curFBO) {
gl.bindFramebuffer(WebGLRenderingContext.FRAMEBUFFER, curFBO);
this.stateCache.glFramebuffer = curFBO;
}
}
private initStates (gl: WebGL2RenderingContext) {
gl.activeTexture(gl.TEXTURE0);
gl.pixelStorei(gl.PACK_ALIGNMENT, 1);
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// rasteriazer state
gl.enable(gl.CULL_FACE);
gl.cullFace(gl.BACK);
gl.frontFace(gl.CCW);
gl.polygonOffset(0.0, 0.0);
// depth stencil state
gl.enable(gl.DEPTH_TEST);
gl.depthMask(true);
gl.depthFunc(gl.LESS);
gl.stencilFuncSeparate(gl.FRONT, gl.ALWAYS, 1, 0xffffffff);
gl.stencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.KEEP);
gl.stencilMaskSeparate(gl.FRONT, 0xffffffff);
gl.stencilFuncSeparate(gl.BACK, gl.ALWAYS, 1, 0xffffffff);
gl.stencilOpSeparate(gl.BACK, gl.KEEP, gl.KEEP, gl.KEEP);
gl.stencilMaskSeparate(gl.BACK, 0xffffffff);
gl.disable(gl.STENCIL_TEST);
// blend state
gl.disable(gl.SAMPLE_ALPHA_TO_COVERAGE);
gl.disable(gl.BLEND);
gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
gl.blendFuncSeparate(gl.ONE, gl.ZERO, gl.ONE, gl.ZERO);
gl.colorMask(true, true, true, true);
gl.blendColor(0.0, 0.0, 0.0, 0.0);
}
}
+74
View File
@@ -0,0 +1,74 @@
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXFramebuffer, IGFXFramebufferInfo } from '../framebuffer';
import { WebGL2CmdFuncCreateFramebuffer, WebGL2CmdFuncDestroyFramebuffer } from './webgl2-commands';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GPUFramebuffer, WebGL2GPUTextureView } from './webgl2-gpu-objects';
import { WebGL2GFXRenderPass } from './webgl2-render-pass';
import { WebGL2GFXTextureView } from './webgl2-texture-view';
export class WebGL2GFXFramebuffer extends GFXFramebuffer {
public get gpuFramebuffer (): WebGL2GPUFramebuffer {
return this._gpuFramebuffer!;
}
private _gpuFramebuffer: WebGL2GPUFramebuffer | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXFramebufferInfo): boolean {
this._renderPass = info.renderPass;
this._colorViews = info.colorViews || [];
this._depthStencilView = info.depthStencilView || null;
this._isOffscreen = info.isOffscreen !== undefined ? info.isOffscreen : false;
if (this._isOffscreen) {
const gpuColorViews: WebGL2GPUTextureView[] = [];
if (info.colorViews !== undefined) {
for (const colorView of info.colorViews) {
gpuColorViews.push((colorView as WebGL2GFXTextureView).gpuTextureView);
}
}
let gpuDepthStencilView: WebGL2GPUTextureView | null = null;
if (info.depthStencilView !== undefined) {
gpuDepthStencilView = (info.depthStencilView as WebGL2GFXTextureView).gpuTextureView;
}
this._gpuFramebuffer = {
gpuRenderPass: (info.renderPass as WebGL2GFXRenderPass).gpuRenderPass,
gpuColorViews,
gpuDepthStencilView,
isOffscreen: info.isOffscreen,
glFramebuffer: 0,
};
WebGL2CmdFuncCreateFramebuffer(this._device as WebGL2GFXDevice, this._gpuFramebuffer);
} else {
this._gpuFramebuffer = {
gpuRenderPass: (info.renderPass as WebGL2GFXRenderPass).gpuRenderPass,
gpuColorViews: [],
gpuDepthStencilView: null,
isOffscreen: info.isOffscreen,
glFramebuffer: 0,
};
}
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
if (this._isOffscreen && this._gpuFramebuffer) {
WebGL2CmdFuncDestroyFramebuffer(this._device as WebGL2GFXDevice, this._gpuFramebuffer);
}
this._gpuFramebuffer = null;
this._status = GFXStatus.UNREADY;
}
}
+232
View File
@@ -0,0 +1,232 @@
import { IGFXDrawInfo } from '../buffer';
import {
GFXBindingType,
GFXBufferUsage,
GFXBufferUsageBit,
GFXDynamicState,
GFXFormat,
GFXMemoryUsage,
GFXMemoryUsageBit,
GFXShaderType,
GFXTextureFlagBit,
GFXTextureFlags,
GFXTextureType,
GFXTextureUsage,
GFXTextureUsageBit,
GFXTextureViewType,
GFXType,
} from '../define';
import { IGFXInputAttribute } from '../input-assembler';
import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../pipeline-state';
import { GFXColorAttachment, GFXDepthStencilAttachment } from '../render-pass';
import { GFXUniformBlock, GFXUniformSampler, IGFXShaderMacro } from '../shader';
export interface IWebGL2GPUUniformInfo {
name: string;
type: GFXType;
count: number;
offset: number;
view: Float32Array | Int32Array;
isDirty: boolean;
}
export class WebGL2GPUBuffer {
public usage: GFXBufferUsage = GFXBufferUsageBit.NONE;
public memUsage: GFXMemoryUsage = GFXMemoryUsageBit.NONE;
public size: number = 0;
public stride: number = 0;
public glTarget: GLenum = 0;
public glBuffer: WebGLBuffer = 0;
public buffer: ArrayBuffer | null = null;
public vf32: Float32Array | null = null;
public uniforms: IWebGL2GPUUniformInfo[] = [];
public indirects: IGFXDrawInfo[] = [];
}
export class WebGL2GPUTexture {
public type: GFXTextureType = GFXTextureType.TEX2D;
public viewType: GFXTextureViewType = GFXTextureViewType.TV2D;
public format: GFXFormat = GFXFormat.UNKNOWN;
public usage: GFXTextureUsage = GFXTextureUsageBit.NONE;
public width: number = 0;
public height: number = 0;
public depth: number = 1;
public arrayLayer: number = 1;
public mipLevel: number = 1;
public flags: GFXTextureFlags = GFXTextureFlagBit.NONE;
public glTarget: GLenum = 0;
public glInternelFmt: GLenum = 0;
public glFormat: GLenum = 0;
public glType: GLenum = 0;
public glUsage: GLenum = 0;
public glTexture: WebGLTexture = 0;
public glWrapS: GLenum = 0;
public glWrapT: GLenum = 0;
public glMinFilter: GLenum = 0;
public glMagFilter: GLenum = 0;
}
export class WebGL2GPUTextureView {
public gpuTexture: WebGL2GPUTexture;
public type: GFXTextureViewType = GFXTextureViewType.TV2D;
public format: GFXFormat = GFXFormat.UNKNOWN;
public baseLevel: number = 0;
public levelCount: number = 1;
constructor (texture: WebGL2GPUTexture) {
this.gpuTexture = texture;
}
}
export class WebGL2GPURenderPass {
public colorAttachments: GFXColorAttachment[] = [];
public depthStencilAttachment: GFXDepthStencilAttachment | null = null;
}
export class WebGL2GPUFramebuffer {
public gpuRenderPass: WebGL2GPURenderPass;
public gpuColorViews: WebGL2GPUTextureView[] = [];
public gpuDepthStencilView: WebGL2GPUTextureView | null = null;
public isOffscreen?: boolean = false;
public glFramebuffer: WebGLFramebuffer = 0;
constructor (gpuRenderPass: WebGL2GPURenderPass) {
this.gpuRenderPass = gpuRenderPass;
}
}
export class WebGL2GPUSampler {
public glMinFilter: GLenum = WebGLRenderingContext.NONE;
public glMagFilter: GLenum = WebGLRenderingContext.NONE;
public glWrapS: GLenum = WebGLRenderingContext.NONE;
public glWrapT: GLenum = WebGLRenderingContext.NONE;
public glWrapR: GLenum = WebGLRenderingContext.NONE;
}
export class WebGL2GPUInput {
public binding: number = -1;
public name: string = '';
public type: GFXType = GFXType.UNKNOWN;
public stride: number = 0;
public count: number = 0;
public size: number = 0;
public glType: GLenum = 0;
public glLoc: GLint = 0;
}
export interface IWebGL2GPUUniform {
binding: number;
name: string;
type: GFXType;
stride: number;
count: number;
size: number;
offset: number;
glType: GLenum;
glLoc: WebGLUniformLocation;
array: number[];
begin: number;
isFirst: boolean;
}
export class WebGL2GPUUniformBlock {
public binding: number = -1;
public name: string = '';
public size: number = 0;
public glUniforms: IWebGL2GPUUniform[] = [];
public glActiveUniforms: IWebGL2GPUUniform[] = [];
public isUniformPackage: boolean = false; // Is a single uniform package?
}
export class WebGL2GPUUniformSampler {
public binding: number = -1;
public name: string = '';
public type: GFXType = GFXType.UNKNOWN;
public units: number[] = [];
public glType: GLenum = 0;
public glLoc: WebGLUniformLocation = -1;
}
export class WebGL2GPUShaderStage {
public type: GFXShaderType = GFXShaderType.VERTEX;
public source: string = '';
public macros: IGFXShaderMacro[] = [];
public glShader: WebGLShader = 0;
}
export class WebGL2GPUShader {
public name: string = '';
public blocks: GFXUniformBlock[] = [];
public samplers: GFXUniformSampler[] = [];
public gpuStages: WebGL2GPUShaderStage[] = [];
public glProgram: WebGLProgram = 0;
public glInputs: WebGL2GPUInput[] = [];
public glUniforms: IWebGL2GPUUniform[] = [];
public glBlocks: WebGL2GPUUniformBlock[] = [];
public glSamplers: WebGL2GPUUniformSampler[] = [];
}
export class WebGL2GPUPipelineLayout {
}
export class WebGL2GPUPipelineState {
public glPrimitive: GLenum = WebGLRenderingContext.TRIANGLES;
public gpuShader: WebGL2GPUShader | null = null;
public rs: GFXRasterizerState = new GFXRasterizerState();
public dss: GFXDepthStencilState = new GFXDepthStencilState();
public bs: GFXBlendState = new GFXBlendState();
public dynamicStates: GFXDynamicState[] = [];
public gpuLayout: WebGL2GPUPipelineLayout | null = null;
public gpuRenderPass: WebGL2GPURenderPass | null = null;
}
export class WebGL2GPUBinding {
public binding: number = 0;
public type: GFXBindingType = GFXBindingType.UNKNOWN;
public name: string = '';
public gpuBuffer: WebGL2GPUBuffer | null = null;
public gpuTexView: WebGL2GPUTextureView | null = null;
public gpuSampler: WebGL2GPUSampler | null = null;
}
export class WebGL2GPUBindingLayout {
public gpuBindings: WebGL2GPUBinding[] = [];
}
export class WebGL2Attrib {
public name: string = '';
public glBuffer: WebGLBuffer = 0;
public glType: GLenum = 0;
public size: number = 0;
public count: number = 0;
public stride: number = 0;
public componentCount: number = 1;
public isNormalized: boolean = false;
public isInstanced: boolean = false;
public offset: number = 0;
}
export class WebGL2GPUInputAssembler {
public attributes: IGFXInputAttribute[] = [];
public gpuVertexBuffers: WebGL2GPUBuffer[] = [];
public gpuIndexBuffer: WebGL2GPUBuffer | null = null;
public gpuIndirectBuffer: WebGL2GPUBuffer | null = null;
public glAttribs: WebGL2Attrib[] = [];
public glIndexType: GLenum = 0;
public glVAO: WebGLVertexArrayObject = 0;
}
+102
View File
@@ -0,0 +1,102 @@
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../input-assembler';
import { WebGL2GFXBuffer } from './webgl2-buffer';
import { WebGL2CmdDraw, WebGL2CmdFuncCreateInputAssember } from './webgl2-commands';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GPUBuffer, WebGL2GPUInputAssembler } from './webgl2-gpu-objects';
export class WebGL2GFXInputAssembler extends GFXInputAssembler {
public get gpuInputAssembler (): WebGL2GPUInputAssembler {
return this._gpuInputAssembler!;
}
private _gpuInputAssembler: WebGL2GPUInputAssembler | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXInputAssemblerInfo): boolean {
if (info.vertexBuffers.length === 0) {
console.error('GFXInputAssemblerInfo.vertexBuffers is null.');
return false;
}
this._attributes = info.attributes;
this._vertexBuffers = info.vertexBuffers;
if (info.indexBuffer !== undefined) {
this._indexBuffer = info.indexBuffer;
this._indexCount = this._indexBuffer.size / this._indexBuffer.stride;
} else {
const vertBuff = this._vertexBuffers[0];
this._vertexCount = vertBuff.size / vertBuff.stride;
}
this._indirectBuffer = info.indirectBuffer || null;
const gpuVertexBuffers: WebGL2GPUBuffer[] = new Array<WebGL2GPUBuffer>(info.vertexBuffers.length);
for (let i = 0; i < info.vertexBuffers.length; ++i) {
const vb = info.vertexBuffers[i] as WebGL2GFXBuffer;
if (vb.gpuBuffer) {
gpuVertexBuffers[i] = vb.gpuBuffer;
}
}
let gpuIndexBuffer: WebGL2GPUBuffer | null = null;
let glIndexType = 0;
if (info.indexBuffer) {
gpuIndexBuffer = (info.indexBuffer as WebGL2GFXBuffer).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 gpuIndirectBuffer: WebGL2GPUBuffer | null = null;
if (info.indirectBuffer !== undefined) {
gpuIndirectBuffer = (info.indirectBuffer as WebGL2GFXBuffer).gpuBuffer;
}
this._gpuInputAssembler = {
attributes: info.attributes,
gpuVertexBuffers,
gpuIndexBuffer,
gpuIndirectBuffer,
glAttribs: [],
glIndexType,
glVAO: 0,
};
WebGL2CmdFuncCreateInputAssember(this._device as WebGL2GFXDevice, this._gpuInputAssembler);
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._gpuInputAssembler = null;
this._status = GFXStatus.UNREADY;
}
public extractCmdDraw (cmd: WebGL2CmdDraw) {
cmd.drawInfo.vertexCount = this._vertexCount;
cmd.drawInfo.firstVertex = this._firstVertex;
cmd.drawInfo.indexCount = this._indexCount;
cmd.drawInfo.firstIndex = this._firstIndex;
cmd.drawInfo.vertexOffset = this._vertexOffset;
cmd.drawInfo.instanceCount = this._instanceCount;
cmd.drawInfo.firstInstance = this._firstInstance;
}
}
@@ -0,0 +1,30 @@
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXPipelineLayout, IGFXPipelineLayoutInfo } from '../pipeline-layout';
import { WebGL2GPUPipelineLayout } from './webgl2-gpu-objects';
export class WebGL2GFXPipelineLayout extends GFXPipelineLayout {
public get gpuPipelineLayout (): WebGL2GPUPipelineLayout {
return this._gpuPipelineLayout!;
}
private _gpuPipelineLayout: WebGL2GPUPipelineLayout | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXPipelineLayoutInfo): boolean {
this._layouts = info.layouts;
this._pushConstantsRanges = info.pushConstantsRanges || [];
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._status = GFXStatus.UNREADY;
}
}
+71
View File
@@ -0,0 +1,71 @@
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXPipelineState, IGFXPipelineStateInfo } from '../pipeline-state';
import { WebGL2GPUPipelineState } from './webgl2-gpu-objects';
import { WebGL2GFXPipelineLayout } from './webgl2-pipeline-layout';
import { WebGL2GFXRenderPass } from './webgl2-render-pass';
import { WebGL2GFXShader } from './webgl2-shader';
const WebGLPrimitives: GLenum[] = [
WebGLRenderingContext.POINTS,
WebGLRenderingContext.LINES,
WebGLRenderingContext.LINE_STRIP,
WebGLRenderingContext.LINE_LOOP,
WebGLRenderingContext.NONE,
WebGLRenderingContext.NONE,
WebGLRenderingContext.NONE,
WebGLRenderingContext.TRIANGLES,
WebGLRenderingContext.TRIANGLE_STRIP,
WebGLRenderingContext.TRIANGLE_FAN,
WebGLRenderingContext.NONE,
WebGLRenderingContext.NONE,
WebGLRenderingContext.NONE,
WebGLRenderingContext.NONE,
];
export class WebGL2GFXPipelineState extends GFXPipelineState {
public get gpuPipelineState (): WebGL2GPUPipelineState {
return this._gpuPipelineState!;
}
private _gpuPipelineState: WebGL2GPUPipelineState | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXPipelineStateInfo): 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._dynamicStates = info.dynamicStates || [];
this._layout = info.layout;
this._renderPass = info.renderPass;
this._gpuPipelineState = {
glPrimitive: WebGLPrimitives[info.primitive],
gpuShader: (info.shader as WebGL2GFXShader).gpuShader,
rs: info.rs,
dss: info.dss,
bs: info.bs,
dynamicStates: (info.dynamicStates !== undefined ? info.dynamicStates : []),
gpuLayout: (info.layout as WebGL2GFXPipelineLayout).gpuPipelineLayout,
gpuRenderPass: (info.renderPass as WebGL2GFXRenderPass).gpuRenderPass,
};
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._gpuPipelineState = null;
this._status = GFXStatus.UNREADY;
}
}
+39
View File
@@ -0,0 +1,39 @@
import { GFXCommandBuffer } from '../command-buffer';
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXQueue, IGFXQueueInfo } from '../queue';
import { WebGL2GFXCommandBuffer } from './webgl2-command-buffer';
import { WebGL2CmdFuncExecuteCmds } from './webgl2-commands';
import { WebGL2GFXDevice } from './webgl2-device';
export class WebGL2GFXQueue extends GFXQueue {
private _isAsync: boolean = false;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXQueueInfo): boolean {
this._type = info.type;
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._status = GFXStatus.UNREADY;
}
public submit (cmdBuffs: GFXCommandBuffer[], fence?) {
// TODO: Async
if (!this._isAsync) {
for (const cmdBuff of cmdBuffs) {
WebGL2CmdFuncExecuteCmds(this._device as WebGL2GFXDevice,
(cmdBuff as WebGL2GFXCommandBuffer).cmdPackage);
}
}
}
}
+38
View File
@@ -0,0 +1,38 @@
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXRenderPass, IGFXRenderPassInfo } from '../render-pass';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GPURenderPass } from './webgl2-gpu-objects';
export class WebGL2GFXRenderPass extends GFXRenderPass {
public get gpuRenderPass (): WebGL2GPURenderPass {
return this._gpuRenderPass!;
}
private _gpuRenderPass: WebGL2GPURenderPass | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXRenderPassInfo): boolean {
this._colorInfos = info.colorAttachments || [];
this._depthStencilInfo = info.depthStencilAttachment || null;
this._gpuRenderPass = {
colorAttachments: this._colorInfos,
depthStencilAttachment: this._depthStencilInfo,
};
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._gpuRenderPass = null;
this._status = GFXStatus.UNREADY;
}
}
+134
View File
@@ -0,0 +1,134 @@
import { GFXFilter, GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXSampler, GFXSamplerState, IGFXSamplerInfo } from '../sampler';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GPUSampler } from './webgl2-gpu-objects';
const WebGLWraps: GLenum[] = [
WebGLRenderingContext.REPEAT,
WebGLRenderingContext.MIRRORED_REPEAT,
WebGLRenderingContext.CLAMP_TO_EDGE,
WebGLRenderingContext.CLAMP_TO_EDGE,
];
export class WebGL2GFXSampler extends GFXSampler {
public get gpuSampler (): WebGL2GPUSampler {
return this._gpuSampler!;
}
private _gpuSampler: WebGL2GPUSampler | null = null;
constructor (device: GFXDevice) {
super(device);
this._state = new GFXSamplerState();
}
public initialize (info: IGFXSamplerInfo): boolean {
if (info.name !== undefined) {
this._state.name = info.name;
}
if (info.minFilter !== undefined) {
this._state.minFilter = info.minFilter;
}
if (info.magFilter !== undefined) {
this._state.magFilter = info.magFilter;
}
if (info.mipFilter !== undefined) {
this._state.mipFilter = info.mipFilter;
}
if (info.addressU !== undefined) {
this._state.addressU = info.addressU;
}
if (info.addressV !== undefined) {
this._state.addressV = info.addressV;
}
if (info.addressW !== undefined) {
this._state.addressW = info.addressW;
}
if (info.maxAnisotropy !== undefined) {
this._state.maxAnisotropy = info.maxAnisotropy;
}
if (info.cmpFunc !== undefined) {
this._state.cmpFunc = info.cmpFunc;
}
if (info.borderColor !== undefined) {
this._state.borderColor = info.borderColor;
}
if (info.minLOD !== undefined) {
this._state.minLOD = info.minLOD;
}
if (info.maxLOD !== undefined) {
this._state.maxLOD = info.maxLOD;
}
if (info.mipLODBias !== undefined) {
this._state.mipLODBias = info.mipLODBias;
}
let glMinFilter = WebGLRenderingContext.NONE;
let glMagFilter = WebGLRenderingContext.NONE;
const minFilter = (info.minFilter !== undefined ? info.minFilter : GFXFilter.LINEAR);
const magFilter = (info.magFilter !== undefined ? info.magFilter : GFXFilter.LINEAR);
const mipFilter = (info.mipFilter !== undefined ? info.mipFilter : GFXFilter.NONE);
if (minFilter === GFXFilter.LINEAR || minFilter === GFXFilter.ANISOTROPIC) {
if (mipFilter === GFXFilter.LINEAR || mipFilter === GFXFilter.ANISOTROPIC) {
glMinFilter = WebGLRenderingContext.LINEAR_MIPMAP_LINEAR;
} else if (mipFilter === GFXFilter.POINT) {
glMinFilter = WebGLRenderingContext.LINEAR_MIPMAP_NEAREST;
} else {
glMinFilter = WebGLRenderingContext.LINEAR;
}
} else {
if (mipFilter === GFXFilter.LINEAR || mipFilter === GFXFilter.ANISOTROPIC) {
glMinFilter = WebGLRenderingContext.NEAREST_MIPMAP_LINEAR;
} else if (mipFilter === GFXFilter.POINT) {
glMinFilter = WebGLRenderingContext.NEAREST_MIPMAP_NEAREST;
} else {
glMinFilter = WebGLRenderingContext.NEAREST;
}
}
if (magFilter === GFXFilter.LINEAR || magFilter === GFXFilter.ANISOTROPIC) {
glMagFilter = WebGLRenderingContext.LINEAR;
} else {
glMagFilter = WebGLRenderingContext.NEAREST;
}
const glWrapS = (info.addressU !== undefined ? WebGLWraps[info.addressU] : WebGLRenderingContext.CLAMP_TO_EDGE);
const glWrapT = (info.addressU !== undefined ? WebGLWraps[info.addressU] : WebGLRenderingContext.CLAMP_TO_EDGE);
const glWrapR = (info.addressU !== undefined ? WebGLWraps[info.addressU] : WebGLRenderingContext.CLAMP_TO_EDGE);
this._gpuSampler = {
glMinFilter,
glMagFilter,
glWrapS,
glWrapT,
glWrapR,
};
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._gpuSampler = null;
this._status = GFXStatus.UNREADY;
}
}
+70
View File
@@ -0,0 +1,70 @@
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXShader, IGFXShaderInfo } from '../shader';
import { WebGL2CmdFuncCreateShader, WebGL2CmdFuncDestroyShader } from './webgl2-commands';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GPUShader, WebGL2GPUShaderStage } from './webgl2-gpu-objects';
export class WebGL2GFXShader extends GFXShader {
public get gpuShader (): WebGL2GPUShader {
return this._gpuShader!;
}
private _gpuShader: WebGL2GPUShader | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXShaderInfo): boolean {
this._name = info.name;
this._stages = info.stages;
if (info.blocks !== undefined) {
this._blocks = info.blocks;
}
if (info.samplers !== undefined) {
this._samplers = info.samplers;
}
this._gpuShader = {
name: info.name ? info.name : '',
blocks: (info.blocks !== undefined ? info.blocks : []),
samplers: (info.samplers !== undefined ? info.samplers : []),
gpuStages: new Array<WebGL2GPUShaderStage>(info.stages.length),
glProgram: 0,
glInputs: [],
glUniforms: [],
glBlocks: [],
glSamplers: [],
};
for (let i = 0; i < info.stages.length; ++i) {
const stage = info.stages[i];
this._gpuShader.gpuStages[i] = {
type: stage.type,
source: stage.source,
macros: stage.macros ? stage.macros : [],
glShader: 0,
};
}
WebGL2CmdFuncCreateShader(this._device as WebGL2GFXDevice, this._gpuShader);
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
if (this._gpuShader) {
WebGL2CmdFuncDestroyShader(this._device as WebGL2GFXDevice, this._gpuShader);
this._gpuShader = null;
}
this._status = GFXStatus.UNREADY;
}
}
+49
View File
@@ -0,0 +1,49 @@
import { GFX_MAX_TEXTURE_UNITS, GFX_MAX_VERTEX_ATTRIBUTES, IGFXRect, IGFXViewport } from '../define';
import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../pipeline-state';
export interface IWebGL2TexUnit {
glTexture: WebGLTexture;
}
export class WebGL2StateCache {
public glArrayBuffer: WebGLBuffer = 0;
public glElementArrayBuffer: WebGLBuffer = 0;
public texUnit: number = 0;
public glTex2DUnits: IWebGL2TexUnit[];
public glTexCubeUnits: IWebGL2TexUnit[];
public glFramebuffer: WebGLFramebuffer = 0;
public viewport: IGFXViewport;
public scissorRect: IGFXRect;
public rs: GFXRasterizerState;
public dss: GFXDepthStencilState;
public bs: GFXBlendState;
public glProgram: WebGLProgram = 0;
public glEnabledAttribLocs: boolean[];
public glCurrentAttribLocs: boolean[];
constructor () {
this.glTex2DUnits = new Array<IWebGL2TexUnit>(GFX_MAX_TEXTURE_UNITS);
this.glTexCubeUnits = new Array<IWebGL2TexUnit>(GFX_MAX_TEXTURE_UNITS);
this.viewport = { left: 0.0, top: 0.0, width: 0.0, height: 0.0, minDepth: 0.0, maxDepth: 0.0 };
this.scissorRect = { x: 0.0, y: 0.0, width: 0.0, height: 0.0 };
this.rs = new GFXRasterizerState();
this.dss = new GFXDepthStencilState();
this.bs = new GFXBlendState();
this.glEnabledAttribLocs = new Array<boolean>(GFX_MAX_VERTEX_ATTRIBUTES);
this.glCurrentAttribLocs = new Array<boolean>(GFX_MAX_VERTEX_ATTRIBUTES);
for (let i = 0; i < GFX_MAX_TEXTURE_UNITS; ++i) {
this.glTex2DUnits[i] = {
glTexture: 0,
};
this.glTexCubeUnits[i] = {
glTexture: 0,
};
}
for (let i = 0; i < GFX_MAX_VERTEX_ATTRIBUTES; ++i) {
this.glEnabledAttribLocs[i] = false;
this.glCurrentAttribLocs[i] = false;
}
}
}
+61
View File
@@ -0,0 +1,61 @@
import { GFXStatus } from '../define';
import { GFXDevice } from '../device';
import { GFXTextureView, IGFXTextureViewInfo } from '../texture-view';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GPUTextureView } from './webgl2-gpu-objects';
import { WebGL2GFXTexture } from './webgl2-texture';
export class WebGL2GFXTextureView extends GFXTextureView {
public get gpuTextureView (): WebGL2GPUTextureView {
return this._gpuTextureView as WebGL2GPUTextureView;
}
private _gpuTextureView: WebGL2GPUTextureView | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXTextureViewInfo): boolean {
this._texture = info.texture;
this._type = info.type;
this._format = info.format;
this._format = info.format;
if (info.baseLevel !== undefined) {
this._baseLevel = info.baseLevel;
}
if (info.levelCount !== undefined) {
this._levelCount = info.levelCount;
}
if (info.baseLayer !== undefined) {
this._baseLayer = info.baseLayer;
}
if (info.layerCount !== undefined) {
this._layerCount = info.layerCount;
}
this._gpuTextureView = {
gpuTexture: (info.texture as WebGL2GFXTexture).gpuTexture,
type: info.type,
format: info.format,
baseLevel: info.baseLevel ? info.baseLevel : 0,
levelCount: info.levelCount ? info.levelCount : 1,
};
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
this._gpuTextureView = null;
this._texture = null;
this._status = GFXStatus.UNREADY;
}
}
+131
View File
@@ -0,0 +1,131 @@
import { GFXFormatSurfaceSize, GFXStatus, GFXTextureFlagBit, GFXTextureType, GFXTextureViewType } from '../define';
import { GFXDevice } from '../device';
import { GFXTexture, IGFXTextureInfo } from '../texture';
import { WebGL2CmdFuncCreateTexture, WebGL2CmdFuncDestroyTexture } from './webgl2-commands';
import { WebGL2GFXDevice } from './webgl2-device';
import { WebGL2GPUTexture } from './webgl2-gpu-objects';
export class WebGL2GFXTexture extends GFXTexture {
public get gpuTexture (): WebGL2GPUTexture {
return this._gpuTexture!;
}
private _gpuTexture: WebGL2GPUTexture | null = null;
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXTextureInfo): boolean {
this._type = info.type;
this._usage = info.usage;
this._format = info.format;
this._width = info.width;
this._height = info.height;
if (info.depth !== undefined) {
this._depth = info.depth;
}
if (info.arrayLayer !== undefined) {
this._arrayLayer = info.arrayLayer;
}
if (info.mipLevel !== undefined) {
this._mipLevel = info.mipLevel;
}
if (info.flags !== undefined) {
this._flags = info.flags;
}
this._size = GFXFormatSurfaceSize(this._format, this.width, this.height,
this.depth, this.mipLevel) * this._arrayLayer;
if (this._flags & GFXTextureFlagBit.BAKUP_BUFFER) {
this._buffer = new ArrayBuffer(this._size);
}
let viewType: GFXTextureViewType;
switch (info.type) {
case GFXTextureType.TEX1D: {
if (info.arrayLayer) {
viewType = info.arrayLayer <= 1 ? GFXTextureViewType.TV1D : GFXTextureViewType.TV1D_ARRAY;
} else {
viewType = GFXTextureViewType.TV1D;
}
break;
}
case GFXTextureType.TEX2D: {
let flags = GFXTextureFlagBit.NONE;
if (info.flags) {
flags = info.flags;
}
if (info.arrayLayer) {
if (info.arrayLayer <= 1) {
viewType = GFXTextureViewType.TV2D;
} else if (flags & GFXTextureFlagBit.CUBEMAP) {
viewType = GFXTextureViewType.CUBE;
} else {
viewType = GFXTextureViewType.TV2D_ARRAY;
}
} else {
viewType = GFXTextureViewType.TV2D;
}
break;
}
case GFXTextureType.TEX3D: {
viewType = GFXTextureViewType.TV3D;
break;
}
default: {
viewType = GFXTextureViewType.TV2D;
}
}
this._gpuTexture = {
type: info.type,
viewType,
format: info.format,
usage: info.usage,
width: info.width,
height: info.height,
depth: Math.max(info.depth || 1, 1),
arrayLayer: Math.max(info.arrayLayer || 1, 1),
mipLevel: Math.max(info.mipLevel || 1, 1),
flags: info.flags || GFXTextureFlagBit.NONE,
glTarget: 0,
glInternelFmt: 0,
glFormat: 0,
glType: 0,
glUsage: 0,
glTexture: 0,
glWrapS: 0,
glWrapT: 0,
glMinFilter: 0,
glMagFilter: 0,
};
WebGL2CmdFuncCreateTexture(this._device as WebGL2GFXDevice, this._gpuTexture);
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
if (this._gpuTexture) {
WebGL2CmdFuncDestroyTexture(this._device as WebGL2GFXDevice, this._gpuTexture);
this._gpuTexture = null;
}
this._status = GFXStatus.UNREADY;
}
}
+90
View File
@@ -0,0 +1,90 @@
import { GFXLoadOp, GFXStatus, GFXStoreOp, GFXTextureLayout } from '../define';
import { GFXDevice } from '../device';
import { GFXWindow, IGFXWindowInfo } from '../window';
export class WebGL2GFXWindow extends GFXWindow {
constructor (device: GFXDevice) {
super(device);
}
public initialize (info: IGFXWindowInfo): boolean {
if (info.title !== undefined) {
this._title = info.title;
}
if (info.left !== undefined) {
this._left = info.left;
}
if (info.top !== undefined) {
this._top = info.top;
}
this._width = info.width;
this._height = info.height;
this._colorFmt = info.colorFmt;
this._depthStencilFmt = info.depthStencilFmt;
this._renderPass = this._device.createRenderPass({
colorAttachments: [{
format: this._colorFmt,
loadOp: GFXLoadOp.CLEAR,
storeOp: GFXStoreOp.STORE,
sampleCount: 1,
beginLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL,
endLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL,
}],
depthStencilAttachment: {
format : this._depthStencilFmt,
depthLoadOp : GFXLoadOp.CLEAR,
depthStoreOp : GFXStoreOp.STORE,
stencilLoadOp : GFXLoadOp.CLEAR,
stencilStoreOp : GFXStoreOp.STORE,
sampleCount : 1,
beginLayout : GFXTextureLayout.DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
endLayout : GFXTextureLayout.DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
},
});
if (!this._renderPass) {
this.destroy();
return false;
}
this._framebuffer = this._device.createFramebuffer({
renderPass: this._renderPass,
// colorViews: [this._colorTexView],
// depthStencilView: this._depthStencilTexView,
isOffscreen: false,
});
if (!this._framebuffer) {
this.destroy();
return false;
}
this._status = GFXStatus.SUCCESS;
return true;
}
public destroy () {
if (this._framebuffer) {
this._framebuffer.destroy();
this._framebuffer = null;
}
if (this._renderPass) {
this._renderPass.destroy();
this._renderPass = null;
}
this._status = GFXStatus.UNREADY;
}
public resize (width: number, height: number) {
this._width = width;
this._height = height;
}
}