x


Extremely slow texture upload in plugin when a model is being drawn

Hi,

I have an iOS unity app that calls a plugin to update a texture in unity. This runs reasonably fast with the texture upload taking about 3-4ms. This has been working fine for a few months.

When I am not drawing any 3d models it still runs at this speed, but as soon as I draw a model it takes 60ms plus!

This is my C++ texture upload code:

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glBindTexture(GL_TEXTURE_2D, texID);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, im->data());

It is solely the glTexImage2D part that becomes 15x slower when a model is drawn. Everything else in the calling path runs at the same speed.

Any clues as to why this is happening? The only real change has been upgrading from Unity 3.5 beta to 3.5 and it was working fine in 3.5.

Addendum:

  • The texture update is being called from an Update function in a script on every update.
  • The image is a 640x480 BGRA image.
more ▼

asked Mar 27 '12 at 06:47 PM

Robert Castle gravatar image

Robert Castle
121 5 5 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, I think I found the answer.

I needed to double buffer my texture. So while one is being used on screen, I update another one, and then swap over. Also using an unpack alignment of 4 was slightly beneficial as I am dealing with images that are divisible by 4. So my updated code looks something like this:

In plugin:

void UpdateTexture( int texToUse )
{
     if (texToUse == 0) {
        texID = unityTexture0;
      }
      else {
        texID = unityTexture1;
      }

    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glBindTexture(GL_TEXTURE_2D, texID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, im->data());
}

and in Unity in Update()

UpdateTexture(texToUse);
videoMaterial.SetTexture("_MainTex", videoTextures[texToUse]);
texToUse = (texToUse == 0) ? 1 : 0;

where videoTextures is an array of 2 Texture2Ds.

more ▼

answered Mar 28 '12 at 06:43 PM

Robert Castle gravatar image

Robert Castle
121 5 5 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2207
x1966
x396
x114

asked: Mar 27 '12 at 06:47 PM

Seen: 778 times

Last Updated: Mar 28 '12 at 06:43 PM