Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

zodiacon

Xor operation with Direct3D

This topic is 5415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, I''m trying to blit an image (texture) on the back buffer (with whatever is there) using Direct3D 9 using an already transformed 4 vertices (simulating a "normal" blit). However, I want to blit it with a XOR operation. I''ve tried blending with various source and destination, but still can''t get the desired effect. (a good test will be to blit the same image twice and watch the original background restored). Does anyone know how it can be done? (note that the images are without an explicit alpha channel).

Share this post


Link to post
Share on other sites
Advertisement
Well, you can try to figure out the appropriate blending stages using the MFCTex sample that comes with the SDK. I''m not sure if the blending stages will be able to do what you want. Play around with that sample and see if you can get the right combination of stages.

Another way to do this is to use HLSL pixel shaders. They support the boolean operations AND, OR, and NOT so it should be pretty easy. A pixel shader that does this would probably look something like this:

sampler2D image0 : register( s0 );
sampler2D image1 : register( s1 );

float4 main( float2 vTexCoord0 : TEXCOORD0 ) : COLOR0
{
float3 vImage0 = tex2D( image0, vTexCoord0 );
float3 vImage1 = tex2D( image1, vTexCoord0 );

// XOR can be implemented by (x && !y) || (!x && y)
return (vImage0 && !vImage1) || (!vImage0 && vImage1);
}

Give that a shot and see if it works. I''ve never tried to do what you''re asking but I think that this will work. I could be wrong though.

I''m sure there are other ways to do what you ask and hopefully someone else on this board will be able to suggest a few of them.

neneboricua

Share this post


Link to post
Share on other sites
Thanks for the idea. I tried, but no success there.

One of the problem is that pixel shaders don''t support boolean operations.
Another (solveable) problem is the need to use a different target so that the back buffer contents are received through a texture register to be able to do any operation related to the already existing pixel.

Any other ideas? Anyone?

Share this post


Link to post
Share on other sites
quote:
Original post by zodiacon
Thanks for the idea. I tried, but no success there.

One of the problem is that pixel shaders don''t support boolean operations.


Well, the HLSL documentations says that shaders do indeed support boolean math. Specifically, the operators &&, ||, and ! are supported.

I just thought that the shader code I posted above could be improved and corrected like this (false is assumed to be 0. true is assumed to be antying > 0):

sampler2D image0 : register( s0 );
sampler2D image1 : register( s1 );

float4 main( float2 vTexCoord0 : TEXCOORD0 ) : COLOR0
{
float4 vImage0 = tex2D( image0, vTexCoord0 );
float4 vImage1 = tex2D( image1, vTexCoord0 );

bool4 bImage0 = ( vImage0 > 0.0f );
bool4 bImage1 = ( vImage1 > 0.0f );


// XOR can be implemented by (x && !y) || (!x && y)


// First half of XOR operation.

float4 vColor0 = (bImage0 && !bImage1) ? vImage1 : 0.0f;

// Second half of XOR operation

float4 vColor1 = (!bImage0 && bImage1) ? vImage2 : 0.0f;

// Combine the result of XOR

return vColor0 + vColor1;
}

This code can probably be cleaned up a bit but I think it will work.
quote:

Another (solveable) problem is the need to use a different target so that the back buffer contents are received through a texture register to be able to do any operation related to the already existing pixel.


Getting data from the back buffer can be expensive. To get the data in the back buffer as a texture, you would need to copy the data from the back buffer onto the surface of a texture, and then feed it through the pipeline.

Instead of doing that, you should render to the surface of a texture directly and only render to the back buffer in your final pass. If you render to the surface of a texture instead, you''ll be able to use those results in the pipeline more easily.

Hope this helps,
neneboricua

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!