master_commandbuffer分支
f3608cd94cc9302a12c272496408a439b6001c3c
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using SRP;
public class SRPRenderPipeline : RenderPipeline
{
private ForwardRender m_forwardReneder;
private CommandBuffer m_commandBuffer = new CommandBuffer { name = "mainxxxx" };
private int m_shadowMap = Shader.PropertyToID("_CameraColorTexture");
public SRPRenderPipeline()
{
m_forwardReneder = new ForwardRender();
}
protected override void Render(ScriptableRenderContext context, Camera[] cameras)
{
for (int i = 0; i < cameras.Length; ++i)
{
RenderSingleCamera(context, cameras[i]);
}
}
private void RenderSingleCamera(ScriptableRenderContext context, Camera camera)
{
CameraData cameraData = new CameraData();
cameraData.m_camera = camera;
RenderData renderData = new RenderData();
renderData.m_cameraData = cameraData;
if (!camera.TryGetCullingParameters(out ScriptableCullingParameters cullingParameters))
{
return;
}
CullingResults cullingResults = context.Cull(ref cullingParameters);
renderData.m_cullingResults = cullingResults;
context.SetupCameraProperties(camera);
m_commandBuffer.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
m_commandBuffer.GetTemporaryRT(m_shadowMap, Screen.width, Screen.height, 32, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
m_commandBuffer.SetRenderTarget(m_shadowMap, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
//m_commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
m_commandBuffer.ClearRenderTarget(true, true, Color.white, 1.0f);
context.ExecuteCommandBuffer(m_commandBuffer);
m_commandBuffer.Clear();
m_forwardReneder.Clear();
m_forwardReneder.Init(context, ref renderData);
m_forwardReneder.Execute(context, ref renderData);
m_commandBuffer.Blit(m_shadowMap, BuiltinRenderTextureType.CameraTarget);
context.ExecuteCommandBuffer(m_commandBuffer);
m_commandBuffer.Clear();
context.Submit();
}
}
使用commandbuffer的步骤:
1、创建private CommandBuffer m_commandBuffer = new CommandBuffer { name = “mainxxxx” };
2、设置矩阵CommandBuffer.SetViewProjectionMatrices
3、申请rt:CommandBuffer.GetTemporaryRT
4、设置rt为当前画布:CommandBuffer.SetRenderTarget
5、清空当前画布:CommandBuffer.ClearRenderTarget
6、开始执行:ScriptableRenderContext.ExecuteCommandBuffer
7、清空命令堆栈:CommandBuffer.Clear
8、让各个pass进行绘制,此时rt为激活的画布,pass的绘制内容都写到了rt上了
9、添加blit命令给commandbuffer:CommandBuffer.Blit
10、再次执行命令:ScriptableRenderContext.ExecuteCommandBuffer
11、清空命令堆栈:CommandBuffer.Clear为下一帧做准备
这就是整个执行过程:
补充,在没有blit之前,会有报错:
Dimensions of color surface does not match dimensions of depth surface
参考解决网址:
https://blog.csdn.net/wodownload2/article/details/107424488