# Unity Native Texture Create/Release causes GPU suspension

**URL:** https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916
**Category:** Shaders & Materials
**Created:** [October 24, 2016, 3:06pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916 "2016-10-24T15:06:01Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [October 24, 2016, 3:06pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/1 "2016-10-24T15:06:01Z")

</div>

Hello!

I have an issue with textures created natively in the native c++ plugin and being passed to Unity causing the GPU to suspend and unity to freeze. The code I use is below.

Creating the Native Texture, Setting it on the Texture2D, Destroying the GameObject, then Creating a New Native texture etc, rapidly, causes the program to freeze and I dont see any reason why.

I caught an error from the HRESULT at one point that said “0x887a0005: The GPU device instance has been suspended. Use GetDeviceRemovedReason to determine the appropriate action.”

The c++ native function for creating texture

```csharp
void* RenderAPI_D3D11::CreateNewTexture(int textureWidth, int textureHeight)
{
    ID3D11Texture2D* m_renderTargetTexture;
    //ID3D11RenderTargetView* m_renderTargetView;
    ID3D11ShaderResourceView* m_shaderResourceView;

    HRESULT result;

    D3D11_TEXTURE2D_DESC desc;
    ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC));
    //D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
    D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
    ZeroMemory(&shaderResourceViewDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
   
    desc.Width = textureWidth;
    desc.Height = textureHeight;
    desc.MipLevels = desc.ArraySize = 1;
    desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    desc.SampleDesc.Count = 1;
    desc.Usage = D3D11_USAGE_DEFAULT;
    desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
    desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    desc.MiscFlags = 0;
    HRESULT res1 = m_Device->CreateTexture2D(&desc, NULL, &m_renderTargetTexture);

    if (FAILED(res1))
    {
        OutputDebugStringW(L"Failed to create Texture2D");
        return nullptr;
    }
   
    // Setup the description of the shader resource view.
    shaderResourceViewDesc.Format = desc.Format;
    shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
    shaderResourceViewDesc.Texture2D.MipLevels = 1;
    // Create the shader resource view.
    HRESULT res2 = m_Device->CreateShaderResourceView(m_renderTargetTexture, &shaderResourceViewDesc, &m_shaderResourceView);

    if (FAILED(res2))
    {
        m_renderTargetTexture->Release();
        OutputDebugStringW(L"Failed to Create Shader Resource View");
        return nullptr;
    }

    return m_shaderResourceView;
}

```

The c++ function for releasing native texture and shader resource view

```csharp
// Removing texture
        ID3D11Texture2D* texture = (ID3D11Texture2D*)winText->g_TextureHandle;

        if (texture != nullptr)
            texture->Release();

        ID3D11ShaderResourceView* shaderResource = (ID3D11ShaderResourceView*)winText->g_ShaderHandle;
        if (shaderResource != nullptr)
            shaderResource->Release();

```

The unity C# code the creates the texture natively then uses Texture2D.CreateExternalTexture

```csharp
IntPtr shaderIndex = CreateNativeTexture(renderData.textureIndex, renderData.resolutionX, renderData.resolutionY);
        if (shaderIndex == null)
            return;
        windowTexture = Texture2D.CreateExternalTexture(renderData.resolutionX, renderData.resolutionY, TextureFormat.BGRA32, false, false, shaderIndex);
      
        windowTexture.filterMode = FilterMode.Bilinear;

        Renderer renderer = GetComponent<Renderer>();
        // Set texture onto our material
        renderer.material.mainTexture = windowTexture;
      
        SetTextureFromUnity(renderData.textureIndex, windowTexture.GetNativeTexturePtr(), renderData.resolutionX, renderData.resolutionY);

```

The Destroy function that tells the native plugin to release the texture/shader resource view

```csharp
public void OnDestroy()
    {
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = null;
        SetTextureFromUnity(renderData.textureIndex, IntPtr.Zero, renderData.resolutionX, renderData.resolutionY);
    }

```

---

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [October 24, 2016, 3:10pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/2 "2016-10-24T15:10:02Z")

</div>

Also waiting between a Full Create-\>Destroy drastically reduces the likelihood that the freeze will occur but it will still happen eventually usually with successive repetitions

---

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [October 25, 2016, 1:47pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/3 "2016-10-25T13:47:02Z")

</div>

This is in my c++ debugger

D3D11: Removing Device.  
Exception thrown at 0x00007FFCBD457788 in Unity.exe: Microsoft C++ exception: \_com\_error at memory location 0x00000000005FB5A0.  
Exception thrown at 0x00007FFCBD457788 in Unity.exe: Microsoft C++ exception: \_com\_error at memory location 0x00000000005FD020.  
Exception thrown at 0x00007FFCBD457788 in Unity.exe: Microsoft C++ exception: \_com\_error at memory location 0x00000000005FD378.  
Failed to create Texture2D

---

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [October 25, 2016, 2:06pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/4 "2016-10-25T14:06:02Z")

</div>

I would turn the debug layer on for the device, but Unity creates it internally T\_T

---

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [October 28, 2016, 12:08pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/5 "2016-10-28T12:08:30Z")

</div>

I am wondering if I should file a bug in unity for this. There isnt any reason why it should be broken. Unity doesnt seem to handle releasing of native textures very well.

---

<div class="post-metadata">

### Author: ![joaocarrion](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joaocarrion/32/454303_2.png) [@joaocarrion](https://discussions.unity.com/u/joaocarrion)
#### Post date: [February 13, 2017, 1:01pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/6 "2017-02-13T13:01:34Z")

</div>

> [@](#):
>
> I am wondering if I should file a bug in unity for this. There isnt any reason why it should be broken. Unity doesnt seem to handle releasing of native textures very well.

Hi! I have this same problem outside Unity, given your report I’m beginning to think it’s either a bug on DirectX or on the video board driver. In my case I allocate and release about two textures per frame, and the quicker it is, the quicker it brakes.  
It only occurs when allocating the texture on video memory, if I use normal RAM it’s ok.

Did you ever find a solution for this?

Thanks!

---

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [February 21, 2017, 3:49pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/7 "2017-02-21T15:49:13Z")

</div>

Unfortunately I havnt found a solution for this yet sorry ☹

---

<div class="post-metadata">

### Author: ![jiulongw](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@jiulongw](https://discussions.unity.com/u/jiulongw)
#### Post date: [July 14, 2017, 6:49pm UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/8 "2017-07-14T18:49:57Z")

</div>

I had exactly same issue that creating texture in native plugin causes “D3D11: Removing Device.” message and Unity freeze with massive COM exceptions. It does not happen every time, more likely a timing issue or race condition.

I fixed this issue by creating texture in rendering thread instead of Unity main thread, using GL.IssuePluginEvent(…)

It is interesting to note that[D3D document](https://msdn.microsoft.com/en-us/library/windows/desktop/ff476891(v=vs.85).aspx) explicitly said that ID3D11Device is thread safe (the immediate context is not thread safe but was not needed to create texture). While Unity doc said “IUnityGraphicsD3D11” should only be used on the rendering thread.

Hope that helps.

---

<div class="post-metadata">

### Author: ![PeterGeneral](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@PeterGeneral](https://discussions.unity.com/u/PeterGeneral)
#### Post date: [July 27, 2017, 11:19am UTC](https://discussions.unity.com/t/unity-native-texture-create-release-causes-gpu-suspension/643916/9 "2017-07-27T11:19:12Z")

</div>

take a look at this : [Unity Asset Store - The Best Assets for Game Making](https://www.assetstore.unity3d.com/en/#!/content/92249)
