このページでは、レンダーグラフシステムを使用してレンダーパスを記述する方法について説明します。
説明内容を図示するために、このページではカメラのアクティブなカラーテクスチャを宛先テクスチャにコピーするレンダーパスの例を使用しています。コードを簡略化するために、この例ではフレーム内の他の場所で宛先テクスチャを使用していません。フレームデバッガーを使用して、コンテンツを検査できます。
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 メソッドを使用して、レンダーグラフシステムに 1 つ以上のレンダーパスを追加して設定します。
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 メソッドは、このレンダーパスが一時的な宛先テクスチャを色のレンダーターゲットとして使用することを宣言します。この宣言は互換性モードで使用できる cmd.SetRenderTarget API と類似しています (レンダーグラフ 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));
}
}
スクリプタブルレンダーパスインスタンスをレンダラーに挿入するには、Renderer 機能実装の AddRenderPasses メソッドを使用します。URP はフレームごとに、各カメラに対して 1 回、AddRenderPasses メソッドを呼び出します。
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(m_CopyRenderPass);
}