#Texture access in threads

1 messages · Page 1 of 1 (latest)

ashen fulcrum
#

Generally you can't access anything in a thread in unity as it isn't thread safe. Even more so for textures since generally only the main thread can safely access GPU context safely.

keen stag
#

i was hoping to be able to set pixels in a coroutine or secondary thread without hogging up the main thread

ashen fulcrum
#

Is this to increase performance? Depending on what you want to do there are better APIs to read and write to textures

keen stag
#

yeah i am writing information for a compute shader

ashen fulcrum
#

Where is the source of the texture data coming from?

celest steppe
#

The actual call to SetPixel does not clug the CPU

#

If I remember correctly

ashen fulcrum
#

SetPixels writes to the texture in RAM, then needs to sync that with the texture VRAM. It's definitely not a quick or memory efficient process

celest steppe
#

The real culprit is Texture2D.Apply

ashen fulcrum
#

Apply is what does the sync from CPU to GPU

keen stag
#

i've got a 3D space of voxels and I write a color index to a pixels color channel by doing SetPixels32 but i can't wait for this to finish before the next update. i need it to just finish itself up on the side and then i can upload the texture to the gpu once it is done.

#

hmm maybe it's the apply that is the issue then

ashen fulcrum
#

SetPixels isn't useful without the apply

#

How are you selecting the color index from your 3D space? The fastest way to write to a texture is with a render as opposed to writing to RAM and copying it since it would all stay in GPU and not need to sync with CPU.

keen stag
#

just reading the color index from the voxel array by setting it into a color32 array. its meant to be loaded from disk or generated on the fly.

ashen fulcrum
#

If it's just to get the information to the compute shader, can you pass it as a buffer as opposed to a texture?

keen stag
#

i was using a buffer but it got extremely complicated with deconstructing and reconstructing the index from 3d to 1d to 3d plus i had to do huge calculations when raytracing to convert the current position index to 1D

#

because i was positioning the data adjacent to itself in the buffer to make it easier to cycle sections out

#

this is why i am trying to use a texture instead. don't know if you can upload a texture asynchronously to the GPU. i just don't want a stall uploading a semi-large 3D texture now and then

ashen fulcrum
#

I am not familiar with uploading async. I know there is an async gpu readback, but not the other direction

#

I haven't dug into jobs too heavily but it may be possible to leverage them to write into a native array and push that to the texture as is hoping that it will be faster

keen stag
#

i have also not looked into jobs

ashen fulcrum
#

I used it on a project recently to handle readbacks when saving texture data as thumbnails. Not ever platform supports the async readback (mobile needed it to be sync) but on other platforms it worked. Going the other direction is not something i've tried, though I have used native arrays in order to save/load hardware compressed textures and the performance wasn't too bad. It wasn't happening often in my case.

#

It still needed that Apply call, though, since the sync in unavoidable if the texture is being written to on CPU

#

the only way to avoid it is to stay entirely on GPU with a buffer or by creating the texture with a render

#

If you haven't already, try profiling and seeing what parts of the texture creation/setpixel/apply are expensive. If jobs let you create the texture with a native array quickly and the apply isn't the bulk of the cost, you might get some savings there.