レンダーグラフ AddUnsafePass API を使用して、レンダーグラフシステムで SetRenderTarget などの互換性モード API を使用できます。
AddUnsafePass API を使用する場合は、以下が適用されます。
RecordRenderGraph メソッドで SetRenderAttachment メソッドを使用できません。代わりに SetRenderFunc メソッドで SetRenderTarget を使用します。アンセーフレンダーパスを作成するには、以下の手順に従います。
RecordRenderGraph メソッドで、AddRasterRenderPass メソッドの代わりに AddUnsafePass メソッドを使用します。
例:
using (var builder = renderGraph.AddUnsafePass<PassData>("My unsafe render pass", out var passData))
SetRenderFunc メソッドを呼び出すときは、RasterGraphContext ではなくUnsafeGraphContext 型を使用します。
例:
builder.SetRenderFunc(
(PassData passData, UnsafeGraphContext context) => ExecutePass(passData, context)
);
レンダーパスがテクスチャに書き込む場合は、そのテクスチャをパスデータクラスとして追加する必要があります。
例:
private class PassData
{
internal TextureHandle textureToWriteTo;
}
レンダーパスがテクスチャに書き込む場合は、そのテクスチャも UseTexture メソッドを使用して書き込み可能に設定する必要があります。
例:
builder.UseTexture(passData.textureToWriteTo, AccessFlags.Write);
これで、SetRenderFunc メソッドで互換性モード API を使用できるようになりました。
以下の例では、互換性モード SetRenderTarget API を使用して、レンダーパスでレンダーターゲットをアクティブなカラーバッファに設定し、表面の法線を色として使用してオブジェクトを描画します。
using UnityEngine;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class DrawNormalsToActiveColorTexture : ScriptableRendererFeature
{
DrawNormalsPass unsafePass;
public override void Create()
{
unsafePass = new DrawNormalsPass();
unsafePass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(unsafePass);
}
class DrawNormalsPass : ScriptableRenderPass
{
private class PassData
{
internal TextureHandle activeColorBuffer;
internal TextureHandle cameraNormalsTexture;
}
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
{
using (var builder = renderGraph.AddUnsafePass<PassData>("Draw normals", out var passData))
{
// Make sure URP generates the normals texture
ConfigureInput(ScriptableRenderPassInput.Normal);
// Get the frame data
UniversalResourceData resourceData = frameContext.Get<UniversalResourceData>();
// Add the active color buffer to our pass data, and set it as writeable
passData.activeColorBuffer = resourceData.activeColorTexture;
builder.UseTexture(passData.activeColorBuffer, AccessFlags.Write);
// Add the camera normals texture to our pass data
passData.cameraNormalsTexture = resourceData.cameraNormalsTexture;
builder.UseTexture(passData.cameraNormalsTexture);
builder.AllowPassCulling(false);
builder.SetRenderFunc((PassData data, UnsafeGraphContext context) => ExecutePass(data, context));
}
}
static void ExecutePass(PassData passData, UnsafeGraphContext context)
{
// Create a command buffer for a list of rendering methods
CommandBuffer unsafeCommandBuffer = CommandBufferHelpers.GetNativeCommandBuffer(context.cmd);
// Add a command to set the render target to the active color buffer so URP draws to it
context.cmd.SetRenderTarget(passData.activeColorBuffer);
// Add a command to copy the camera normals texture to the render target
Blitter.BlitTexture(unsafeCommandBuffer, passData.cameraNormalsTexture, new Vector4(1, 1, 0, 0), 0, false);
}
}
}
別の例については、ユニバーサルレンダーパイプライン (URP) パッケージサンプル の UnsafePass を参照してください。