UnityAsyncImageLoader icon indicating copy to clipboard operation
UnityAsyncImageLoader copied to clipboard

Texture Blinking Issue

Open MurkesM opened this issue 3 years ago • 4 comments

I'm using, await AsyncImageLoader.LoadImageAsync(tex, byteArray); and it loads the correct texture and is greatly increasing performance but the main issue is sometimes the texture loads as completely black for 1 frame and it happens multiple times every second causing the texture to blink. I'm not simply loading a static texture but instead loading a series of textures that play a video. Any thoughts on what might be causing this?

video of the issue: https://user-images.githubusercontent.com/91104156/204570541-7a66809b-432d-443b-95a1-705793665cbe.mp4

MurkesM avatar Nov 29 '22 15:11 MurkesM

The texture is black initially because the texture data is being uploaded from the CPU to the GPU and the transfer is not finished yet. You can check the troubleshooting guide here.

In your case, you can implement something like double-buffering. Make an additional texture which acts as a buffer and is displayed on the screen. When you finished loading texture from file, transfer the texture data on the GPU to this buffer texture using shader or compute shader. This will only present a black screen at first when the buffer texture is not initialized. There will be no black screen when the buffer texture is updated with new frame.

Looooong avatar Nov 29 '22 17:11 Looooong

I'm not sure exactly what you mean or how to do this part: " When you finished loading texture from file, transfer the texture data on the GPU to this buffer texture using shader or compute shader."

MurkesM avatar Nov 29 '22 22:11 MurkesM

Something like this:

var bufferTexture = new RenderTexture(...);

var frame0 = await AsyncImageLoader.LoadImageAsync(...); // This loads image from disk -> CPU side -> GPU side
Graphics.Blit(frame0, bufferTexture); // Transfer texture data to the buffer only on the GPU side.

var frame1 = await AsyncImageLoader.LoadImageAsync(...);
Graphics.Blit(frame1, bufferTexture);

var frame2 = await AsyncImageLoader.LoadImageAsync(...);
Graphics.Blit(frame2, bufferTexture);

// And so on...

What you want to put on the screen is the bufferTexture.

Looooong avatar Nov 30 '22 13:11 Looooong

that worked using Graphics.CopyTexture :) Although I'm now trying to build to android to test and I'm getting these 2 errors. I assume the first error is causing the second error. Any thoughts? Unity_NHQ7IQlbfU Unity_xD1R7ihPCr

MurkesM avatar Dec 02 '22 21:12 MurkesM