Search Unity

Upload only part of a texture when applying changes

Discussion in 'General Graphics' started by imaginaryhuman, Dec 5, 2019.

  1. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    In OpenGL v1, like 20 years ago, there was a function glTexSubImage2D, which allowed you to upload a portion of pixel data (e.g. a sub rectangle within a larger rectangle), up to a texture, but not the whole size of the texture. ie say your texture is 1024x1024, you could upload just a 256x256 portion of it.

    In Unity, this has never been implemented. The scenario I need it for is where I have a large amount of texture data representing a game environment, I want to make changes to some stuff on the CPU side, and upload the changes efficiently. Problem is, since Apply() only uploads the "whole texture", the textures therefore have to be quite small, which massively ramps up the draw calls. So now you have a grid of small textures. Yes the upload speed is now 'okay' but at the expense of draw calls hiking up.

    The ideal is that the textures remain larger and fewer (or even just one large texture), and then an equivalent to glTexSubImage2D is used to upload only a small rectangle from a main memory buffer into a position in the texture. Sort of similar to CopyTexture but between the cpu and gpu over the graphics bus.

    Why hasn't this been implemented STILL after all these years, and is there any way to do this in Unity at all?

    There should be a nice easy command such as:

    Texture2D.Apply(x,y,width,height);

    Which allows you to specify where in the texture the upload will end up, and the size of the operation. And additionally...

    Texture2D.Apply(sourcex,sourcey,destx,desty,width,height);

    Which now lets you select where in the main-memory (is readable memory buffer) to pull pixels from and where to write to plus the size of the op.

    This would greatly help with performance in this kind of scenario. Is there any way to do this or anything close to it?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Could upload a new texture that's just the area you're modifying, and use CopyTexture to copy that section into the new texture. A little clunkier, but should be far less expensive.
     
  3. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    True but doing that is still slower than only uploading the texture data.