0
\$\begingroup\$

I noticed that composing frames in a big texture will consume more RAM rather than having 1 frame per texture (or sprite if you would like).

The renderer is SDL2 backed by Direct3D.

Is there any reason why Direct3D (or SDL2 /w Direct3D) is consuming more RAM with 1 big texture containing 10 frames, rather than 10 textures with 1 frame each? basically the size is:

num_textures * num_frames * frame_width * frame_height * RGBA_bytes

so 1 big textures theoretically is equal to 10 single frame textures, e.g.:

big texure size    = 1 * 100 * 100 * 100 * 4 => 4000000 (~4MB)
frame_texures size = 100 * 1 * 100 * 100 * 4 => 4000000 (~4MB)

but in practice the big texture has a 5-6 times factor of RAM usage:

  • Having multiple big textures containing around 10 different animations the RAM consumption goes up around ~1.2GB
  • loading the same data into single frame/sprite texture the RAM consumption is around 220MB.

Is there any reason why this discrepancy in D3D Texture RAM usage?

P.S.

The texture size in both cases are exactly the size needed to store the frame/frames

P.P.S

I know that probably I shouldn't be using GPU in this basic case, but i would like to know the why of the RAM consumption if anyone knows, as rotating 2D sprites might be faster anyway using GPUs rather than CPU operations.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Can you share code to reproduce a Minimal Complete Verifiable Example of this issue? \$\endgroup\$
    – DMGregory
    Commented Aug 18, 2023 at 12:11
  • \$\begingroup\$ @DMGregory thank you for your comment, you led me to consider i had a bug in my code and I found it! When I was computing the size of the texture, instead of adding 1 for the eventual height remainder i was adding the remainder itself, it was not covered by tests neither as it was coming out only when the reminder was > 1 .. thanks. Now i have basically the same size in this 2 extreme scenario. \$\endgroup\$
    – Raffaello
    Commented Aug 18, 2023 at 13:42
  • 1
    \$\begingroup\$ Glad you found the issue! Taking the steps to make a Minimal Complete Verifiable Example when drafting a question can often help isolate root causes like this, so it's a good habit to get into. \$\endgroup\$
    – DMGregory
    Commented Aug 18, 2023 at 14:13
  • 3
    \$\begingroup\$ I’m voting to close this question because it was caused by a mistake in code \$\endgroup\$
    – Kromster
    Commented Aug 18, 2023 at 15:36

1 Answer 1

2
\$\begingroup\$

There is no discrepancy in RAM usage using a texture-atlas or multiple texture per frame/sprite.

The discrepancy that i had it was a small bug that instead of adding one for a remainder, it was adding the remainder itself...

e.g.

// wrong version, unreadable, hidden bug, coming out only when the remainder is > 1
 h = _height * (_tot_frames / n) + (_tot_frames % n);
// correct version, unreadable
 h = _height * ((_tot_frames / n) + (_tot_frames % n > 0));
 // equivalent version, more readable easier to spot an eventual error.
 h = _height * (_tot_frames / n);
 if (_tot_frames % n > 0)
     h += _height;
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .