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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bug Latest URP forward + sort process

Discussion in 'Universal Render Pipeline' started by unity_7024A8A4ADC4FA396E4B, Jul 27, 2022.

  1. unity_7024A8A4ADC4FA396E4B

    unity_7024A8A4ADC4FA396E4B

    Joined:
    Dec 17, 2021
    Posts:
    7
    I found it when merging the latest repository because the forward + process is not working in unity2021.3

    https://github.com/Unity-Technologi...elines.universal/Runtime/Tiling/ReorderJob.cs

    Probably this is correct

    //var newIndex = indices[index];
    //output[newIndex] = input[index];

    var fromIndex = indices[index];
    output[index] = input[fromIndex];

    About Minor glitches that have not been reflected in unity yet,
    May I report on the forum?


    (Sorry, I'm using machine translation.)
     
  2. peterbay

    peterbay

    Unity Technologies

    Joined:
    Nov 2, 2017
    Posts:
    100
    Which issues are you seeing? And do these changes fix those issues? Note that URP Forward+ in 2021.3 has some bugs that have been fixed in later versions, so maybe what you're seeing is caused by something else. These fixes are not being backported because the feature was experimental and hidden in those versions.

    Regarding the original code, `indices[index]` contains the index that the value at `index` should move to. Given that, we want to read the value at `index`, and then write it to the index given by `indices[index]`, so the original code should be correct.
     
  3. unity_7024A8A4ADC4FA396E4B

    unity_7024A8A4ADC4FA396E4B

    Joined:
    Dec 17, 2021
    Posts:
    7

    The Forward + bug in unity2021.3 was due to an indefinite memory reference and was fixed in the latest version.

    The keyword is around here
    CBuffer SetGlobalConstant ComputeBuffer Dynamic NativeArray TempJob

    (In the latest, it became Graphic Buffer and the Dynamic setting disappeared.)



    Write the sort job flow below
    -----------------------

    Example.

    1. ForwardLights.cs

    light: [lightA, lightB, lightC, ]
    key = [10, 20, 5, ]
    indices = [?, ?, ?, ]

    2. RadixSortJob

    1.
    light: [lightA, lightB, lightC, ]
    key = [10, 20, 5, ]
    indices = [0, 1, 2, ]


    2.
    light: [lightC, lightA, lightB, ]
    key = [5, 10, 20, ]
    indices = [2, 0, 1, ]


    3. ReorderJob.cs

    index : [0, 1, 2,]
    indices = [2, 0, 1,]

    ----------------
    var newIndex = indices[index];
    output[newIndex] = input[index];

    input: [lightA, lightB, lightC, ] key: [10, 20, 5, ]
    output: [lightB, lightC, lightA, ] key: [20, 5, 10, ]


    ----------------
    var fromIndex = indices[index];
    output[index] = input[fromIndex];

    input: [lightA, lightB, lightC, ] key: [10, 20, 5, ]
    output: [lightC, lightA, lightB, ] key: [5, 10, 20, ]


    Reference:

    -------------------------
    ForwardLights.cs(https://github.com/Unity-Technologi...-pipelines.universal/Runtime/ForwardLights.cs(

    var indices = new NativeArray<int>(lightCount * 2, Allocator.TempJob);
    var radixSortJob = new RadixSortJob
    {
    // Floats can be sorted bitwise with no special handling if positive floats only
    keys = meanZs.Reinterpret<uint>(),
    indices = indices
    };

    var zSortHandle = radixSortJob.Schedule(minMaxZHandle);

    var reorderedLights = new NativeArray<VisibleLight>(lightCount, Allocator.TempJob);
    var reorderedMinMaxZs = new NativeArray<LightMinMaxZ>(lightCount, Allocator.TempJob);

    var reorderLightsJob = new ReorderJob<VisibleLight> { indices = indices, input = visibleLights, output = reorderedLights };
    var reorderLightsHandle = reorderLightsJob.ScheduleParallel(lightCount, 32, zSortHandle);

    var reorderMinMaxZsJob = new ReorderJob<LightMinMaxZ> { indices = indices, input = minMaxZs, output = reorderedMinMaxZs };
    var reorderMinMaxZsHandle = reorderMinMaxZsJob.ScheduleParallel(lightCount, 32, zSortHandle);


    -------------------------


    RadixSortJob.cs (https://github.com/Unity-Technologi...ines.universal/Runtime/Tiling/RadixSortJob.cs)

    public void Execute()
    {
    ...
    for (var j = n - 1; j >= 0; j--)
    {
    var key = keys[currentOffset + j];
    var bucket = (key >> (8 * i)) & 0xFF;
    var newIndex = counts[(int)bucket] - 1;
    counts[(int)bucket]--;
    keys[nextOffset + newIndex] = key;
    indices[nextOffset + newIndex] = indices[currentOffset + j];
    }
    ...
    }

    -------------------------

    ReorderJob.cs(https://github.com/Unity-Technologi...elines.universal/Runtime/Tiling/ReorderJob.cs)

    public void Execute(int index)
    {
    var newIndex = indices[index];
    output[newIndex] = input[index];
    }

    -------------------------
     
    peterbay likes this.
  4. peterbay

    peterbay

    Unity Technologies

    Joined:
    Nov 2, 2017
    Posts:
    100
    Oh wow, you are absolutely right about that. Can't believe I missed that. Thank you very much for reporting this :) The fix should make it into the 2022.2 release.