- Home /
Load Texture from GPU
Hello Everyone,
I want to know if it's possible to load a texture from data stored in GPU and if yes, how to do that. In my project, I have a server that sends encoded Images and the client (on Unity) decodes this image and display it on an object. I use NvPipe to encode and decode on both sides and it uses the GPU to do that. Currently, I use LoadRawTextureData to load a texture.
I ask this question because there is a transfer GPU -> CPU and CPU -> GPU and I want to remove this useless and expensive transfer if it's possible.
Thank you in advance for your answer.
best regards
Answer by Pangamini · Aug 15, 2020 at 11:51 PM
You can use Texture2D.ReadPixels to download the data from GPU memory. After that, you can use GetPixels() to get the actual color data array. This method is synchronous, and stalls the frame for some time. It's better to use AsyncGPUReadback.
He is trying to avoid coping data from gpu to cpu. He wants a gpu(native code)->gpu(Unity) workflow.
Well. I doubt that GPU communicates directly with a distant server. Somebody somewhere will have to download the texture to the CPU memory. Also, the transfer is not so expensive, but for downloads to CPU, the GPU has to finish rendering first (drawcalls are executed asynchronously, download has to happen after) which causes a hiccup. Solved by the async download
Answer by gameblaster · Aug 16, 2020 at 06:12 PM
I tried some method like Texture2d::CreateExternalTexture() to get the texture data in gpu .I created a native Intptr for the CreateExternalTexture() using a native plugin but failed in the first time. Then I realize maybe I should use the device that Unity is using to create the textrue native pointer.I modified the "Unity native plugin" example, Finally I succeeded. You can create a native plugin to pass the address of a gpu texture (in windows, as a D3D11Texture2D object ) to the Texture2d::CreateExternalTexture() method in unity script, then In Unity you assign this texture to some shader,then you will see the texture without copying it from gpu memory to cpu system memory. Here is the example code.Use this example code to get the right device (in my project it's "ID3D11Device").Use this device to get the gpu texture and pass the texture to Unity. If you create the device in your own way without the Unity interface. Unity will crash when you try to pass the texture to it.
Your answer
Follow this Question
Related Questions
Reduce GPU Memory usage from textures 1 Answer
Texture looks different in different devices? 0 Answers
DXT texture format on Mali GPU 0 Answers
GPU Memory : Impact of Grayscale Textures 0 Answers
Texture Atlas Duplicating Because Of Material Differences? 0 Answers