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 NAPSR Scam on 20 de julho de 2017 - 11:21 pm

    “I have been examinating out a few of your posts and i must say pretty clever stuff. I will surely bookmark your website”

  2. #2 by asia carrera on 20 de julho de 2017 - 11:29 pm

  3. #3 by Health Article on 20 de julho de 2017 - 11:30 pm

    Great amazing issues here. I am very happy to peer your article. Thanks so much and i am having a look ahead to contact you. Will you kindly drop me a mail?

  4. #4 by louis vuitton bags outlet on 20 de julho de 2017 - 11:42 pm

    REPLY;The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future itll do even better in those areas, but for now its a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPods strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.
    louis vuitton bags outlet

  5. #5 by look at here on 21 de julho de 2017 - 2:01 am

    that may be the end of this report. Right here youll discover some websites that we assume you will appreciate, just click the hyperlinks over

  6. #6 by read this on 21 de julho de 2017 - 5:24 am

    very few internet sites that transpire to become comprehensive below, from our point of view are undoubtedly properly really worth checking out

  7. #7 by student nurse t shirt designs on 21 de julho de 2017 - 5:35 am

    He made religion interesting and was fully unbiased.

  8. #8 by cole haan outlet on 21 de julho de 2017 - 5:54 am

    The moment I saw your blog was like wow. Thank you for putting your effort in making this post. Conversion Prophet Review
    cole haan outlet

  9. #9 by navigate to this site on 21 de julho de 2017 - 5:55 am

    just beneath, are numerous entirely not associated sites to ours, however, they’re surely worth going over

  10. #10 by stand off installation glass railings on 21 de julho de 2017 - 6:24 am

    although sites we backlink to beneath are considerably not associated to ours, we really feel they are essentially really worth a go through, so possess a look

  11. #11 by stand off installation glass railings on 21 de julho de 2017 - 6:34 am

    although web sites we backlink to beneath are considerably not connected to ours, we feel they may be really really worth a go by, so have a look

  12. #12 by discover this info here on 21 de julho de 2017 - 7:48 am

    although internet websites we backlink to beneath are considerably not related to ours, we feel they are really worth a go by way of, so possess a look

  13. #13 by best quality glass on 21 de julho de 2017 - 8:03 am

    we came across a cool internet site that you just could take pleasure in. Take a look if you want

  14. #14 by lace frontal on 21 de julho de 2017 - 8:09 am

    lace frontal are classy, great and great for any situation.

  15. #15 by خرید اپل ایدی on 21 de julho de 2017 - 8:39 am

    just beneath, are a lot of absolutely not connected internet sites to ours, however, they may be surely worth going over

  16. #16 by runaway-luv on 21 de julho de 2017 - 10:13 am

    Howdy! I just wish to offer you a big thumbs up for your great information you have here on this post. I’ll be returning to your web site for more soon.

  17. #17 by MCM Sale on 21 de julho de 2017 - 11:36 am

    This is a interesting post by the way. I am going to go ahead and bookmark this article for my brother to read later on tonight. Keep up the good work.
    MCM Sale

  18. #18 by nfl for sale on 21 de julho de 2017 - 12:12 pm

    I really enjoyed your post. However, it is not properly displaying in my monitor. I recommend you make sure your webdesign is compatible with all browsers. Otherwise, Three thumbs up
    nfl for sale

  19. #19 by hermes belt replica on 21 de julho de 2017 - 1:05 pm

    That supplement ended up being at that awesome pricing I never thought all excellence would be quite exceptional. It’s stunning. Your mom does love things at Holiday morning once she opens gift therefore appearance like I devoted much more, still pricing is only ideal!!

  20. #20 by Christian Louboutin Replica on 21 de julho de 2017 - 1:05 pm

    I’ve bought our brand name concerning bracelet a few times. Every a person is very adorable, created very well, does not tarnish plus meaningful based on which kind of single you purchase furthermore just who we promote they in order to.

  21. #21 by on 21 de julho de 2017 - 3:11 pm

    This product had been with that awesome price tag I never idea your excellent would be hence excellent. Its beautiful. This mama is going to like this on Christmas time morning when she opens gift therefore appearances as though I spent far more, still pricing ended up being exclusively very good!!

  22. #22 by Louis Vuitton Handbags Outlet on 21 de julho de 2017 - 3:22 pm

    I consider this to be a great article! I have only one thought. How do you do it? Everything on this website is really good.
    Louis Vuitton Handbags Outlet

  23. #23 by parajumpers store online on 21 de julho de 2017 - 3:24 pm

    This is a good subject to talk about. Usually when I find stuff like this I stumble it. I dont think this would be the best to submit though. Ill take a look around your site though and submit something else.
    parajumpers store online

  24. #24 by php web programming on 21 de julho de 2017 - 3:30 pm

    Wonderful story, reckoned we could combine some unrelated data, nonetheless truly worth taking a look, whoa did one particular find out about Mid East has got more problerms also

  25. #25 by Shopping on 21 de julho de 2017 - 3:42 pm

    You could definitely see your expertise within the paintings you write. The arena hopes for even more passionate writers such as you who aren’t afraid to say how they believe. At all times follow your heart. “Faith in the ability of a leader is of slight service unless it be united with faith in his justice.” by George Goethals.

1 990 991 992
(não será publicado)