Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

CreateExternalTexture crash (Win10 64, D3D11, Unity 5.2.3f1)

Discussion in 'Scripting' started by JAKJ, Dec 7, 2015.

  1. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    I create a texture in D3D11 on the rendering thread with the following code. (Verified the creation works and the result texture pointer is non-null, function returning S_OK.)

    Code (csharp):
    1.  
    2.   D3D11_TEXTURE2D_DESC TextureDesc ;
    3.  
    4.    TextureDesc . Width = Target -> Width ;
    5.    TextureDesc . Height = Target -> Height;
    6.    TextureDesc . MipLevels = 1 ;
    7.    TextureDesc . ArraySize = 1 ;
    8.    TextureDesc . Format = DXGI_FORMAT_B8G8R8A8_UNORM ;
    9.    TextureDesc . SampleDesc . Count = 1 ;
    10.    TextureDesc . SampleDesc . Quality = 0 ;
    11.    TextureDesc . Usage = D3D11_USAGE_DEFAULT ;
    12.    TextureDesc . BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET ;
    13.    TextureDesc . CPUAccessFlags = 0 ;
    14.    TextureDesc . MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE ;
    15.  
    16.    ID3D11Device * Device = UnityRenderDevice ( ) ;
    17.  
    18.    if ( ! Device )
    19.    {
    20.      Log ( "Can't create texture: Device not available." ) ;
    21.  
    22.      return ;
    23.    }
    24.  
    25.    if ( Device -> CreateTexture2D ( & TextureDesc , nullptr , & Target -> Texture ) != S_OK )
    26.    {
    27.      Log ( "Can't create texture: CreateTexture2D error." ) ;
    28.  
    29.      return ;
    30.    }
    31.  
    32.    if ( Target -> Texture -> QueryInterface < IDXGISurface1 > ( & Target -> Surface ) != S_OK )
    33.    {
    34.      Log ( "Can't create texture: QueryInterface < IDXGISurface1 > error." ) ;
    35.  
    36.      Target -> Texture -> Release ( ) ;
    37.  
    38.      Target -> Texture = nullptr ;
    39.    }
    40.  
    Once I have synchronized with the main thread in C++ and gotten the texture IntPtr back into Unity (verifying it is not IntPtr.Zero), I try to do this:

    Code (csharp):
    1.  
    2. CurrTexture = Texture2D . CreateExternalTexture ( Width , Height , TextureFormat . BGRA32 , false , false , TextureObject ) ;
    3.  
    This completely crashes (not to the Unity crash screen: Straight to the OS with no error indication at all). I have absolutely no way to get some sort of error code out of this. I have verified with debug statements that every single value is non-null. If I create a normal dummy unity texture instead of trying to use CreateExternalTexture, all of the rest of my code does not crash (but obviously does not use the actual texture I need it to). I have also verified the width/height being supplied are correct: I tried a static 128x128 instead of my supplied values, and it still crashes.

    How can I debug this? What could be causing CreateExternalTexture to crash without Unity even able to catch the exception?
     
  2. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    Sorry, disregard: The Texture2D has to be passed as ID3D11ShaderResourceView created from the ID3D11Texture2D, and then it works fine.
     
  3. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Try event viewer?
     
unityunity