diff --git a/cocos/3d/ui/mesh-buffer.ts b/cocos/3d/ui/mesh-buffer.ts index db84548763..35e439bfc0 100644 --- a/cocos/3d/ui/mesh-buffer.ts +++ b/cocos/3d/ui/mesh-buffer.ts @@ -35,7 +35,6 @@ export class MeshBuffer { private _initIDataCount: number = 0; private _dirty: boolean = false; - constructor (/*device: GFXDevice*/) { // const device = cc.game._renderContext; @@ -157,7 +156,7 @@ export class MeshBuffer { // } // } - if (this.vData && this.vData.byteLength === this._initVDataCount){ + if (this.vData && this.vData.byteLength === this._initVDataCount) { return; } diff --git a/cocos/core/platform/CCView.js b/cocos/core/platform/CCView.js index b3a42a57f6..b63807bef8 100644 --- a/cocos/core/platform/CCView.js +++ b/cocos/core/platform/CCView.js @@ -552,8 +552,12 @@ class View extends EventTarget { var canvas = game.canvas; var container = game.container; - canvas.width = width * this._devicePixelRatio; - canvas.height = height * this._devicePixelRatio; + //canvas.width = width * this._devicePixelRatio; + //canvas.height = height * this._devicePixelRatio; + + const w = width * this._devicePixelRatio; + const h = height * this._devicePixelRatio; + cc.director.root.resize(w, h); canvas.style.width = width + 'px'; canvas.style.height = height + 'px'; diff --git a/cocos/gfx/command-allocator.ts b/cocos/gfx/command-allocator.ts index 56a96506d3..6bcc133239 100644 --- a/cocos/gfx/command-allocator.ts +++ b/cocos/gfx/command-allocator.ts @@ -1,6 +1,7 @@ import { GFXObject, GFXObjectType } from './define'; import { GFXDevice } from './device'; +// tslint:disable-next-line:no-empty-interface export interface IGFXCommandAllocatorInfo { } diff --git a/cocos/gfx/device.ts b/cocos/gfx/device.ts index fcab2760b7..e9d17c6ba7 100644 --- a/cocos/gfx/device.ts +++ b/cocos/gfx/device.ts @@ -16,6 +16,9 @@ import { GFXTextureView, IGFXTextureViewInfo } from './texture-view'; import { GFXWindow, IGFXWindowInfo } from './window'; export enum GFXFeature { + TEXTURE_FLOAT, + TEXTURE_HALF_FLOAT, + COUNT, } export interface IGFXDeviceInfo { @@ -105,6 +108,7 @@ export abstract class GFXDevice { protected _renderer: string = ''; protected _vendor: string = ''; protected _version: string = ''; + protected _features: boolean[] = new Array(GFXFeature.COUNT); protected _queue: GFXQueue | null = null; protected _width: number = 0; protected _height: number = 0; @@ -145,6 +149,10 @@ export abstract class GFXDevice { public abstract copyImageSourceToTexture (source: CanvasImageSource, texture: GFXTexture, regions: GFXBufferTextureCopy[]); public abstract copyFramebufferToBuffer (srcFramebuffer: GFXFramebuffer, dstBuffer: ArrayBuffer, regions: GFXBufferTextureCopy[]); + public hasFeature (feature: GFXFeature): boolean { + return this._features[feature]; + } + public genShaderId (): number { return this._shaderIdGen++; } diff --git a/cocos/gfx/webgl/webgl-command-allocator.ts b/cocos/gfx/webgl/webgl-command-allocator.ts index fa5790db8d..12bdee0502 100644 --- a/cocos/gfx/webgl/webgl-command-allocator.ts +++ b/cocos/gfx/webgl/webgl-command-allocator.ts @@ -27,12 +27,11 @@ export class WebGLGFXCommandPool { 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; @@ -56,6 +55,7 @@ export class WebGLGFXCommandPool { ++cmd.refCount; return cmd; } + */ public free (cmd: T) { if (--cmd.refCount === 0) { @@ -64,6 +64,7 @@ export class WebGLGFXCommandPool { } public freeCmds (cmds: CachedArray) { + return ; for (let i = 0; i < cmds.length; ++i) { if (--cmds.array[i].refCount === 0) { this._freeCmds.push(cmds.array[i]); diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index a585011c53..fc56e5b707 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -15,7 +15,7 @@ import { GFXTextureType, GFXTextureViewType, } from '../define'; -import { GFXDevice, IGFXDeviceInfo } from '../device'; +import { GFXDevice, GFXFeature, IGFXDeviceInfo } from '../device'; import { GFXFramebuffer, IGFXFramebufferInfo } from '../framebuffer'; import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../input-assembler'; import { GFXPipelineLayout, IGFXPipelineLayoutInfo } from '../pipeline-layout'; @@ -351,6 +351,15 @@ export class WebGLGFXDevice extends GFXDevice { this._OES_element_index_uint = gl.getExtension('OES_element_index_uint'); this._ANGLE_instanced_arrays = gl.getExtension('ANGLE_instanced_arrays'); + this._features.fill(false); + if (this._OES_texture_float) { + this._features[GFXFeature.TEXTURE_FLOAT] = true; + } + + if (this._OES_texture_half_float) { + this._features[GFXFeature.TEXTURE_HALF_FLOAT] = true; + } + // init states this.initStates(gl); diff --git a/cocos/renderer/ui/ui-material.ts b/cocos/renderer/ui/ui-material.ts new file mode 100644 index 0000000000..1dee43b2da --- /dev/null +++ b/cocos/renderer/ui/ui-material.ts @@ -0,0 +1,51 @@ +import { Material } from '../../3d/assets/material'; +import { GFXBindingLayout } from '../../gfx/binding-layout'; +import { GFXPipelineState } from '../../gfx/pipeline-state'; +import { Pass } from '../core/pass'; + +export interface IUIMaterialInfo { + material: Material; +} + +export class UIMaterial { + + public get material (): Material { + return this._material!; + } + + public get pass (): Pass { + return this._pass!; + } + + public get pipelineState (): GFXPipelineState { + return this._pipelineState!; + } + + public get bindingLayout (): GFXBindingLayout { + return this._bindingLayout!; + } + + protected _material: Material | null = null; + protected _pass: Pass | null = null; + protected _pipelineState: GFXPipelineState | null = null; + protected _bindingLayout: GFXBindingLayout | null = null; + + constructor () { + } + + public initialize (info: IUIMaterialInfo): boolean { + this._material = info.material; + + this._pass = this._material.passes[0]; + this._pipelineState = this._pass.createPipelineState(); + this._bindingLayout = this._pipelineState!.pipelineLayout.layouts[0]; + + return true; + } + + public destroy () { + this._bindingLayout = null; + this._pipelineState = null; + this._material = null; + } +} diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 6a8ea70132..5e407f0606 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -25,26 +25,18 @@ import { BaseNode } from '../../scene-graph/base-node'; import { Pass } from '../core/pass'; import { Camera } from '../scene/camera'; import { RenderScene } from '../scene/render-scene'; +import { IUIMaterialInfo, UIMaterial } from './ui-material'; export interface IUIBufferBatch { vb: GFXBuffer; ib: GFXBuffer; -} - -export interface IUIMaterial { - material: Material; - bindingLayout: GFXBindingLayout; - pipelineLayout: GFXPipelineLayout; - pipelineState: GFXPipelineState; + ia: GFXInputAssembler; } export interface IUIRenderItem { camera: Camera; - pass: Pass; - bindingLayout: GFXBindingLayout; - pipelineLayout: GFXPipelineLayout; - pipelineState: GFXPipelineState; - inputAssembler: GFXInputAssembler; + bufferBatch: IUIBufferBatch; + uiMaterial: UIMaterial; } export interface IUIRenderData { @@ -68,8 +60,9 @@ export class UI { private _scene: RenderScene; private _attributes: IGFXInputAttribute[] = []; private _bufferBatches: IUIBufferBatch[] = []; - private _uiMaterials: IUIMaterial[] = []; - private _items: CachedArray; + private _uiMaterial: UIMaterial | null = null; + private _uiMaterials: UIMaterial[] = []; + private _items: CachedArray; private _bufferInitData: IMeshBufferInitData | null = null; private _commitUIRenderDatas: IUIRenderData[] = []; @@ -90,9 +83,9 @@ export class UI { public initialize () { this._attributes = [ - { name: 'a_color', format: GFXFormat.RGBA32F, binding: 0, }, - { name: 'a_position', format: GFXFormat.RGB32F, binding: 1, }, - { name: 'a_texCoord', format: GFXFormat.RG32F, binding: 2, }, + { name: 'a_color', format: GFXFormat.RGBA32F, binding: 0 }, + { name: 'a_position', format: GFXFormat.RGB32F, binding: 1 }, + { name: 'a_texCoord', format: GFXFormat.RG32F, binding: 2 }, ]; this.createBufferBatch(); @@ -102,17 +95,22 @@ export class UI { type: GFXCommandBufferType.PRIMARY, }); + // create ui material + + /* + const material = new Material(); + material.effectName = 'sprite-material'; + // material.setProperty('mainTexture', texture); + material.update(); + + this._uiMaterial = this.createUIMaterial( { material }); + */ + return true; } public destroy () { - - for (const uiMaterial of this._uiMaterials) { - uiMaterial.bindingLayout.destroy(); - uiMaterial.pipelineLayout.destroy(); - uiMaterial.pipelineState.destroy(); - } - this._uiMaterials = []; + this.destroyUIMaterials(); for (const buffBatch of this._bufferBatches) { buffBatch.vb.destroy(); @@ -126,46 +124,32 @@ export class UI { } } - /* - public registerMaterial (material: Material): IUIMaterial { - const pass = material.passes[0]; - for (const mtrl of this._uiMaterials) { - if (mtrl.pass === pass) { - return mtrl; + public createUIMaterial (info: IUIMaterialInfo): UIMaterial | null { + const uiMtrl = new UIMaterial(); + if (uiMtrl.initialize(info)) { + this._uiMaterials.push(uiMtrl); + return uiMtrl; + } else { + return null; + } + } + + public destroyUIMaterial (uiMaterial: UIMaterial) { + for (let i = 0; i < this._uiMaterials.length; ++i) { + if (this._uiMaterials[i] === uiMaterial) { + uiMaterial.destroy(); + this._uiMaterials.splice(i); + return; } } - - const bindingLayout = this._device.createBindingLayout({ - bindings: pass.bindings, - }); - - const pipelineLayout = this._device.createPipelineLayout({ - layouts: [bindingLayout], - }); - - const pipelineState = this._device.createPipelineState({ - primitive: pass.primitive, - shader: pass.shader, - is: { attributes: this._attributes }, - rs: pass.rasterizerState, - dss: pass.depthStencilState, - bs: pass.blendState, - layout: pipelineLayout, - renderPass: pass.renderPass, - }); - - const uiMaterial = { - pass, - bindingLayout, - pipelineLayout, - pipelineState, - }; - - this._uiMaterials.push(uiMaterial); - - return uiMaterial; } - */ + + public destroyUIMaterials () { + for (const uiMaterial of this._uiMaterials) { + uiMaterial.destroy(); + } + this._uiMaterials = []; + } public addScreen (comp) { this._screens.push(comp); @@ -198,26 +182,62 @@ export class UI { return buffer; } - public createUIRenderData(){ + public createUIRenderData () { return this._commitUIRenderDataPool.add(); } - public addToQueue(uiRenderData: IUIRenderData){ + public addToQueue (uiRenderData: IUIRenderData) { this._commitUIRenderDatas.push(uiRenderData); } - public flush(){ - this.render(); - } - public update (dt: number) { this._items.clear(); - // TODO: Merge batch here. this._renderScreens(); - // this.render(); - this.flush(); + // TODO: Merge batch here. + let curCamera: Camera | null = null; + + for (const uiRenderData of this._commitUIRenderDatas) { + + if (curCamera !== uiRenderData.camera) { + curCamera = uiRenderData.camera; + } + + // uiRenderData.meshBuffer.iData.length; + } + } + + public render () { + + const material = this._uiMaterial!; + + if (this._items.length) { + const framebuffer = this._root.curWindow!.framebuffer; + const cmdBuff = this._cmdBuff!; + + cmdBuff.begin(); + + for (let i = 0; i < this._items.length; ++i) { + const item = this._items.array[i]; + const camera = item.camera; + + this._renderArea.width = camera.width; + this._renderArea.height = camera.height; + + cmdBuff.beginRenderPass(framebuffer, this._renderArea, [], camera.clearDepth, camera.clearStencil); + cmdBuff.bindPipelineState(material.pipelineState); + cmdBuff.bindBindingLayout(material.bindingLayout); + // cmdBuff.bindInputAssembler(item.meshBuffer.indiceOffset); + // cmdBuff.draw(item.inputAssembler); + + cmdBuff.endRenderPass(); + } + + cmdBuff.end(); + + this._device.queue.submit([cmdBuff]); + } } private _walk (node, fn1, fn2, level = 0) { @@ -273,50 +293,6 @@ export class UI { comp.postUpdateAssembler(); } - private render () { - /* - const framebuffer = this._root.curWindow!.framebuffer; - const cmdBuff = this._cmdBuff!; - - cmdBuff.begin(); - cmdBuff.beginRenderPass(framebuffer, this._renderArea, - [{r: 1, g: 1, b: 1, a: 1}], 1.0, 0); - - cmdBuff.end(); - - this._device.queue.submit([cmdBuff]); - */ - - if (this._items.length) { - const framebuffer = this._root.curWindow!.framebuffer; - const cmdBuff = this._cmdBuff!; - - cmdBuff.begin(); - - for (let i = 0; i < this._items.length; ++i) { - const item = this._items.array[i]; - const camera = item.camera; - - this._renderArea.width = camera.width; - this._renderArea.height = camera.height; - - cmdBuff.beginRenderPass(framebuffer, this._renderArea, - [camera.clearColor], camera.clearDepth, camera.clearStencil); - - cmdBuff.bindPipelineState(item.pipelineState); - cmdBuff.bindBindingLayout(item.bindingLayout); - cmdBuff.bindInputAssembler(item.inputAssembler); - cmdBuff.draw(item.inputAssembler); - - cmdBuff.endRenderPass(); - } - - cmdBuff.end(); - - this._device.queue.submit([cmdBuff]); - } - } - private createBufferBatch (): IUIBufferBatch { const vbStride = Float32Array.BYTES_PER_ELEMENT * 6; @@ -343,7 +319,7 @@ export class UI { indexBuffer: ib, }); - const batch: IUIBufferBatch = { vb, ib }; + const batch: IUIBufferBatch = { vb, ib, ia }; this._bufferBatches.push(batch); return batch; }