happy-turtle / oit-unity

Order-independent Transparency Implementation in Unity with Per-Pixel Linked Lists
Apache License 2.0
181 stars 25 forks source link

in Postprocess mode, the GPU crashes steadily #33

Open SnowWindSaveYou opened 1 month ago

SnowWindSaveYou commented 1 month ago

cq0rJKRSkO

After examining the issue, it turns out that the main cause is the failure to bind the new buffer to the clearStartOffsetBufferKernel after reallocating the buffer due to a change in screen size.

This issue can be simply resolved by binding the buffer before dispatching the kernel:

            //reset StartOffsetBuffer to zeros
            oitComputeUtils.SetInt("screenWidth", screenWidth);
            oitComputeUtils.SetBuffer(clearStartOffsetBufferKernel, startOffsetBufferId, startOffsetBuffer);
            oitComputeUtils.Dispatch(clearStartOffsetBufferKernel, dispatchGroupSizeX, dispatchGroupSizeY, 1);

And, when I investigated this issue, I also discovered that the Screen.width value retrieved here changes frequently, because this variable refers to the current window , which could be the preview window, the editor window, and the game window. It might be a better approach to bind an independent buffer for each camera?

Additionally, switching windows can causes the OIT effect to disappear, and I have not yet found the reason for this.

happy-turtle commented 1 month ago

Thank you for investigating! Those are some really good points. I agree, setting the buffer size based on the screen size is not ideal. We should instead set it based on the current camera buffer size. I already have an idea on how to implement this for all three pipelines, but it will require some refactoring and the extension of IOrderIndependentTransparency to include the width and height of the texture. Maybe similar to this:

interface IOrderIndependentTransparency
{
  void PreRender(CommandBuffer command, int width, int height);
}

Though I wonder if we need more flexibility here and pass a RTHandle or something similar. Anyways, I will try and set up a pull request. 😊 But feel free to start one as well if you are up for it.

happy-turtle commented 1 month ago

I tried to work on it at https://github.com/happy-turtle/oit-unity/pull/34, but it didn't quite fix the issue for me. Using the camera parameters is fine for URP and HDRP. But I can't get it to work with the Built-in pipeline. It works if only the scene view or only the game view is visible. But it breaks once multiple views are active at the same time.

happy-turtle commented 3 days ago

Alright, I can now offer a solution for the first issue, the frequent crashes you mentioned, with #40. Regarding the effect often disappearing, I am still a bit clueless as well. I guess something with the effect initalization and cleanup order is not correct...