Bitmap performance question

If you look in my profie you see that I’ve written a .NET compiler for Scratch. What is Scratch? It’s a language to teach children the basics of programming (http://scratch.mit.edu) but it’s pretty addictive for older “children” too! I chose monogame as the basis for the compiler’s graphics side of things and have been very happy except for one set of Scratch blocks. These blocks return true or false if a sprite is touching another sprite or colour or one sprite’s colour is touching another colour. In other words they perform pixel perfect collision detection. The latest version of Scratch is written in Adobe ActionScript (Flash) and they have recently opened the source code for this https://github.com/LLK/scratch-flash/blob/master/src/primitives/SensingPrims.as. Having been intrigued I had a look at the “sensing” code. e.g.

		if(true || !app.isIn3D) {
		var sBM:BitmapData = s.bitmap();
		for each (s2 in app.stagePane.spritesAndClonesNamed(arg))
			if (s2.visible && sBM.hitTest(s.bounds().topLeft, 1, s2.bitmap(), s2.bounds().topLeft, 1))
				return true;
	}

and for colour sensing they can filter bitmaps by colour which is pretty neat e.g.

		var outBM:BitmapData = new BitmapData(srcBM.width, srcBM.height, true, 0);
	outBM.threshold(srcBM, srcBM.rect, srcBM.rect.topLeft, '==', c, 0xFF000000, 0xF0F8F8F0); // match only top five bits of each component

For my code I don’t have the those functions but use rectangular bounds for the initial hit test before using the texture map as an array and walking the pixels for a pixel perfect hit test. It works well but is no where near as quick as Scratch. Anyone any ideas how to speed things up or insight as to how Adobe get their bitmap speed?
Thanks in advance.

This is quite problematic with MonoGame not just because of performance, but also because you need the byte arrays of the textures which are not possible to get on some platforms like iOS.
My recommendation is to use a content preprocessor that generates a bool map for the textures and includes them in the XNB file. This way you would not just load a Texture2D, but a combination of a Texture2D and a bool array. Checking for hits on the two bool arrays is then as fast as it can be. This is exactly what we have done in our game Rum Run to check if what’s water and what’s not on the map (a ship can go on the water, but not anywhere else).

Never heard of a bool map? What is that; true for pixel, false for not? If so not good for colour detection plus another problem for me is that the sprite’s bitmap change. If you’ve seen my other post I now have pixelshaders which apply 7 effects to a texture. These effects need to be included in the hit detection, so the bitmaps are effectively potentially dynamic. It all works beautifully just not as quick as Scratch itself!

Yes, that’s basically it. Does your pixel shader change whether the pixel is totally transparent or not?

Yes the ghost effect changes transparency but that is always removed for pixel hit testing purposes. Some of the other pixelshaders do change the position of the pixels though; see this for the 7 effects http://youtu.be/7ZMUuey-I4U

is there no Texture2D SetData and GetData on ios ?

No, it’s not supported by OpenGL ES.

This article http://blogs.msdn.com/b/shawnhar/archive/2008/12/31/pixel-perfect-collision-detection-using-gpu-occlusion-queries.aspx has always intrigued me but I’ve never been brave enough to try it!