Sign in to follow this  
Automagisk

Quickly update Texture2D on every frame?

Recommended Posts

Im trying to implement a 2D fluid simulation based on a paper by Jos Stam (grid based fluid) where I need to be able to update a grid of pixels (512x512) on the screen at each frame. Im a bit new to XNA but I have understood that pixels and lines etc doesnt exist in the world of a 3d-engine, its only triangles. But what is the best method to render a set of pixels like this? I have tried using a Texture2D but it is extremely slow to update for some reason. Is it really the best option? All pixels will change on every frame so the texture will be completely new every time. Is it better to draw each pixel with two triangles instead?

Share this post


Link to post
Share on other sites
Maybee its my implementation thats not right:

protected override void Update(GameTime gameTime)
{
// for testing:
for (int y = 0; y < pixels.Height; ++y)
{
for (int x = 0; x < pixels.Width; ++x)
{
data[y * pixels.Width + x] = (based on gameTime)
}
}

GraphicsDevice.Textures[0] = null; // I seem to have to do this first...
pixels.SetData<Color>(data);

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);

spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Texture, SaveStateMode.None);
spriteBatch.Draw(pixels, new Vector2(0, 0), null, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}

Share this post


Link to post
Share on other sites
data[y * pixels.Width + x] =

is executed 262,144 times
thats a lot

depending on what platform your target is DX11 compute shader is the perfect thing for this

EDIT

but the problem is not to copy the data to the graphic memory but to generate the data

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