From 44628557718ea316552ab5fbda0ef8bb4563b0aa Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Thu, 28 Feb 2019 13:58:50 +0800 Subject: [PATCH] support sampler & mrt in webgl2 --- cocos/gfx/buffer.ts | 5 +- cocos/gfx/webgl/webgl-buffer.ts | 37 +++---- cocos/gfx/webgl/webgl-commands.ts | 40 +++++--- cocos/gfx/webgl/webgl-device.ts | 3 + cocos/gfx/webgl/webgl-sampler.ts | 4 +- cocos/gfx/webgl2/webgl2-buffer.ts | 32 ++---- cocos/gfx/webgl2/webgl2-commands.ts | 134 +++++++++++++++++-------- cocos/gfx/webgl2/webgl2-gpu-objects.ts | 24 +++-- cocos/gfx/webgl2/webgl2-sampler.ts | 72 +++++-------- cocos/gfx/webgl2/webgl2-state-cache.ts | 3 + cocos/pipeline/define.ts | 3 +- playground/simple.html | 16 ++- 12 files changed, 203 insertions(+), 170 deletions(-) diff --git a/cocos/gfx/buffer.ts b/cocos/gfx/buffer.ts index 13fcec273b..1e83fa7bb4 100644 --- a/cocos/gfx/buffer.ts +++ b/cocos/gfx/buffer.ts @@ -62,9 +62,11 @@ export abstract class GFXBuffer extends GFXObject { return this._count; } + /* public get buffer (): GFXBufferSource | null { return this._buffer; } + */ protected _device: GFXDevice; protected _usage: GFXBufferUsage = GFXBufferUsageBit.NONE; @@ -72,8 +74,7 @@ export abstract class GFXBuffer extends GFXObject { protected _size: number = 0; protected _stride: number = 1; protected _count: number = 0; - protected _buffer: GFXBufferSource | null = null; - protected _uniforms: GFXBufferSource | null = null; + // protected _buffer: GFXBufferSource | null = null; constructor (device: GFXDevice) { super(GFXObjectType.BUFFER); diff --git a/cocos/gfx/webgl/webgl-buffer.ts b/cocos/gfx/webgl/webgl-buffer.ts index f734340e2d..8a1a589769 100644 --- a/cocos/gfx/webgl/webgl-buffer.ts +++ b/cocos/gfx/webgl/webgl-buffer.ts @@ -17,6 +17,8 @@ export class WebGLGFXBuffer extends GFXBuffer { } private _gpuBuffer: WebGLGPUBuffer | null = null; + private _uniformBuffer: ArrayBuffer | null = null; + private _indirectBuffer: IGFXIndirectBuffer | null = null; constructor (device: GFXDevice) { super(device); @@ -30,12 +32,10 @@ export class WebGLGFXBuffer extends GFXBuffer { this._stride = Math.max(info.stride || this._size, 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); - } + if (this._usage & GFXBufferUsageBit.INDIRECT) { + this._indirectBuffer = { drawInfos: [] }; + } else if (this._usage & GFXBufferUsageBit.UNIFORM) { + this._uniformBuffer = new ArrayBuffer(this._size); } this._gpuBuffer = { @@ -50,12 +50,10 @@ export class WebGLGFXBuffer extends GFXBuffer { 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; - } + if (info.usage & GFXBufferUsageBit.INDIRECT) { + this._gpuBuffer.indirects = this._indirectBuffer!.drawInfos; + } else if (this._usage & GFXBufferUsageBit.UNIFORM) { + this._gpuBuffer.buffer = this._uniformBuffer; } WebGLCmdFuncCreateBuffer(this._device as WebGLGFXDevice, this._gpuBuffer); @@ -71,7 +69,6 @@ export class WebGLGFXBuffer extends GFXBuffer { this._gpuBuffer = null; } - this._buffer = null; this._status = GFXStatus.UNREADY; } @@ -79,19 +76,15 @@ export class WebGLGFXBuffer extends GFXBuffer { 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._uniformBuffer) { + this._uniformBuffer = 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; - } + if (this._uniformBuffer) { + this._gpuBuffer.buffer = this._uniformBuffer; } + this._gpuBuffer.size = this._size; WebGLCmdFuncResizeBuffer(this._device as WebGLGFXDevice, this._gpuBuffer); } diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 256cdfdf14..297ba38e31 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -38,6 +38,7 @@ import { IWebGLGPUUniform, WebGLAttrib, WebGLGPUBindingLayout, WebGLGPUBuffer, import { IWebGLTexUnit } from './webgl-state-cache'; const _uniformValues = new Array(1024 * 4); +const _f32v4 = new Float32Array(4); // tslint:disable: max-line-length @@ -830,6 +831,7 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb if (!gpuFramebuffer.isOffscreen) { const gl = device.gl; + const attachments: GLenum[] = []; const glFramebuffer = gl.createFramebuffer(); if (glFramebuffer) { @@ -850,6 +852,8 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb gpuColorView.gpuTexture.glTarget, gpuColorView.gpuTexture.glTexture, gpuColorView.baseLevel); + + attachments.push(WebGL2RenderingContext.COLOR_ATTACHMENT0 + i); } } @@ -866,6 +870,10 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb gpuFramebuffer.gpuDepthStencilView.baseLevel); } + if (device.WEBGL_draw_buffers) { + device.WEBGL_draw_buffers.drawBuffersWEBGL(attachments); + } + /* const status = gl.checkFramebufferStatus(WebGLRenderingContext.FRAMEBUFFER); if (status !== WebGLRenderingContext.FRAMEBUFFER_COMPLETE) { @@ -1219,14 +1227,11 @@ export function WebGLCmdFuncCreateInputAssember (device: WebGLGFXDevice, gpuInpu export function WebGLCmdFuncDestroyInputAssembler (device: WebGLGFXDevice, gpuInputAssembler: WebGLGPUInputAssembler) { } -const cmdIds = [0, 0, 0, 0, 0, 0]; // 6 -const invalidateAttachments: GLenum[] = [0, 0, 0, 0]; +const cmdIds = new Array(WebGLCmd.COUNT); export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: WebGLCmdPackage) { const gl = device.gl; - for (let initID = 0; initID < cmdIds.length; initID++) { - cmdIds[initID] = 0; - } + cmdIds.fill(0); let gpuPipelineState: WebGLGPUPipelineState | null = null; let gpuShader: WebGLGPUShader | null = null; @@ -1277,14 +1282,15 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web } const curGPURenderPass = cmd0.gpuFramebuffer.gpuRenderPass; + // const invalidateAttachments: GLenum[] = []; + let clearCount = cmd0.clearColors.length; - for (let initAtt = 0; initAtt < invalidateAttachments.length; initAtt++) { - invalidateAttachments[initAtt] = 0; + if (!device.WEBGL_draw_buffers) { + clearCount = 1; } - let numInvalidAttach = 0; - if (curGPURenderPass.colorAttachments.length > 0 && cmd0.clearColors.length) { - const colorAttachment = curGPURenderPass.colorAttachments[0]; + for (let j = 0; j < clearCount; ++j) { + const colorAttachment = curGPURenderPass.colorAttachments[j]; if (colorAttachment.format !== GFXFormat.UNKNOWN) { switch (colorAttachment.loadOp) { @@ -1295,15 +1301,17 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web gl.colorMask(true, true, true, true); } - const clearColor = cmd0.clearColors[0]; - gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); - clears |= WebGLRenderingContext.COLOR_BUFFER_BIT; + if (!cmd0.gpuFramebuffer.isOffscreen) { + const clearColor = cmd0.clearColors[0]; + gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); + clears |= WebGL2RenderingContext.COLOR_BUFFER_BIT; + } break; } case GFXLoadOp.DISCARD: { // invalidate the framebuffer - invalidateAttachments[numInvalidAttach++] = WebGLRenderingContext.COLOR_ATTACHMENT0; + // invalidateAttachments.push(WebGLRenderingContext.COLOR_ATTACHMENT0 + j); break; } default: @@ -1330,7 +1338,7 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web } case GFXLoadOp.DISCARD: { // invalidate the framebuffer - invalidateAttachments[numInvalidAttach++] = WebGLRenderingContext.DEPTH_ATTACHMENT; + // invalidateAttachments.push(WebGLRenderingContext.DEPTH_ATTACHMENT); break; } default: @@ -1356,7 +1364,7 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web } case GFXLoadOp.DISCARD: { // invalidate the framebuffer - invalidateAttachments[numInvalidAttach++] = WebGLRenderingContext.STENCIL_ATTACHMENT; + // invalidateAttachments.push(WebGLRenderingContext.STENCIL_ATTACHMENT); break; } default: diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index 88e6be1d74..c6f3cda52d 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -236,6 +236,9 @@ export class WebGLGFXDevice extends GFXDevice { this._width = this._canvas.width; this._height = this._canvas.height; + this._nativeWidth = Math.max(info.nativeWidth || this._width, 0); + this._nativeHeight = Math.max(info.nativeHeight || this._width, 0); + this._colorFmt = GFXFormat.RGBA8; if (this._depthBits === 24) { diff --git a/cocos/gfx/webgl/webgl-sampler.ts b/cocos/gfx/webgl/webgl-sampler.ts index 2701965a76..23b03ec2b5 100644 --- a/cocos/gfx/webgl/webgl-sampler.ts +++ b/cocos/gfx/webgl/webgl-sampler.ts @@ -110,8 +110,8 @@ export class WebGLGFXSampler extends GFXSampler { } 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); + const glWrapT = (info.addressV !== undefined ? WebGLWraps[info.addressV] : WebGLRenderingContext.CLAMP_TO_EDGE); + const glWrapR = (info.addressW !== undefined ? WebGLWraps[info.addressW] : WebGLRenderingContext.CLAMP_TO_EDGE); this._gpuSampler = { glMinFilter, diff --git a/cocos/gfx/webgl2/webgl2-buffer.ts b/cocos/gfx/webgl2/webgl2-buffer.ts index 42527c0a3d..c96c93c334 100644 --- a/cocos/gfx/webgl2/webgl2-buffer.ts +++ b/cocos/gfx/webgl2/webgl2-buffer.ts @@ -17,6 +17,7 @@ export class WebGL2GFXBuffer extends GFXBuffer { } private _gpuBuffer: WebGL2GPUBuffer | null = null; + private _indirectBuffer: IGFXIndirectBuffer | null = null; constructor (device: GFXDevice) { super(device); @@ -30,12 +31,8 @@ export class WebGL2GFXBuffer extends GFXBuffer { this._stride = Math.max(info.stride || this._size, 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); - } + if (this._usage & GFXBufferUsageBit.INDIRECT) { + this._indirectBuffer = { drawInfos: [] }; } this._gpuBuffer = { @@ -50,12 +47,8 @@ export class WebGL2GFXBuffer extends GFXBuffer { 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; - } + if (info.usage & GFXBufferUsageBit.INDIRECT) { + this._gpuBuffer.indirects = this._indirectBuffer!.drawInfos; } WebGL2CmdFuncCreateBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer); @@ -71,7 +64,6 @@ export class WebGL2GFXBuffer extends GFXBuffer { this._gpuBuffer = null; } - this._buffer = null; this._status = GFXStatus.UNREADY; } @@ -79,20 +71,8 @@ export class WebGL2GFXBuffer extends GFXBuffer { 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; - } - } - + this._gpuBuffer.size = this._size; WebGL2CmdFuncResizeBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer); } } diff --git a/cocos/gfx/webgl2/webgl2-commands.ts b/cocos/gfx/webgl2/webgl2-commands.ts index fc92654e74..d4019d31c4 100644 --- a/cocos/gfx/webgl2/webgl2-commands.ts +++ b/cocos/gfx/webgl2/webgl2-commands.ts @@ -7,6 +7,7 @@ import { GFXColorMask, GFXCullMode, GFXDynamicState, + GFXFilter, GFXFormat, GFXFormatInfos, GFXFormatSize, @@ -42,6 +43,7 @@ import { WebGL2GPUInput, WebGL2GPUInputAssembler, WebGL2GPUPipelineState, + WebGL2GPUSampler, WebGL2GPUShader, WebGL2GPUTexture, WebGL2GPUUniformBlock, @@ -49,7 +51,15 @@ import { } from './webgl2-gpu-objects'; import { IWebGL2TexUnit } from './webgl2-state-cache'; +const WebGLWraps: GLenum[] = [ + WebGL2RenderingContext.REPEAT, + WebGL2RenderingContext.MIRRORED_REPEAT, + WebGL2RenderingContext.CLAMP_TO_EDGE, + WebGL2RenderingContext.CLAMP_TO_EDGE, +]; + const _uniformValues = new Array(1024 * 4); +const _f32v4 = new Float32Array(4); // tslint:disable: max-line-length @@ -991,11 +1001,63 @@ export function WebGL2CmdFuncDestroyTexture (device: WebGL2GFXDevice, gpuTexture } } +export function WebGL2CmdFuncCreateSampler (device: WebGL2GFXDevice, gpuSampler: WebGL2GPUSampler) { + + const gl = device.gl; + const glSampler = gl.createSampler(); + if (glSampler) { + if (gpuSampler.minFilter === GFXFilter.LINEAR || gpuSampler.minFilter === GFXFilter.ANISOTROPIC) { + if (gpuSampler.mipFilter === GFXFilter.LINEAR || gpuSampler.mipFilter === GFXFilter.ANISOTROPIC) { + gpuSampler.glMinFilter = WebGLRenderingContext.LINEAR_MIPMAP_LINEAR; + } else if (gpuSampler.mipFilter === GFXFilter.POINT) { + gpuSampler.glMinFilter = WebGLRenderingContext.LINEAR_MIPMAP_NEAREST; + } else { + gpuSampler.glMinFilter = WebGLRenderingContext.LINEAR; + } + } else { + if (gpuSampler.mipFilter === GFXFilter.LINEAR || gpuSampler.mipFilter === GFXFilter.ANISOTROPIC) { + gpuSampler.glMinFilter = WebGLRenderingContext.NEAREST_MIPMAP_LINEAR; + } else if (gpuSampler.mipFilter === GFXFilter.POINT) { + gpuSampler.glMinFilter = WebGLRenderingContext.NEAREST_MIPMAP_NEAREST; + } else { + gpuSampler.glMinFilter = WebGLRenderingContext.NEAREST; + } + } + + if (gpuSampler.magFilter === GFXFilter.LINEAR || gpuSampler.magFilter === GFXFilter.ANISOTROPIC) { + gpuSampler.glMagFilter = WebGLRenderingContext.LINEAR; + } else { + gpuSampler.glMagFilter = WebGLRenderingContext.NEAREST; + } + + gpuSampler.glWrapS = WebGLWraps[gpuSampler.addressU]; + gpuSampler.glWrapT = WebGLWraps[gpuSampler.addressV]; + gpuSampler.glWrapR = WebGLWraps[gpuSampler.addressW]; + + gpuSampler.glSampler = glSampler; + gl.samplerParameteri(glSampler, gl.TEXTURE_MIN_FILTER, gpuSampler.glMinFilter); + gl.samplerParameteri(glSampler, gl.TEXTURE_MAG_FILTER, gpuSampler.glMagFilter); + gl.samplerParameteri(glSampler, gl.TEXTURE_WRAP_S, gpuSampler.glWrapS); + gl.samplerParameteri(glSampler, gl.TEXTURE_WRAP_T, gpuSampler.glWrapT); + gl.samplerParameteri(glSampler, gl.TEXTURE_WRAP_R, gpuSampler.glWrapR); + gl.samplerParameteri(glSampler, gl.TEXTURE_MIN_LOD, gpuSampler.minLOD); + gl.samplerParameteri(glSampler, gl.TEXTURE_MAX_LOD, gpuSampler.maxLOD); + } +} + +export function WebGL2CmdFuncDestroySampler (device: WebGL2GFXDevice, gpuSampler: WebGL2GPUSampler) { + if (gpuSampler.glSampler) { + device.gl.deleteSampler(gpuSampler.glSampler); + gpuSampler.glSampler = 0; + } +} + export function WebGL2CmdFuncCreateFramebuffer (device: WebGL2GFXDevice, gpuFramebuffer: WebGL2GPUFramebuffer) { if (!gpuFramebuffer.isOffscreen) { const gl = device.gl; + const attachments: GLenum[] = []; const glFramebuffer = gl.createFramebuffer(); if (glFramebuffer) { @@ -1016,6 +1078,8 @@ export function WebGL2CmdFuncCreateFramebuffer (device: WebGL2GFXDevice, gpuFram gpuColorView.gpuTexture.glTarget, gpuColorView.gpuTexture.glTexture, gpuColorView.baseLevel); + + attachments.push(WebGL2RenderingContext.COLOR_ATTACHMENT0 + i); } } @@ -1032,6 +1096,8 @@ export function WebGL2CmdFuncCreateFramebuffer (device: WebGL2GFXDevice, gpuFram gpuFramebuffer.gpuDepthStencilView.baseLevel); } + gl.drawBuffers(attachments); + /* const status = gl.checkFramebufferStatus(WebGL2RenderingContext.FRAMEBUFFER); if (status !== WebGL2RenderingContext.FRAMEBUFFER_COMPLETE) { @@ -1391,14 +1457,11 @@ export function WebGL2CmdFuncCreateInputAssember (device: WebGL2GFXDevice, gpuIn export function WebGL2CmdFuncDestroyInputAssembler (device: WebGL2GFXDevice, gpuInputAssembler: WebGL2GPUInputAssembler) { } -const cmdIds = [0, 0, 0, 0, 0, 0]; // 6 -const invalidateAttachments: GLenum[] = [0, 0, 0, 0]; +const cmdIds = new Array(WebGL2Cmd.COUNT); export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: WebGL2CmdPackage) { const gl = device.gl; - for (let initID = 0; initID < cmdIds.length; initID++) { - cmdIds[initID] = 0; - } + cmdIds.fill(0); let gpuPipelineState: WebGL2GPUPipelineState | null = null; let gpuShader: WebGL2GPUShader | null = null; @@ -1449,14 +1512,10 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W } const curGPURenderPass = cmd0.gpuFramebuffer.gpuRenderPass; + const invalidateAttachments: GLenum[] = []; - for (let initAtt = 0; initAtt < invalidateAttachments.length; initAtt++) { - invalidateAttachments[initAtt] = 0; - } - let numInvalidAttach = 0; - - if (curGPURenderPass.colorAttachments.length > 0 && cmd0.clearColors.length) { - const colorAttachment = curGPURenderPass.colorAttachments[0]; + for (let j = 0; j < cmd0.clearColors.length; ++j) { + const colorAttachment = curGPURenderPass.colorAttachments[j]; if (colorAttachment.format !== GFXFormat.UNKNOWN) { switch (colorAttachment.loadOp) { @@ -1467,15 +1526,23 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W gl.colorMask(true, true, true, true); } - const clearColor = cmd0.clearColors[0]; - gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); - clears |= WebGL2RenderingContext.COLOR_BUFFER_BIT; + if (!cmd0.gpuFramebuffer.isOffscreen) { + const clearColor = cmd0.clearColors[0]; + gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); + clears |= WebGL2RenderingContext.COLOR_BUFFER_BIT; + } else { + _f32v4[0] = cmd0.clearColors[j].r; + _f32v4[1] = cmd0.clearColors[j].g; + _f32v4[2] = cmd0.clearColors[j].b; + _f32v4[3] = cmd0.clearColors[j].a; + gl.clearBufferfv(gl.COLOR, j, _f32v4); + } break; } case GFXLoadOp.DISCARD: { // invalidate the framebuffer - invalidateAttachments[numInvalidAttach++] = WebGL2RenderingContext.COLOR_ATTACHMENT0; + invalidateAttachments.push(WebGL2RenderingContext.COLOR_ATTACHMENT0 + j); break; } default: @@ -1502,7 +1569,7 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W } case GFXLoadOp.DISCARD: { // invalidate the framebuffer - invalidateAttachments[numInvalidAttach++] = WebGL2RenderingContext.DEPTH_ATTACHMENT; + invalidateAttachments.push(WebGL2RenderingContext.DEPTH_ATTACHMENT); break; } default: @@ -1528,7 +1595,7 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W } case GFXLoadOp.DISCARD: { // invalidate the framebuffer - invalidateAttachments[numInvalidAttach++] = WebGL2RenderingContext.STENCIL_ATTACHMENT; + invalidateAttachments.push(WebGL2RenderingContext.STENCIL_ATTACHMENT); break; } default: @@ -1537,11 +1604,9 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W } } // if (curGPURenderPass.depthStencilAttachment) - /* - if (numInvalidAttach) { - gl.invalidateFramebuffer(WebGL2RenderingContext.FRAMEBUFFER, numInvalidAttach, invalidateAttachments); + if (invalidateAttachments.length) { + gl.invalidateFramebuffer(WebGL2RenderingContext.FRAMEBUFFER, invalidateAttachments); } - */ if (clears) { gl.clear(clears); @@ -1899,28 +1964,9 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W } } - if (glTexUnit) { - const gpuSampler = gpuBinding.gpuSampler; - - if (gpuTexture.glWrapS !== gpuSampler.glWrapS) { - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_S, gpuSampler.glWrapS); - gpuTexture.glWrapS = gpuSampler.glWrapS; - } - - if (gpuTexture.glWrapT !== gpuSampler.glWrapT) { - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_T, gpuSampler.glWrapT); - gpuTexture.glWrapT = gpuSampler.glWrapT; - } - - if (gpuTexture.glMinFilter !== gpuSampler.glMinFilter) { - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MIN_FILTER, gpuSampler.glMinFilter); - gpuTexture.glMinFilter = gpuSampler.glMinFilter; - } - - if (gpuTexture.glMagFilter !== gpuSampler.glMagFilter) { - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MAG_FILTER, gpuSampler.glMagFilter); - gpuTexture.glMagFilter = gpuSampler.glMagFilter; - } + if (device.stateCache.glSamplerUnits[texUnit] !== gpuBinding.gpuSampler.glSampler) { + gl.bindSampler(texUnit, gpuBinding.gpuSampler.glSampler); + device.stateCache.glSamplerUnits[texUnit] = gpuBinding.gpuSampler.glSampler; } } else { console.error('Not found texture view on binding unit ' + gpuBinding.binding); diff --git a/cocos/gfx/webgl2/webgl2-gpu-objects.ts b/cocos/gfx/webgl2/webgl2-gpu-objects.ts index a62038bb78..c68c44b520 100644 --- a/cocos/gfx/webgl2/webgl2-gpu-objects.ts +++ b/cocos/gfx/webgl2/webgl2-gpu-objects.ts @@ -15,6 +15,8 @@ import { GFXTextureUsageBit, GFXTextureViewType, GFXType, + GFXFilter, + GFXAddress, } from '../define'; import { IGFXInputAttribute } from '../input-assembler'; import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../pipeline-state'; @@ -101,11 +103,21 @@ export class WebGL2GPUFramebuffer { } 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; + public glSampler: WebGLSampler = 0; + public minFilter: GFXFilter = GFXFilter.NONE; + public magFilter: GFXFilter = GFXFilter.NONE; + public mipFilter: GFXFilter = GFXFilter.NONE; + public addressU: GFXAddress = GFXAddress.CLAMP; + public addressV: GFXAddress = GFXAddress.CLAMP; + public addressW: GFXAddress = GFXAddress.CLAMP; + public minLOD: number = 0; + public maxLOD: number = 1000; + + public glMinFilter: GLenum = WebGL2RenderingContext.NONE; + public glMagFilter: GLenum = WebGL2RenderingContext.NONE; + public glWrapS: GLenum = WebGL2RenderingContext.NONE; + public glWrapT: GLenum = WebGL2RenderingContext.NONE; + public glWrapR: GLenum = WebGL2RenderingContext.NONE; } export class WebGL2GPUInput { @@ -183,7 +195,7 @@ export class WebGL2GPUPipelineLayout { export class WebGL2GPUPipelineState { - public glPrimitive: GLenum = WebGLRenderingContext.TRIANGLES; + public glPrimitive: GLenum = WebGL2RenderingContext.TRIANGLES; public gpuShader: WebGL2GPUShader | null = null; public rs: GFXRasterizerState = new GFXRasterizerState(); public dss: GFXDepthStencilState = new GFXDepthStencilState(); diff --git a/cocos/gfx/webgl2/webgl2-sampler.ts b/cocos/gfx/webgl2/webgl2-sampler.ts index 99b2cc0394..e8c340a640 100644 --- a/cocos/gfx/webgl2/webgl2-sampler.ts +++ b/cocos/gfx/webgl2/webgl2-sampler.ts @@ -1,16 +1,10 @@ -import { GFXFilter, GFXStatus } from '../define'; +import { GFXStatus } from '../define'; import { GFXDevice } from '../device'; import { GFXSampler, GFXSamplerState, IGFXSamplerInfo } from '../sampler'; +import { WebGL2CmdFuncCreateSampler, WebGL2CmdFuncDestroySampler } from './webgl2-commands'; 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 { @@ -79,56 +73,36 @@ export class WebGL2GFXSampler extends GFXSampler { 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, + glSampler: 0, + minFilter: this._state.minFilter, + magFilter: this._state.magFilter, + mipFilter: this._state.mipFilter, + addressU: this._state.addressU, + addressV: this._state.addressV, + addressW: this._state.addressW, + minLOD: this._state.minLOD, + maxLOD: this._state.maxLOD, + + glMinFilter: WebGL2RenderingContext.NONE, + glMagFilter: WebGL2RenderingContext.NONE, + glWrapS: WebGL2RenderingContext.NONE, + glWrapT: WebGL2RenderingContext.NONE, + glWrapR: WebGL2RenderingContext.NONE, }; + WebGL2CmdFuncCreateSampler(this._device as WebGL2GFXDevice, this._gpuSampler); + this._status = GFXStatus.SUCCESS; return true; } public destroy () { - this._gpuSampler = null; + if (this._gpuSampler) { + WebGL2CmdFuncDestroySampler(this._device as WebGL2GFXDevice, this._gpuSampler); + this._gpuSampler = null; + } this._status = GFXStatus.UNREADY; } } diff --git a/cocos/gfx/webgl2/webgl2-state-cache.ts b/cocos/gfx/webgl2/webgl2-state-cache.ts index 51f5235640..b8e2a4d69e 100644 --- a/cocos/gfx/webgl2/webgl2-state-cache.ts +++ b/cocos/gfx/webgl2/webgl2-state-cache.ts @@ -13,6 +13,7 @@ export class WebGL2StateCache { public texUnit: number = 0; public glTex2DUnits: IWebGL2TexUnit[]; public glTexCubeUnits: IWebGL2TexUnit[]; + public glSamplerUnits: WebGLSampler[]; public glFramebuffer: WebGLFramebuffer = 0; public viewport: IGFXViewport; public scissorRect: IGFXRect; @@ -29,6 +30,7 @@ export class WebGL2StateCache { this.glTex2DUnits = new Array(GFX_MAX_TEXTURE_UNITS); this.glTexCubeUnits = new Array(GFX_MAX_TEXTURE_UNITS); + this.glSamplerUnits = new Array(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(); @@ -44,6 +46,7 @@ export class WebGL2StateCache { this.glTexCubeUnits[i] = { glTexture: 0, }; + this.glSamplerUnits[i] = 0; } for (let i = 0; i < GFX_MAX_VERTEX_ATTRIBUTES; ++i) { diff --git a/cocos/pipeline/define.ts b/cocos/pipeline/define.ts index a47650e9a2..2ad2c5a8d4 100644 --- a/cocos/pipeline/define.ts +++ b/cocos/pipeline/define.ts @@ -1,6 +1,5 @@ import { GFXBuffer } from '../gfx/buffer'; -import { GFXBindingType } from '../gfx/define'; -import { GFXType } from '../gfx/define'; +import { GFXBindingType, GFXType } from '../gfx/define'; import { GFXSampler } from '../gfx/sampler'; import { GFXUniformBlock, GFXUniformSampler } from '../gfx/shader'; import { GFXTextureView } from '../gfx/texture-view'; diff --git a/playground/simple.html b/playground/simple.html index 0a21a697e4..7c0f4b34ac 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -66,7 +66,7 @@ cameraNode.addComponent(cc.CameraComponent); cameraNode.addComponent(FirstPersonCamera); - const skybox = cameraNode.addComponent(cc.SkyboxComponent); + // const skybox = cameraNode.addComponent(cc.SkyboxComponent); // const textureCube = new cc.TextureCube(); // textureCube.image = { // right: new cc.ImageAsset(imgs[1]), @@ -123,6 +123,20 @@ modelCom.mesh = cc.utils.createMesh(shape); } + /* + const material2 = new cc.Material(); + material2.initialize({ effectName: 'builtin-effect-pbr' }); + const modelNode2 = new cc.Node('test2'); + modelNode2.parent = scene; + const modelCom2 = modelNode2.addComponent('cc.ModelComponent'); + modelCom2.material = material2; + const shape2 = cc.primitives.quad(); + shape2.colors = Array(shape2.uvs.length / 2).fill(0) + .map((t) => [Math.random(), Math.random(), Math.random(), 0.8]) + .reduce((c, a) => a.concat(c), []); + modelCom2.mesh = cc.utils.createMesh(shape2); + */ + let buttonDown = false, pos = cc.v2(); canv.addEventListener('mousedown', (e) => buttonDown = (!e.button) ? true : buttonDown); canv.addEventListener('mouseup', (e) => buttonDown = (!e.button) ? false : buttonDown);