Forward模式下,当使用CommandBuffer来实现后处理的时候,发现cb.Blit(BuiltinRenderTextureType.CameraTarget,...)无效,在FrameDebug中看到传入的_MainTex为Whilte,(在Deferred渲染模式下有效)
原因:
在Forward模式下,默认相机是渲染到BackBuffer,而在渲染的时候,无法直接读取BackBuffer(Deffered模式下有GBuffer,所以没有问题),解决办法,手动Grab一次(调用cb.Blit指定入材质即可,有额外性能消耗),或者相机开启forceIntoRenderTexture。(注:必须5.6以后才生效,如果开启了HDR或者使用了OnRenderImage则不需要开启(5.6以上))
类似问题:
So it seems that when OnRenderImage is being used, or when HDR rendering is on, or when anything that needs the camera to render to an intermediate buffer is enabled, the camera automatically renders to an intermediate buffer instead of the backbuffer (screen) directly. This is nice.
However when Unity cannot predict that the camera is going to need an intermediate buffer, and your command buffer does a Blit(), things go wrong: since Unity cannot know in advance what you're doing in the CB, it grabs the image from the back buffer again and the Blit() (which does not know where the input is coming from) flips it.
The issue can be fixed by enabling the camera's "forceIntoRenderTexture". This forces Unity to quit rendering to the back buffer directly and always render the camera to an intermediate buffer (regardless of HDR state, whether or not there are image effects present, etc.)
参考
https://forum.unity3d.com/threads/commandbuffer-rendering-scene-flipped-upside-down-in-forward-rendering.415922/
https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetGlobalTexture.html
注:使用CommandBuffer.Blit的时候,_MainTex的传入需要手动调用CommandBuffer.SetGlobalTexture,并且Shader中的_MainTex属性设置成从外部设置(不暴露)。