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 14 de julho de 2017 - 9:30 pmスイス 時計 ブランド
    [url=]シャネル 時計 偽物 見分け[/url]

  2. #2 by Merrell Shoes sale on 14 de julho de 2017 - 10:01 pm

    This blog is extremely cool! How did you make it ?
    Merrell Shoes sale

  3. #3 by اپل ایدی رایگان on 14 de julho de 2017 - 10:25 pm

    You might unquestionably visit knowledge of the task you are writing اپل ایدی رایگان. Your sector hopes for far more zealous internet writers such as you which are certainly not scared to convey how they feel. Continually pursue the soul.

  4. #4 by Grirmsor on 14 de julho de 2017 - 10:44 pm

    Ist Einverstanden, die nГјtzliche Information
    Rexuiz FPS Game

  5. #5 by ecco online sale on 14 de julho de 2017 - 11:14 pm

    Interesting point of view. Wondering what you think of its implication on society as a whole though? People obviously get frustrated when it begins to affect them locally. Ill be around soon to check out your response.
    ecco online sale

  6. #6 by Asics Outlet Online on 14 de julho de 2017 - 11:14 pm

    This website has some really helpful info on it! Thank you for informing me.
    Asics Outlet Online

  7. #7 by furla outlet online on 14 de julho de 2017 - 11:14 pm

    Interesting article, thank you. Could you tell me about the second paragraph in more detail?
    furla outlet online

  8. #8 by cheap lv bags on 14 de julho de 2017 - 11:14 pm

    I really enjoy reading from his blog, keep up the great work.
    cheap lv bags

  9. #9 by louis vuitton for cheap on 14 de julho de 2017 - 11:15 pm

    Hi there I like your post
    louis vuitton for cheap

  10. #10 by chanel bags store on 14 de julho de 2017 - 11:15 pm

    Hi I have just bought a new Nec and I thought you could tell me where to find music ringtones any ideas?
    chanel bags store

  11. #11 by cheap LK Bennett on 14 de julho de 2017 - 11:15 pm

    Heya i am for the first time here. I found this board and I find It truly useful & it helped me out a lot. I hope to give something back and help others like you helped me.
    cheap LK Bennett

  12. #12 by hermes belt replica on 14 de julho de 2017 - 11:29 pm

    The device is in such a ideal expense I did not considered all excellence would be and so excellent. It’s perfect. This one mom might adore that on top of Xmas morning when she opens up gift therefore appearance like I invested even more, still cost ended up being really ideal!!

  13. #13 by Arcteryx Sale on 14 de julho de 2017 - 11:29 pm

    Took me time to read all the feedback, however I really loved the article. It proved to be very useful to me and I am positive to all the commenters here! It is always good when you cannot only learn, but additionally engaged! I am certain you had pleasure writing this article. Anyway, in my language, there usually are not much good source like this.
    Arcteryx Sale

  14. #14 by why not find out more on 14 de julho de 2017 - 11:47 pm

    we prefer to honor quite a few other online internet sites on the internet, even though they arent linked to us, by linking to them. Underneath are some webpages worth checking out

  15. #15 by anal sex on 14 de julho de 2017 - 11:52 pm

    usually posts some pretty intriguing stuff like this. If youre new to this site

  16. #16 by hermes eau de toilette on 15 de julho de 2017 - 12:01 am

    freedomand pas un murmure. Encore était chaque homme son frère, et si certains forgothis main une fois ouvert, il oublia pas plus complètement que ne le Senator.He est allé très loin pour payer ses dettes. Il sentait l’honneur lié, en effet, askhis soeur de redonner la ferme qu’il lui avait donné, qui, très properlypeople dit, elle a refusé de le faire. Rien ne pourrait tuer l’espoir dans le Senatorsbreast; il restituer la ferme dans une autre année, dit-il; mais le cabinet d’sisterwas, et sans un mot encore, le sénateur ont d’autres façons andschemed à travers les nuits, et travaillaient et montaient et marchaient et tradedHell Fer Sartain et autres Stories20through les jours, jusqu’à maintenant, quand la lumière commençait à poindre, hisend était come.This était les sénateurs dernière transaction, et en vue, dans un Kentuckyvalley, était à la maison. Curieusement,

  17. #17 by draftkings promo code on 15 de julho de 2017 - 2:42 am

    New draftkings promo code, win big

  18. #18 by mk outlet online on 15 de julho de 2017 - 4:36 am

    I was just chatting with my coworker about this today at Outback steak house. Don’t remember how in the world we landed on the topic in reality, they brought it up. I do recall eating a exceptional fruit salad with cranberries on it. I digress
    mk outlet online

  19. #19 by replica christian louboutin sale on 15 de julho de 2017 - 5:41 am

    It supplement was with such a great rate I not considered all high quality would be quite exceptional. It’s breathtaking. Your mama will love it in Christmas day whenever she opens present therefore appears as though I invested way more, however rates was only great!!

  20. #20 by hermes belt replica on 15 de julho de 2017 - 5:42 am

    We have purchased this brand name regarding bracelet a number of days. Every single a person is extremely attractive, made very well, cannot tarnish and meaningful based on what single you purchase then who one provide that it in order to.

  21. #21 by cheap parajumpers jackets on 15 de julho de 2017 - 6:25 am

    brilliant web publication article. I’m going to bookmark and read much more often. I adore the blog template
    cheap parajumpers jackets

  22. #22 by dang ky logo on 15 de julho de 2017 - 6:42 am

    Hi there friends, its great piece of writing on the topic of teachingand
    fully explained, keep it up all the time.

  23. #23 by Get More Information on 15 de julho de 2017 - 6:50 am

    Wonderful story, reckoned we could combine some unrelated information, nonetheless actually really worth taking a look, whoa did a single discover about Mid East has got a lot more problerms as well

  24. #25 by Super Head Honcho Masturbator on 15 de julho de 2017 - 7:41 am

    Wonderful story, reckoned we could combine a handful of unrelated information, nevertheless genuinely really worth taking a look, whoa did a single find out about Mid East has got additional problerms too

  25. #26 by Your Domain Name on 15 de julho de 2017 - 8:31 am

    that may be the end of this write-up. Here youll locate some websites that we believe youll enjoy, just click the hyperlinks over

  26. #27 by Cartier Love Bracelet Replica on 15 de julho de 2017 - 8:41 am

    This product or service is in that awesome rates I by no means considered the high quality would be hence excellent. Its stunning. This particular mom does like they concerning Xmas day where she opens up gifts and it looks like I devoted far more, but pricing ended up being just ideal!!

  27. #28 by hermes h bracelet replica on 15 de julho de 2017 - 8:42 am

    I have purchased your brand name concerning bracelet some circumstances. Every single a person is extremely adorable, created very well, does not tarnish to meaningful based on which kind of an you buy and whom shoppers present that it in order to.

  28. #29 by coach handbags outlet online on 15 de julho de 2017 - 10:08 am

    Thnkx so much for this! I havent been this moved by a blog for a long time! You have got it, whatever that means in blogging. Well, You are definitely somebody that has something to say that people should hear. Keep up the wonderful work. Keep on inspiring the people!
    coach handbags outlet online

  29. #30 by web site on 15 de julho de 2017 - 10:37 am

    very few websites that occur to become detailed below, from our point of view are undoubtedly effectively worth checking out

  30. #31 by useful source on 15 de julho de 2017 - 11:44 am

    just beneath, are numerous completely not related web sites to ours, on the other hand, they are surely worth going over

  31. #32 by moncler outlet online on 15 de julho de 2017 - 11:57 am

    I have been reading the posts, and I have to concur with what John said.
    moncler outlet online

  32. #33 by on 15 de julho de 2017 - 12:39 pm

    This one goods was with such a awesome expense I do not attention that the excellence would be hence excellent. It is stunning. This particular mama does really like things regarding Xmas early morning after she opens up gifts therefore appearance as though I invested more, although rates ended up being only very good!!

  33. #34 by hoer zu on 15 de julho de 2017 - 12:54 pm

    Die krassen Close-Up Aufnahmen bei den Dreharbeiten ließen keine Zweifel aufkommen, dass Hauptdarsteller Kerry Fox und Rylance echten Oralverkehr miteinander hatten.

  34. #35 by blog on 15 de julho de 2017 - 3:38 pm

    Der Kunde kann sich im Laden oder zu Hause kompetent und individuell beraten lassen.

  35. #36 by van cleef and arpels replica on 15 de julho de 2017 - 4:29 pm

    I’ve purchased this brand name out of bracelet a number of instances. Every single one is very pretty, done well, does not tarnish plus significant depending on which an individual you buy then that we present this at.

  36. #37 by click for more info on 15 de julho de 2017 - 5:20 pm

    Wonderful story, reckoned we could combine a number of unrelated information, nevertheless actually really worth taking a look, whoa did one particular find out about Mid East has got much more problerms as well

  37. #38 by cheap marmot jackets on 15 de julho de 2017 - 5:28 pm

    cheap marmot jackets

  38. #39 by on 15 de julho de 2017 - 6:25 pm

    This device was in that ideal pricing I did not attention some sort of grade would be and so exceptional. Its gorgeous. This one mama will like they on Christmas day when she starts gifts therefore appearance as though I spent far more, but cost had been simply very good!!

(não será publicado)