Skip to main content
gamedev.net
🔒 Locked

Texture swapping question.

Started by MrCSCUndergrad 2 replies 600+ views
Original Post
MrCSCUndergrad
MrCSCUndergrad
So i'm about to start work on a 2D game that will have a tiled background. I will be writing it in C# using managed DirectX. All of my textures will be applied to triangle quads so I can take advantage of DirectX pixel shaders. My question is, would it be faster to store each tile as a seperate texture and swap in the correct textures to be used on a standardized quad for all of the tiles, or store a reasonable number (64tiles @ 32X32?) of tiles in one or more larger textures and then index into a vertex buffer with correct texture coords set for each tile in the larger texture? I was hoping someone had some experience with this and could give me some real world performance numbers. Since i'll be in C# and I know swapping textures is supposed to take awhile anyway, I could see how one large texture would be faster. But, one large texture seems like a texture cache nightmare. Thank you in advance. ~Chris
Supernat02
Supernat02
Definitely go with accessing textures within one larger texture if you can. Calling SetTexture will be an overhead burden you want as little of as possible. Sort by textures too, so you don't have to call SetTexture very often. Also, consider using larger textures. Some games come to mind that used large nice looking tiles. You may only need 4 or 5 tiles visible on the screen and you can still tile them easily enough. Most 2D games I see in work are using smaller tiles and the obvious tiling effect is really noticable.

Speaking of texture cache nightmares, there are several ways to help you with this. You could write an algorithm that went through your "Map" and determined which textures were next to which other textures more often. I.e. Grass and Snow appeared nearby more than Grass and Rock, so put grass and snow on the same texture together. Another way to help is to keep a look-ahead thread running in your software. As the player moves, you can be looking ahead 10 tile spaces (or whatever) and be loading those textures ahead of time. If they move to where those textures are 10 tile spaces out and are no longer visible, then you can free that texture. You would just want to have some limits and say no more than X number of textures will be on the screen at once.
Chris ByersMicrosoft DirectX MVP - 2005
MrCSCUndergrad
MrCSCUndergrad
Thank you. That's exactly what I wanted to know.

Using larger tiles also seems like a very good idea. I know what you mean about visible tiling, and the backgrounds shouldn't be too complex in my case anyway.
It wasn't necesarily the system caching of textures that I was worried about, I was going to have to handle that anyway, so much as the on chip GPU cache that I was worried about. (continuous lines of texel info in a small texture VS large gaps between lines in one large texture) Maybee that was too much CPU thinking though.

Anyway, thanks again. I'll go with the larger combined textures. :)
~Chris
c t o a n
c t o a n
Try and call SetTexture a while before you actually draw the polygons. If using bigger textures, its going to take a while to upload the texture, you're right. I've found DirectX trys to do this asynchronously, as long as you satisfy its conditions for non-blocking behaviour, such as the video card isn't using the texture you're swapping out, you're not trying to render before the texture is fully uploaded, etc.

In these cases, DirectX will block, and slow you down. What you're trying to do is get the CPU and VPU(s) to work in parallel. Experiment! Good luck.
Chris PergrossiMy Realm | "Good Morning, Dave"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.