• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Christian Thorvik

spriteBatch.Draw slow?

8 posts in this topic

I'm trying to make a particle system, but I'm having problems when drawing. At around 30 000 particles the program slows down. So I tested by making a new empty program with only a for loop drawing a single pixel texture. I found out that when drawing around 60 000 times it slows down drastically from 60 to 30 FPS. Yet theres videos of particle systems that can handle up to a million particles (not my goal, but at least 100 000 would be nice). How is that possible? Do I have to use pixel shaders or something? And if so, isn't spriteBatch already a shader, and should I just write my own spriteBatch instead then?

0

Share this post


Link to post
Share on other sites

Do you only have a single SpriteBatch.Begin before and a single SpriteBatch.End after your drawing?

Having many separate spritebatches with the begin/end will slow your drawing down a lot.

0

Share this post


Link to post
Share on other sites

Yes, but I'm using additive blending. I didn't get any difference in FPS from other blend modes, or just plain spriteBatch.Begin(), though, so that doesn't seem to be the problem.

0

Share this post


Link to post
Share on other sites

First, ensure each sprite is using the same texture, and that you are using SpriteSortMode.Deferred (this is the default). That will max out your speed. But even then, 30,000 sprites is going to be taxing for the system. SpriteBatch is not designed for really large numbers (i.e. tens of thousands) of sprites.

 

Spritebatch is slow if you're drawing lots of items, because it needs to send that data to the GPU every frame. Each sprite is 96 bytes, and so 30,000 particles is nearly 3MB of data you're sending each frame.

 

One fix is to use GPU-based particles. There is a sample for this in the XNA education catalog on Microsoft's XNA website (it's a 3d implementation though, I'm not sure if they also have a 2d version). The sprites are drawn from a vertex buffer which is already on the GPU. As sprites are added or removed, only parts of the vertex buffer are modified accordingly. The end result is that there is minimal CPU->GPU communication. That is where you'll get your big speed up.

 

A drawback of GPU-based particles is that their position is calculated based off their initial position/velocity/timestamp and the current timestemp. So that makes it much more difficult to have particles that interact with your environment (e.g. if you have logic that makes your particles bounce off objects in your game, for instance).

2

Share this post


Link to post
Share on other sites

Thanks for the answer, I'll look into that then. If I want collision detection etc, I'll just have a seperate list for collidable particles and do them normally on the cpu, I guess :)

0

Share this post


Link to post
Share on other sites

Could you post your test update and draw methods? I've seen severe errors in implementation born out of bad examples, first clue would be if you are executing the End method for every particle, that would be VERY bad, there are other common errors though.

EDIT: Also no, SpriteBatch is not a shader, it does HAVE a shader, which you can have access to through the XNA resources, you'll find it under sprite batch default shader or something like that, what srpite batch is, is a batchnig and sorting abstaction algorithm, so you don't have to deal with those things yourself, which works wonderfully until you WANT to deal with those things yourself.

 

It takes away the tedious task of figuring out what is in front of what and if used correctly it optimizes instructions to the graphics card.

Edited by NEXUSKill
0

Share this post


Link to post
Share on other sites

I'm pretty certain that I'm drawing everything correctly. I've found out that I can draw aound 100 000 sprites on my test project before it slows down, though. There must have been some slowdown on my computer when I tested.

 

Here is the draw code on my test project:

            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            for (int i = 0; i < 100000; i++)
            {
                spriteBatch.Draw(pixel, rect, Color.ForestGreen);
            }
            spriteBatch.End();

            base.Draw(gameTime);

Rect and pixel are created in the initialization, and are never changed after that.

Edited by Dobbydoo
0

Share this post


Link to post
Share on other sites

Then I'd go with phil_t's explaination, spritebatch is not meant for point sprite particle systems, there are samples in xna's resources webpage that implement point sprites, particle sistems both in 2d and 3d, look it up.

0

Share this post


Link to post
Share on other sites

Yes, that is my plan. I haven't had time to start yet, but I think I'm gonna use this opportunity to properly learn hlsl shaders. I've been meddling a bit with them, but not enough to properly understand them, so it'll be fun :)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0