Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

DX11 Rendering plugin scenario?

Discussion in 'Windows' started by mashman, Sep 20, 2013.

  1. mashman

    mashman

    Joined:
    Aug 23, 2013
    Posts:
    8
    I have an open-ended question for the experienced Unity DX11 plugin developers out there.

    I would like to create a native Unity plugin with the following properties:

    1) Accepts a pointer to ID3D11Texture2D from Unity obtained via Texture.GetNativeTexturePtr().
    2) Performs native rendering to the texture by creating an ID3D11RenderTargetView with said pointer in response to a UnityRenderEvent() provided by Unity by calling GL.IssuePluginEvent() in OnPreRender() for the camera object.
    3) Presents the results of this rendering by applying the texture to a mesh in Unity.

    My approach thus far has been to create a RenderTexture and call GetNativeTexturePtr() to pass into my rendering plugin.

    If I create a Texture2D and pass in the result of Texture2D.GetNativeTexturePtr() I cannot create an ID3D11RenderTargetView since the texture cannot be bound as a render target.

    Unfortunately, I can't see any visible results in Unity from rendering to the native texture so far, even though I've successfully bound the RenderTexture as a render target in native code.

    EXPERTS:
    How would you approach a problem like this?
    Am I headed in the right direction?
    I can provide code if needed.
     
    Last edited: Sep 20, 2013
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,783
    Do you mean creating native texture?
    Texture2D.CreateExternalTexture and Texture2D.UpdateExternalTexture are meant for this, unfortunately they are not yet supported for D3D11. Hopefully they will be in 4.3. You would pass ID3D11ShaderResourceView to these functions (when they do work).
     
  3. mashman

    mashman

    Joined:
    Aug 23, 2013
    Posts:
    8
    Thanks for the reply! I was unable to find any documentation on CreateExternalTexture and UpdateExternalTexture so I have a couple of questions about this method.

    1) Are these methods supported for OpenGL on Windows 8?

    2) What is the usage pattern for these methods? I would guess to CreateExternalTexture() in setup code and then call UpdateExternalTexture after GL.IssuePluginEvent to apply any rendered changes into Unity? Just want to avoid going down a blind alley only to find out I'm using the API incorrectly.
     
    Last edited: Sep 20, 2013
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    1. OpenGL is generally a worst case fallback solution on Windows (command line enforcement). You would normally want to use DX9 or DX11 if you care about performance and the full rendering capabilities.
    OpenGL is only possible for classic windows application, not for Store / Tablet.

    2. If you update it every frame then yes. Generally you would call the UpdateExternalTexture function whenever the external texture got updated, for whatever reason that might be.
     
  5. mashman

    mashman

    Joined:
    Aug 23, 2013
    Posts:
    8
    Thanks, this is very helpful information.

    Re: point #1, my first thought was to stick with DX11 and try to find a way to do off-screen rendering in my native plugin by creating a ID3D11RenderTargetView from the texture pointer provided by calling RenderTexture.GetNativeTexturePtr() but that doesn't seem to work as I cannot see the results of my off-screen render operation in Unity. I am probably missing some subtle (undocumented?) point that prevents this from working.

    If CreateExternalTexture() is the only "right" way to do this, I will stick with OpenGL for now until support for D3D11 is added in some future version of Unity. Thanks again for your help.