Sign in to follow this  
kevroy314

Best Way to Implement Cellular Automata

Recommended Posts

I'm trying to make a game in which the background (which will contribute to the game mechanics) is a cellular automata. I'm absolutely new to XNA and only an amateur at graphics programming but I think a simple cellular automata implementation is a good starting goal. I've got a "working" project that just repaints black on the screen over and over (as well as a fps counter I found online and plugged in). I've tried creating a Texture2D and using SetData<int>() to give it the integer values for the pixels and then calling spriteBatch.Draw(frontBuffer1, new Rectangle(0, 0, sizX, sizY), Color.White); where frontBuffer1 is the Texture2D that I modify. I've got a function that calculates the next frame and calls SetData<int>() in the Update() function, but when I do that it slows down to 3-6 fps instead of 60 fps without the calculations (which don't matter because the texture isn't showing up on the screen). Questions I have are... Is modifying a Texture2D the most efficient way to do what I'm trying to do? Is it purely the inefficiency of my calculations which is slowing the frame rate down or could it be something else? The calculation basically finds the surrounding pixels (or rather data that is associated with them), adds their values together and runs a small equation (and does so for every pixel).

Share this post


Link to post
Share on other sites
Quote:
Original post by kevroy314
Is modifying a Texture2D the most efficient way to do what I'm trying to do?
It probably is, though you might want to do it on the GPU using a pixel shader, instead of on the CPU. (Or even on both simultaneously, if you need the data on the CPU side for things like collision detection).

Quote:
Is it purely the inefficiency of my calculations which is slowing the frame rate down or could it be something else? The calculation basically finds the surrounding pixels (or rather data that is associated with them), adds their values together and runs a small equation (and does so for every pixel).
The first step in answering any kind of 'why is X slow' question is to profile it. It might be your calculations, or it might be the transfer of the data from system to video memory. Setting up your texture so that it's flagged for dynamic updates will probably help.

Share this post


Link to post
Share on other sites
"you might want to do it on the GPU using a pixel shader, instead of on the CPU. (Or even on both simultaneously, if you need the data on the CPU side for things like collision detection)."

I will eventually need access to the data upon which the texture is built (it's a short[,]). The short[,] is then used to choose int colors (stored in an int[] where each index corresponds to any of the possible short values) which is then turned into a texture by

frontBuffer1.SetData<int>(backBuffer);

(where the frontBuffer1 is a Texture2D object and backBuffer is an int[] of the aforementioned colors).

The only thing I need access to is the short[,].

"Setting up your texture so that it's flagged for dynamic updates will probably help."

I'm really ignorant when it comes to graphics. Could you point me in the directions of articles/reference material which might guide me in understanding how to do what you say?

Thanks for the help!!!

Share this post


Link to post
Share on other sites
Ok so I've been doing some reading to try to understand shaders. I think I'm starting to get it but I still have a lot of questions...

So I've got this short[,] which is really the game data. I want the vertex shader to handle the computation but I want the CPU to have access to the data. Where do I put the data to allow this to happen? Also, how do I store large arrays (like a color palette with 256 colors)? I've looked at http://msdn.microsoft.com/en-us/library/ee418289(VS.85).aspx but I'm not sure which applies. This is my first shader so I'm not sure I'm on the right track (mainly because it's hard to see where all the variables go...) but any help would be useful.

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