   need help: HLSL PS2.0 - XOR filter
  Hello GameDevelopers !I'm a beginner when it comes to pixelshaders so i need some tips and tricks for the following task. I've been googling for days now - so i hope that gamedev might have the answer:I want to make a XOR-filter in PixelShader 2.0 (Silverlight)until now i've come to the following code:sampler2D image0 : register( s0 ); //image1sampler2D image1 : register( s1 ); //image2float4 main( float2 vTexCoord0 : TEXCOORD0 ) : COLOR0{   //now what ?}PS2.0 doesn't support XOR operators between Integers...so what do i do ?   
  Really?  PS2.0 doesn't support the ^ (xor) operator?   ;  ps_2_0 doesn't support integer operations at all. That sort of functionality wasn't added until DX10 with SM4.0. You would have to emulate an XOR with floating point, and honestly I'm not sure you could do that with ps_2_0's 64 instruction limit.   ;  Hi,You know the equivalent of XOR operation is:A XOR B = {[NOT(A)] AND B} OR {A AND [NOT(B)]}So, I think that you can emulate XOR filter with some math. For example;NOT (A) = (1.0 - A)A OR B  = A + B (This must be clamped to a value, I think)A AND B = A * BI don't know what you mean with XOR-filter but I think that you wanted to do "image0 XOR image1" operation. Here a sample:sampler2D image0 : register(s0);sampler2D image1 : register(s1);half4 main (float2 vTC : TEXCOORD0) : COLOR0{    half4 A = tex2D (image0, vTC),B = tex2D (image1, vTC);   half4 notA = 1.0f - A,notB = 1.0f - B;    half4 result = (notA * B) + (notB * A);    //result = clamp (result, something, another thing);    return result;    }hth.Rohat.   ;  I think he wants a "bitwise operator" where the code you describes (which is pretty ingenious btw) does it all as if it were one bit (so to speak :P)   ;  Use a look-up-table. If you need to XOR two bytes together, then that's a function that takes two 8-bit inputs and produces an 8-bit output. You can encode such a function in a 256x256 single channel 8bpp texture.The CPU-side code for generating the texture is straightforwardtypedef unsigned char ubyte;ubyte texture[256*256];for( int y=0, i=0; y!=256; ++y )  for( int x=0; x!=256; ++x, ++i )    texture = (ubyte)(x^y);And then to use the LUT in your shader, you just use the two color values as texture coordinates:float3 inputA = tex2D(image0, vTexCoord0).rgb;float3 inputB = tex2D(image1, vTexCoord0).rgb;float4 result;result.r = tex2D(imageLUT, float2(inputA.r, inputB.r)).r;result.g = tex2D(imageLUT, float2(inputA.g, inputB.g)).r;result.b = tex2D(imageLUT, float2(inputA.b, inputB.b)).r;result.a = 1.0;;Quote:Original post by programci_84A XOR B = {[NOT(A)] AND B} OR {A AND [NOT(B)]}NOT (A) = (1.0 - A)A OR B  = A + B (This must be clamped to a value, I think)A AND B = A * Bit's almost true what you are saying:floating point:NOT A = 1.0 - A;Integer (ubyte)NOT A = 256 - A;but these are wrong:A OR B = A + B;x1010 OR x0101 = 10 + 5 = 15x1101 OR x0101 = 13 + 5 = 18 = x10010 (which is thereby wrong)A AND B = A * B;x1000 OR x0100 = x0000 (the AND operator require two 1's to make a 1)like this:x1100 OR x0100 = x0100the AND operator is more or less a modulus operator i think, and i don't think it's possible to use normal Math operations to calculate the output... unless using a LUT (i may be wrong)[Edited by - Montago on June 3, 2010 5:15:18 AM];Quote:Original post by HodgmanUse a look-up-table. If you need to XOR two bytes together, then that's a function that takes two 8-bit inputs and produces an 8-bit output. You can encode such a function in a 256x256 single channel 8bpp texture.The CPU-side code for generating the texture is straightforward*** Source Snippet Removed ***And then to use the LUT in your shader, you just use the two color values as texture coordinates:*** Source Snippet Removed ***Hello - this solution looks very interesting, a couple of questions:1. do i need to pass the LUT along with the 2 textures/images or is it stored/generated by the Shader ?2. why does this work : tex2D(imageLUT, float2(inputA.r, inputB.r))tex2D takes a 2D texture (in this case a 1D texture) then uses the position (in this case a float2(r1,r2)) does the function tex2D translate the float2 position into a 1d range-index ?Thanks   ;  1) Yeah the LUT is a 3rd texture. You generate this texture on the CPU once, and then you can use it as "image3".2) It should be a 2D texture (256 x 256), not a 1D texture (1x65536).In memory, it's allocated as 65536 bytes, but when you give this array of data to Direct3D/OpenGL, you should tell it that this array has a width and height of 256 (which works out to the same number of bytes).Just looking at the first 2 bits, the table looks like below. The italic numbers aren't part of the table, they're the column/row numbers. By using your two inputs numbers (the left/right side of the ^ operator) as row/column numbers, then you find the result (bold number) in that cell of the table.   00 01 10 11 ..  <- input 100 00 01 10 11 ..01 01 00 11 10 ..10 10 11 00 01 ..11 11 10 01 00 .. ^ .. .. .. .. .. |input 2So, when we use float2(input1, input2) as a texture coordinate, input1 chooses a column and input2 chooses a row.One more thing -- make sure you use Point/Nearest texture filtering, not Linear/Smooth texture filtering (and no mip-mapping) for the LUT texture!Quote:Original post by Montagobut this is wrong:A AND B = A * BProgramci_84 is actually correct, but only if you're treating each float as a single bit (boolean).If you assume that the float-inputs are either zeros or ones then AND is a*b, OR is saturate(a+b), and as Programci_84 shows you can build an XOR from this.You can perform a cast, like bool b = aByte ? true : false;withfloat aByte = 0.1337;//~34/255float aBool = step(1.0/255.0, aByte);// aBool = (aByte >= 1/255) ? 1 : 0If you need to do your XOR on bytes not bits though, then a LUT is the only thing I can think of.[Edited by - Hodgman on June 3, 2010 5:09:27 AM];Quote:Original post by HodgmanOne more thing -- make sure you use Point/Nearest texture filtering, not Linear/Smooth texture filtering (and no mip-mapping) for the LUT Awesome !!now, final question... how do i control the filtering that you talk about ?ps. I've ordered 2 books on Amazon about HLSL PS2.0 (the ShaderX 2.0 books) - i hope i'm soon up'n running on PixelShaders :-)[Edited by - Montago on June 3, 2010 7:59:21 AM]