Blaxx
Blaxx

Reputation: 225

C# DirectX Performance

For a falling sand game I need to lock a surface, then modify the pixels according to a set of rules, and then unlock it. The Texture has a Size of 800x500 and has the Format A8R8G8B8.

TEXTURE DECLARATION

texture = new Texture(device, 800, 500, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);

The Update-Method (which should ideally be called about 40/s)

    public new void Update()
    {
        count++;
        byte[] data = (byte[])texture.LockRectangle(typeof(byte), 0, LockFlags.None, 800 * 500 * 4);
        for (int i = 0; i < 1600000; i++)
        {
            data[i] = 255;
        }
        texture.UnlockRectangle(0);
    }

I have a highend graphics card and this simple loop reduces my fps rate to 10-13.

Is there a faster way in DirectX to directly change the pixels of a surface?

Upvotes: 1

Views: 738

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100630

Usually game model is stored in regular memory and handled by CPU, while graphic card is used purely to render results. If you have no particular reasons not go this way it would be easier to write and debug.

In you case you seem to have all game data in video memory. As Euphoric pointed out in this case you should make all logic run in shaders. There are plenty of samples how to perform data manipulation using several texture targets (2 would be probably enough in your case).

I'd recommend to find "game of life" sample written using shaders (search - http://www.bing.com/search?q=directx+shader+sample+%22game+of+life%22, I'd probably start with this one - http://www.xnainfo.com/content.php?content=21). Game of life is probably simplest thing close enough to waht you trying to achieve. DirectX SDK and Nvidia SDK are good sources of samples too like - http://developer.download.nvidia.com/SDK/9.5/Samples/3dgraphics_samples.html.

For more samples search for "prostprocess shader" as pretty much all of them are implemented by manipulating final rendered image with shaders.

Upvotes: 0

Euphoric
Euphoric

Reputation: 12859

What are you doing is reading this texture from memory of graphics card into computer memory, then modyfying it and then writing it back. This obviously means huge performance kill because you need to wait for copying between both memories.

Usualy, this kind of operation is achieved using Shaders all on graphics card.

Upvotes: 3

Related Questions