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
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
// 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
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
public void OnDestroy()
{
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = null;
SetTextureFromUnity(renderData.textureIndex, IntPtr.Zero, renderData.resolutionX, renderData.resolutionY);
}