10 Replies - 1821 Views - Last Post: 08 August 2013 - 07:28 AM

#1 TropicalFlesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 13

Large Tilemap Optimizations

Posted 03 August 2013 - 05:04 PM

So, I've been working on a tilemap for some time now, and I've never really been satisfied with the draw performance. I need to render about 8k tiles to fill a screen at 1080p, and the Spritebatch seems to be limiting me, here.

I've tried all sorts of things, and can't quite figure out how everyone else did it. I tend to get a fairly good framerate, but I need a whole lot of room for other things to come.

Without going too far into detail, what's the best way to call the spritebatch thousands of times per frame with as little lag as possible for a large tilemap?

Is This A Good Question/Topic? 0
  • +

Replies To: Large Tilemap Optimizations

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 560
  • View blog
  • Posts: 1,258
  • Joined: 24-April 12

Re: Large Tilemap Optimizations

Posted 03 August 2013 - 06:10 PM

I don't know if you're aware of this, but SpriteBatch is intended to be called once per frame. That's why its a batch. There's huge overhead every time it's called. So, you want to call it once per frame. If you have a million sprites, you want to put them in one SpriteBatch.
Was This Post Helpful? 1
  • +
  • -

#3 TropicalFlesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 13

Re: Large Tilemap Optimizations

Posted 03 August 2013 - 07:00 PM

View PostBBeck, on 03 August 2013 - 06:10 PM, said:

I don't know if you're aware of this, but SpriteBatch is intended to be called once per frame. That's why its a batch. There's huge overhead every time it's called. So, you want to call it once per frame. If you have a million sprites, you want to put them in one SpriteBatch.


Of course I'm aware. I'm talking about the Spritebatch.Draw. How am I meant to draw other than with that?
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Large Tilemap Optimizations

Posted 04 August 2013 - 04:15 AM

Still, drawing 8000 is not a lot of tiles. There must be something in your code that is causing an issue. Can you post the rendering code?

You don't optimize your calls to Draw, you limit what you draw. If you've got an extremely large map you only draw what is visible on the screen plus or minus 1 tile. Same goes with other objects on the map. If they're not visible, don't draw them.

This post has been edited by SixOfEleven: 04 August 2013 - 04:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 TropicalFlesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 13

Re: Large Tilemap Optimizations

Posted 04 August 2013 - 02:14 PM

View PostSixOfEleven, on 04 August 2013 - 04:15 AM, said:

Can you post the rendering code?


public void Draw(SpriteBatch spriteBatch, Texture2D[] tiles, Rectangle camRegion) {
            for (int i = 0; i < map.Length; i++)
                map[i].Draw(spriteBatch, tiles[map[i].ID], camRegion);
        }


I tried to use a primitive array and a for loop, rather than a List and a foreach loop to see if it'd make a difference, but it didn't seem to.

I pass in the camRegion to cull the tiles out of the camera's view.

The Tile.Draw looks like this

public void Draw(SpriteBatch spriteBatch, Texture2D tex, Rectangle camRegion) {
            if (position.X < camRegion.X)
                return;
            if (position.X > camRegion.Right)
                return;
            if (position.Y < camRegion.Y)
                return;
            if (position.Y > camRegion.Bottom)
                return;

            spriteBatch.Draw(tex, position, Color.White);
        }


I'm even storing the position in RAM so there aren't any realtime calculations.
Was This Post Helpful? 0
  • +
  • -

#6 TropicalFlesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 13

Re: Large Tilemap Optimizations

Posted 04 August 2013 - 02:59 PM

Posted Image

Here's my performance, as of yet. I've actually increased the size of the tiles to 32x32 to try to reduce the draw calls, but I'm very unsatisfied with the look. In this image in particular, I've forced it to draw as if the tiles were 16x16 to show how little room I have for the rest of my game. Sure, 170 fps will do, but I'm not sure I'm happy with so much of my loop being unavailable so early.

Am I being unrealistic with my performance desires, or am I missing something vital?
Was This Post Helpful? 0
  • +
  • -

#7 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Large Tilemap Optimizations

Posted 07 August 2013 - 08:54 AM

I don't understand where the problem is. Your current FPS is 174 in the screenshot. You're obviously not taking much of a performance hit by this tilemap, as a framerate that high is still way beyond the point at which humans can no longer distinguish between differences in framerate. I wouldn't be so concerned with it unless it becomes a major issue later. I don't think 8k 16x16 tiles are going to be as much of a problem as you seem to think they will be.
Was This Post Helpful? 0
  • +
  • -

#8 TropicalFlesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 13

Re: Large Tilemap Optimizations

Posted 07 August 2013 - 11:04 AM

View PostKilorn, on 07 August 2013 - 08:54 AM, said:

I don't understand where the problem is. Your current FPS is 174 in the screenshot. You're obviously not taking much of a performance hit by this tilemap, as a framerate that high is still way beyond the point at which humans can no longer distinguish between differences in framerate.


Yes, however, I'm on a $1600 machine. I don't trust that the everyday desktop will run this fast enough. Also, I'm trying to keep in consideration the fact that I'm left with only about 10ms to do EVERY other operation necessary for a large RPG.
Was This Post Helpful? 0
  • +
  • -

#9 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Large Tilemap Optimizations

Posted 07 August 2013 - 11:18 AM

I'd suggest finding a computer that you could test it on that is a couple of years old. I'm sure a friend of yours wouldn't mind letting you test it out. I'm also not sure where you're getting the math that tells you that you will only have 10ms to do every other operation necessary for a large RPG. How are you calculating how much time it takes to render this tilemap?
Was This Post Helpful? 0
  • +
  • -

#10 TropicalFlesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 13

Re: Large Tilemap Optimizations

Posted 07 August 2013 - 02:36 PM

View PostKilorn, on 07 August 2013 - 11:18 AM, said:

How are you calculating how much time it takes to render this tilemap?


Rendering at 170 fps means I'm using 5.8ms per update loop just for my tilemap. I have to maintain a 16.66ms or lower loop to keep the game from dropping under 60fps. That leaves me with just 10.8ms for everything else - shaders, logic, etc.
Was This Post Helpful? 0
  • +
  • -

#11 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Large Tilemap Optimizations

Posted 08 August 2013 - 07:28 AM

So according to your math, you're using about 1/3rd of your time to load, analyze, and render your tilemap, which is going to take up at least half of your screen. Considering that roughly 8000 tiles make up the viewable area of your tilemap, that means you could theoretically render another 16,000 sprites in the time remaining before you notice any frame drop. Do you really think you're going to have more than 16,000 sprites that need to be rendered beyond the tilemap? If so, I'd dare to say that your game is going to be incredibly busy on screen and completely chaotic with that many sprites/images/tiles/whatever to deal with on screen at the same time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1