Author Topic: Texture Memory Usage  (Read 2724 times)

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Texture Memory Usage
« on: February 09, 2014, 07:02:27 am »
Is there an easy way to determine the texture memory usage in the software renderer? Trying to optimize textures and the would be quite helpful.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Texture Memory Usage
« Reply #1 on: February 09, 2014, 12:23:06 pm »
That's basically sizeX*sizeY*8+((sizeX/2)*(sizeY/2))*8+((sizeX/4)*(sizeY/4))*8...until either sizeX/a or sizeY/a are down to 16.

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Texture Memory Usage
« Reply #2 on: February 09, 2014, 01:33:36 pm »
Works ok if you're working with large textures. However very inaccurate if you're working with many small textures (<=16x16).

Just found this method. Will TextureManager.getInstance().getMemoryUsage(true) produce an accurate result in the software renderer?

Code: [Select]
        public long getMemoryUsage(boolean paramBoolean) {
            long l = 0L;
            for (int i = 0; i < this.textureCount; i++) {
                int[] arrayOfInt1 = this.textures[i].texels;
                int[] arrayOfInt2 = this.textures[i].alpha;
                if (arrayOfInt1 != null) {
                    l += (arrayOfInt1.length << 2);
                }
                if (arrayOfInt2 != null) {
                    l += (arrayOfInt2.length << 2);
                }
                if ((!paramBoolean) && (arrayOfInt1 == null)) {
                    l += (this.textures[i].width * this.textures[i].height << 2);
                }
            }
            return l;
        }

Edit: Mhmm, no that seems to consider removed textures as well. Would it be possible to add this functionality to the Texture object to determine how much memory it uses?
« Last Edit: February 09, 2014, 01:51:06 pm by aZen »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Texture Memory Usage
« Reply #3 on: February 09, 2014, 07:44:52 pm »
I will look into it, albeit i'm not sure what the actual purpose of such a method is...?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Texture Memory Usage
« Reply #4 on: February 09, 2014, 08:27:35 pm »
Try this jar: http://jpct.de/download/beta/jpct.jar. It adds a method getMemoryUsage() to Texture that should take everything into account.

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Texture Memory Usage
« Reply #5 on: February 10, 2014, 12:20:49 am »
Thank you! That finally works correctly with my testcase!

What I'm using this for:
Basically the voxel rendering works as following:
(1) A batch of voxel sides (that look into one direction on the same plane) are converted into a polygon.
(2) The polygon is converted into triangles. Each triangle can contain different colors (because it might span more than one voxel). Now we need to find or create a texture that can be used for this triangle.
(3) Complex algorithm determines whether there is an available triangle. If there is not, a new texture is created. It might be beneficial to reorganize textures (this is done with idle capacity). For this reorganization I was estimating the size of the texture by using the dimensions, but that wasn't very accurate. Much cleaner to get the real size (this will even be accurate if you ever update the internal structure of the Texture object).

Why do I even bother with this? Well, if voxel contain only a single color it's no problem at all. But they can also be textured and in that case you run out of memory faster than you can say "Texture".