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 have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Updating RenderTarget Texture ID from Native engine

Discussion in 'General Graphics' started by Venkatachalam, Feb 23, 2023.

  1. Venkatachalam

    Venkatachalam

    Joined:
    Sep 9, 2022
    Posts:
    3
    Iam new to Unity and can you help me with some tips on how to design the following application?

    1) Unity uses DirectX backend and we use another Native Library to render on RenderTarget.

    2) Native Library returns Texture ID after rendering on RenderTarget.

    3) I need to use this ID to display image on the main Unity application.

    I see applications where we create materials and add Images to it and use them script / shaders.
    In my application initially there will be a solid plain window (say white) and later the RenderTarget Id given by Native engine should be used as Texture and just needs to be texture mapped.

    Do i need to write HLSL shader and/or script to handle this.

    If i need to write shaders how do i update the texture id before the call to SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);

    Please let me know, thanks in advance.

    Venkat
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    560
  3. Venkatachalam

    Venkatachalam

    Joined:
    Sep 9, 2022
    Posts:
    3
    Thank you for your reply.

    I tried this earlier but for some reason, this doesn't seem to work.

    I get texture ID from the native engine and assign it to createExternalTexture(), I don't see the O/P.
    I tried writing a simple OpenGL application that uses the same native engine and did a texture mapping with the texture ID. It works well. Only in Unity, I don't see the results.

    Please check below.

    ############################################
    Texture2D tex ;

    uint OpenGLTextureID = update_demo(byteArray, Width, Height); // call to native engine which returns texture Id

    tex = Texture2D.CreateExternalTexture(Width, Height, TextureFormat.RGBA32, false, true, new IntPtr(OpenGLTextureID));

    GetComponent<Renderer>().material.mainTexture = tex;

    Regards
    Venkat
     
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    560
    Looks good to me (just don't forget to destroy the material and texture in the end). I have never used it personally, though, so I'm not familiar with pitfalls.

    My only guess is that update_demo and Unity are using separate OpenGL contexts and the contexts are not sharing data.
     
  5. Venkatachalam

    Venkatachalam

    Joined:
    Sep 9, 2022
    Posts:
    3
    No they share the same Context, the native engine is initialized with Unity Context
     
  6. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    560
    Maybe try taking a RenderDoc capture with validation enabled...