Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VR] Add a method to get native Ptr from a texture #5552

Closed
demonixis opened this issue Mar 7, 2017 · 11 comments
Closed

[VR] Add a method to get native Ptr from a texture #5552

demonixis opened this issue Mar 7, 2017 · 11 comments

Comments

@demonixis
Copy link
Contributor

@demonixis demonixis commented Mar 7, 2017

Hi,

I'm working on a VR implementation with Monogame for my game engine. The implementation is almost done, however I'm stuck because I need to have an access to the texture's native pointer.

There is a method called GetSharedHandle() that returns an IntPtr which is what I want. But Microsoft doesn't recommand to use it and it's not working in my case (it returns IntPtr.Zero).

Having access to the native pointer is mandatory for a VR implementation using a compositor (Oculus Home, SteamVR and soon OSVR). In fact you have to create two RenderTarget2D and pass their IntPtr to the VR SDK (the Submit method in my sources).

Without that, you can only have a non direct mode rendering and have to manage the screen size, placement, etc. (this is what Oculus did when the DK1 was released).

I'm working on the three implementations (Oculus, OpenVR and OSVR).

Thanks.

@nkast
Copy link
Contributor

@nkast nkast commented Mar 7, 2017

You can use reflection to get the native resource.
As an example, the following code will get the native vertex buffer resourse.

#if W8_1
            FieldInfo _bufferInfo = typeof(VertexBuffer).GetTypeInfo().GetDeclaredField("_buffer");
#else
            FieldInfo _bufferInfo = typeof(VertexBuffer).GetField("_buffer", BindingFlags.Instance | BindingFlags.NonPublic);
#endif
            _buffer = _bufferInfo.GetValue(this) as SharpDX.Direct3D11.Buffer;
@demonixis
Copy link
Contributor Author

@demonixis demonixis commented Mar 7, 2017

Hi,

Thank you! How can I get the native pointer of a RenderTexture2D with the DesktopGL paltform? It's ok for DirectX I did that

var renderTarget = new RenderTarget2D(Game.GraphicsDevice, (int)width, (int)height);
_textures[eye] = new Texture_t();

var info = typeof(RenderTarget2D).GetField("_texture", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var handle = info.GetValue(renderTarget) as SharpDX.Direct3D11.Resource;
_textures[eye].handle = handle.NativePointer;
_textures[eye].eType = ETextureType.DirectX;
@nkast
Copy link
Contributor

@nkast nkast commented Mar 7, 2017

I don't know much about the implementation of openGL platforms.

@nkast
Copy link
Contributor

@nkast nkast commented Mar 7, 2017

With that said, I don't think MG will ever expose implementation details like native pointers.
The public API tries to be as cross-platform as possible and because the target audience ranges from students to more experienced developers. IMO, using reflection or compiling MG from source with custom patches is fine for those who need more control.

From my point of view the spirit of MonoGame is more about providing a common API for things like VR, 3DTVs, etc.

@demonixis
Copy link
Contributor Author

@demonixis demonixis commented Mar 7, 2017

OK, if we can have access of this using the reflection it's fine for me.

@demonixis
Copy link
Contributor Author

@demonixis demonixis commented Mar 7, 2017

Hi again,

The native pointer from DirectX doesn't work with OpenVR, however it works great with DesktopGL and SteamVR/OpenGL. This is my code (for information).

var info = typeof(RenderTarget2D).GetField("glTexture", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var glTexture = (int)info.GetValue(renderTarget);
_textures[eye].handle = new IntPtr(glTexture);
_textures[eye].eType = ETextureType.OpenGL;

I still have an issue with the view/project matrix but OpenVR is working with MonoGame ;)

Thanks a lot for your help!

@Jjagg
Copy link
Contributor

@Jjagg Jjagg commented Mar 7, 2017

Cool stuff! I can imagine a MonoGame VR target platform that exposes whatever you need.

@demonixis
Copy link
Contributor Author

@demonixis demonixis commented Mar 7, 2017

This is what I'm trying to do in my engine. I have a IVRDevice interface and a VRRenderer. You're free to implement the VR SDK you want to use.

@JeroMiya
Copy link
Contributor

@JeroMiya JeroMiya commented Mar 7, 2017

@Jjagg @nkast Please reconsider implementing GetSharedHandle in MG as there is a clear need for it. It's also part of the original API, so MS thought it was important enough to include. On OpenGL platforms it should just return the texture ID.

I don't think "just use reflection" or "just fork MonoGame" are viable solutions to this use case, long term.

@demonixis
Copy link
Contributor Author

@demonixis demonixis commented Mar 7, 2017

You can cache the result of the reflection. That's what I do and it works well. I'm agree that it's a better solution to have a reflection less implementation.

@demonixis
Copy link
Contributor Author

@demonixis demonixis commented Mar 10, 2017

I close the issue because it's fine for now, thank you guys!

@demonixis demonixis closed this Mar 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.