Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to pass ScriptableRenderContext.BeginSubPass data into shader

Discussion in 'General Graphics' started by unity_5w20XO9ra_gm3g, Sep 10, 2021.

  1. unity_5w20XO9ra_gm3g

    unity_5w20XO9ra_gm3g

    Joined:
    Jul 4, 2018
    Posts:
    8
    I've read the docs
    https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.BeginSubPass.html
    But still have no idea how to pass samplers to shader

    var albedo = new AttachmentDescriptor(RenderTextureFormat.ARGB32);
    var depth = new AttachmentDescriptor(RenderTextureFormat.Depth);

    var attachments = new NativeArray<AttachmentDescriptor>(6, Allocator.Temp);
    const int depthIndex = 0, albedoIndex = 1;
    attachments[depthIndex] = depth;
    attachments[albedoIndex] = albedo;

    context.BeginRenderPass(camera.pixelWidth, camera.pixelHeight, 1, attachments, depthIndex);

    ...

    var lightingInputs = new NativeArray<int>(1, Allocator.Temp);
    lightingInputs[0] = albedoIndex;

    context.BeginSubPass(cameraOut, lightingInputs, true);
    // shader have no data


    How to pass albedo AttachmentDescriptor into shader? Should i resolve it into other RT, or is there another way?
    albedo.loadStoreTarget is supposed for that use like
    buffer.SetGlobalTexture("GBuffer0", albedo.loadStoreTarget);
    ?
     
  2. ekakiya

    ekakiya

    Joined:
    Jul 25, 2011
    Posts:
    70
    If you use mobile tile-based GPU with Vulkan or Metal API, you can sample from cbuffer hlslcc_SubpassInput_f_##idx { float4 hlslcc_fbinput_##idx; }. Otherwise, you must fall back to load from TEXTURE2D_FLOAT(_UnityFBInput##idx). Latast SRP may have the LOAD_FRAMEBUFFER_INPUT macro for that.
    The way to output to the on-tile buffer is same as regular MRT.
     
  3. unity_5w20XO9ra_gm3g

    unity_5w20XO9ra_gm3g

    Joined:
    Jul 4, 2018
    Posts:
    8
    Tried to use that in shader

    TEXTURE2D_FLOAT(_UnityFBInput0);
    SAMPLER(sampler_UnityFBInput0);
    TEXTURE2D_FLOAT(_UnityFBInput1);
    SAMPLER(sampler_UnityFBInput1);
    TEXTURE2D_FLOAT(_UnityFBInput2);
    SAMPLER(sampler_UnityFBInput2);
    TEXTURE2D_FLOAT(_UnityFBInput3);
    SAMPLER(sampler_UnityFBInput3);


    But shader decompiler shows that bindings start from 4
    -- Vertex shader for "vulkan":
    Set 2D Texture "_UnityFBInput0" to set: 0, binding: 4, used in: Fragment using sampler in set: 0, binding: 0, used in: Fragment
    Set 2D Texture "_UnityFBInput1" to set: 0, binding: 5, used in: Fragment using sampler in set: 0, binding: 1, used in: Fragment
    Set 2D Texture "_UnityFBInput2" to set: 0, binding: 6, used in: Fragment using sampler in set: 0, binding: 2, used in: Fragment
    Set 2D Texture "_UnityFBInput3" to set: 0, binding: 7, used in: Fragment using sampler in set: 0, binding: 3, used in: Fragment
     
    Last edited: Sep 10, 2021
  4. unity_5w20XO9ra_gm3g

    unity_5w20XO9ra_gm3g

    Joined:
    Jul 4, 2018
    Posts:
    8
    Found working version:
    FRAMEBUFFER_INPUT_FLOAT +
    LOAD_FRAMEBUFFER_INPUT
    Thank you a lot.