Page 1 of 1

Unity error exit code after rendering View to RenderTexture

Posted: 25 Apr 2024, 09:16
by kristoffer
Hello!

Windows 11, Unity 2022.3.25 and Noesis 3.2.3

I have code pretty much identical to NoesisXamlEditor.RenderPreview(...).
When that code runs I get the correct result in the produced RenderTexture but it seems to leave Unity in a strange state because after this, when Unity exits, the return code is -1073741819.
And since I'm checking the exit code after automated tests this causes a problem.

It seems to be when Graphics.ExecuteCommandBuffer(_commands) runs that the problem occurs, so I'm thinking something goes wrong in the rendering part of Unity, which I know very little about. No errors are logged in the regular Unity log, perhaps there is a way to dig deeper into the rendering mechanism?

To be clear this all about the Unity Editor and not about any built artifacts, which is not relevant for my case.

Thanks for any input on the subject.

Re: Unity error exit code after rendering View to RenderTexture

Posted: 25 Apr 2024, 12:03
by kristoffer
Seems the error code sometimes can be -1073740791 also, depending on the View sent in.

Re: Unity error exit code after rendering View to RenderTexture

Posted: 25 Apr 2024, 19:48
by jsantos
Are you getting this problem in both D3D11 and D3D12 renderers? Just commenting the Graphics.ExecuteCommandBuffer line fixes the issue? Could you please paste the relevant code here?

Re: Unity error exit code after rendering View to RenderTexture

Posted: 26 Apr 2024, 09:41
by kristoffer
I think I've figured it out.

The code used to look like this
private RenderTexture RenderPreview(View view, int width, int height)
{
    if (view?.Content == null)
        return null;

    try
    {
        NoesisRenderer.SetRenderSettings();

        view.SetSize(width, height);
        view.Update(0.0);

        NoesisRenderer.RegisterView(view, _commands);
        NoesisRenderer.UpdateRenderTree(view, _commands);
        NoesisRenderer.RenderOffscreen(view, _commands, false);

        var rt = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Default, 8);
        _commands.SetRenderTarget(rt);
        _commands.ClearRenderTarget(true, true, UnityEngine.Color.clear, 0.0f);

        NoesisRenderer.RenderOnscreen(view, false, _commands, true);
 
        UnityEngine.Graphics.ExecuteCommandBuffer(_commands);
        _commands.Clear();

        RenderTexture.ReleaseTemporary(rt);
        return rt;
    }
    catch (System.Exception e)
    {
        Debug.LogException(e);
    }

    return null;
}
Adding a call to
NoesisRenderer.UnregisterView(view, _commands)
just before the call to
UnityEngine.Graphics.ExecuteCommandBuffer(_commands)
resolved the issue.

This leads me to believe that you have the same issue in NoesisXamlEditor.RenderPreview(...) where NoesisRenderer.RenderOnscreen(...) is called before hitting NoesisRenderer.UnregisterView(...).

Re: Unity error exit code after rendering View to RenderTexture

Posted: 29 Apr 2024, 15:18
by jsantos
I am not sure your fix makes sense. For example, NoesisXamlEditor is already unregistering views in OnDisable() method.

Unregistering a view before rendering with it seems suspicious to me.

Re: Unity error exit code after rendering View to RenderTexture

Posted: 29 Apr 2024, 19:16
by kristoffer
Sure, but in NoesisXamlEditor the view is unregistered after ExecuteCommandBuffer(...), which appears to have been causing issues for me (in my own implementation above at least).

Again, I don't know a whole lot of how rendering works so I'm not saying this is the right solution to the problem, but it worked like a charm.

I too thought unregistering before rendering seemed strange, and the only reason I tried was on a whim that since the _commands field is passed into UnregisterView(...) it maybe queues up additional commands for that unregistering action and since, I assumed, _commands is executed in ExecuteCommandBuffer(...) I thought it could make sense to fully populate it before sending it off.

Re: Unity error exit code after rendering View to RenderTexture

Posted: 01 May 2024, 11:06
by jsantos
Could you please create a ticket about this? I will find time to investigate what's going on.

Re: Unity error exit code after rendering View to RenderTexture

Posted: 02 May 2024, 10:20
by kristoffer