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 tutorial seputar komputer on 30 de setembro de 2016 - 5:25 pm

    The right way to change the preferences on a secondary Tumblr blog?

  2. #2 by on 30 de setembro de 2016 - 5:51 pm

    man you guys who are saying fuck drake he’s bad and all this fuck you guys for real he said one thing in his song and yeah it might have not been respectful to tupac but he’s still a great artist he can sing and kill it on the mic so to you guys sayin he’s just a chorus yall some fucken idiots, just cuz he wasnt as big of a pac fan as he is of jay doesnt mean he isnt a good artist

  3. #3 by permanent resident on 30 de setembro de 2016 - 6:00 pm

    Good post! :)

  4. #4 by deportivas nike running baratas on 30 de setembro de 2016 - 6:18 pm

    you’re really a just right webmaster. The website loading pace is amazing. It sort of feels that you are doing any distinctive trick. Also, The contents are masterwork. you have done a great process on this topic!

  5. #5 by real swords on 30 de setembro de 2016 - 6:24 pm

    Beautiful person and fast shipping

  6. #6 by apply pr in singapore on 30 de setembro de 2016 - 6:42 pm

    Good post! :)

  7. #7 by zusätzliche Ressourcen on 30 de setembro de 2016 - 6:50 pm

    Feucht kann es beim Überqueren der Bäche durchaus werden.

  8. #8 by NMD Runner Triple Black Boost on 30 de setembro de 2016 - 7:30 pm

    Youre so cool! I dont suppose Ive read anything like this before. So nice to find somebody with some authentic ideas on this subject. realy thank you for starting this up. this website is something that’s needed on the internet, somebody with a little originality. useful job for bringing one thing new to the internet!

  9. #9 by on 30 de setembro de 2016 - 7:39 pm

    "please type in why ur interested" in red, even though I had already typed it in… I clickd submit a few times but the same thing came up…

  10. #10 by UFC 204 Live Stream on 30 de setembro de 2016 - 7:40 pm

    Yes! Finally someone writes about keyword1.|

  11. #11 by UFC 204 Live Stream on 30 de setembro de 2016 - 7:51 pm

    Hi there everybody, here every person is sharing such familiarity, so it’s good to read this website, and I used to visit this web site every day.|

  12. #12 by Swords Bag on 30 de setembro de 2016 - 8:39 pm

    Great transaction. Exp茅dition rapide superbe et grand emballage . Merci !

  13. #13 by UFC 204 Live Stream on 30 de setembro de 2016 - 8:40 pm

    It is appropriate time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I wish to suggest you some interesting things or advice. Perhaps you can write next articles referring to this article. I want to read more things about it!|

  14. #14 by Besonders interessante Seiten on 30 de setembro de 2016 - 8:42 pm

    wie es scheint ist sie alleine.

  15. #15 by Tanto knife on 30 de setembro de 2016 - 8:50 pm

    Great transaction et le vendeur – livraison rapide! Handshoe est belle! Je vous remercie !

  16. #16 by UFC 204 Live Stream on 30 de setembro de 2016 - 9:29 pm

    If you are going for most excellent contents like me, simply visit this site everyday for the reason that it provides quality contents, thanks|

  17. #17 by on 30 de setembro de 2016 - 9:50 pm

    Fantastic stories! The South of France idea intrigues, maybe for one of my characters. Right now, I'm propped in bed with my laptop, my morning 'office' for about an hour. Then I flop on the sofa in the afternoon for a couple of hours.

  18. #18 by UFC 204 Live Stream on 30 de setembro de 2016 - 9:57 pm

    It’s going to be ending of mine day, except before ending I am reading this fantastic article to improve my experience.|

  19. #19 by Skate Hoverboard on 30 de setembro de 2016 - 10:15 pm

    Reduce Several components.

  20. #20 by UFC 204 Live Stream on 1 de outubro de 2016 - 12:01 am

    Hey! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a outstanding job!|

  21. #21 by UFC 204 Live Stream on 1 de outubro de 2016 - 1:01 am

    Fine way of telling, and nice paragraph to obtain facts about my presentation subject matter, which i am going to convey in university.|

  22. #22 by on 1 de outubro de 2016 - 1:27 am

    Katha –Yes! I guess Tom is “cute” and watchable. I don’t think he’s a very good actor, though, and his performances are not unique or everlasting. If he has a talent it is picking the right script that fits his limited persona.Yes, Anthony Quinn is irreplaceable — as is Audre Hepburn. Barbara Streisand is another irreplaceable as is Liza Minelli.

  23. #23 by ray ban pilot solbriller dame danser on 1 de outubro de 2016 - 1:42 am

    I know this website offers quality dependent articles or reviews and extra data, is there any other web site which presents these information in quality?

  24. #24 by privatlån on 1 de outubro de 2016 - 1:49 am

    macht nicht die Dosis das Gift? aber du hast recht…. mafia hatte mir seinerzeit enorm viel zeit gekostet…. bin ja damals dann auch radikal weg…. war eine kluge Entscheidung, aber ich schließe mich da Gabi an…du wirst hier fehlen

  25. #25 by visakort on 1 de outubro de 2016 - 1:53 am

    A reasoned analysis, but it avoids the central question, which is the policy of appeasement to the Muslim Brotherhood, the fountainhead of Islamic terror, including Hamas and Al Qaeda. How do you explain a shoulder fired missile ending up in the hands of Hamas a mere week later? The nexus of the criminality lies in the decision to expand US assets (human lives) for the agenda of the Muslim Brotherhood. That, my friend, is aiding and abetting the enemy.

  26. #26 by real sword on 1 de outubro de 2016 - 2:08 am

    Great transaction. Article comme décrit . Bateau rapide . Merci !

  27. #27 by best wakizashi on 1 de outubro de 2016 - 2:09 am

    super prix , expédition rapide

  28. #28 by ninja weapons on 1 de outubro de 2016 - 2:41 am

    Expédié rapidement . Je vous remercie . Un vendeur de +

  29. #29 by katana sword on 1 de outubro de 2016 - 2:45 am

    Lisser transaction! Je vous remercie !

  30. #30 by nike air max 1 questlove customs on 1 de outubro de 2016 - 2:50 am

    If you are going for most excellent contents like myself, just visit this site every day because it offers feature contents, thanks

  31. #31 by UFC 204 Live Stream on 1 de outubro de 2016 - 3:16 am

    I think this is one of the most significant information for me. And i’m glad reading your article. But wanna remark on some general things, The site style is ideal, the articles is really excellent : D. Good job, cheers|

  32. #32 by UFC 204 Live Stream on 1 de outubro de 2016 - 3:34 am

    I’m amazed, I must say. Seldom do I encounter a blog that’s both educative and engaging, and let me tell you, you have hit the nail on the head. The problem is something that too few folks are speaking intelligently about. Now i’m very happy that I came across this during my hunt for something relating to this.|

  33. #33 by visakort on 1 de outubro de 2016 - 3:50 am

    Undskyld undskyld undskyld. Jeg ved godt, man ikke må grine af andres ulykke. Men kan lige se dig, som dette her ulykkelige menneske, der kigger ærgerligt op på opkastende blødende folk, for derefter at gå amok med et buskort. Men ØV med hånden! Og god bedring med dig.

1 453 454 455
(não será publicado)