OSVR / OSVR-Unity-Rendering

Rendering plugin for Unity
Apache License 2.0
34 stars 13 forks source link

Add ATW support #15

Closed DuFF14 closed 8 years ago

DuFF14 commented 8 years ago

Now that RenderManager supports Asynchronous Timewarp, support needs to be added to the Unity Rendering Plugin before it can be used from Unity. This should involve adding an extra set of RenderTextures to the plugin and Unity, switching between them each frame.

DuFF14 commented 8 years ago

Unity crashes when the CreateTexture2D call is made (unhandled exception, access violation). https://github.com/OSVR/OSVR-Unity-Rendering/blob/atwSupport/OsvrRenderingPlugin.cpp#L450

Not yet able to get much more info than this, as its crashing in a d3d11 function. Everything going into that call looks OK, however. @JeroMiya @russell-taylor any ideas what could be going wrong here?

russell-taylor commented 8 years ago

If you leave the flag that makes the keyed semaphore off, does that fix things (well, it won't let ATW work, but...). I'm trying to figure out whether the problem is with your double-buffering or with this ne flag. You should be able to get double-buffering to work without ATW by leaving this flag out and get everything to work like it did before.

DuFF14 commented 8 years ago

No, it fails in the same place with that line commented out or not.

DuFF14 commented 8 years ago

As I mentioned in our chat, the "frame" variable passed into ConstructBuffersD3D11 is always garbage when I inspect in VS2013 debugger. But as you pointed out, that could be the debugger lying.

russell-taylor commented 8 years ago

Yeah, frame is almost certainly elided by the compiler unless you are compiling in debug mode, no matter where it is declared. I'd assume that is working and continue chasing elsewhere. Like making sure the relevant buffers have been allocated so that ConstructBuffersD3D11() doesn't write past the end of an array.

For hunting the crash in CreateTexture2D(), I'd first make sure that the device pointer is not NULL and then I'd try (as you already have) removing the MiscFlags setting. Then I'd try setting things up to be the case that frame is only ever 1 so that this would only try to do things once (like the working code does) and then I'd walk through and figure out what the new code is doing differently in that case, if it still crashes.

DuFF14 commented 8 years ago

I pushed my Unity branch to test this: https://github.com/OSVR/OSVR-Unity/tree/atwSupport

Tried with and without MiscFlags, then with one frame only, but it still crashes at CreateTexture2D when "asynchronous" is true in the config file. If it's set to false, it does work with 2 sets of RenderBuffers.

russell-taylor commented 8 years ago

What happens if you use the Unity device rather than the one from renderInfo[eye].library.D3D11? Let me check and make sure we're actually filling that in properly in the ATW-wrapped path...

russell-taylor commented 8 years ago

Yeah, I'm able to reproduce it by trying to used the returned device. Looking into a fix.

DuFF14 commented 8 years ago

So I've updated to the latest RenderManager master and made some changes in the plugin and Unity branch. It no longer crashes at CreateTexture2D, but still not there yet. If asynchronous is false, it's working. If asynchronous is true, the first time it runs with tracking, but nothing shows up on the display, and no crash. Hitting play a second time crashes at this line: https://github.com/sensics/OSVR-RenderManager/blob/master/osvr/RenderKit/RenderManagerD3D11ATW.h#L389

Seems to be the same result whether the shared texture flag is set or not.

DuFF14 commented 8 years ago

This screenshot captures some debug output from Unity, where it looks like PresentRenderBuffers fails inside CreateRenderManager. image

I was able to chase the failure to this line: https://github.com/sensics/OSVR-RenderManager/blob/master/osvr/RenderKit/RenderManagerD3D11ATW.h#L424 "RenderManagerD3D11ATW::RegisterRenderBuffersInternal: Could not AcquireSync on a game render target's IDXGIKeyedMutex during registration."

Which also causes this call to fail: https://github.com/sensics/OSVR-RenderManager/blob/master/osvr/RenderKit/RenderManagerD3D11ATW.h#L490 "RegisterRenderBuffersInternal: Could not Register render buffers on harnessed RenderManager"

DuFF14 commented 8 years ago

Closing as this should not require changes to the Unity rendering plugin at this time. ATW work as-is, meaning it doesn't crash, on Windows 10 only. Otherwise if ATW is true in the config file, Windows 7 or 8 apps will crash. There are still issues with preemption granularity, so we may not see improvements unless testing on a newer Pascal GPU architecture.