Search code examples
c#windowsxnaxna-4.0

Is there an inexpensive way to transfer colour data from a RenderTarget2D on a per frame basis?


Until recently, our game checked collisions by getting the colour data from a section of the background texture of the scene. This worked very well, but as the design changed, we needed to check against multiple textures and it was decided to render these all to a single RenderTarget2D and check collisions on that.

public bool TouchingBlackPixel(GameObject p)
    {
        /*
        Calculate rectangle under the player...
        SourceX,SourceY: Position of top left corner of rectangle
        SizeX,SizeY: Aproximated (cast to int from float) size of box
        */

        Rectangle sourceRectangle = new Rectangle(sourceX, sourceY,
                                                   (int)sizeX, (int)sizeY);

        Color[] retrievedColor = new Color[(int)(sizeX * sizeY)];

        p.tileCurrentlyOn.collisionLayer.GetData(0, sourceRectangle, retrievedColor,
                                                     0, retrievedColor.Count());

        /*
        Check collisions
        */
    }

The problem that we've been having is that, since moving to the render target, we are experiencing massive reductions in FPS.

From what we've read, it seems as if the issue is that in order to get data from the RenderTarget2D, you need to transfer data from the GPU to the CPU and that this is slow. This is compounded by us needing to run the same function twice (once for each player) and not being able to keep the same data (they may not be on the same tile).

We've tried moving the GetData calls to the tile's Draw function and storing the data in a member array, but this does not seem to have solved the problem (As we are still calling GetData on a tile quite regularly - down from twice every update to once every draw).

Any help which you could give us would be great as the power that this collision system affords us is quite fantastic, but the overhead which render targets have introduced will make it impossible to keep.


Solution

  • The simple answer is: Don't do that.

    It sounds like offloading the compositing of your collision data to the GPU was a performance optimisation that didn't work - so the correct course of action would be to revert the change.

    You should simply do your collision checks all on the CPU. And I would further suggest that it is probably faster to run your collision algorithm multiple times and determine a collision response by combining the results, rather than compositing the whole scene onto a single layer and running collision detection once.

    This is particularly the case if you are using the render target to support transformations before doing collision.

    (For simple 2D pixel collision detection, see this sample. If you need support for transformations, see this sample as well.)