Search code examples
c++shaderdirectx-9

determine area of screen covered


We have an app that allows a user to wipe away a top layer image using a "brush" attached to the mouse. The brush can be any type of shape, such as a circle, star, W, or any random bizarre shape.

This is currently implemented in C++ using DirectX9 and shaders (3.0). It is a legacy system and later versions of DirectX are not available. A single channel opacity map accumulates the brush strokes. The opacity map is then used as the alpha layer between the top and bottom images.

The problem is there is a requirement to auto-wipe the rest of the image when a certain percentage of the image has been revealed. For example, if the user has already revealed 60% of the bottom layer, the app would automatically clear the rest of the opacity map to reveal all of it. The problem is trying to figure out how much of the opacity map has been wiped without locking the surface and counting the pixels, as that is really slow.


Solution

  • You could try the following approach:

    1. Create a 1x framebuffer
    2. Create a shader that renders into this framebuffer by taking the sum of all opacity values for a column of pixels and writing it into that index in the framebuffer. Each shader invocation should be responsible for a single column of pixels.
    3. Lock the framebuffer surface and sum up all of these pixel values. This should be a lot faster than doing it for the whole screen.
    4. Compare your sum to the product of the original width and height.