From 121c189e3edeac6604cc06213d38908b18d254dc Mon Sep 17 00:00:00 2001 From: Weiliang Wang Date: Wed, 12 Jun 2019 18:44:55 +0800 Subject: [PATCH] add pipleine api docs (#4603) --- cocos/pipeline/define.ts | 54 +++ cocos/pipeline/forward/forward-flow.ts | 26 ++ cocos/pipeline/forward/forward-pipeline.ts | 64 ++++ cocos/pipeline/forward/forward-stage.ts | 35 +- cocos/pipeline/pipeline-funcs.ts | 10 + cocos/pipeline/ppfx/smaa-blend-stage.ts | 4 + cocos/pipeline/ppfx/smaa-edge-stage.ts | 4 + cocos/pipeline/ppfx/smaa-flow.ts | 4 + cocos/pipeline/ppfx/tonemap-flow.ts | 4 + cocos/pipeline/ppfx/tonemap-stage.ts | 4 + cocos/pipeline/render-flow.ts | 78 +++++ cocos/pipeline/render-pipeline.ts | 375 ++++++++++++++++++++- cocos/pipeline/render-queue.ts | 61 +++- cocos/pipeline/render-stage.ts | 125 +++++++ cocos/pipeline/render-view.ts | 80 ++++- cocos/pipeline/ui/ui-flow.ts | 8 +- cocos/pipeline/ui/ui-stage.ts | 4 + cocos/pipeline/utils.ts | 22 +- 18 files changed, 937 insertions(+), 25 deletions(-) diff --git a/cocos/pipeline/define.ts b/cocos/pipeline/define.ts index d1c8121ad7..8390838fe1 100644 --- a/cocos/pipeline/define.ts +++ b/cocos/pipeline/define.ts @@ -11,21 +11,37 @@ export const PIPELINE_FLOW_FORWARD: string = 'ForwardFlow'; export const PIPELINE_FLOW_SMAA: string = 'SMAAFlow'; export const PIPELINE_FLOW_TONEMAP: string = 'ToneMapFlow'; +/** + * @zh + * 渲染过程阶段 + */ export enum RenderPassStage { DEFAULT = 100, } +/** + * @zh + * 渲染优先级 + */ export enum RenderPriority { MIN = 0, MAX = 0xff, DEFAULT = 0x80, } +/** + * @zh + * 渲染对象 + */ export interface IRenderObject { model: Model; depth: number; } +/** + * @zh + * 渲染过程 + */ export interface IRenderPass { hash: number; depth: number; @@ -34,6 +50,10 @@ export interface IRenderPass { cmdBuff: GFXCommandBuffer; } +/** + * @zh + * 渲染队列描述 + */ export interface IRenderQueueDesc { isTransparent: boolean; phases: number; @@ -41,6 +61,11 @@ export interface IRenderQueueDesc { } const MAX_BINDING_SUPPORTED = 24; // from WebGL 2 spec + +/** + * @zh + * Uniform参数绑定 + */ export enum UniformBinding { // UBOs UBO_GLOBAL = MAX_BINDING_SUPPORTED - 1, @@ -55,7 +80,12 @@ export enum UniformBinding { SAMPLER_JOINTS = MAX_BINDING_SUPPORTED + 1, } +/** + * @zh + * 全局UBO + */ export class UBOGlobal { + public static TIME_OFFSET: number = 0; public static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4; public static SCREEN_SCALE_OFFSET: number = UBOGlobal.SCREEN_SIZE_OFFSET + 4; @@ -99,6 +129,10 @@ export class UBOGlobal { public view: Float32Array = new Float32Array(UBOGlobal.COUNT); } +/** + * @zh + * 阴影UBO + */ export class UBOShadow { public static MAT_LIGHT_PLANE_PROJ_OFFSET: number = 0; public static SHADOW_COLOR_OFFSET: number = UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET + 16; @@ -117,6 +151,10 @@ export class UBOShadow { export const localBindingsDesc: Map = new Map(); +/** + * @zh + * 本地UBO + */ export class UBOLocal { public static MAT_WORLD_OFFSET: number = 0; public static MAT_WORLD_IT_OFFSET: number = UBOLocal.MAT_WORLD_OFFSET + 16; @@ -137,6 +175,10 @@ localBindingsDesc.set(UBOLocal.BLOCK.name, { blockInfo: UBOLocal.BLOCK, }); +/** + * @zh + * 前向灯光UBO + */ export class UBOForwardLight { public static MAX_SPHERE_LIGHTS = 2; public static MAX_SPOT_LIGHTS = 2; @@ -177,6 +219,10 @@ localBindingsDesc.set(UBOForwardLight.BLOCK.name, { // and the JOINT_UNIFORM_CAPACITY macro in cc-skinning shader header. export const JointUniformCapacity = 30; +/** + * @zh + * 蒙皮UBO + */ export class UBOSkinning { public static MAT_JOINT_OFFSET: number = 0; public static COUNT: number = JointUniformCapacity * 12; @@ -193,6 +239,10 @@ localBindingsDesc.set(UBOSkinning.BLOCK.name, { blockInfo: UBOSkinning.BLOCK, }); +/** + * @zh + * 蒙皮纹理信息UBO + */ export class UBOSkinningTexture { public static JOINTS_TEXTURE_SIZE_INV_OFFSET: number = 0; public static COUNT: number = 4; @@ -209,6 +259,10 @@ localBindingsDesc.set(UBOSkinningTexture.BLOCK.name, { blockInfo: UBOSkinningTexture.BLOCK, }); +/** + * @zh + * 骨骼纹理采样器 + */ export const UNIFORM_JOINTS_TEXTURE: GFXUniformSampler = { binding: UniformBinding.SAMPLER_JOINTS, name: 'cc_jointsTexture', type: GFXType.SAMPLER2D, count: 1, }; diff --git a/cocos/pipeline/forward/forward-flow.ts b/cocos/pipeline/forward/forward-flow.ts index 82aaf09f6c..de5ec938ed 100644 --- a/cocos/pipeline/forward/forward-flow.ts +++ b/cocos/pipeline/forward/forward-flow.ts @@ -2,16 +2,34 @@ import { IRenderFlowInfo, RenderFlow } from '../render-flow'; import { RenderPipeline } from '../render-pipeline'; import { ForwardStage } from './forward-stage'; +/** + * @zh + * 前向阶段优先级 + */ export enum ForwardStagePriority { FORWARD = 0, } +/** + * @zh + * 前向渲染流程 + */ export class ForwardFlow extends RenderFlow { + /** + * @zh + * 构造函数 + * @param pipeline 渲染管线 + */ constructor (pipeline: RenderPipeline) { super(pipeline); } + /** + * @zh + * 初始化函数 + * @param info 渲染流程描述信息 + */ public initialize (info: IRenderFlowInfo): boolean { if (info.name !== undefined) { @@ -28,10 +46,18 @@ export class ForwardFlow extends RenderFlow { return true; } + /** + * @zh + * 销毁函数 + */ public destroy () { this.destroyStages(); } + /** + * @zh + * 重构函数 + */ public rebuild () { } } diff --git a/cocos/pipeline/forward/forward-pipeline.ts b/cocos/pipeline/forward/forward-pipeline.ts index 162effdb29..e54753ec0b 100644 --- a/cocos/pipeline/forward/forward-pipeline.ts +++ b/cocos/pipeline/forward/forward-pipeline.ts @@ -19,6 +19,10 @@ import { RenderView } from '../render-view'; import { UIFlow } from '../ui/ui-flow'; import { ForwardFlow } from './forward-flow'; +/** + * @zh + * 前向渲染流程优先级 + */ export enum ForwardFlowPriority { FORWARD = 0, UI = 10, @@ -30,18 +34,51 @@ const _tempLightIndex = [] as number[]; const _tempLightDist = [] as number[]; const _tempVec3 = v3(); +/** + * @zh + * 前向渲染管线 + */ export class ForwardPipeline extends RenderPipeline { + /** + * @zh + * 全部光源的UBO结构描述 + */ protected _uboLights: UBOForwardLight = new UBOForwardLight(); + + /** + * @zh + * 全部光源的UBO缓冲 + */ protected _lightsUBO: GFXBuffer | null = null; + + /** + * @zh + * 参与渲染的灯光 + */ private _validLights: Light[]; + + /** + * @zh + * 灯光索引偏移量数组 + */ private _lightIndexOffset: number[]; + + /** + * @zh + * 灯光索引数组 + */ private _lightIndices: number[]; public get lightsUBO (): GFXBuffer { return this._lightsUBO!; } + /** + * @zh + * 构造函数 + * @param root Root类实例 + */ constructor (root: Root) { super(root); this._validLights = []; @@ -49,6 +86,11 @@ export class ForwardPipeline extends RenderPipeline { this._lightIndices = []; } + /** + * @zh + * 初始化函数 + * @param info 渲染管线描述信息 + */ public initialize (info: IRenderPipelineInfo): boolean { if (!this._initialize(info)) { @@ -116,6 +158,10 @@ export class ForwardPipeline extends RenderPipeline { return true; } + /** + * @zh + * 销毁函数 + */ public destroy () { const lightsUBO = this._globalBindings.get(UBOForwardLight.BLOCK.name); if (lightsUBO) { @@ -126,6 +172,10 @@ export class ForwardPipeline extends RenderPipeline { this._destroy(); } + /** + * @zh + * 重构函数 + */ public rebuild () { super.rebuild(); for (const flow of this._flows) { @@ -133,6 +183,10 @@ export class ForwardPipeline extends RenderPipeline { } } + /** + * @zh + * 更新UBO + */ protected updateUBOs (view: RenderView) { super.updateUBOs(view); @@ -222,6 +276,11 @@ export class ForwardPipeline extends RenderPipeline { } + /** + * @zh + * 场景裁剪 + * @param view 渲染视图 + */ protected sceneCulling (view: RenderView) { super.sceneCulling(view); this._validLights.splice(0); @@ -254,6 +313,11 @@ export class ForwardPipeline extends RenderPipeline { } } + /** + * @zh + * 对每个模型裁剪光源 + * @param model 模型 + */ private cullLightPerModel (model: Model) { _tempLightIndex.splice(0); for (let i = 0; i < this._validLights.length; i++) { diff --git a/cocos/pipeline/forward/forward-stage.ts b/cocos/pipeline/forward/forward-stage.ts index a7156c36a5..80c83d4987 100644 --- a/cocos/pipeline/forward/forward-stage.ts +++ b/cocos/pipeline/forward/forward-stage.ts @@ -1,7 +1,5 @@ import { GFXCommandBuffer } from '../../gfx/command-buffer'; import { GFXClearFlag, GFXCommandBufferType, GFXFilter, IGFXColor } from '../../gfx/define'; -import { Model } from '../../renderer'; -import { cullSceneWithDirectionalLight } from '../culling'; import { getPhaseID } from '../pass-phase'; import { SRGBToLinear } from '../pipeline-funcs'; import { RenderFlow } from '../render-flow'; @@ -12,11 +10,20 @@ import { RenderView } from '../render-view'; const colors: IGFXColor[] = []; const bufs: GFXCommandBuffer[] = []; +/** + * @zh + * 前向渲染阶段 + */ export class ForwardStage extends RenderStage { private _opaqueQueue: RenderQueue; private _transparentQueue: RenderQueue; + /** + * @zh + * 构造函数 + * @param flow 渲染阶段 + */ constructor (flow: RenderFlow) { super(flow); @@ -32,6 +39,11 @@ export class ForwardStage extends RenderStage { }); } + /** + * @zh + * 初始化函数 + * @param info 渲染阶段描述信息 + */ public initialize (info: IRenderStageInfo): boolean { if (info.name !== undefined) { @@ -48,6 +60,10 @@ export class ForwardStage extends RenderStage { return true; } + /** + * @zh + * 销毁函数 + */ public destroy () { if (this._cmdBuff) { this._cmdBuff.destroy(); @@ -55,12 +71,27 @@ export class ForwardStage extends RenderStage { } } + /** + * @zh + * 重置大小 + * @param width 屏幕宽度 + * @param height 屏幕高度 + */ public resize (width: number, height: number) { } + /** + * @zh + * 重构函数 + */ public rebuild () { } + /** + * @zh + * 渲染函数 + * @param view 渲染视图 + */ public render (view: RenderView) { this._opaqueQueue.clear(); diff --git a/cocos/pipeline/pipeline-funcs.ts b/cocos/pipeline/pipeline-funcs.ts index e13f1896b0..aff32dc61f 100644 --- a/cocos/pipeline/pipeline-funcs.ts +++ b/cocos/pipeline/pipeline-funcs.ts @@ -1,5 +1,10 @@ import { IGFXColor } from '../gfx/define'; +/** + * @zh + * SRGB颜色空间转换为线性空间 + * @param gamma SRGB颜色空间下的GAMMA值 + */ export function SRGBToLinear (gamma: IGFXColor): IGFXColor { const r = Math.pow(gamma.r, 2.2); const g = Math.pow(gamma.g, 2.2); @@ -7,6 +12,11 @@ export function SRGBToLinear (gamma: IGFXColor): IGFXColor { return { r, g, b, a: 1.0 }; } +/** + * @zh + * 线性空间转换为SRGB颜色空间 + * @param linear 线性空间下的颜色值 + */ export function LinearToSRGB (linear: IGFXColor): IGFXColor { const r = Math.pow(linear.r, 0.454545); const g = Math.pow(linear.g, 0.454545); diff --git a/cocos/pipeline/ppfx/smaa-blend-stage.ts b/cocos/pipeline/ppfx/smaa-blend-stage.ts index 13f2a953fb..c7593ae2be 100644 --- a/cocos/pipeline/ppfx/smaa-blend-stage.ts +++ b/cocos/pipeline/ppfx/smaa-blend-stage.ts @@ -7,6 +7,10 @@ import { RenderFlow } from '../render-flow'; import { IRenderStageInfo, RenderStage } from '../render-stage'; import { RenderView } from '../render-view'; +/** + * @zh + * SMAA混合权重计算渲染阶段 + */ export class SMAABlendStage extends RenderStage { private _areaTexImage = new Image(); diff --git a/cocos/pipeline/ppfx/smaa-edge-stage.ts b/cocos/pipeline/ppfx/smaa-edge-stage.ts index a13c8001d2..87fbfdd1b5 100644 --- a/cocos/pipeline/ppfx/smaa-edge-stage.ts +++ b/cocos/pipeline/ppfx/smaa-edge-stage.ts @@ -5,6 +5,10 @@ import { RenderFlow } from '../render-flow'; import { IRenderStageInfo, RenderStage } from '../render-stage'; import { RenderView } from '../render-view'; +/** + * @zh + * SMAA边缘检测渲染阶段 + */ export class SMAAEdgeStage extends RenderStage { private _hTexSampler: number = 0; diff --git a/cocos/pipeline/ppfx/smaa-flow.ts b/cocos/pipeline/ppfx/smaa-flow.ts index 1189fe7903..0a55ade88d 100644 --- a/cocos/pipeline/ppfx/smaa-flow.ts +++ b/cocos/pipeline/ppfx/smaa-flow.ts @@ -3,6 +3,10 @@ import { RenderPipeline } from '../render-pipeline'; import { SMAABlendStage } from './smaa-blend-stage'; import { SMAAEdgeStage } from './smaa-edge-stage'; +/** + * @zh + * SMAA边缘检测渲染流程 + */ export class SMAAEdgeFlow extends RenderFlow { constructor (pipeline: RenderPipeline) { diff --git a/cocos/pipeline/ppfx/tonemap-flow.ts b/cocos/pipeline/ppfx/tonemap-flow.ts index e64837f676..4babe0cfb8 100644 --- a/cocos/pipeline/ppfx/tonemap-flow.ts +++ b/cocos/pipeline/ppfx/tonemap-flow.ts @@ -2,6 +2,10 @@ import { IRenderFlowInfo, RenderFlow } from '../render-flow'; import { RenderPipeline } from '../render-pipeline'; import { ToneMapStage } from './tonemap-stage'; +/** + * @zh + * 色调映射渲染流程 + */ export class ToneMapFlow extends RenderFlow { constructor (pipeline: RenderPipeline) { diff --git a/cocos/pipeline/ppfx/tonemap-stage.ts b/cocos/pipeline/ppfx/tonemap-stage.ts index 596f7e3859..036f22c075 100644 --- a/cocos/pipeline/ppfx/tonemap-stage.ts +++ b/cocos/pipeline/ppfx/tonemap-stage.ts @@ -5,6 +5,10 @@ import { RenderFlow } from '../render-flow'; import { IRenderStageInfo, RenderStage } from '../render-stage'; import { RenderView } from '../render-view'; +/** + * @zh + * 色调映射渲染阶段 + */ export class ToneMapStage extends RenderStage { private _hTexSampler: number = 0; diff --git a/cocos/pipeline/render-flow.ts b/cocos/pipeline/render-flow.ts index 00c6e55a9d..252c4f2e3c 100644 --- a/cocos/pipeline/render-flow.ts +++ b/cocos/pipeline/render-flow.ts @@ -4,11 +4,19 @@ import { RenderPipeline } from './render-pipeline'; import { IRenderStageInfo, RenderStage } from './render-stage'; import { RenderView } from './render-view'; +/** + * @zh + * 渲染流程描述信息 + */ export interface IRenderFlowInfo { name?: string; priority: number; } +/** + * @zh + * 渲染流程 + */ export abstract class RenderFlow { public get device (): GFXDevice { @@ -35,34 +43,100 @@ export abstract class RenderFlow { return this._material; } + /** + * @zh + * GFX设备 + */ protected _device: GFXDevice; + + /** + * @zh + * 渲染管线 + */ protected _pipeline: RenderPipeline; + + /** + * @zh + * 名称 + */ protected _name: string = ''; + + /** + * @zh + * 优先级 + */ protected _priority: number = 0; + + /** + * @zh + * 渲染阶段数组 + */ protected _stages: RenderStage[] = []; + + /** + * @zh + * 材质 + */ protected _material: Material = new Material(); + /** + * @zh + * 构造函数 + * @param pipeline 渲染管线 + */ constructor (pipeline: RenderPipeline) { this._device = pipeline.device; this._pipeline = pipeline; } + /** + * @zh + * 初始化函数 + * @param info 渲染流程描述信息 + */ public abstract initialize (info: IRenderFlowInfo): boolean; + + /** + * @zh + * 销毁函数 + */ public abstract destroy (); + + /** + * @zh + * 重构函数 + */ public abstract rebuild (); + /** + * @zh + * 重置大小 + * @param width 屏幕宽度 + * @param height 屏幕高度 + */ public resize (width: number, height: number) { for (const stage of this._stages) { stage.resize(width, height); } } + /** + * @zh + * 渲染函数 + * @param view 渲染视图 + */ public render (view: RenderView) { for (const stage of this._stages) { stage.render(view); } } + /** + * @zh + * 创建渲染阶段 + * @param clazz 渲染阶段类 + * @param info 渲染阶段描述信息 + */ public createStage ( clazz: new(flow: RenderFlow) => T, info: IRenderStageInfo): RenderStage | null { @@ -80,6 +154,10 @@ export abstract class RenderFlow { } } + /** + * @zh + * 销毁全部渲染阶段 + */ public destroyStages () { for (const stage of this._stages) { stage.destroy(); diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 654fc80792..c7208d374d 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -15,7 +15,7 @@ import { GFXTextureType, GFXTextureUsageBit, GFXTextureViewType} from '../gfx/define'; -import { GFXDevice, GFXFeature, GFXAPI } from '../gfx/device'; +import { GFXDevice, GFXFeature } from '../gfx/device'; import { GFXFramebuffer } from '../gfx/framebuffer'; import { GFXInputAssembler, IGFXAttribute } from '../gfx/input-assembler'; import { GFXRenderPass } from '../gfx/render-pass'; @@ -35,6 +35,10 @@ const _mat4Array = new Float32Array(16); const _outMat = new Mat4(); const _v3tmp = new Vec3(); +/** + * @zh + * 渲染流程描述信息 + */ export interface IRenderPipelineInfo { enablePostProcess?: boolean; enableHDR?: boolean; @@ -42,6 +46,10 @@ export interface IRenderPipelineInfo { enableSMAA?: boolean; } +/** + * @zh + * 渲染流程 + */ export abstract class RenderPipeline { public get root (): Root { @@ -164,67 +172,332 @@ export abstract class RenderPipeline { return this._defaultUboGlobal!.view; } + /** + * @zh + * Root类对象 + */ protected _root: Root; + + /** + * @zh + * GFX设备 + */ protected _device: GFXDevice; + + /** + * @zh + * 名称 + */ protected _name: string = 'BasePipeline'; + + /** + * @zh + * 渲染对象数组 + */ protected _renderObjects: IRenderObject[] = []; + + /** + * @zh + * 渲染过程数组 + */ protected _renderPasses: Map = new Map(); + + /** + * @zh + * 渲染流程数组 + */ protected _flows: RenderFlow[] = []; + + /** + * @zh + * 是否支持HDR + */ protected _isHDRSupported: boolean = false; + + /** + * @zh + * 是否为HDR管线 + */ protected _isHDR: boolean = false; + + /** + * @zh + * 灯光距离缩放系数(以米为单位) + */ protected _lightMeterScale: number = 10000.0; + + /** + * @zh + * 着色渲染过程 + */ protected _shadingPass: GFXRenderPass | null = null; + + /** + * @zh + * 帧缓冲数量 + */ protected _fboCount: number = 1; + + /** + * @zh + * MSAA着色纹理 + */ protected _msaaShadingTex: GFXTexture | null = null; + + /** + * @zh + * MSAA着色纹理视图 + */ protected _msaaShadingTexView: GFXTextureView | null = null; + + /** + * @zh + * MSAA深度模板纹理 + */ protected _msaaDepthStencilTex: GFXTexture | null = null; + + /** + * @zh + * MSAA深度模板纹理视图 + */ protected _msaaDepthStencilTexView: GFXTextureView | null = null; + + /** + * @zh + * MSAA着色帧缓冲 + */ protected _msaaShadingFBO: GFXFramebuffer | null = null; + /** + * @zh + * 颜色格式 + */ protected _colorFmt: GFXFormat = GFXFormat.UNKNOWN; + + /** + * @zh + * 深度模板格式 + */ protected _depthStencilFmt: GFXFormat = GFXFormat.UNKNOWN; + + /** + * @zh + * 着色纹理数组 + */ protected _shadingTextures: GFXTexture[] = []; + + /** + * @zh + * 着色纹理视图数组 + */ protected _shadingTexViews: GFXTextureView[] = []; + + /** + * @zh + * 深度模板纹理 + */ protected _depthStencilTex: GFXTexture | null = null; + + /** + * @zh + * 深度模板纹理视图 + */ protected _depthStencilTexView: GFXTextureView | null = null; + + /** + * @zh + * 着色帧缓冲数组 + */ protected _shadingFBOs: GFXFramebuffer[] = []; + + /** + * @zh + * 着色尺寸宽度 + */ protected _shadingWidth: number = 0.0; + + /** + * @zh + * 着色尺寸高度 + */ protected _shadingHeight: number = 0.0; + + /** + * @zh + * 着色尺寸缩放 + */ protected _shadingScale: number = 1.0; + + /** + * @zh + * 当前帧缓冲索引 + */ protected _curIdx: number = 0; + + /** + * @zh + * 上一帧缓冲索引 + */ protected _prevIdx: number = 1; + + /** + * @zh + * 启用后期处理 + */ protected _usePostProcess: boolean = false; + + /** + * @zh + * 启用MSAA + */ protected _useMSAA: boolean = false; + + /** + * @zh + * 启用SMAA + */ protected _useSMAA: boolean = false; + + /** + * @zh + * SMAA渲染过程 + */ protected _smaaPass: GFXRenderPass | null = null; + + /** + * @zh + * SMAA边缘帧缓冲 + */ protected _smaaEdgeFBO: GFXFramebuffer | null = null; + + /** + * @zh + * SMAA边缘纹理 + */ protected _smaaEdgeTex: GFXTexture | null = null; + + /** + * @zh + * SMAA边缘纹理视图 + */ protected _smaaEdgeTexView: GFXTextureView | null = null; + + /** + * @zh + * SMAA混合帧缓冲 + */ protected _smaaBlendFBO: GFXFramebuffer | null = null; + + /** + * @zh + * SMAA混合纹理 + */ protected _smaaBlendTex: GFXTexture | null = null; + + /** + * @zh + * SMAA混合纹理视图 + */ protected _smaaBlendTexView: GFXTextureView | null = null; + + /** + * @zh + * 四边形顶点缓冲 + */ protected _quadVB: GFXBuffer | null = null; + + /** + * @zh + * 四边形索引缓冲 + */ protected _quadIB: GFXBuffer | null = null; + + /** + * @zh + * 四边形输入汇集器 + */ protected _quadIA: GFXInputAssembler | null = null; + + /** + * @zh + * 默认的全局UBO + */ protected _defaultUboGlobal: UBOGlobal = new UBOGlobal(); + + /** + * @zh + * 默认的全局绑定表 + */ protected _globalBindings: Map = new Map(); + + /** + * @zh + * 默认纹理 + */ protected _defaultTex: GFXTexture | null = null; + + /** + * @zh + * 默认纹理视图 + */ protected _defaultTexView: GFXTextureView | null = null; + + /** + * @zh + * 浮点精度缩放 + */ protected _fpScale: number = 1.0 / 1024.0; + + /** + * @zh + * 浮点精度缩放的倒数 + */ protected _fpScaleInv: number = 1024.0; + + /** + * @zh + * 管线宏定义 + */ protected _macros: IDefineMap = {}; + /** + * @zh + * 构造函数 + * @param root Root类实例 + */ constructor (root: Root) { this._root = root; this._device = root.device; } + /** + * @zh + * 初始化函数 + * @param info 渲染管线描述信息 + */ public abstract initialize (info: IRenderPipelineInfo): boolean; + + /** + * @zh + * 销毁函数 + */ public abstract destroy (); - public rebuild () { this.updateMacros(); } + /** + * @zh + * 重构函数 + */ + public rebuild () { + this.updateMacros(); + } + + /** + * @zh + * 重置大小 + * @param width 屏幕宽度 + * @param height 屏幕高度 + */ public resize (width: number, height: number) { - const w = Math.floor(width * this._shadingScale); const h = Math.floor(height * this._shadingScale); if (w > this._shadingWidth || @@ -240,6 +513,11 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 渲染函数 + * @param view 渲染视图 + */ public render (view: RenderView) { view.camera.update(); @@ -253,18 +531,33 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 交换帧缓冲 + */ public swapFBOs () { const temp = this._curIdx; this._curIdx = this._prevIdx; this._prevIdx = temp; } + /** + * @zh + * 添加渲染过程 + * @param stage 渲染阶段 + * @param renderPass 渲染过程 + */ public addRenderPass (stage: number, renderPass: GFXRenderPass) { if (renderPass) { this._renderPasses.set(stage, renderPass); } } + /** + * @zh + * 得到指定阶段的渲染过程 + * @param stage 渲染阶段 + */ public getRenderPass (stage: number): GFXRenderPass | null { const renderPass = this._renderPasses.get(stage); if (renderPass) { @@ -274,18 +567,30 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 移除指定阶段的渲染过程 + * @param stage 渲染阶段 + */ public removeRenderPass (stage: number) { this._renderPasses.delete(stage); } + /** + * @zh + * 清空渲染过程 + */ public clearRenderPasses () { this._renderPasses.clear(); } + /** + * @zh + * 创建渲染流程 + */ public createFlow ( clazz: new (pipeline: RenderPipeline) => T, - info: IRenderFlowInfo, - ): RenderFlow | null { + info: IRenderFlowInfo): RenderFlow | null { const flow: RenderFlow = new clazz(this); if (flow.initialize(info)) { this._flows.push(flow); @@ -299,6 +604,10 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 销毁全部渲染流程 + */ public destroyFlows () { for (const flow of this._flows) { flow.destroy(); @@ -306,6 +615,11 @@ export abstract class RenderPipeline { this._flows = []; } + /** + * @zh + * 得到指定名称的渲染流程 + * @param name 名称 + */ public getFlow (name: string): RenderFlow | null { for (const flow of this._flows) { if (flow.name === name) { @@ -316,6 +630,11 @@ export abstract class RenderPipeline { return null; } + /** + * @zh + * 内部初始化函数 + * @param info 渲染流程描述信息 + */ protected _initialize (info: IRenderPipelineInfo): boolean { if (info.enablePostProcess !== undefined) { @@ -563,6 +882,10 @@ export abstract class RenderPipeline { return true; } + /** + * @zh + * 内部销毁函数 + */ protected _destroy () { this.destroyFlows(); @@ -654,6 +977,12 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 重置帧缓冲大小 + * @param width 屏幕宽度 + * @param height 屏幕高度 + */ protected resizeFBOs (width: number, height: number) { this._shadingWidth = width; @@ -743,6 +1072,10 @@ export abstract class RenderPipeline { console.info('Resizing shading fbos: ' + this._shadingWidth + 'x' + this._shadingHeight); } + /** + * @zh + * 更新宏定义 + */ protected updateMacros () { this._macros.CC_USE_HDR = (this._isHDR); programLib.destroyShaderByDefines(this._macros); @@ -751,6 +1084,10 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 创建四边形输入汇集器 + */ protected createQuadInputAssembler (): boolean { // create vertex buffer @@ -815,6 +1152,10 @@ export abstract class RenderPipeline { return true; } + /** + * @zh + * 销毁四边形输入汇集器 + */ protected destroyQuadInputAssembler () { if (this._quadVB) { this._quadVB.destroy(); @@ -832,6 +1173,10 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 创建所有UBO + */ protected createUBOs (): boolean { if (!this._globalBindings.get(UBOGlobal.BLOCK.name)) { const globalUBO = this._root.device.createBuffer({ @@ -872,6 +1217,10 @@ export abstract class RenderPipeline { return true; } + /** + * @zh + * 销毁全部UBO + */ protected destroyUBOs () { const globalUBO = this._globalBindings.get(UBOGlobal.BLOCK.name); if (globalUBO) { @@ -885,6 +1234,11 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 更新指定渲染视图的UBO + * @param view 渲染视图 + */ protected updateUBOs (view: RenderView) { const camera = view.camera; @@ -993,6 +1347,11 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 场景裁剪 + * @param view 渲染视图 + */ protected sceneCulling (view: RenderView) { const camera = view.camera; @@ -1034,6 +1393,12 @@ export abstract class RenderPipeline { } } + /** + * @zh + * 添加可见对象 + * @param model 模型 + * @param camera 相机 + */ protected addVisibleModel (model: Model, camera: Camera) { let depth = 0; if (model.node) { diff --git a/cocos/pipeline/render-queue.ts b/cocos/pipeline/render-queue.ts index f2ce5cd7ce..b0bd67b804 100644 --- a/cocos/pipeline/render-queue.ts +++ b/cocos/pipeline/render-queue.ts @@ -1,13 +1,13 @@ import { CachedArray } from '../core/memop/cached-array'; -import { Vec3 } from '../core/value-types'; -import { vec3 } from '../core/vmath'; import { GFXCommandBuffer } from '../gfx/command-buffer'; -import { Camera } from '../renderer/scene/camera'; -import { Model } from '../renderer/scene/model'; -import { SubModel } from '../renderer/scene/submodel'; import { IRenderObject, IRenderPass, IRenderQueueDesc } from './define'; -// Opaque objects are sorted by depth priority -> depth front to back -> shader id. +/** + * @en + * Comparison sorting function. Opaque objects are sorted by depth priority -> depth front to back -> shader id. + * @zh + * 比较排序函数。不透对象按优先级 -> 深度由前向后 -> ShaderId 顺序排序 + */ export function opaqueCompareFn (a: IRenderPass, b: IRenderPass) { if (a.hash === b.hash) { if (a.depth === b.depth) { @@ -20,7 +20,12 @@ export function opaqueCompareFn (a: IRenderPass, b: IRenderPass) { } } -// Transparent objects are sorted by pass priority -> depth back to front -> shader id. +/** + * @en + * Comparison sorting function. Transparent objects are sorted by pass priority -> depth back to front -> shader id. + * @zh + * 比较排序函数。半透对象按优先级 -> 深度由后向前 -> ShaderId 顺序排序 + */ export function transparentCompareFn (a: IRenderPass, b: IRenderPass) { if (a.hash === b.hash) { if (a.depth === b.depth) { @@ -33,24 +38,60 @@ export function transparentCompareFn (a: IRenderPass, b: IRenderPass) { } } +/** + * @zh + * 渲染队列 + */ export class RenderQueue { + + /** + * @zh + * 基于缓存数组的队列 + */ public queue: CachedArray; + + /** + * @zh + * 基于缓存数组的命令缓冲 + */ public cmdBuffs: CachedArray; + + /** + * @zh + * 命令缓冲数量 + */ public cmdBuffCount: number = 0; + + /** + * @zh + * 渲染队列描述 + */ private _passDesc: IRenderQueueDesc; + /** + * @zh + * 构造函数 + * @param desc 渲染队列描述 + */ constructor (desc: IRenderQueueDesc) { - this._passDesc = desc; this.cmdBuffs = new CachedArray(64); this.queue = new CachedArray(64, this._passDesc.sortFunc); } + /** + * @zh + * 清空渲染队列 + */ public clear () { this.queue.clear(); this.cmdBuffCount = 0; } + /** + * @zh + * 插入渲染过程 + */ public insertRenderPass (renderObj: IRenderObject, modelIdx: number, passIdx: number): boolean { const subModel = renderObj.model.getSubModel(modelIdx); const pass = subModel.passes[passIdx]; @@ -104,6 +145,10 @@ export class RenderQueue { // } // } + /** + * @zh + * 排序渲染队列 + */ public sort () { this.queue.sort(); diff --git a/cocos/pipeline/render-stage.ts b/cocos/pipeline/render-stage.ts index d8c3ebe2e0..8e29d66626 100644 --- a/cocos/pipeline/render-stage.ts +++ b/cocos/pipeline/render-stage.ts @@ -8,12 +8,20 @@ import { RenderFlow } from './render-flow'; import { RenderPipeline } from './render-pipeline'; import { RenderView } from './render-view'; +/** + * @zh + * 渲染阶段描述信息 + */ export interface IRenderStageInfo { name?: string; priority: number; framebuffer?: GFXFramebuffer; } +/** + * @zh + * 渲染阶段 + */ export abstract class RenderStage { public get flow (): RenderFlow { @@ -32,20 +40,89 @@ export abstract class RenderStage { return this._framebuffer; } + /** + * @zh + * 渲染流程 + */ protected _flow: RenderFlow; + + /** + * @zh + * 渲染管线 + */ protected _pipeline: RenderPipeline; + + /** + * @zh + * GFX设备 + */ protected _device: GFXDevice; + + /** + * @zh + * 名称 + */ protected _name: string = ''; + + /** + * @zh + * 优先级 + */ protected _priority: number = 0; + + /** + * @zh + * 渲染流程 + */ protected _framebuffer: GFXFramebuffer | null = null; + + /** + * @zh + * 命令缓冲 + */ protected _cmdBuff: GFXCommandBuffer | null = null; + + /** + * @zh + * 清空颜色数组 + */ protected _clearColors: IGFXColor[]; + + /** + * @zh + * 清空深度 + */ protected _clearDepth: number = 1.0; + + /** + * @zh + * 清空模板 + */ protected _clearStencil: number = 0; + + /** + * @zh + * 渲染区域 + */ protected _renderArea: IGFXRect; + + /** + * @zh + * 着色过程 + */ protected _pass: Pass | null = null; + + /** + * @zh + * GFX管线状态 + */ protected _pso: GFXPipelineState | null = null; + /** + * @zh + * 构造函数 + * @param flow 渲染流程 + */ constructor (flow: RenderFlow) { this._flow = flow; this._pipeline = flow.pipeline; @@ -60,12 +137,44 @@ export abstract class RenderStage { this._renderArea = { x: 0, y: 0, width: 0, height: 0 }; } + /** + * @zh + * 初始化函数 + * @param info 渲染阶段描述信息 + */ public abstract initialize (info: IRenderStageInfo): boolean; + + /** + * @zh + * 销毁函数 + */ public abstract destroy (); + + /** + * @zh + * 渲染函数 + * @param view 渲染视图 + */ public abstract render (view: RenderView); + + /** + * @zh + * 重置大小 + * @param width 屏幕宽度 + * @param height 屏幕高度 + */ public abstract resize (width: number, height: number); + + /** + * @zh + * 重构函数 + */ public abstract rebuild (); + /** + * @zh + * 设置清空颜色 + */ public setClearColor (color: IGFXColor) { if (this._clearColors.length > 0) { this._clearColors[0] = color; @@ -74,18 +183,34 @@ export abstract class RenderStage { } } + /** + * @zh + * 设置清空颜色数组 + */ public setClearColors (colors: IGFXColor[]) { this._clearColors = colors; } + /** + * @zh + * 设置清空深度 + */ public setClearDepth (depth: number) { this._clearDepth = depth; } + /** + * @zh + * 设置清空模板 + */ public setClearStencil (stencil: number) { this._clearStencil = stencil; } + /** + * @zh + * 设置渲染区域 + */ public setRenderArea (width: number, height: number) { this._renderArea.width = width; this._renderArea.height = height; diff --git a/cocos/pipeline/render-view.ts b/cocos/pipeline/render-view.ts index 567dc7c345..258b160e7b 100644 --- a/cocos/pipeline/render-view.ts +++ b/cocos/pipeline/render-view.ts @@ -1,13 +1,20 @@ import { Root } from '../core/root'; -import { GFXFramebuffer } from '../gfx/framebuffer'; import { GFXWindow } from '../gfx/window'; import { Camera } from '../renderer/scene/camera'; import { RenderFlow } from './render-flow'; +/** + * @zh + * 渲染视图优先级 + */ export enum RenderViewPriority { GENERAL = 100, } +/** + * @zh + * 渲染视图描述信息 + */ export interface IRenderViewInfo { camera: Camera; name: string; @@ -16,11 +23,19 @@ export interface IRenderViewInfo { flows?: string[]; } +/** + * @zh + * 渲染目标描述信息 + */ export interface IRenderTargetInfo { width?: number; height?: number; } +/** + * @zh + * 渲染视图 + */ export class RenderView { public get name () { @@ -73,21 +88,76 @@ export class RenderView { root._createViewFun = (_root: Root, _camera: Camera): RenderView => new RenderView(_root, _camera); } + /** + * @zh + * Root类实例 + */ private _root: Root; + + /** + * @zh + * 名称 + */ private _name: string = ''; + + /** + * @zh + * GFX窗口 + */ private _window: GFXWindow | null = null; + + /** + * @zh + * 优先级 + */ private _priority: number = 0; + + /** + * @zh + * 可见性 + */ private _visibility: number = 0; + + /** + * @zh + * 相机 + */ private _camera: Camera; + + /** + * @zh + * 是否启用 + */ private _isEnable: boolean = true; + + /** + * @zh + * 是否是UI视图 + */ private _isUI: boolean = false; + + /** + * @zh + * 渲染流程列表 + */ private _flows: RenderFlow[] = []; + /** + * @zh + * 构造函数 + * @param root Root类实例 + * @param camera 相机 + */ private constructor (root: Root, camera: Camera) { this._root = root; this._camera = camera; } + /** + * @zh + * 初始化函数 + * @param info 渲染视图描述信息 + */ public initialize (info: IRenderViewInfo): boolean { this._name = info.name; @@ -106,11 +176,19 @@ export class RenderView { return true; } + /** + * @zh + * 销毁函数 + */ public destroy () { this._window = null; this._priority = 0; } + /** + * @zh + * 启用该渲染视图 + */ public enable (isEnable: boolean) { this._isEnable = isEnable; } diff --git a/cocos/pipeline/ui/ui-flow.ts b/cocos/pipeline/ui/ui-flow.ts index b6b45ac5f4..b5c886d6e0 100644 --- a/cocos/pipeline/ui/ui-flow.ts +++ b/cocos/pipeline/ui/ui-flow.ts @@ -1,9 +1,13 @@ +import { UBOGlobal } from '../define'; import { IRenderFlowInfo, RenderFlow } from '../render-flow'; import { RenderPipeline } from '../render-pipeline'; -import { UIStage } from './ui-stage'; import { RenderView } from '../render-view'; -import { UBOGlobal } from '../define'; +import { UIStage } from './ui-stage'; +/** + * @zh + * UI渲染流程 + */ export class UIFlow extends RenderFlow { constructor (pipeline: RenderPipeline) { diff --git a/cocos/pipeline/ui/ui-stage.ts b/cocos/pipeline/ui/ui-stage.ts index ab126b3aee..4895790583 100644 --- a/cocos/pipeline/ui/ui-stage.ts +++ b/cocos/pipeline/ui/ui-stage.ts @@ -8,6 +8,10 @@ import { RenderView } from '../render-view'; const bufs: GFXCommandBuffer[] = []; +/** + * @zh + * UI渲阶段 + */ export class UIStage extends RenderStage { private _uiQueue: RenderQueue; diff --git a/cocos/pipeline/utils.ts b/cocos/pipeline/utils.ts index ea1111d546..35848a025e 100644 --- a/cocos/pipeline/utils.ts +++ b/cocos/pipeline/utils.ts @@ -5,15 +5,14 @@ import { Node } from '../scene-graph'; const _temp_vec3_1 = new Vec3(); /** - * !#en + * @en * Conversion of non-UI nodes to UI Node (Local) Space coordinate system. - * !#zh + * @zh * 非 UI 节点转换到 UI 节点(局部) 空间坐标系 - * @method convertToNodeSpaceAR - * @param {CameraComponent} mainCamera - * @param {Vec3} wpos - * @param {Node} uiNode - * @return {Vec3} + * @param mainCamera 主相机 + * @param wpos 世界空间位置 + * @param uiNode UI节点 + * @param out 返回局部坐标 */ export function WorldNode3DToLocalNodeUI (mainCamera: CameraComponent, wpos: Vec3, uiNode: Node, out?: Vec3) { if (!out) { @@ -36,6 +35,15 @@ export function WorldNode3DToLocalNodeUI (mainCamera: CameraComponent, wpos: Vec return out; } +/** + * @en + * Conversion of non-UI nodes to UI Node (World) Space coordinate system. + * @zh + * 非 UI 节点转换到 UI 节点(世界) 空间坐标系 + * @param mainCamera 主相机 + * @param wpos 世界空间位置 + * @param out 返回世界坐标 + */ export function WorldNode3DToWorldNodeUI (mainCamera: CameraComponent, wpos: Vec3, out?: Vec3){ if (!out) { out = new Vec3();