diff --git a/cocos/gfx/define.ts b/cocos/gfx/define.ts index cd7386c753..68de6cba03 100644 --- a/cocos/gfx/define.ts +++ b/cocos/gfx/define.ts @@ -18,7 +18,7 @@ export enum WebGLEXT { COMPRESSED_RGB_ETC1_WEBGL = 0x8D64, } -export const GFX_MAX_VERTEX_ATTRIBUTES: number = 8; +export const GFX_MAX_VERTEX_ATTRIBUTES: number = 16; export const GFX_MAX_TEXTURE_UNITS: number = 16; export const GFX_MAX_ATTACHMENTS: number = 4; @@ -695,7 +695,6 @@ export function GFXGetTypeSize(type: GFXType): number { } } -<<<<<<< HEAD:cocos/gfx/gfx-define.ts export function GFXGetAttributeBinding(name: string) : number { if (name === GFX_ATTRIBUTE_POSITION) { return 6; @@ -721,18 +720,3 @@ export function GFXGetAttributeBinding(name: string) : number { return -1; } } -======= -const GFXBuiltinAttributeBindings = { - [GFX_ATTRIBUTE_POSITION]: 20, - [GFX_ATTRIBUTE_COLOR]: 21, - [GFX_ATTRIBUTE_COLOR1]: 22, - [GFX_ATTRIBUTE_NORMAL]: 23, - [GFX_ATTRIBUTE_TANGENT]: 24, - [GFX_ATTRIBUTE_BINORMAL]: 25, - [GFX_ATTRIBUTE_TEXCOORD]: 26, - [GFX_ATTRIBUTE_TEXCOORD1]: 27, - [GFX_ATTRIBUTE_JOINTS]: 28, - [GFX_ATTRIBUTE_WEIGHTS]: 29, -}; -export { GFXBuiltinAttributeBindings }; ->>>>>>> f39457540e35b3cb5fe6bbf7586a0df2d602a94c:cocos/gfx/define.ts diff --git a/cocos/gfx/webgl/webgl-buffer.ts b/cocos/gfx/webgl/webgl-buffer.ts index 86dff88ccf..c79dcf7ebd 100644 --- a/cocos/gfx/webgl/webgl-buffer.ts +++ b/cocos/gfx/webgl/webgl-buffer.ts @@ -15,7 +15,11 @@ export class WebGLGFXBuffer extends GFXBuffer { this._usage = info.usage; this._memUsage = info.memUsage; this._size = info.size; - this._stride = info.stride? info.stride : 1; + + if(info.stride) { + this._stride = info.stride; + } + this._stride = Math.max(this._stride, 1); if (this._memUsage & GFXMemoryUsageBit.HOST) { this._buffer = new ArrayBuffer(this._size); diff --git a/cocos/gfx/webgl/webgl-command-allocator.ts b/cocos/gfx/webgl/webgl-command-allocator.ts index 8d54f06868..2a7b30d167 100644 --- a/cocos/gfx/webgl/webgl-command-allocator.ts +++ b/cocos/gfx/webgl/webgl-command-allocator.ts @@ -4,8 +4,12 @@ import { WebGLCmdUpdateBuffer, WebGLCmdBeginRenderPass, WebGLCmdBindInputAssembl export class WebGLGFXCommandPool { - constructor(count: number, increase : number) { + constructor(clazz: new() => T, count: number, increase : number) { this._frees = new Array(count); + for(let i = 0; i < count; ++i) { + this._frees[i] = new clazz(); + } + this._freeIdx = count - 1; this._increase = increase; } @@ -55,13 +59,13 @@ export class WebGLGFXCommandAllocator extends GFXCommandAllocator { constructor(device : GFXDevice) { super(device); - this.beginRenderPassCmdPool = new WebGLGFXCommandPool(64, 32); - this.bindPipelineStateCmdPool = new WebGLGFXCommandPool(64, 32); - this.bindBindingLayoutCmdPool = new WebGLGFXCommandPool(64, 32); - this.bindInputAssemblerCmdPool = new WebGLGFXCommandPool(64, 32); - this.drawCmdPool = new WebGLGFXCommandPool(64, 32); - this.updateBufferCmdPool = new WebGLGFXCommandPool(64, 32); - this.copyBufferToTextureCmdPool = new WebGLGFXCommandPool(64, 32); + this.beginRenderPassCmdPool = new WebGLGFXCommandPool(WebGLCmdBeginRenderPass, 64, 32); + this.bindPipelineStateCmdPool = new WebGLGFXCommandPool(WebGLCmdBindPipelineState, 64, 32); + this.bindBindingLayoutCmdPool = new WebGLGFXCommandPool(WebGLCmdBindBindingLayout, 64, 32); + this.bindInputAssemblerCmdPool = new WebGLGFXCommandPool(WebGLCmdBindInputAssembler, 64, 32); + this.drawCmdPool = new WebGLGFXCommandPool(WebGLCmdDraw, 64, 32); + this.updateBufferCmdPool = new WebGLGFXCommandPool(WebGLCmdUpdateBuffer, 64, 32); + this.copyBufferToTextureCmdPool = new WebGLGFXCommandPool(WebGLCmdCopyBufferToTexture, 64, 32); } public initialize(info: GFXCommandAllocatorInfo) : boolean { diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 46b4ec0946..897437d9bd 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -1,6 +1,6 @@ import { WebGLGPUBuffer, WebGLGPUTexture, WebGLGPUInputAssembler, WebGLGPUFramebuffer, WebGLGPUShader, WebGLGPUInput, WebGLAttrib, WebGLGPUPipelineState, WebGLGPUUniformBlock, WebGLGPUBindingLayout, WebGLGPUUniform, WebGLGPUUniformSampler } from "./webgl-gpu-objects"; import { WebGLGFXDevice } from "./webgl-device"; -import { GFXFormatInfos, GFXFormat, WebGLEXT, GFXType, GFX_MAX_VERTEX_ATTRIBUTES, GFXFormatSize, GFXColor, GFXRect, GFXBindingType, GFXBufferTextureCopy, GFXColorMask, GFXCullMode, GFXShaderType, GFXTextureViewType, GFXTextureLayout, GFXLoadOp, GFXTextureFlagBit, GFXMemoryUsageBit, GFXBufferUsageBit, GFXBuiltinAttributeBindings } from "../define"; +import { GFXFormatInfos, GFXFormat, WebGLEXT, GFXType, GFX_MAX_VERTEX_ATTRIBUTES, GFXFormatSize, GFXColor, GFXRect, GFXBindingType, GFXBufferTextureCopy, GFXColorMask, GFXCullMode, GFXShaderType, GFXTextureViewType, GFXTextureLayout, GFXLoadOp, GFXTextureFlagBit, GFXMemoryUsageBit, GFXBufferUsageBit, GFXGetAttributeBinding } from "../define"; import { WebGLGFXCommandAllocator } from "./webgl-command-allocator"; import { WebGLTexUnit } from "./webgl-state-cache"; @@ -719,6 +719,29 @@ export function WebGLCmdFuncCreateFramebuffer(device: WebGLGFXDevice, gpuFramebu gpuFramebuffer.gpuDepthStencilView.gpuTexture.glTexture, gpuFramebuffer.gpuDepthStencilView.baseLevel); } + + let status = gl.checkFramebufferStatus(WebGLRenderingContext.FRAMEBUFFER); + if (status !== WebGLRenderingContext.FRAMEBUFFER_COMPLETE) { + switch (status) { + case WebGLRenderingContext.FRAMEBUFFER_INCOMPLETE_ATTACHMENT: { + console.error("glCheckFramebufferStatus() - FRAMEBUFFER_INCOMPLETE_ATTACHMENT"); + break; + } + case WebGLRenderingContext.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: { + console.error("glCheckFramebufferStatus() - FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"); + break; + } + case WebGLRenderingContext.FRAMEBUFFER_INCOMPLETE_DIMENSIONS: { + console.error("glCheckFramebufferStatus() - FRAMEBUFFER_INCOMPLETE_DIMENSIONS"); + break; + } + case WebGLRenderingContext.FRAMEBUFFER_UNSUPPORTED: { + console.error("glCheckFramebufferStatus() - FRAMEBUFFER_UNSUPPORTED"); + break; + } + default: ; + } + } } } } @@ -818,9 +841,8 @@ export function WebGLCmdFuncCreateShader(device: WebGLGFXDevice, gpuShader: WebG varName = info.name; } - let glLoc = GFXBuiltinAttributeBindings[varName]; - - if(glLoc >= 0) { + let glLoc = GFXGetAttributeBinding(varName); + if (glLoc >= 0) { gl.bindAttribLocation(gpuShader.glProgram, glLoc, varName); }/* else { //glLoc = gl.getAttribLocation(gpuShader.glProgram, info.name); @@ -1010,9 +1032,17 @@ export function WebGLCmdFuncCreateInputAssember(device: WebGLGFXDevice, gpuInput let glType = GFXFormatToWebGLType(attrib.format, device); let size = WebGLGetTypeSize(glType); + let glLoc = -1; + if (attrib.binding) { + glLoc = attrib.binding; + } else { + glLoc = GFXGetAttributeBinding(attrib.name); + } + gpuInputAssembler.glAttribs[i] = { + name: attrib.name, glBuffer: gpuBuffer.glBuffer, - glLoc: attrib.binding? attrib.binding : -1, + glLoc: glLoc, glType: glType, size: size, count: GFXFormatInfos[attrib.format].count, @@ -1817,55 +1847,65 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG break; } case WebGLCmd.DRAW: { - if (gpuInputAssembler) { + if (gpuInputAssembler && gpuShader) { let cmd = cmdPackage.drawCmds[drawCmdIdx++]; for (let a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { device.stateCache.glCurrentAttribLocs[a] = false; } - for (let a = 0; a < gpuInputAssembler.glAttribs.length; ++a) { - let glAttrib = gpuInputAssembler.glAttribs[a]; + for (let n = 0; n < gpuShader.glInputs.length; ++n) { + let glInput = gpuShader.glInputs[n]; + let glAttrib: WebGLAttrib | null = null; - if (device.stateCache.glArrayBuffer !== glAttrib.glBuffer) { - gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, glAttrib.glBuffer); - device.stateCache.glArrayBuffer = glAttrib.glBuffer; - } - - for (let c = 0; c < glAttrib.componentCount; ++c) { - let glLoc = glAttrib.glLoc + c; - let offset = glAttrib.offset + glAttrib.size * c; - - if (!device.stateCache.glEnabledAttribLocs[a]) { - gl.enableVertexAttribArray(glLoc); - device.stateCache.glEnabledAttribLocs[a] = true; + for (let a = 0; a < gpuInputAssembler.glAttribs.length; ++a) { + if (gpuInputAssembler.glAttribs[a].name === glInput.name) { + glAttrib = gpuInputAssembler.glAttribs[a]; + break; } - device.stateCache.glCurrentAttribLocs[a] = true; - - gl.vertexAttribPointer(glLoc, glAttrib.count, glAttrib.glType, false, glAttrib.stride, offset); - } - } - - for (let a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { - if (device.stateCache.glEnabledAttribLocs[a] != device.stateCache.glCurrentAttribLocs[a]) { - gl.disableVertexAttribArray(a); - device.stateCache.glEnabledAttribLocs[a] = false; - } - } - - if (gpuInputAssembler.gpuIndexBuffer && cmd.indexCount > 0) { - let gpuBuffer = gpuInputAssembler.gpuIndexBuffer; - - if (device.stateCache.glElementArrayBuffer !== gpuBuffer.glBuffer) { - gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); - device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; } - let offset = cmd.firstIndex * gpuBuffer.stride; - gl.drawElements(glPrimitive, cmd.indexCount, gpuInputAssembler.glIndexType, offset); - } else { - gl.drawArrays(glPrimitive, cmd.firstVertex, cmd.vertexCount); - } + if (glAttrib) { + if (device.stateCache.glArrayBuffer !== glAttrib.glBuffer) { + gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, glAttrib.glBuffer); + device.stateCache.glArrayBuffer = glAttrib.glBuffer; + } + + for (let c = 0; c < glAttrib.componentCount; ++c) { + let glLoc = glAttrib.glLoc + c; + let offset = glAttrib.offset + glAttrib.size * c; + + if (!device.stateCache.glEnabledAttribLocs[glLoc] && glLoc >= 0) { + gl.enableVertexAttribArray(glLoc); + device.stateCache.glEnabledAttribLocs[glLoc] = true; + } + device.stateCache.glCurrentAttribLocs[glLoc] = true; + + gl.vertexAttribPointer(glLoc, glAttrib.count, glAttrib.glType, false, glAttrib.stride, offset); + } + } + + if (gpuInputAssembler.gpuIndexBuffer && cmd.indexCount > 0) { + let gpuBuffer = gpuInputAssembler.gpuIndexBuffer; + + if (device.stateCache.glElementArrayBuffer !== gpuBuffer.glBuffer) { + gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); + device.stateCache.glElementArrayBuffer = gpuBuffer.glBuffer; + } + + let offset = cmd.firstIndex * gpuBuffer.stride; + gl.drawElements(glPrimitive, cmd.indexCount, gpuInputAssembler.glIndexType, offset); + } else { + gl.drawArrays(glPrimitive, cmd.firstVertex, cmd.vertexCount); + } + + for (let a = 0; a < GFX_MAX_VERTEX_ATTRIBUTES; ++a) { + if (device.stateCache.glEnabledAttribLocs[a] != device.stateCache.glCurrentAttribLocs[a]) { + gl.disableVertexAttribArray(a); + device.stateCache.glEnabledAttribLocs[a] = false; + } + } + } // if } break; } diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index aeeedf932b..3f4516adc2 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -480,6 +480,9 @@ export class WebGLGFXDevice extends GFXDevice { return gpuTextureView; } + public emitCmdDestroyGPUTextureView(gpuTextureView: WebGLGPUTextureView) { + } + public emitCmdCreateGPURenderPass(info: GFXRenderPassInfo): WebGLGPURenderPass { let gpuRenderPass: WebGLGPURenderPass = { objType: WebGLGPUObjectType.RENDER_PASS, @@ -490,19 +493,21 @@ export class WebGLGFXDevice extends GFXDevice { return gpuRenderPass; } - public emitCmdDestroyGPURenderPass(renderPass: WebGLGPURenderPass) { + public emitCmdDestroyGPURenderPass(gpuRenderPass: WebGLGPURenderPass) { } public emitCmdCreateGPUFramebuffer(info: GFXFramebufferInfo): WebGLGPUFramebuffer { let renderPass = info.renderPass; - let gpuColorViews: (WebGLGPUTextureView | null)[] = new Array(); + let gpuColorViews: WebGLGPUTextureView[] = []; if (info.colorViews) { for (let i = 0; i < info.colorViews.length; ++i) { let webGLColorView = info.colorViews[i]; - gpuColorViews[i] = webGLColorView.gpuTextureView; + if(webGLColorView.gpuTextureView) { + gpuColorViews.push(webGLColorView.gpuTextureView); + } } } diff --git a/cocos/gfx/webgl/webgl-gpu-objects.ts b/cocos/gfx/webgl/webgl-gpu-objects.ts index 8a3877d582..1133d9bfd2 100644 --- a/cocos/gfx/webgl/webgl-gpu-objects.ts +++ b/cocos/gfx/webgl/webgl-gpu-objects.ts @@ -94,7 +94,7 @@ export class WebGLGPURenderPass extends WebGLGPUObject { export class WebGLGPUFramebuffer extends WebGLGPUObject { gpuRenderPass: WebGLGPURenderPass; - gpuColorViews: (WebGLGPUTextureView | null)[] = []; + gpuColorViews: WebGLGPUTextureView[] = []; gpuDepthStencilView: WebGLGPUTextureView | null = null; isOffscreen?: boolean = false; @@ -228,6 +228,7 @@ export class WebGLGPUBindingLayout extends WebGLGPUObject { }; export class WebGLAttrib { + name: string = ""; glBuffer: WebGLBuffer = 0; glLoc: number = 0; glType: GLenum = 0; diff --git a/cocos/gfx/webgl/webgl-input-assembler.ts b/cocos/gfx/webgl/webgl-input-assembler.ts index bda7373d77..9c9061185e 100644 --- a/cocos/gfx/webgl/webgl-input-assembler.ts +++ b/cocos/gfx/webgl/webgl-input-assembler.ts @@ -10,12 +10,21 @@ export class WebGLGFXInputAssembler extends GFXInputAssembler { } public initialize(info: GFXInputAssemblerInfo): 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) { this._indexBuffer = info.indexBuffer; + this._indexCount = this._indexBuffer.size / this._indexBuffer.stride; + } else { + let vertBuff = this._vertexBuffers[0]; + this._vertexCount = vertBuff.size / vertBuff.stride; } if (info.isIndirect) { diff --git a/cocos/gfx/webgl/webgl-texture-view.ts b/cocos/gfx/webgl/webgl-texture-view.ts index 51debcfc44..c98fc4d510 100644 --- a/cocos/gfx/webgl/webgl-texture-view.ts +++ b/cocos/gfx/webgl/webgl-texture-view.ts @@ -5,50 +5,53 @@ import { WebGLGFXDevice } from './webgl-device'; export class WebGLGFXTextureView extends GFXTextureView { - constructor(device : GFXDevice) { + constructor(device: GFXDevice) { super(device); } - public initialize(info : GFXTextureViewInfo) : boolean { + public initialize(info: GFXTextureViewInfo): boolean { this._texture = info.texture; this._type = info.type; this._format = info.format; this._format = info.format; - if(info.baseLevel) { + if (info.baseLevel) { this._baseLevel = info.baseLevel; } - if(info.levelCount) { + if (info.levelCount) { this._levelCount = info.levelCount; } - if(info.baseLayer) { + if (info.baseLayer) { this._baseLayer = info.baseLayer; } - if(info.layerCount) { + if (info.layerCount) { this._layerCount = info.layerCount; } - this.webGLDevice.emitCmdCreateGPUTextureView(info); + this._gpuTextureView = this.webGLDevice.emitCmdCreateGPUTextureView(info); return true; } public destroy() { - this._gpuTextureView = null; + if (this._gpuTextureView) { + this.webGLDevice.emitCmdDestroyGPUTextureView(this._gpuTextureView); + this._gpuTextureView = null; + } this._texture = null; } - public get webGLDevice() : WebGLGFXDevice { + public get webGLDevice(): WebGLGFXDevice { return this._device; } - public get gpuTextureView() : WebGLGPUTextureView | null { + public get gpuTextureView(): WebGLGPUTextureView | null { return this._gpuTextureView; } - private _gpuTextureView : WebGLGPUTextureView | null = null; + private _gpuTextureView: WebGLGPUTextureView | null = null; }; diff --git a/cocos/gfx/webgl/webgl-window.ts b/cocos/gfx/webgl/webgl-window.ts index f07a612024..47b1caf56e 100644 --- a/cocos/gfx/webgl/webgl-window.ts +++ b/cocos/gfx/webgl/webgl-window.ts @@ -54,6 +54,11 @@ export class WebGLGFXWindow extends GFXWindow { }, }); + if(!this._renderPass) { + this.destroy(); + return false; + } + this._colorTex = this._device.createTexture({ type : GFXTextureType.TEX2D, usage : GFXTextureUsageBit.COLOR_ATTACHMENT, @@ -66,16 +71,24 @@ export class WebGLGFXWindow extends GFXWindow { flags : GFXTextureFlagBit.NONE, }); - if(this._colorTex) { - this._colorTexView = this._device.createTextureView({ - texture : this._colorTex, - type : GFXTextureViewType.TV2D, - format : this._colorFmt, - baseLevel : 0, - levelCount : 1, - baseLayer : 0, - layerCount : 1, - }); + if(!this._colorTex) { + this.destroy(); + return false; + } + + this._colorTexView = this._device.createTextureView({ + texture : this._colorTex, + type : GFXTextureViewType.TV2D, + format : this._colorFmt, + baseLevel : 0, + levelCount : 1, + baseLayer : 0, + layerCount : 1, + }); + + if(!this._colorTexView) { + this.destroy(); + return false; } this._depthStencilTex = this._device.createTexture({ @@ -90,25 +103,36 @@ export class WebGLGFXWindow extends GFXWindow { flags : GFXTextureFlagBit.NONE, }); - if(this._depthStencilTex) { - this._depthStencilTexView = this._device.createTextureView({ - texture : this._depthStencilTex, - type : GFXTextureViewType.TV2D, - format : this._depthStencilFmt, - baseLevel : 0, - levelCount : 1, - baseLayer : 0, - layerCount : 1, - }); + if(!this._depthStencilTex) { + this.destroy(); + return false; } - if(this._colorTexView && this._renderPass && this._depthStencilTexView) { - this._framebuffer = this._device.createFramebuffer({ - renderPass: this._renderPass, - colorViews: [this._colorTexView], - depthStencilView: this._depthStencilTexView, - isOffscreen: false, - }); + this._depthStencilTexView = this._device.createTextureView({ + texture : this._depthStencilTex, + type : GFXTextureViewType.TV2D, + format : this._depthStencilFmt, + baseLevel : 0, + levelCount : 1, + baseLayer : 0, + layerCount : 1, + }); + + if(!this._depthStencilTexView) { + 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; } return true;