Version: Unity 6.0 (6000.0)
言語 : 日本語
Optimize a render graph
URP の Render Graph Viewer ウィンドウのリファレンス

レンダーグラフのレンダーパスに互換性モード API を使用する

レンダーグラフ AddUnsafePass API を使用して、レンダーグラフシステムで SetRenderTarget などの互換性モード API を使用できます。

AddUnsafePass API を使用する場合は、以下が適用されます。

  • RecordRenderGraph メソッドで SetRenderAttachment メソッドを使用できません。代わりに SetRenderFunc メソッドで SetRenderTarget を使用します。
  • レンダーパスを最適化できないため、レンダリングが遅くなる場合があります。例えば、レンダーパスがアクティブなカラーバッファに書き込む場合、後で使用するレンダーパスが同じバッファに書き込むかどうかを URP は検出できません。その結果、URP は 2 つのレンダーパスをマージできず、GPU はバッファを不必要にメモリ内外に転送することになります。

アンセーフレンダーパスの作成

アンセーフレンダーパスを作成するには、以下の手順に従います。

  1. RecordRenderGraph メソッドで、AddRasterRenderPass メソッドの代わりに AddUnsafePass メソッドを使用します。

    例:

    using (var builder = renderGraph.AddUnsafePass<PassData>("My unsafe render pass", out var passData))
    
  2. SetRenderFunc メソッドを呼び出すときは、RasterGraphContext ではなくUnsafeGraphContext 型を使用します。

    例:

    builder.SetRenderFunc(
        (PassData passData, UnsafeGraphContext context) => ExecutePass(passData, context)
    );
    
  3. レンダーパスがテクスチャに書き込む場合は、そのテクスチャをパスデータクラスとして追加する必要があります。

    例:

    private class PassData
    {
        internal TextureHandle textureToWriteTo;
    }
    
  4. レンダーパスがテクスチャに書き込む場合は、そのテクスチャも 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 を参照してください。

Optimize a render graph
URP の Render Graph Viewer ウィンドウのリファレンス
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961