Unity SRP渲染流程源码分析
2023-01-28 16:57:25 0 举报
Unity SRP渲染流程源码分析
作者其他创作
大纲/内容
准备一般cull参数cullResults.sceneCullParameters
ExecuteCommandBuffer
是
从Umbra获取一些数据用于阴影裁减
Cull?
instancing?
1
从源码中发现,Hybrid V1的扩展必须要在C++层去做,而且会非常麻烦,比如我想尝试支持rendering layer,就必须做大量改动参见(GetHybridV1InstancingVarMask、AllocAndFillInstancedData、FillInstancedDataT)。而且Unity也不打算继续支持V1了,所以我们的目标是支持V2。
ScriptableRenderLoopDrawDispatch
准备cull参数(light for shadow)SetupShadowCullData
执行CullingGroupCullingGroupManager::Get().CullAndSendEvents
ScriptableRenderLoopJob
HybridV2?
Cull结束
准备要渲染的物体(一个node就是一个renderer)PrepareSceneNodes
这俩压根不走Culling,直接DrawBuffers
WinEditorMain:::WinMainwinutils::UninstallInternalCrashHandler();PlayerLoopController::UpdateSceneIfNeeded()PlayerLoopController::UpdateScene
Umbra OCC?
C#RenderPipeline::InternalRender
CopyActiveReflectionProbes
kRenderCommand_DrawRenderer
主动暴露一些cull结果参数给C#端的SRP使用CullResultsToScriptingData
Editor渲染
填充deviceBatchParams参数 deviceBatchParams.items = batch; deviceBatchParams.itemCount = batchWrite - batch; deviceBatchParams.drawBufferRangesStream = bufferRangesBuffer; deviceBatchParams.builtInSysmemCBuffer = builtInSysmemCBuffer; deviceBatchParams.builtInCBSize = CBSize; deviceBatchParams.realCBSize = m_CurrentBuiltInInfo->constantBufferSize;
Draw Renderers
否
提取裁减结果render,填充到sharedRendererScene中的RenderNodeQueue中ExtractSceneRenderNodeQueue
0
ScriptableRenderContext::Cull(根据需要可以多次调用)
ScriptableBatchRenderer::Flush ->ScriptableBatchRenderer::RenderMultipleMeshes
CullShadowCastersWithoutUmbraThreaded
前后排序在哪?
每一级shadowmap 的cullingBatchRendererGroup::OnBeforeCulling
??CullAllPerObjectLights
一些打断Batch的判断
CullLocalLightsLocalLightShadowCasterCullingCullLocalLightShadowCasters
RenderManager::RenderOffscreenCameras
itemCount>0?
TerrainManager::CullAllTerrains
内置渲染管线
一些初始化、内存分配等deviceBatchParams.instanceItemsPStream = builtinInstanceDatasPtrs;(BRG instance数据)
foreach(BatchInstanceData)-填充batchWrite
DrawShadows
1.Editor下离屏渲染(Camera设置了RT)
使用SRP管线?RenderCamerasWithScriptableRenderLoop
GameView.OnGUI
foreach(renderer in rendererArrays){//将场景中的物体数组分组,每组交由一个job处理,job中处理每一个物体的包围盒和相机frustum的culling。 CullDynamicObjectsJob() { 1,通过一些mask参数快速剔除一些 2,包围盒测试裁减 3,一些比较慢的culling操作放到最后 }}
kRenderCommand_DrawRendererList(对应C#的cmd.DrawRendererList)
RenderManager::RenderCameras
执行SRP渲染管线ScriptableRenderContext::ExtractAndExecuteRenderPipeline
PlayModeView.OnGUI
执行Renders BRG CullBatchRendererGroup::OnBeforeCulling结果存储在results.sceneCullingOutput.batchRendererCullingOutputs
BatchInstanceData
要支持多线程渲染,得保证线程安全,SharedXXX是线程安全的这里就是要把数据填充到线程安全的数据结构中(sharedRendererScene)去,这个数据还是在cull results之中CullResults::GetOrCreateSharedRendererScene()
计算drawBufferRangeCountdrawBufferRangeCount = PrepareDrawRangesbatchWrite->drawBufferRangesToConsume = drawBufferRangeCount;
设置RTkRenderCommand_SetRT
存在terrain?
V2设置HybridV2AllocAndFillInstancedData拷贝hybridMetadataBuffer
foreach(command) =>
ScheduleRenderJobs
ExecuteDrawRenderersCommand
CullDirectionalShadowCastersforeach(dirShadowLightCount)
非方向光BatchRendererGroup::OnBeforeCulling
SrpBatch
设置Textures
RepaintController::RenderPlayModeViewCameras
C#调用C++ Cull接口::CullScriptable(ScriptableCulling.cpp)
如果有必要cull lights && shadow castersCullLights && CullDirectionalShadowCasters
Commands
Container
C#实现具体的Render例如HDRenderPipeline::Render
SRP Batch渲染流程:ScriptableRenderLoopDrawSRPBatcher(ScriptableDrawRenderers.cpp)foreach(ScriptableLoopObjectData)
真正的cull开始了void CullScene(CullResults& results)
EditorGUIUtility.RenderPlayModeViewCamerasInternal
回到C++进行渲染ScriptableRenderContext::ExecuteScriptableRenderLoop()
RenderQueue在哪排序的?
BatchRendererGroup::OnBeforeCulling 是相机culling还是shadow culling取决于被调用的位置,相应的culling matrix和culling planes会被传给用户。
2
ProcessShadowCasterNodeVisibilityAndCullWithoutUmbra
2.Editor下Game窗口刷新
通知如ParticleSystem/LineRenderer/TrailRenderer之类的执行一些回调函数DispatchGeometryJobs
广播裁减完毕通知CullSendEvents-》InvokeOnWillRenderObject
CullStaticSceneWithUmbra
ScriptableLoopObjectData
各种渲染逻辑cmd.DrawXXXcmd.RenderXXX
......
CullDynamicScene
如果有必要,CullReflectionProbesCullReflectionProbes
V1设置AllocAndFillInstancedData
CommandBuffers
目前的instancing基本上就是BRG。
for m_DrawShadowCommands
SetupBatchRendererGroupContext
打包到各平台下的渲染
ScriptableLoopObjectData打包到BatchInstanceData
调用C#端的SRP管线invocation(GetCoreScriptingClasses().doRenderLoop_Internal)
Shader兼容SRP Batch?
填充active lightsCopyActiveLights
同级别排列
??CullPerObjectReflectionProbes
先OffScreen再OnScreenPlayerRender(Player.cpp)
普通渲染流程:ScriptableRenderLoopDraw
阴影投射体裁减foreach(sceneCullParameters.totalRendererListsCount)
Culling
只为为光源执行shadow castersBatchRendererGroup::OnBeforeCulling
PrepareDrawShadowsCommandStep1
Draw Shadows
CullDirectionalShadowCastersJob
for drawShadowData.size()PrepareDrawShadowsCommandStep2
BatchRenderGroup依然是DrawRenderList来驱动。
device.DrawBuffersBatchMode(deviceBatchParams);
C#端Submit例如RenderPipeline::Submit
阴影:Runtime\\Camera\\Shadows.cpp 这里大都是内置管线的代码Runtime\\Graphics\\ScriptableRenderLoop\\ScriptableDrawShadows.cpp 这是SRP的代码
ScriptableBatchRenderer::RenderMultipleMeshes(填充GfxBatchItem,并提交DrawBuffers)
计算staticBatchCountstaticBatchCount = GetNumberOfConsecutiveStaticBatches
收藏
0 条评论
下一页