A word with Unity Devs about Native Unity SDK

Hi,
I’m a c++ programmer and I’m developing a plugin for unity.

There’s a question in my pipeline that I didn’t find the answer anywhere so I want to ask from unity developers to make sure.

Facts :

  1. I make the plugin for DirectX 11 & 12.
  2. In D3D11 & D3D12 interfaces there’s a key based value named as UnityTextureID
  3. In unity documintation this is noted that result of GetNativeTextureID is only working on OpenGL devices.
  4. API function SRVFromNativeTexture not works with result of GetNativeTextureID.
  5. I ran this test and made sure SRVFromNativeTexture is working fine and it did.
for (UnityTextureID i = 0; i < 1048575; i++)
    {
        ID3D11ShaderResourceView* in_srv = _Interface->SRVFromNativeTexture(i);
        if (!in_srv) continue;
        LOG(DEBUG) << "Texture " << std::dec << i << " SRV Ptr : " << std::hex << in_srv;
    }

Questions :

  1. Why are there SRVFromNativeTexture & TextureFromNativeTexture in DirectX SDK when UnityTextureID is only usable on OpenGL devices?

  2. How can I obtain the valid ID to get the SRV from a texture in native backend?

  3. Is this all deprecated?

Regards

Hi,

looks like Texture.GetNativeTextureID returned a fake “texture id” on D3D11 and D3D12, which could later be used with SRVFromNativeTexture to retrieve ID3D11ShaderResourceView on D3D11 and TextureFromNativeTexture to retrieve ID3D12Resource on D3D12. However, doing it that was clunky as you needed a 2 step process. Texture.GetNativeTextureID API was deprecated a while back, but it seems the native plugin equivalents were not (to me this looks like an oversight).

To retrieve a native texture pointer, you can use Texture.GetNativeTexturePtr instead.

Thanks, So there’s no way to get existing srv for the texture and I need to create it myself?

Correct. Fortunately, SRVs are lightweight so creating additional ones shouldn’t be a performance concern.

1 Like