Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Passing rendertexture data to native plugin

Discussion in 'Shaders' started by Neran28, May 18, 2022.

  1. Neran28

    Neran28

    Joined:
    Mar 14, 2016
    Posts:
    15
    Hello,
    im not quite sure into which forum to put this question.
    I am writing a native plugin that needs to set a D3D11Texture2D in code. The texDescriptor looks like this:

    Code (C++):
    1.  
    2.     D3D11_TEXTURE2D_DESC g_srsDesc;
    3.     ZeroMemory(&g_srsDesc, sizeof(g_srsDesc));
    4.     g_srsDesc.Width = (UINT)x;
    5.     g_srsDesc.Height = (UINT)y;
    6.     g_srsDesc.ArraySize = (UINT)1;
    7.     g_srsDesc.MipLevels = (UINT)1;
    8.     g_srsDesc.SampleDesc.Count = (UINT)1;
    9.     g_srsDesc.SampleDesc.Quality = (UINT)0;
    10.     g_srsDesc.Format = DXGI_FORMAT_R8_UINT;
    11.     g_srsDesc.Usage = D3D11_USAGE_DEFAULT;
    12.     g_srsDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS;
    13.     g_srsDesc.CPUAccessFlags = (UINT)0;
    14.     g_srsDesc.MiscFlags = (UINT)0;
    Now on the unity side im running a compute shader that creates the texture using a RenderTexture. This is the RenderTexture setup:
    Code (CSharp):
    1.  
    2. renderTexture = new RenderTexture(variableShadingGranularity.x, variableShadingGranularity.y, GraphicsFormat.R8_UInt, GraphicsFormat.None)
    3.             {
    4.                 enableRandomWrite = true
    5.             };
    6.         renderTexture.Create();
    I am not sure whether the Format settings are correct to be used with the mentioned texture2d_desc. The shader output is uint. However in the inspector RenderTexture preview im not seeing any color for the red channel when filling the texture with 1s or 255s. This may probably be intended because of the color format.

    The bigger problem right now is how can I access the raw data written by the compute shader and pass it to the c++ side to create the texture_2d?
     
  2. Neran28

    Neran28

    Joined:
    Mar 14, 2016
    Posts:
    15
    I found a solution to this by copying the RenderTexture into a Texture2D with readpixels(), then copy the data into a byte array and passing this to the native code. To pass the array to native code I am using a GCHandle.

    Here is the code if anyone faces the same problem.

    Code (CSharp):
    1.     Texture2D tex2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.R8, 1, true);
    2.     RenderTexture.active = renderTexture;
    3.     tex2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    4.     RenderTexture.active = null;
    5.     byte[] data = tex2D.GetRawTextureData();
    6.      
    7.     renderTexture.Release();
    8.  
    9.             ...
    10.  
    11.     GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
    12.     Plugin_API.Set_Data(dataHandle.AddrOfPinnedObject(), data.Length);
    13.     if(dataHandle.IsAllocated) { dataHandle. Free(); }
    14.  
     
    aparajithsairam likes this.