이 페이지에서는 렌더 그래프 시스템을 사용하여 렌더 패스를 작성하는 방법을 설명합니다.
이 페이지에서는 설명을 위해 카메라의 활성 컬러 텍스처를 대상 텍스처로 복사하는 렌더 패스 예시를 사용합니다. 코드를 간소화하기 위해 이 예시에서는 프레임의 다른 위치에서 대상 텍스처를 사용하지 않습니다. 프레임 디버거 사용하여 콘텐츠를 검사할 수 있습니다.
렌더 패스를 ScriptableRenderPass 클래스에서 상속하는 클래스로 선언합니다.
렌더 패스 내에서 렌더 패스가 사용하는 리소스를 포함하는 클래스를 선언합니다.
리소스는 일반 C# 변수와 렌더 그래프 리소스 레퍼런스가 될 수 있습니다. 렌더 그래프 시스템은 렌더링 코드 실행 중에 이 데이터 구조에 액세스할 수 있습니다. 렌더 패스가 사용하는 변수만 선언해야 합니다. 불필요한 변수를 추가하면 성능이 저하될 수 있습니다.
class PassData
{
internal TextureHandle copySourceTexture;
}
RecordRenderGraph 메서드는 데이터를 채우며 렌더 그래프는 이를 렌더링 함수에 파라미터로 전달합니다.
렌더 패스에 대한 렌더링 커맨드를 생성하는 렌더링 함수를 선언합니다. 또한 이 예시에서 RecordRenderGraph 메서드는 SetRenderFunc 메서드를 사용하여 함수를 사용하도록 렌더 그래프에 지시합니다.
static void ExecutePass(PassData data, RasterGraphContext context)
{
// Records a rendering command to copy, or blit, the contents of the source texture
// to the color render target of the render pass.
// The RecordRenderGraph method sets the destination texture as the render target
// with the UseTextureFragment method.
Blitter.BlitTexture(context.cmd, data.copySourceTexture,
new Vector4(1, 1, 0, 0), 0, false);
}
RecordRenderGraph 메서드를 사용하여 렌더 그래프 시스템에서 하나 이상의 렌더 패스를 추가하고 설정합니다.
Unity는 렌더 그래프 설정 단계에서 이 메서드를 호출하며, 렌더 그래프 실행과 관련된 패스와 리소스를 등록할 수 있게 합니다. 이 메서드를 사용하여 커스텀 렌더링을 구현합니다.
RecordRenderGraph 메서드에서는 렌더 패스 입력과 출력을 선언하되, 커맨드 버퍼에 커맨드를 추가하지 마십시오.
다음 섹션에서는 RecordRenderGraph 메서드의 주요 요소와 구현 예시를 제시합니다.
builder 변수는 IRasterRenderGraphBuilder 인터페이스의 인스턴스입니다. 이 변수는 렌더 패스와 관련된 정보를 설정하기 위한 엔트리 포인트입니다.
UniversalResourceData 클래스에는 카메라의 활성 컬러 및 뎁스 텍스처를 비롯해 URP에서 사용하는 모든 텍스처 리소스가 포함됩니다.
UniversalCameraData 클래스에는 현재 활성화된 카메라와 관련된 데이터가 포함되어 있습니다.
설명을 위해 이 샘플은 임시 대상 텍스처를 생성합니다. UniversalRenderer.CreateRenderGraphTexture는 RenderGraph.CreateTexture 메서드를 호출하는 헬퍼 메서드입니다.
TextureHandle destination =
UniversalRenderer.CreateRenderGraphTexture(renderGraph, desc, "CopyTexture", false);
builder.UseTexture 메서드는 이 렌더 패스가 소스 텍스처를 읽기 전용 입력으로 사용한다고 선언합니다.
builder.UseTexture(passData.copySourceTexture);
이 예시에서 builder.SetRenderAttachment 메서드는 이 렌더 패스가 임시 대상 텍스처를 자신의 컬러 렌더 타겟으로 사용한다고 선언합니다. 이 선언은 호환성 모드(렌더 그래프 API 없이)에서 사용할 수 있는 cmd.SetRenderTarget API와 유사합니다.
SetRenderFunc 메서드는 ExecutePass 메서드를 렌더 패스를 실행할 때 렌더 그래프가 호출하는 렌더링 함수로 설정합니다. 이 샘플은 람다 표현식을 사용하여 메모리 할당을 방지합니다.
builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context));
RecordRenderGraph 메서드의 전체 예시:
// This method adds and configures one or more render passes in the render graph.
// This process includes declaring their inputs and outputs,
// but does not include adding commands to command buffers.
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
{
string passName = "Copy To Debug Texture";
// Add a raster render pass to the render graph. The PassData type parameter determines
// the type of the passData output variable.
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName,
out var passData))
{
// UniversalResourceData contains all the texture references used by URP,
// including the active color and depth textures of the camera.
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
// Populate passData with the data needed by the rendering function
// of the render pass.
// Use the camera's active color texture
// as the source texture for the copy operation.
passData.copySourceTexture = resourceData.activeColorTexture;
// Create a destination texture for the copy operation based on the settings,
// such as dimensions, of the textures that the camera uses.
// Set msaaSamples to 1 to get a non-multisampled destination texture.
// Set depthBufferBits to 0 to ensure that the CreateRenderGraphTexture method
// creates a color texture and not a depth texture.
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
RenderTextureDescriptor desc = cameraData.cameraTargetDescriptor;
desc.msaaSamples = 1;
desc.depthBufferBits = 0;
// For demonstrative purposes, this sample creates a temporary destination texture.
// UniversalRenderer.CreateRenderGraphTexture is a helper method
// that calls the RenderGraph.CreateTexture method.
// Using a RenderTextureDescriptor instance instead of a TextureDesc instance
// simplifies your code.
TextureHandle destination =
UniversalRenderer.CreateRenderGraphTexture(renderGraph, desc,
"CopyTexture", false);
// Declare that this render pass uses the source texture as a read-only input.
builder.UseTexture(passData.copySourceTexture);
// Declare that this render pass uses the temporary destination texture
// as its color render target.
// This is similar to cmd.SetRenderTarget prior to the RenderGraph API.
builder.SetRenderAttachment(destination, 0);
// RenderGraph automatically determines that it can remove this render pass
// because its results, which are stored in the temporary destination texture,
// are not used by other passes.
// For demonstrative purposes, this sample turns off this behavior to make sure
// that render graph executes the render pass.
builder.AllowPassCulling(false);
// Set the ExecutePass method as the rendering function that render graph calls
// for the render pass.
// This sample uses a lambda expression to avoid memory allocations.
builder.SetRenderFunc((PassData data, RasterGraphContext context)
=> ExecutePass(data, context));
}
}
스크립터블 렌더 패스 인스턴스를 렌더러에 삽입하려면 렌더러 기능 구현에서 AddRenderPasses 메서드를 사용하십시오. URP는 매 프레임에 각 카메라에 대해 한 번씩 AddRenderPasses 메서드를 호출합니다.
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(m_CopyRenderPass);
}