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 27 de maio de 2017 - 7:54 pm

    I have your gift to the mom of Holiday mainly because she is actually the best precious jewelry freak. All something she does not wear far to is actually, bracelets. I bought her that allure bracelet and/or anytime she exposed information technology yesterday she absolutley liked they! Right now the problem is, her acquiring it in and/or off of by herself. Haha… general very good supplement, things sent additionally arrived extremely very early and/or my mother was enjoying things. Thank shoppers.

  2. #2 by christian louboutin replica on 27 de maio de 2017 - 8:39 pm

    it comes within a gorgeous box appearance very good but it is a bit small also for the our moms wrist but it appearance great really want it is further

  3. #3 by on 27 de maio de 2017 - 8:40 pm

    I had gotten your gift towards my personal mother for the Holiday due she was one precious jewelry freak. The something she cannot put on far out of is actually, bracelets. I bought her that allure bracelet additionally after she opened this yesterday she absolutley enjoyed it! Now the problem is actually, her getting that on top of furthermore off by just by herself. Haha… in general awesome goods, things sent additionally arrived severely early and/or the mom is actually enjoying that. Thank you.

  4. #4 by soccer on 27 de maio de 2017 - 9:35 pm

    Hello.This article was extremely interesting, particularly because I was investigating for thoughts on this subject last Tuesday.

  5. #5 by adventure games on 27 de maio de 2017 - 9:35 pm

    Thanks for sharing superb informations. Your web site is so cool. I am impressed by the details that you have on this web site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles. You, my friend, ROCK! I found simply the information I already searched everywhere and just could not come across. What a perfect website.

  6. #6 by mens fashion on 27 de maio de 2017 - 9:36 pm

    Good ¡V I should certainly pronounce, impressed with your 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 at all. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, website theme . a tones way for your client to communicate. Excellent task..

  7. #7 by gsa ser download on 27 de maio de 2017 - 11:24 pm

    My brother recommended I might like this website.
    He was entirely right. This put up actually made my day.
    You can not imagine just how so much time I had spent for this info!
    Thank you!

  8. #8 by on 27 de maio de 2017 - 11:51 pm

    I had gotten the headphonesfor the mother of parents evening, and/or she completely enjoyed information technology! The truly sweet headphonesand saying regarding the card just that comes inside box is really emotional!! And grade of the beads is actually ideal!

  9. #9 by on 27 de maio de 2017 - 11:51 pm

    I had gotten our headphonesfor my mom towards mothers time, and/or she completely adored that it! Its completely cute headphonesand the suggesting on the card you already know comes into the package is very sentimental!! As well as the high quality of beads is actually ideal!

  10. #10 by Matthew Brickman on 28 de maio de 2017 - 3:45 am

    The data talked about in the report are some of the top available

  11. #11 by super schnelle antwort on 28 de maio de 2017 - 4:04 am

    Deshalb geben wir Ihnen für Ihre Pärchenfotos einige Tipps, um Ihre Kleidung zu perfektionieren: Suchen Sie für Ihre Pärchenfotos noch Posen?

  12. #12 by on 28 de maio de 2017 - 4:28 am

    Very good headphoneschoose, nicely priced to what is presented. Awesome gift package then poem always enclosed. Ideal concerning mother’s evening!

  13. #13 by on 28 de maio de 2017 - 4:29 am

    Awesome headphonesinvest, nicely listed and exactly what was shown. Beautiful gift package and poem always enclosed. Ideal for the mother’s time!

  14. #14 by hilfreiche ressourcen on 28 de maio de 2017 - 4:41 am

    Problemlos gab das ergänzend zum Gesicht voller Ficksosse sowie tüchtigem Pudern auch immernoch eine Ladung Natursekt Pisse den Maul der vorlauten Oralpussy.

  15. #15 by cheap christian louboutin heels on 28 de maio de 2017 - 7:07 am

    I have this one gifts towards my mom for the Xmas as she is actually a jewelry freak. Their something she cannot put on a great deal regarding are, bracelets. I bought her this charm bracelet plus anytime she opened up information technology yesterday she absolutley adored that it! Now the issue is actually, the lady buying that it concerning furthermore off of with by herself. Haha… total ideal product, it shipped additionally came completely early plus my personal mother was experiencing that. Thank one.

  16. #16 by on 28 de maio de 2017 - 7:07 am

    it comes within a striking container appearances ideal and yet it really is a bit smaller consistent for the my mothers wrist but it seems great simply want things is extended

  17. #17 by on 28 de maio de 2017 - 8:26 am

    I have that headphonesfor my mom for the moms day, furthermore she definitely adored things! It is completely sweet headphonesas well as the saying on the card that goes within the box is very sentimental!! And the excellence of the beads are great!

  18. #18 by Learning Department on 28 de maio de 2017 - 8:27 am

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

  19. #19 by Family Trip on 28 de maio de 2017 - 8:27 am

    You made some nice points there. I did a search on the subject and found most individuals will agree with your website.

  20. #20 by smd ceramic capacitos shenzhen on 28 de maio de 2017 - 9:02 am

    Thanks for every other wonderful article. Where else may anyone get that type of info in such a perfect means of writing? I’ve a presentation next week, and I’m on the search for such info.

  21. #21 by JamEffox on 28 de maio de 2017 - 9:53 am

    Amoxicilline Teva 250mg 5ml [url=]Viagra Cost[/url] Cephalexin 500mg For Toothach Levitra User Reviews [url=]Buy Xenical 120mg[/url] Discount Macrobid Website Cash Delivery Cialis Montreal Kijiji [url=]Antabuse Online Fast[/url] Cheap Viagra Online Canada Pharmacy Kamagra Legal [url=]Levitra Brand[/url] Levitra Duration Of Effect Animal Dosage Amoxicillin [url=]Comprar Priligy En Internet[/url] On sale discount isotretinoin tablets online visa accepted Keflex And Alcohol [url=]Cheap 40 Deltasone Online[/url] Amount Of Amoxicillin For Urinary Infection Le Viagra Est Il Efficace [url=]Nolvadex Dosage Pct[/url] Cialis generika 20mg kaufen Dutasteride 0.5mg Enlarged Prostate [url=]Purchase Generic Kamagra[/url] Amoxicillin Causes Yeast Infection Buy Levitra Online Canada Buy [url=]Order Prozac Tablets[/url] Cialiis Fake Companies Azithromycin No Prescription Overnight [url=]Lasix Online Usa[/url] Cephalexin Interaction With Acohol Viagra Le Monde [url=]Doxycycline Online[/url] Cialis Remboursement Lioresal Intrathecal [url=]Propecia Dosage[/url] Acheter Cipro En Andorre Cialis Verschreiben Lassen [url=]Cheap Deltasone 40mg[/url] Le Cialis Generique Est Il Efficace Prenisone Drug Store [url=]Vibramycin Online Store[/url] Cialis Pas Cher Amoxicillin Natural Remedy [url=]Levitra Brand Online[/url] Celebrex Discount Program Cialis Rezeptfrei Kaufen Paypal [url=]Fast Delivery Vibramycin[/url] Cialis Patentti Umpeutuu Suspension Liquid Amoxicillin [url=]Order Kamagra On Line[/url] Cialis Erfahrungen Forum Viagra Kosten In Der Apotheke [url=]Nolvadex Online For Sale[/url] Dapoxetine Chlorhydrate Precios Cialis Espana [url=]How Much Is Zithromax[/url] Where To Buy Periactin Pills Viagra Pfizer 25 Mg [url=]Buy Accutane Online Uk[/url] Comprar Pastillas Para Abortar Commander Cialis [url=]Order Nolvadex No Prescription[/url] Comment Durer Plus Longtemps Chambres Buy Merck Propecia [url=]Levitra For Cheap[/url] Amoxicillin And Sinus Infections Cialis Con Alcohol [url=]Order Zithromax Onlines[/url] Cephalexin Over The Counter Cialis Acquistare In Farmacia [url=]Propecia Products[/url] Levitra Fur Junge Manner Amoxicillin Urine [url=]Nolvadex Generic Name[/url] Amoxil Effets Propecia Acidos [url=]Vibramycin For Sale[/url] Canadian Pharmacy Prednisone How To Buy Elocon Website Direct Pharmacy [url=]Oratane[/url] Cheap Nexium Online No Prescription Buy Xenical Uk [url=]Cheap Nolvadex Online[/url] Boutique En Ligne Site Dura Dapoxetine Apply by phone online or in person. Scammers count on their victims being too embarrassed to report the crime. [url=]unsecured loans[/url] Protectionism and real people City may buy new police fire radios for MovieTimes CarsTrucks Trump settles lawsuit against Univision Capitol Dispatch Local leaders push for SR funding State of the art cosmetic braces Submit your youth sports phototo the ENTEK youth photo page.Unexpected utility bills Customer Notice Installment loans should be used for shortterm financial needs only not as a longterm financial solution.Antibiotics In The Keflex Family [url=]Antabuse Online Stores[/url] Mavidol

  22. #22 by klicken sie hier um zu untersuchen on 28 de maio de 2017 - 10:19 am

    Auf unserer Internet-Seite gibt es Lackfetisch Porno !

  23. #23 by hermes bracelet replica on 28 de maio de 2017 - 10:34 am

    I got your gift for the the mother for Holiday considering she was one jewelry freak. Their something she cannot wear a lot of is, bracelets. I bought her that charm bracelet and also when she opened up it yesterday she absolutley enjoyed this! Right now the issue is, this girl obtaining this regarding additionally down through by herself. Haha… general ideal product, it shipped and came severely early to our mother is actually enjoying that it. Thank we.

  24. #24 by on 28 de maio de 2017 - 11:15 am

    it comes down within a gorgeous package seems very good still the slightly smaller including concerning our parents wrist however it appears very good only really want it ended up being extended

  25. #25 by Christian Louboutin Replica on 28 de maio de 2017 - 11:15 am

    I had gotten it present of my personal mom for the Christmas due to the fact she are the jewelry freak. Your something she does not put on much of was, bracelets. I purchased her your charm bracelet and/or whenever she opened up this yesterday she absolutley enjoyed that! Today the problem is actually, her getting it upon furthermore down through by herself. Haha… overall very good supplement, that it delivered and also arrived extremely early and/or my personal mom is enjoying things. Thank a person.

1 933 934 935
(não será publicado)