diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 6f0d83c216..1ef5140250 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -1,4 +1,44 @@ export default [ + { + "name": "test", + "techniques": [{"queue": 0, "lod": 500, "passes": [{"stage": 100, "program": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574"}], "priority": 0}], + "properties": {"u_color": {"type": 17, "value": [1, 1, 1, 1]}, "u_sampler": {"type": 29, "value": null}}, + "shaders": [ + { + "name": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574", + "vert": ` + attribute vec3 a_position; + attribute vec2 a_texCoord; + uniform mat4 cc_matWorld; + varying vec2 v_texCoord; + void main() { + gl_Position = cc_matWorld*vec4(a_position, 1.0); + v_texCoord = a_texCoord; + } +`, + "frag": ` + varying vec2 v_texCoord; + uniform vec4 u_color; + uniform sampler2D u_sampler; + void main() { + + gl_FragColor = texture2D(u_sampler, v_texCoord); + gl_FragColor.r = u_color.r; + } +`, + "defines": [], + "blocks": [ + {"name": "Constants", "size": 16, "defines": [], "binding": 0, "members": [ + {"name": "u_color", "type": 16, "count": 1, "size": 16} + ]} + ], + "samplers": [ + {"name": "u_sampler", "type": 29, "count": 1, "defines": [], "binding": 1} + ], + "dependencies": {} + } + ] + }, { "name": "builtin-effect-unlit", "techniques": [{"queue": "opaque", "lod": 100, "passes": [{"properties": {"color": {"type": 17, "value": [1, 1, 1, 1]}, "mainTiling": {"type": 14, "value": [1, 1]}, "mainOffset": {"type": 14, "value": [0, 0]}, "mainTexture": {"type": 29, "value": "grey"}}, "program": "e238b675dacba30ad29961fac06cb18d8c771e16d4fb0f9662acbe3c"}]}], diff --git a/cocos/gfx/binding-layout.ts b/cocos/gfx/binding-layout.ts index 33156a860e..cb591daa98 100644 --- a/cocos/gfx/binding-layout.ts +++ b/cocos/gfx/binding-layout.ts @@ -27,6 +27,7 @@ export abstract class GFXBindingLayout { protected _device: GFXDevice; protected _bindingUnits: GFXBindingUnit[] = []; + protected _isDirty = false; constructor (device: GFXDevice) { this._device = device; @@ -40,7 +41,10 @@ export abstract class GFXBindingLayout { for (const bindingUnit of this._bindingUnits) { if (bindingUnit.binding === binding) { if (bindingUnit.type === GFXBindingType.UNIFORM_BUFFER) { - bindingUnit.buffer = buffer; + if (bindingUnit.buffer !== buffer) { + bindingUnit.buffer = buffer; + this._isDirty = true; + } } else { console.error('Setting binding is not GFXBindingType.UNIFORM_BUFFER.'); } @@ -53,7 +57,10 @@ export abstract class GFXBindingLayout { for (const bindingUnit of this._bindingUnits) { if (bindingUnit.binding === binding) { if (bindingUnit.type === GFXBindingType.SAMPLER) { - bindingUnit.sampler = sampler; + if (bindingUnit.sampler !== sampler) { + bindingUnit.sampler = sampler; + this._isDirty = true; + } } else { console.error('Setting binding is not GFXBindingType.SAMPLER.'); } @@ -66,7 +73,10 @@ export abstract class GFXBindingLayout { for (const bindingUnit of this._bindingUnits) { if (bindingUnit.binding === binding) { if (bindingUnit.type === GFXBindingType.SAMPLER) { - bindingUnit.texView = texView; + if (bindingUnit.texView !== texView) { + bindingUnit.texView = texView; + this._isDirty = true; + } } else { console.error('Setting binding is not GFXBindingType.SAMPLER.'); } @@ -75,7 +85,12 @@ export abstract class GFXBindingLayout { } } - public getBindingUnit (binding: number): GFXBindingUnit { - return this._bindingUnits[binding]; + public getBindingUnit (binding: number): GFXBindingUnit | null { + for (const unit of this._bindingUnits) { + if (unit.binding === binding) { + return unit; + } + } + return null; } } diff --git a/cocos/gfx/webgl/webgl-binding-layout.ts b/cocos/gfx/webgl/webgl-binding-layout.ts index d3d12821d2..2a661ef7f4 100644 --- a/cocos/gfx/webgl/webgl-binding-layout.ts +++ b/cocos/gfx/webgl/webgl-binding-layout.ts @@ -47,8 +47,9 @@ export class WebGLGFXBindingLayout extends GFXBindingLayout { } public update () { - if (this._gpuBindingLayout) { - this.webGLDevice.emitCmdUpdateGPUBindingLayout(this._gpuBindingLayout, this._bindingUnits); + if (this._isDirty) { + this.webGLDevice.emitCmdUpdateGPUBindingLayout(this._gpuBindingLayout as WebGLGPUBindingLayout, this._bindingUnits); + this._isDirty = false; } } } diff --git a/cocos/pipeline/forward/forward-flow.ts b/cocos/pipeline/forward/forward-flow.ts index bcb552f123..55007b77f9 100644 --- a/cocos/pipeline/forward/forward-flow.ts +++ b/cocos/pipeline/forward/forward-flow.ts @@ -1,6 +1,8 @@ import { IRenderFlowInfo, RenderFlow } from '../render-flow'; import { RenderPipeline } from '../render-pipeline'; +// import { TestStage } from "./test-stage"; import { TestMaterialStage } from './test-material-stage'; +import { TestModelStage } from './test-model-stage'; import { TestStage } from './test-stage'; export enum ForwardStagePriority { @@ -32,19 +34,20 @@ export class ForwardFlow extends RenderFlow { priority: ForwardStagePriority.FORWARD, framebuffer: mainWindow.framebuffer, }); - */ - this.createStage(TestMaterialStage, { - name: 'TestMaterialStage', + /* */ + + this.createStage(TestModelStage, { + name: 'TestModelStage', priority: ForwardStagePriority.FORWARD, framebuffer: mainWindow.framebuffer, }); - /* - this.createStage(TestStage, { - name: 'TestStage', - priority: ForwardStagePriority.FORWARD, - framebuffer: mainWindow.framebuffer, - }); - */ + /* */ + // this.createStage(TestStage, { + // name: 'TestStage', + // priority: ForwardStagePriority.FORWARD, + // framebuffer: mainWindow.framebuffer, + // }); + /* */ return true; } diff --git a/cocos/pipeline/forward/forward-stage.ts b/cocos/pipeline/forward/forward-stage.ts index 5eb9acff31..e9cf5104d8 100644 --- a/cocos/pipeline/forward/forward-stage.ts +++ b/cocos/pipeline/forward/forward-stage.ts @@ -1,4 +1,4 @@ -import { RenderStage, RenderStageInfo } from "../render-stage"; +import { RenderStage, IRenderStageInfo } from "../render-stage"; import { RenderFlow } from "../render-flow"; import { GFXCommandBuffer } from "../../gfx/command-buffer"; import { RenderView } from "../render-view"; @@ -12,7 +12,7 @@ export class ForwardStage extends RenderStage { super(flow); } - public initialize(info: RenderStageInfo): boolean { + public initialize(info: IRenderStageInfo): boolean { if (info.name !== undefined) { this._name = info.name; diff --git a/cocos/pipeline/forward/test-material-stage.ts b/cocos/pipeline/forward/test-material-stage.ts index 4f908f7666..7627dac090 100644 --- a/cocos/pipeline/forward/test-material-stage.ts +++ b/cocos/pipeline/forward/test-material-stage.ts @@ -6,7 +6,7 @@ import { GFXCommandBufferType } from '../../gfx/define'; import { GFXFramebuffer } from '../../gfx/framebuffer'; import { GFXInputAssembler } from '../../gfx/input-assembler'; import { RenderFlow } from '../render-flow'; -import { RenderStage, RenderStageInfo } from '../render-stage'; +import { RenderStage, IRenderStageInfo } from '../render-stage'; import { RenderView } from '../render-view'; export class TestMaterialStage extends RenderStage { @@ -18,7 +18,7 @@ export class TestMaterialStage extends RenderStage { super(flow); } - public initialize (info: RenderStageInfo): boolean { + public initialize (info: IRenderStageInfo): boolean { if (info.name !== undefined) { this._name = info.name; diff --git a/cocos/pipeline/forward/test-model-stage.ts b/cocos/pipeline/forward/test-model-stage.ts new file mode 100644 index 0000000000..22198e5b84 --- /dev/null +++ b/cocos/pipeline/forward/test-model-stage.ts @@ -0,0 +1,131 @@ +import { Material } from '../../3d/assets/material'; +import { GFXCommandBuffer } from '../../gfx/command-buffer'; +import { + GFXBufferTextureCopy, GFXCommandBufferType, GFXFormat, + GFXTextureFlagBit, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType, GFXType +} from '../../gfx/define'; +import { GFXFramebuffer } from '../../gfx/framebuffer'; +import { GFXInputAssembler } from '../../gfx/input-assembler'; +import { GFXTexture } from '../../gfx/texture'; +import { GFXTextureView } from '../../gfx/texture-view'; +import { RenderFlow } from '../render-flow'; +import { RenderStage, IRenderStageInfo } from '../render-stage'; +import { RenderView } from '../render-view'; +import Model from '../../renderer/scene/model'; +import { Scene } from '../../scene-graph'; +import { EffectAsset } from '../../3d/assets/effect-asset'; +import { UBOLocal } from '../render-pipeline'; +import Texture2D from '../../assets/texture-2d'; +import ImageAsset from '../../assets/image-asset'; + +export class TestModelStage extends RenderStage { + + private _material: Material = new Material(); + private _model: Model; + private _scene: Scene; + private _init: boolean = false; + private _textureAsset: Texture2D | null = null; + + constructor(flow: RenderFlow) { + super(flow); + } + + public initialize(info: IRenderStageInfo): boolean { + + if (info.name !== undefined) { + this._name = info.name; + } + + this._priority = info.priority; + this._framebuffer = info.framebuffer; + if (!this._framebuffer) { + return false; + } + + this._cmdBuff = this._device.createCommandBuffer({ + allocator: this._device.commandAllocator, + type: GFXCommandBufferType.PRIMARY, + }); + + // load texture + const imgElms = this._device.canvas.getElementsByTagName('img'); + if (imgElms.length) { + if (!this.loadTexture(imgElms[0])) { + console.error('Load texture failed.'); + return false; + } + } else { + console.error('Not found image, load texture failed.'); + } + + + return true; + } + + public destroy() { + + if (this._texView) { + this._texView.destroy(); + this._texView = null; + } + + if (this._texture) { + this._texture.destroy(); + this._texture = null; + } + + if (this._cmdBuff) { + this._cmdBuff.destroy(); + this._cmdBuff = null; + } + } + + public render(view: RenderView) { + + if (!this._init) { + + + this._material.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js + this._material.setProperty('u_sampler', this._textureAsset); + this._material.setProperty('u_color', cc.color('#ff0000')); + this._material.inited = true; + + this._scene = new Scene(); + this._scene._scene = this._scene; + let modelCom = this._scene.addComponent('cc.ModelComponent'); + modelCom.material = this._material; + modelCom.mesh = cc.utils.createMesh(cc.game._renderContext, cc.primitives.box()); + this._model = modelCom._models[0]; + this._scene.setRotationFromEuler(45, 45, 45); + this._scene._rot = this._scene._lrot; + this._init = true; + } + + const cmdBuff = this._cmdBuff; + this._renderArea.width = view.width; + this._renderArea.height = view.height; + this._model._updateTransform(); + this._model.updateRenderData(); + cmdBuff.begin(); + cmdBuff.beginRenderPass(this._framebuffer, this._renderArea, this._clearColors, + this._clearDepth, this._clearStencil); + + cmdBuff.execute(this._model.commandBuffers); + cmdBuff.endRenderPass(); + cmdBuff.end(); + + this._device.queue.submit([cmdBuff]); + } + + private loadTexture (image: HTMLImageElement): boolean { + const textureAsset = this._textureAsset = new Texture2D(); + textureAsset.image = new ImageAsset(image); + + const texture = textureAsset.getGFXTexture(); + if (!texture) { return false; } + const texView = textureAsset.getGFXTextureView(); + if (!texView) { return false; } + + return true; + } +} diff --git a/cocos/pipeline/forward/test-stage.ts b/cocos/pipeline/forward/test-stage.ts index 0d115daa94..4e0744bda0 100644 --- a/cocos/pipeline/forward/test-stage.ts +++ b/cocos/pipeline/forward/test-stage.ts @@ -12,7 +12,7 @@ import { GFXTexture } from '../../gfx/texture'; import { GFXTextureView } from '../../gfx/texture-view'; import { RenderFlow } from '../render-flow'; import { UBOGlobal } from '../render-pipeline'; -import { RenderStage, RenderStageInfo } from '../render-stage'; +import { RenderStage, IRenderStageInfo } from '../render-stage'; import { RenderView } from '../render-view'; /* @@ -72,7 +72,7 @@ export class TestStage extends RenderStage { super(flow); } - public initialize (info: RenderStageInfo): boolean { + public initialize (info: IRenderStageInfo): boolean { if (info.name !== undefined) { this._name = info.name; diff --git a/cocos/pipeline/render-flow.ts b/cocos/pipeline/render-flow.ts index 5870f5ce93..e7377b1e29 100644 --- a/cocos/pipeline/render-flow.ts +++ b/cocos/pipeline/render-flow.ts @@ -1,6 +1,6 @@ import { GFXDevice } from '../gfx/device'; import { RenderPipeline } from './render-pipeline'; -import { RenderStage, RenderStageInfo } from './render-stage'; +import { RenderStage, IRenderStageInfo } from './render-stage'; import { RenderView } from './render-view'; export interface IRenderFlowInfo { @@ -43,7 +43,7 @@ export abstract class RenderFlow { } } - public createStage (clazz: new(flow: RenderFlow) => T, info: RenderStageInfo): RenderStage | null { + public createStage (clazz: new(flow: RenderFlow) => T, info: IRenderStageInfo): RenderStage | null { const stage: RenderStage = new clazz(this); if (stage.initialize(info)) { this._stages.push(stage); diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 2bfa1b3a09..aa1c6f8d96 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -34,7 +34,7 @@ export class UBOGlobal { public static SIZE: number = UBOGlobal.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 0, name: 'Global', members: [ + binding: 30, name: 'Global', members: [ { name: 'u_time', type: GFXType.FLOAT4, count: 1 }, { name: 'u_screenSize', type: GFXType.FLOAT4, count: 1 }, { name: 'u_screenScale', type: GFXType.FLOAT4, count: 1 }, @@ -53,18 +53,14 @@ export class UBOGlobal { export class UBOLocal { public static MAT_WORLD_OFFSET: number = 0; - public static MAT_WORLD_VIEW_OFFSET: number = UBOLocal.MAT_WORLD_OFFSET + 16; - public static MAT_WORLD_VIEW_PROJ_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_OFFSET + 16; - public static WORLD_POS_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_PROJ_OFFSET + 16; - public static COUNT: number = UBOLocal.WORLD_POS_OFFSET + 4; + public static MAT_WORLD_IT_OFFSET: number = UBOLocal.MAT_WORLD_OFFSET + 16; + public static COUNT: number = UBOLocal.MAT_WORLD_IT_OFFSET + 16; public static SIZE: number = UBOLocal.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 1, name: 'Local', members: [ - { name: 'u_matWorld', type: GFXType.MAT4, count: 1 }, - { name: 'u_matWorldView', type: GFXType.MAT4, count: 1 }, - { name: 'u_matWorldViewProj', type: GFXType.MAT4, count: 1 }, - { name: 'u_worldPos', type: GFXType.FLOAT4, count: 1 }, + binding: 31, name: 'CCLocal', members: [ + { name: 'cc_matWorld', type: GFXType.MAT4, count: 1 }, + { name: 'cc_matWorldIT', type: GFXType.MAT4, count: 1 }, ], }; @@ -348,18 +344,16 @@ export abstract class RenderPipeline { for (const model of scene.models) { // filter model by view visibility - if (view.visibility > 0 && model._viewID !== view.visibility) { + if (view.visibility > 0 && model.viewID !== view.visibility) { continue; } model._updateTransform(); // frustum culling - if (model._boundingShape) { // if model does not have boundingBox, skip culling. - if (!intersect.aabb_frustum(model._boundingShape, camera.frustum)) { - // console.log('model is not in view frustum.'); - continue; - } + if (!intersect.aabb_frustum(model.boundingShape, camera.frustum)) { + // console.log('model is not in view frustum.'); + continue; } // add model pass to render queue diff --git a/cocos/pipeline/render-stage.ts b/cocos/pipeline/render-stage.ts index b2d2ba2106..2f4a56b7ee 100644 --- a/cocos/pipeline/render-stage.ts +++ b/cocos/pipeline/render-stage.ts @@ -1,83 +1,39 @@ -import { RenderView } from "./render-view"; -import { GFXRenderPass } from "../gfx/render-pass"; -import { GFXFramebuffer } from "../gfx/framebuffer"; -import { GFXCommandBuffer } from "../gfx/command-buffer"; -import { RenderFlow } from "./render-flow"; -import { GFXDevice } from "../gfx/device"; -import { RenderPipeline } from "./render-pipeline"; -import { IGFXColor, IGFXRect } from "../gfx/define"; +import { GFXCommandBuffer } from '../gfx/command-buffer'; +import { IGFXColor, IGFXRect } from '../gfx/define'; +import { GFXDevice } from '../gfx/device'; +import { GFXFramebuffer } from '../gfx/framebuffer'; +import { RenderFlow } from './render-flow'; +import { RenderPipeline } from './render-pipeline'; +import { RenderView } from './render-view'; -export interface RenderStageInfo { +export interface IRenderStageInfo { name?: string; priority: number; framebuffer: GFXFramebuffer; -}; +} export abstract class RenderStage { - constructor(flow: RenderFlow) { - this._flow = flow; - this._pipeline = flow.pipeline; - this._device = flow.device; - - if (!this._flow.pipeline.root.device) { - throw new Error(""); - } - - this._device = this._flow.pipeline.root.device; - this._clearColors = [{ r: 0.3, g: 0.6, b: 0.9, a: 1.0 }]; - this._renderArea = { x: 0, y: 0, width: 0, height: 0 }; - } - - public abstract initialize(info: RenderStageInfo): boolean; - public abstract destroy(); - public abstract render(view: RenderView); - - public setClearColor(color: IGFXColor) { - if (this._clearColors.length > 0) { - this._clearColors[0] = color; - } else { - this._clearColors.push(color); - } - } - - public setClearColors(colors: IGFXColor[]) { - this._clearColors = colors; - } - - public setClearDepth(depth: number) { - this._clearDepth = depth; - } - - public setClearStencil(stencil: number) { - this._clearStencil = stencil; - } - - public setRenderArea(width: number, height: number) { - this._renderArea.width = width; - this._renderArea.height = height; - } - - public get flow(): RenderFlow { + public get flow (): RenderFlow { return this._flow; } - public get pipeline(): RenderPipeline { + public get pipeline (): RenderPipeline { return this._pipeline; } - public get priority(): number { + public get priority (): number { return this._priority; } - public get framebuffer(): GFXFramebuffer | null { + public get framebuffer (): GFXFramebuffer | null { return this._framebuffer; } protected _flow: RenderFlow; protected _pipeline: RenderPipeline; protected _device: GFXDevice; - protected _name: string = ""; + protected _name: string = ''; protected _priority: number = 0; protected _framebuffer: GFXFramebuffer | null = null; protected _cmdBuff: GFXCommandBuffer | null = null; @@ -85,4 +41,47 @@ export abstract class RenderStage { protected _clearDepth: number = 1.0; protected _clearStencil: number = 0; protected _renderArea: IGFXRect; -}; + + constructor (flow: RenderFlow) { + this._flow = flow; + this._pipeline = flow.pipeline; + this._device = flow.device; + + if (!this._flow.pipeline.root.device) { + throw new Error(''); + } + + this._device = this._flow.pipeline.root.device; + this._clearColors = [{ r: 0.3, g: 0.6, b: 0.9, a: 1.0 }]; + this._renderArea = { x: 0, y: 0, width: 0, height: 0 }; + } + + public abstract initialize (info: IRenderStageInfo): boolean; + public abstract destroy (); + public abstract render (view: RenderView); + + public setClearColor (color: IGFXColor) { + if (this._clearColors.length > 0) { + this._clearColors[0] = color; + } else { + this._clearColors.push(color); + } + } + + public setClearColors (colors: IGFXColor[]) { + this._clearColors = colors; + } + + public setClearDepth (depth: number) { + this._clearDepth = depth; + } + + public setClearStencil (stencil: number) { + this._clearStencil = stencil; + } + + public setRenderArea (width: number, height: number) { + this._renderArea.width = width; + this._renderArea.height = height; + } +} diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 52c6d904b2..09548c8496 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -87,7 +87,6 @@ export class Pass { protected _stage: RenderPassStage = RenderPassStage.DEFAULT; protected _handleMap: Record = {}; protected _blocks: IBlock[] = []; - protected _layoutDirty: boolean = false; // external references protected _device: GFXDevice; protected _shader: GFXShader | null = null; @@ -184,19 +183,11 @@ export class Pass { } public bindTextureView (binding: number, value: GFXTextureView) { - const bl = this._bindingLayout; - if (bl && bl.getBindingUnit(binding).texView !== value) { - bl.bindTextureView(binding, value); - this._layoutDirty = true; - } + (this._bindingLayout as GFXBindingLayout).bindTextureView(binding, value); } public bindSampler (binding: number, value: GFXSampler) { - const bl = this._bindingLayout; - if (bl && bl.getBindingUnit(binding).sampler !== value) { - bl.bindSampler(binding, value); - this._layoutDirty = true; - } + (this._bindingLayout as GFXBindingLayout).bindSampler(binding, value); } public setStates (info: IPassInfo) { @@ -221,10 +212,7 @@ export class Pass { block.dirty = false; } } - if (this._bindingLayout && this._layoutDirty) { - this._bindingLayout.update(); - this._layoutDirty = false; - } + (this._bindingLayout as GFXBindingLayout).update(); } protected createPipelineState (info: IPassInfo) { diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index 15fb5cff82..470fdb4e33 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -1,10 +1,11 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. -import { IDefineInfo, IShaderInfo } from '../../3d/assets/effect-asset'; -import { GFXShaderType } from '../../gfx/define'; +import { IDefineInfo, IShaderInfo, IBlockInfo, IBlockMember } from '../../3d/assets/effect-asset'; +import { GFXShaderType, GFXGetTypeSize } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; -import { GFXShader } from '../../gfx/shader'; +import { GFXShader, GFXUniformBlock, GFXUniform } from '../../gfx/shader'; import { IDefineMap } from './effect'; +import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline'; function _generateDefines( device: GFXDevice, @@ -125,6 +126,8 @@ export class ProgramLib { const vert = customDef + tmpl.vert; const frag = customDef + tmpl.frag; + tmpl.blocks = tmpl.blocks.concat(convertToBlockInfo(UBOGlobal.BLOCK), convertToBlockInfo(UBOLocal.BLOCK)); + program = this._device.createShader({ name, blocks: tmpl.blocks, @@ -138,3 +141,28 @@ export class ProgramLib { return program; } } + +function convertToUniformInfo(uniform: GFXUniform): IBlockMember { + return { + name: uniform.name, + type: uniform.type, + count: uniform.count, + size: GFXGetTypeSize(uniform.type) * uniform.count + } +} + +function convertToBlockInfo(block: GFXUniformBlock): IBlockInfo { + let members: IBlockMember[] = []; + let size: number = 0; + for (let i = 0; i < block.members.length; i++) { + members.push(convertToUniformInfo(block.members[i])); + size += members[i].size; + } + return { + name: block.name, + binding: block.binding, + defines: [], + members: members, + size: size + }; +} diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index b36952660f..41df922035 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -1,39 +1,47 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. import { Material } from '../../3d/assets/material'; import { aabb } from '../../3d/geom-utils'; +import { IGFXInputAttribute, GFXInputAssembler } from '../../gfx/input-assembler'; +import { GFXFormat, GFXBufferUsageBit, GFXMemoryUsageBit, GFXCommandBufferType } from '../../gfx/define'; +import { RenderScene } from './render-scene'; +import { vec3, mat4 } from '../../core/vmath'; import { Vec3 } from '../../core/value-types'; import { GFXCommandBuffer } from '../../gfx/command-buffer'; -import { GFXCommandBufferType } from '../../gfx/define'; -import { GFXInputAssembler } from '../../gfx/input-assembler'; import { Node } from '../../scene-graph'; import { Effect } from '../core/effect'; import { Pass } from '../core/pass'; -import { RenderScene } from './render-scene'; +import { UBOLocal } from '../../pipeline/render-pipeline'; +import { GFXBuffer } from '../../gfx/buffer'; + +let _temp_floatx16 = new Float32Array(16); +let _temp_mat4 = mat4.create(); /** * A representation of a model */ export default class Model { - public _scene: RenderScene | null; - public _id: number; - public _enable: boolean; - public _type: string; - public _poolID: number; - public _isEnable: boolean; - public _node: Node; - public _inputAssembler: GFXInputAssembler | null; - public _effect: Effect | null; - public _defines: Object; - public _dependencies: Object; - public _viewID: number; - public _cameraID: number; - public _userKey: number; - public _castShadow: boolean; - public _boundingShape: aabb; - public _bsModelSpace: aabb; - public _material: Material | null; - public _cmdBuffers: GFXCommandBuffer[]; + private _scene: RenderScene | null; + private _id: number; + private _enable: boolean; + private _type: string; + private _poolID: number; + private _isEnable: boolean; + private _node: Node; + private _inputAssembler: GFXInputAssembler | null; + private _effect: Effect | null; + private _defines: Object; + private _dependencies: Object; + private _viewID: number; + private _cameraID: number; + private _userKey: number; + private _castShadow: boolean; + private _boundingShape: aabb; + private _bsModelSpace: aabb; + private _material: Material | null; + private _cmdBuffers: GFXCommandBuffer[]; + private _localUniforms: UBOLocal | null; + private _localUBO: GFXBuffer; /** * Setup a default empty model */ @@ -57,6 +65,18 @@ export default class Model { this._boundingShape = null; this._material = null; this._cmdBuffers = new Array(); + this._localUniforms = null; + } + + initialize() { + this._localUniforms = new UBOLocal(); + this._localUBO = cc.director.root.device.createBuffer({ + usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST, + size: UBOLocal.SIZE, + stride: UBOLocal.SIZE, + }); + this._localUBO.update(this._localUniforms.view); } set scene (scene: RenderScene | null) { @@ -82,6 +102,15 @@ export default class Model { this._node._rot, this._node._scale, this._boundingShape); } + updateRenderData() { + mat4.array(_temp_floatx16, this._node._mat); + this._node._mat.invert(_temp_mat4); + this._localUniforms.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET); + mat4.array(_temp_floatx16, _temp_mat4); + this._localUniforms.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET); + this._localUBO.update(this._localUniforms.view); + } + /** * Create the bounding shape of this model * @param {vec3} minPos the min position of the model @@ -125,6 +154,14 @@ export default class Model { this._inputAssembler = ia; } + get boundingShape (): aabb { + return this._boundingShape; + } + + get viewID (): number { + return this._viewID; + } + /** * Set the model effect * @param {?Effect} effect the effect to use @@ -163,6 +200,8 @@ export default class Model { if (this._cmdBuffers[index] == null) { this._cmdBuffers[index] = cc.director.root.device.createCommandBuffer(cmdBufferInfo); } + pass.bindingLayout.bindBuffer(UBOLocal.BLOCK.binding, this._localUBO); + pass.bindingLayout.update(); this._cmdBuffers[index].begin(); this._cmdBuffers[index].bindPipelineState(pass.pipelineState); this._cmdBuffers[index].bindBindingLayout(pass.bindingLayout); diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 044286e592..52a0135342 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -119,6 +119,7 @@ export class RenderScene { public createModel(clazz: new () => T): Model { let model = new clazz; + model.initialize(); model.scene = this; this._models.push(model); return model;