diff --git a/cocos/3d/framework/camera-component.ts b/cocos/3d/framework/camera-component.ts index cbd7b289b2..678c305a9b 100644 --- a/cocos/3d/framework/camera-component.ts +++ b/cocos/3d/framework/camera-component.ts @@ -112,30 +112,6 @@ export class CameraComponent extends Component { super(); } - /** - * !#en Create offscreen render target - * - * !#ch 创建离屏渲染目标 - */ - public createRenderTarget (info: IRenderTargetInfo): boolean { - if (this._camera) { - return this._camera.view.createRenderTarget(info); - } else { - return false; - } - } - - /** - * !#en Destroy offscreen render target - * - * !#ch 销毁离屏渲染目标 - */ - public destroyRenderTarget () { - if (this._camera) { - return this._camera.view.destroyRenderTarget(); - } - } - /** * !#en The projection type of the camera * diff --git a/cocos/gfx/framebuffer.ts b/cocos/gfx/framebuffer.ts index f9a3729389..000122231e 100644 --- a/cocos/gfx/framebuffer.ts +++ b/cocos/gfx/framebuffer.ts @@ -5,8 +5,8 @@ import { GFXTextureView } from './texture-view'; export interface IGFXFramebufferInfo { renderPass: GFXRenderPass; - colorViews?: GFXTextureView[]; - depthStencilView?: GFXTextureView; + colorViews: GFXTextureView[]; + depthStencilView: GFXTextureView | null; isOffscreen?: boolean; } diff --git a/cocos/gfx/webgl/webgl-window.ts b/cocos/gfx/webgl/webgl-window.ts index 573b59c35e..caba748898 100644 --- a/cocos/gfx/webgl/webgl-window.ts +++ b/cocos/gfx/webgl/webgl-window.ts @@ -1,5 +1,16 @@ -import { GFXLoadOp, GFXStatus, GFXStoreOp, GFXTextureLayout } from '../define'; +import { + GFXFormat, + GFXLoadOp, + GFXStatus, + GFXStoreOp, + GFXTextureFlagBit, + GFXTextureLayout, + GFXTextureType, + GFXTextureUsageBit, + GFXTextureViewType, +} from '../define'; import { GFXDevice } from '../device'; +import { GFXTextureView } from '../texture-view'; import { GFXWindow, IGFXWindowInfo } from '../window'; export class WebGLGFXWindow extends GFXWindow { @@ -22,6 +33,10 @@ export class WebGLGFXWindow extends GFXWindow { this._top = info.top; } + if (info.isOffscreen !== undefined) { + this._isOffscreen = info.isOffscreen; + } + this._width = info.width; this._height = info.height; this._colorFmt = info.colorFmt; @@ -48,115 +63,90 @@ export class WebGLGFXWindow extends GFXWindow { }, }); - if (!this._renderPass) { - this.destroy(); - return false; + const colorViews: GFXTextureView[] = []; + + if (this._isOffscreen) { + if (this._colorFmt !== GFXFormat.UNKNOWN) { + this._colorTex = this._device.createTexture({ + type : GFXTextureType.TEX2D, + usage : GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, + format : this._colorFmt, + width : this._width, + height : this._height, + depth : 1, + arrayLayer : 1, + mipLevel : 1, + flags : GFXTextureFlagBit.NONE, + }); + this._colorTexView = this._device.createTextureView({ + texture : this._colorTex, + type : GFXTextureViewType.TV2D, + format : this._colorFmt, + baseLevel : 0, + levelCount : 1, + baseLayer : 0, + layerCount : 1, + }); + colorViews.push(this._colorTexView); + } + + if (this._depthStencilFmt !== GFXFormat.UNKNOWN) { + this._depthStencilTex = this._device.createTexture({ + type : GFXTextureType.TEX2D, + usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT, + format : this._depthStencilFmt, + width : this._width, + height : this._height, + depth : 1, + arrayLayer : 1, + mipLevel : 1, + flags : GFXTextureFlagBit.NONE, + }); + + this._depthStencilTexView = this._device.createTextureView({ + texture : this._depthStencilTex, + type : GFXTextureViewType.TV2D, + format : this._depthStencilFmt, + baseLevel : 0, + levelCount : 1, + baseLayer : 0, + layerCount : 1, + }); + } } - /* - this._colorTex = this._device.createTexture({ - type : GFXTextureType.TEX2D, - usage : GFXTextureUsageBit.COLOR_ATTACHMENT, - format : this._colorFmt, - width : this._width, - height : this._height, - depth : 1, - arrayLayer : 1, - mipLevel : 1, - flags : GFXTextureFlagBit.NONE, - }); - - 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({ - type : GFXTextureType.TEX2D, - usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT, - format : this._depthStencilFmt, - width : this._width, - height : this._height, - depth : 1, - arrayLayer : 1, - mipLevel : 1, - flags : GFXTextureFlagBit.NONE, - }); - - if(!this._depthStencilTex) { - this.destroy(); - return 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, + colorViews, + depthStencilView: this._depthStencilTexView, + isOffscreen: this._isOffscreen, }); - if (!this._framebuffer) { - this.destroy(); - return false; - } this._status = GFXStatus.SUCCESS; return true; } public destroy () { - /* - if(this._depthStencilTexView) { + if (this._depthStencilTexView) { this._depthStencilTexView.destroy(); this._depthStencilTexView = null; } - if(this._depthStencilTex) { + if (this._depthStencilTex) { this._depthStencilTex.destroy(); this._depthStencilTex = null; } - if(this._colorTexView) { + if (this._colorTexView) { this._colorTexView.destroy(); this._colorTexView = null; } - if(this._colorTex) { + if (this._colorTex) { this._colorTex.destroy(); this._colorTex = null; } - */ if (this._framebuffer) { this._framebuffer.destroy(); diff --git a/cocos/gfx/webgl2/webgl2-window.ts b/cocos/gfx/webgl2/webgl2-window.ts index fa587974f4..e25285fb31 100644 --- a/cocos/gfx/webgl2/webgl2-window.ts +++ b/cocos/gfx/webgl2/webgl2-window.ts @@ -1,5 +1,16 @@ -import { GFXLoadOp, GFXStatus, GFXStoreOp, GFXTextureLayout } from '../define'; +import { + GFXFormat, + GFXLoadOp, + GFXStatus, + GFXStoreOp, + GFXTextureFlagBit, + GFXTextureLayout, + GFXTextureType, + GFXTextureUsageBit, + GFXTextureViewType, +} from '../define'; import { GFXDevice } from '../device'; +import { GFXTextureView } from '../texture-view'; import { GFXWindow, IGFXWindowInfo } from '../window'; export class WebGL2GFXWindow extends GFXWindow { @@ -22,6 +33,10 @@ export class WebGL2GFXWindow extends GFXWindow { this._top = info.top; } + if (info.isOffscreen !== undefined) { + this._isOffscreen = info.isOffscreen; + } + this._width = info.width; this._height = info.height; this._colorFmt = info.colorFmt; @@ -48,28 +63,91 @@ export class WebGL2GFXWindow extends GFXWindow { }, }); - if (!this._renderPass) { - this.destroy(); - return false; + const colorViews: GFXTextureView[] = []; + + if (this._isOffscreen) { + if (this._colorFmt !== GFXFormat.UNKNOWN) { + this._colorTex = this._device.createTexture({ + type : GFXTextureType.TEX2D, + usage : GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, + format : this._colorFmt, + width : this._width, + height : this._height, + depth : 1, + arrayLayer : 1, + mipLevel : 1, + flags : GFXTextureFlagBit.NONE, + }); + this._colorTexView = this._device.createTextureView({ + texture : this._colorTex, + type : GFXTextureViewType.TV2D, + format : this._colorFmt, + baseLevel : 0, + levelCount : 1, + baseLayer : 0, + layerCount : 1, + }); + colorViews.push(this._colorTexView); + } + + if (this._depthStencilFmt !== GFXFormat.UNKNOWN) { + this._depthStencilTex = this._device.createTexture({ + type : GFXTextureType.TEX2D, + usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT, + format : this._depthStencilFmt, + width : this._width, + height : this._height, + depth : 1, + arrayLayer : 1, + mipLevel : 1, + flags : GFXTextureFlagBit.NONE, + }); + + this._depthStencilTexView = this._device.createTextureView({ + texture : this._depthStencilTex, + type : GFXTextureViewType.TV2D, + format : this._depthStencilFmt, + baseLevel : 0, + levelCount : 1, + baseLayer : 0, + layerCount : 1, + }); + } } this._framebuffer = this._device.createFramebuffer({ renderPass: this._renderPass, - // colorViews: [this._colorTexView], - // depthStencilView: this._depthStencilTexView, - isOffscreen: false, + colorViews, + depthStencilView: this._depthStencilTexView, + isOffscreen: this._isOffscreen, }); - if (!this._framebuffer) { - this.destroy(); - return false; - } this._status = GFXStatus.SUCCESS; return true; } public destroy () { + if (this._depthStencilTexView) { + this._depthStencilTexView.destroy(); + this._depthStencilTexView = null; + } + + if (this._depthStencilTex) { + this._depthStencilTex.destroy(); + this._depthStencilTex = null; + } + + if (this._colorTexView) { + this._colorTexView.destroy(); + this._colorTexView = null; + } + + if (this._colorTex) { + this._colorTex.destroy(); + this._colorTex = null; + } + if (this._framebuffer) { this._framebuffer.destroy(); this._framebuffer = null; diff --git a/cocos/gfx/window.ts b/cocos/gfx/window.ts index 1191e16a51..3a18ece46a 100644 --- a/cocos/gfx/window.ts +++ b/cocos/gfx/window.ts @@ -2,6 +2,8 @@ import { GFXFormat, GFXObject, GFXObjectType } from './define'; import { GFXDevice } from './device'; import { GFXFramebuffer } from './framebuffer'; import { GFXRenderPass } from './render-pass'; +import { GFXTexture } from './texture'; +import { GFXTextureView } from './texture-view'; export interface IGFXWindowInfo { title?: string; @@ -11,6 +13,7 @@ export interface IGFXWindowInfo { height: number; colorFmt: GFXFormat; depthStencilFmt: GFXFormat; + isOffscreen?: boolean; } export abstract class GFXWindow extends GFXObject { @@ -35,6 +38,14 @@ export abstract class GFXWindow extends GFXObject { return this._renderPass!; } + public get colorTexView (): GFXTextureView | null { + return this._colorTexView; + } + + public get depthStencilTexView (): GFXTextureView | null { + return this._depthStencilTexView; + } + public get framebuffer (): GFXFramebuffer { return this._framebuffer!; } @@ -47,11 +58,12 @@ export abstract class GFXWindow extends GFXObject { protected _height: number = 0; protected _colorFmt: GFXFormat = GFXFormat.UNKNOWN; protected _depthStencilFmt: GFXFormat = GFXFormat.UNKNOWN; + protected _isOffscreen: boolean = false; protected _renderPass: GFXRenderPass | null = null; - // protected _colorTex: GFXTexture | null = null; - // protected _colorTexView: GFXTextureView | null = null; - // protected _depthStencilTex: GFXTexture | null = null; - // protected _depthStencilTexView: GFXTextureView | null = null; + protected _colorTex: GFXTexture | null = null; + protected _colorTexView: GFXTextureView | null = null; + protected _depthStencilTex: GFXTexture | null = null; + protected _depthStencilTexView: GFXTextureView | null = null; protected _framebuffer: GFXFramebuffer | null = null; constructor (device: GFXDevice) { diff --git a/cocos/pipeline/render-view.ts b/cocos/pipeline/render-view.ts index c95265d93e..4c6acf7eb7 100644 --- a/cocos/pipeline/render-view.ts +++ b/cocos/pipeline/render-view.ts @@ -1,9 +1,5 @@ import { Root } from '../core/root'; -import { GFXFormat, GFXLoadOp, GFXStoreOp, GFXTextureLayout, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define'; import { GFXFramebuffer } from '../gfx/framebuffer'; -import { GFXRenderPass } from '../gfx/render-pass'; -import { GFXTexture } from '../gfx/texture'; -import { GFXTextureView } from '../gfx/texture-view'; import { GFXWindow } from '../gfx/window'; import { Camera } from '../renderer/scene/camera'; import { RenderFlow } from './render-flow'; @@ -103,13 +99,6 @@ export class RenderView { private _isUI: boolean = false; private _flows: RenderFlow[] = []; private _isOffscreen: boolean = false; - - // For offscreen - private _renderPass: GFXRenderPass | null = null; - private _renderTex: GFXTexture | null = null; - private _renderTexView: GFXTextureView | null = null; - private _depthStencilTex: GFXTexture | null = null; - private _depthStencilTexView: GFXTextureView | null = null; private _framebuffer: GFXFramebuffer | null = null; private constructor (root: Root, camera: Camera) { @@ -138,93 +127,6 @@ export class RenderView { public destroy () { this._window = null; this._priority = 0; - this.destroyRenderTarget(); - } - - public createRenderTarget (info: IRenderTargetInfo): boolean { - this._isOffscreen = true; - const device = this._root.device; - const colorFmt = GFXFormat.RGBA8; - const width = (info.width !== undefined ? info.width : this._camera.width); - const height = (info.height !== undefined ? info.height : this._camera.height); - - if (this._renderTex) { - this._renderTex.destroy(); - } - - this._renderTex = device.createTexture({ - type: GFXTextureType.TEX2D, - usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, - format: colorFmt, - width, - height, - }); - - if (this._renderTexView) { - this._renderTexView.destroy(); - } - - this._renderTexView = device.createTextureView({ - texture : this._renderTex, - type : GFXTextureViewType.TV2D, - format : colorFmt, - }); - - if (this._renderPass) { - this._renderPass.destroy(); - } - - this._renderPass = device.createRenderPass({ - colorAttachments: [{ - format: colorFmt, - loadOp: GFXLoadOp.CLEAR, - storeOp: GFXStoreOp.STORE, - sampleCount: 1, - beginLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL, - endLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL, - }], - }); - - if (this._framebuffer) { - this._framebuffer.destroy(); - } - - this._framebuffer = device.createFramebuffer({ - renderPass: this._renderPass, - colorViews: [ this._renderTexView ], - isOffscreen: true, - }); - - return true; - } - - public destroyRenderTarget () { - if (this._renderTex) { - this._renderTex.destroy(); - } - - if (this._renderTexView) { - this._renderTexView.destroy(); - } - - if (this._depthStencilTex) { - this._depthStencilTex.destroy(); - } - - if (this._depthStencilTexView) { - this._depthStencilTexView.destroy(); - } - - if (this._framebuffer && this._isOffscreen) { - this._framebuffer.destroy(); - } - - this._renderTex = null; - this._renderTexView = null; - this._depthStencilTex = null; - this._depthStencilTexView = null; - this._framebuffer = null; - this._isOffscreen = false; } public enable (isEnable: boolean) {