BitField in Shaders (SM 3 and before)

Sometimes we need to encode lots of boolean flags in a texture channel and recover it in a shader. I found that shader model 3 and before dont have bitwise operations (they only support floats … that dont have this …) !!!
Fortunately i found some help on the old good float math and i managed to solve this even on SM 2.

Here is how it is done:

1. Encoding some flags in a binary format:

Helper Class

public class ShaderUtils
        public static int CreateSpecificBitField(bool DoNotIlluminate = false, bool isBackGround = false,bool NotEffectedByMotionBlur = false , bool useCubeMapAmbientLightining = false)
            return CreateBitField(DoNotIlluminate, isBackGround, NotEffectedByMotionBlur, useCubeMapAmbientLightining);

        public static int CreateBitField(bool op1 = false, bool op2 = false, bool op3 = false, bool op4 = false, bool op5 = false,bool op6 = false,bool op7 = false,bool op8 = false,bool op9 = false)
            int flags = 
            (op1 ? 1 : 0) |
            (op2 ? 2 : 0) |
            (op3 ? 4 : 0) |
            (op4 ? 8 : 0) |
            (op5 ? 16 : 0) |
            (op6 ? 32 : 0) |
            (op7 ? 64 : 0) |
            (op8 ? 128 : 0) |
            (op9 ? 256 : 0) 

            return flags;

Creating the bitfield

var bitfield = ShaderUtils.CreateSpecificBitField(false, true,false , false)

2. Saving it to a texture channel (in some Pixel Shader, using render to texture)

float id;   /// recieve the bitfield value
PixelShaderOutput PShader( PS_INPUT pin ) : COLOR0   
    PixelShaderOutput output;   
    ///bla bla bla   
    output.color.a = id / 255;   
    return output;   

3. Recovering it in another shader and testing the flags !!!:

      sampler extraSampler :register(s1);
        float4 extra =  tex2D(extraSampler,input.TexCoord).rgba;
        int procces = round(extra.a * 255 );
        bool DoNotIlluminate = fmod(procces, 2) == 1; 
	bool isBackGround = fmod(procces, 4) >= 2; 
	bool isAmbienteCubeMap = fmod(procces, 16) >= 8; 

         if(DoNotIlluminate  || isBackGround)
		return float4(diffuseColor,1);

There is no rounding problem with the float parameters (the explanation is a bit harder, you will need to know how the worst case scenario is encoded as a mantissa and a expoent).
Shader Model > 3 introduced real integers and Bitwise operations =P

, , ,

  1. #1 by on 19 de agosto de 2017 - 3:31 pm

    Hi there! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to no backup. Do you have any solutions to protect against hackers?

  2. #2 by on 19 de agosto de 2017 - 3:33 pm

    After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  3. #3 by live tv on 19 de agosto de 2017 - 4:10 pm

    Hi there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my iphone4. I’m trying to find a template or plugin that might be able to correct this issue. If you have any suggestions, please share. Appreciate it!

  4. #4 by technology entrepreneurs on 19 de agosto de 2017 - 4:23 pm

    I’ve been browsing online greater than 3 hours as of late, but I by no means discovered
    any interesting article like yours. It is pretty value sufficient for me.
    In my opinion, if all web owners and bloggers made good content material as you did,
    the internet might be much more helpful than ever before.

  5. #5 by on 19 de agosto de 2017 - 4:44 pm

    I’ve been surfing on-line more than three hours as of late, but I never discovered any interesting article like yours. It is beautiful worth sufficient for me. In my view, if all website owners and bloggers made just right content material as you probably did, the web will likely be much more useful than ever before.

  6. #6 by Anisa Door on 19 de agosto de 2017 - 5:10 pm

    Come on baby and take the black cock in you ass already. Give us what we want. Let me suck that anal creampie out of your ass

  7. #7 by on 19 de agosto de 2017 - 6:24 pm

    Thanks a bunch for sharing this with all of us you actually know what you are talking about! Bookmarked. Please also visit my web site =). We could have a link exchange arrangement between us!

  8. #8 by watch tv on 19 de agosto de 2017 - 6:28 pm

    I have been reading out many of your articles and i can state pretty nice stuff. I will definitely bookmark your blog.

  9. #9 by watch tv on 19 de agosto de 2017 - 6:28 pm

    Woh I enjoy your posts, saved to favorites! .

  10. #10 by michael kors outlet online on 19 de agosto de 2017 - 7:17 pm

  11. #11 by hermes belt replica on 19 de agosto de 2017 - 7:18 pm

    I had gotten the headphonesof my mom of mothers day, to she absolutely adored they! Their actually sweet headphonesas well as the saying regarding the card that goes into the box is really emotional!! And high quality of the beads is actually ideal!

  12. #12 by on 19 de agosto de 2017 - 7:33 pm

    Hey! I kknow this іs kind of off topic but I ᴡɑs wondering ᴡhich bog platform are
    you using fⲟr ths website? І’m getting tired of WordPress Ƅecause Ӏ’vе had issues with hackers ɑnd I’m l᧐oking at alternatives f᧐r another platform.

    І woսld be awesome іf you coulԀ poont me іn the direction of a good

  13. #13 by on 19 de agosto de 2017 - 7:43 pm

    I got the headphonestowards my mother for the mothers day, as well as she completely loved things! It is quite pretty headphonesand stating in the card just that comes within the box is very emotional!! As well as the premium of the beads is actually great!

  14. #14 by red bottom shoes replica on 19 de agosto de 2017 - 7:43 pm

    I had gotten this one headphonesof our mom to parents time, additionally she definitely adored that! It is actually adorable headphonesas well as the stating in the card your goes inside box is very emotional!! And the grade of beads was awesome!

  15. #15 by yeezy 350 boost on 19 de agosto de 2017 - 7:52 pm

  16. #16 by garage roller doors sydney on 19 de agosto de 2017 - 8:30 pm

    Here is an excellent Blog You may Locate Fascinating that we Encourage You

  17. #17 by football betting systems on 19 de agosto de 2017 - 8:38 pm

    I like this blog so much, bookmarked.

  18. #18 by football betting systems on 19 de agosto de 2017 - 8:39 pm

    Howdy I am so happy I found your blog page, I really found you by accident, while I was researching on Aol for something else, Anyhow I am here now and would just like to say thanks for a marvelous post and a all round thrilling blog (I also love the theme/design), I don’t have time to look over it all at the moment but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the great work.

  19. #19 by cash on 19 de agosto de 2017 - 8:41 pm

    I truly appreciate this post. I?¦ve been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thx again

  20. #20 by Dual Glazed Windows on 19 de agosto de 2017 - 8:42 pm

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest
    twitter updates. I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your
    blog and I look forward to your new updates.

  21. #21 by click on 19 de agosto de 2017 - 8:42 pm

    Great – I should definitely pronounce, impressed with your web site. I had no trouble navigating through all tabs and related info ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your client to communicate. Excellent task..

  22. #22 by cheap patagonia sale on 19 de agosto de 2017 - 9:13 pm

    How do you make your site look this good. Email me if you get the chance and share your wisdom. Id be thankful!
    cheap patagonia sale

1 1.021 1.022 1.023
(não será publicado)