• 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

2d drawing pipeline, how to get good performance, i just figured out i got no clue what i am doing.

3 posts in this topic

Hello, after i written 500+ characters long boring story, i decided to go with a simpler and easier approach.


I require a tutorial / (example made) to 2d graphics pipeline tutorial, i want to learn how to draw stuff, how textures should be applied/used/managed, and what to do in what order to achieve fastest drawing.
Essentially i am unsure how to manage my objects, i don't know what sizes of my textures should be, should i when drawing, force to draw stuff that requires current texture, or can i freely switch back and forth textures and not worry about it, and much more.


What i just want to know is how to manage my "Entities" and how should i handle their drawing.

If you could suggest a tutorial that explains stuff noted above, i would be gratefull!


What i currently will face is:

//Draw 8 vertices with texture1
//Draw 24 vertices with texture2
//Draw 8 vertices with texture1
//draw 4 vertices with texture3
//Draw 8 vertices with texture2
//Draw 16 vertices with texture2
//Draw 24 vertices with texture1
//draw 8 vertices with texture3

I got few ideas for optimization, but as i stated in the topic name "i just figured out i got no clue what i am doing"

Edited by BaneTrapper

Share this post

Link to post
Share on other sites

It sometimes can't be helped, but in most cases you can have many objects in the same texture (within reason).


So, while I've made many renderers in my time, right now I'm making a 2D game.

And for each layer in my game, I have to sort by each objects sorting mechanism, eg. by the bottom end of the sprite as its drawn.

For that to happen, I have to sort by layer then by texture and then by its sorting mechanism.


It ended up being pretty fast, much faster than I thought it would be


I have a class that contains all renderable/visible objects in a specific layer, eg. a wrapper for a vector, then under that I have a class that represents sorted sprites (another wrapper for a vector).

Lots of dynamic allocation, except it only happens until the vectors reach a certain growth level, then they stop growing. The allocations stop, and even after new frame begins nothing is going to be allocated again unless I spam even more objects.


So, vector of layers which contain vector of sprites. Each sprite know their texture, but they also have a drawing order. I sort by that drawing order, then by texture. It works out OK in the end.

After all that is done, I generate vertices for everything and set markers for when I need to change texture.

Then I upload ALL the vertices once (DYNAMIC_DRAW), and render in the given order by changing texture as needed.


I basically stream data from CPU to GPU, and it's working pretty well for me.

In my case the objects are pretty static (they are sprites,) but even so, I gain alot by simply generating them on the fly, and rendering as much as possible in one go.


My reasoning for different textures is simply for convinience. The textures represents sprites that differ greatly in functionality in my game.

Maybe for you the textures are unique per object, something that you should avoid if you can.

In my case, I could probably live with a single texture, but it's also a matter of making life easier.


Btw. Are you sure you have a performance issue? And if so, are you sure it's where you think it is? Any data?


1. Don't worry about switching textures, as long as you are not overdoing it. Do some profiling to make sure you need to do it, if you think you do. tongue.png

2. In a 2D game you can probably use a megashader to avoid ever changing shader. That means you can more easily sort by texture where needed.

3. You need to know the sizes of your textures to generate proper texcoords. It's not always possible  to use Texture Arrays, so sometimes you just have to do it the old-fashioned way where you subtract 1/2 to 1/64th of a pixel to avoid pixel bleeding.

Edited by Kaptein

Share this post

Link to post
Share on other sites

I was working on an iOS game and found that sprite batching made a huge difference in performance. Keep in mind I didn't try sprite batching until I found it to be a bottleneck. 


For sprite batching you pack separate images into a single image then define texture coordinates that 'cut' out the image you want from the packed image. When drawing your scene, you create a single vertex buffer that contains all of the images you want to draw. You need to move and rotate the points of the quad you are drawing on the CPU before putting them into the vertex buffer.


You wont want to batch all draw calls, just the ones that make up the largest portion of your sprites. The most obvious one is a tilemap. If your scene contains hundreds of tiles, you don't want each tile to consume a draw call. Instead, you put all of the tiles into the same image then create a vertex buffer that contains all of the tiles. This way you can draw the whole map in a single draw call.


On mobile platforms, it is even faster to repopulate a vertex buffer every frame from pre-transformed data generated on the CPU than it is to have hundreds of separate draw calls.


The disadvantage to this approach is it can complicate your drawing code and you have less control over draw order. If you still need to enforce a specific draw order you can simply use a depth buffer and have the z component of each vertex determine the draw order.


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