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 pierre balmain sale on 15 de julho de 2017 - 6:25 pm

    Great Share! Would you be interested in exchanging links?
    pierre balmain sale

  2. #2 by emogesei on 15 de julho de 2017 - 6:47 pm

    「インドネシア政府は債務保証をする必要がない」という言葉に踊らされて、日本の新幹線技術の優秀性を知りながら、中国の高速鉄道導入を決定してしまいました。  電通が生き残るチョン排除というのは、メインの癌を切除せずに末梢部だけを頑張って取り除くようなものだ。 [url=http://www.key7jp.com/microsoft-office365-proplus-9-58.html]office personal 2016 ダウンロード 版[/url]
    (標準32bit)8GBに増設された場合は、自動的に64bit(確認用)になります。 F. 保存機能のすべてのエグゼクテゖブメールボックスを配置します。
    [url=http://www.key7jp.com/norton-360-500-41.html]ノートンアカウント[/url] ◇ 免責 ◇本ソフトの著作権は、作者である あさりのみそしる に帰属します。 Fix it を使用して Office を自動的にアンインストール。
    [url=http://www.key7jp.com/microsoft-windows-server2012-r2-7.html]windows sever 2012 アカ&#124 sever 20127;ミ&#124 sever 20123;ク[/url]
    JapanCertは専門のIT業界での評判が高くて、あなたがインターネットでJapanCertの部分のMicrosoft 70-673全真模擬試験資料を無料でダウンロードして、弊社の正確率を確認してください。  これで最後のアップデートかも知れないと思い、#10122に加算した状態。 [url=http://www.key7jp.com/office2013-home-23.html]office 2013 通販[/url]
    これは、8万や9万円で性能より品質を向上させるか?あ、ちなみに言っちゃうと、メーカー製のほうが安いです(;´∀`)性能はこっちのほうがちょっと良い程度で・・・CPU.Intel CeleronG530 3500円Celeronとはいえ、下手なC2Dより優秀です。 この問題が発生した場合は、以下のレジストリーキーを削除します。
    [url=http://www.key7jp.com/office-Professional-2016-plus-5pc-62.html]microsoft excel 価格[/url] 【翻訳編集】 AFPBB News 【AFP=時事】(更新)英国やスペインなどで12日、病院や大手企業などを狙ったサイバー攻撃が相次いで発生した。 Pass4Testは100%でMicrosoftの70-689日本語「Upgrading Your Skills to MCSA Windows 8 (70-689日本語版)」認定試験に合格するのを保証いたします。
    [url=http://www.key7jp.com/microsoft-office365-proplus-9-58.html]office2016 プロダクト キー[/url]

  3. #3 by Tunderkert on 15 de julho de 2017 - 7:00 pm

    here are some links to websites that we link to simply because we think they are really worth visiting

  4. #4 by Sharita Raposa on 15 de julho de 2017 - 7:03 pm

    Hey! I simply would like to give a huge thumbs up for the nice information you will have right here on this post. I shall be coming again to your weblog for more soon.

  5. #5 by i88Cash on 15 de julho de 2017 - 9:10 pm

    Yes! Finally something about Judi Poker Online.

  6. #6 by http://www.ljusihus.se/moncleroutlet.aspx on 15 de julho de 2017 - 9:53 pm

    The product or service is with such a awesome price I do not considered some sort of grade would be quite exceptional. It’s pretty. Your mother can love information technology on Christmas morning where she opens gifts therefore seems as though I devoted a lot more, but prices had been simply great!!

  7. #7 by Women on 16 de julho de 2017 - 12:01 am

    I was reading through some of your posts on this site and I think this web site is real instructive! Keep putting up.

  8. #8 by Factory Michael Kors Outlet Online on 16 de julho de 2017 - 2:46 am

    Hi. Magnificent job. I did not expect this on a friday night. This is a terrific story. Thanks!
    Factory Michael Kors Outlet Online

  9. #9 by van cleef and arpels replica on 16 de julho de 2017 - 6:09 am

    I’ve bought the brand name regarding bracelet various occasions. Every single a person is ultra attractive, created very well, cannot tarnish then significant based on which one particular you purchase as well as which we render that it and.

  10. #10 by Jeff on 16 de julho de 2017 - 6:29 am

    Wow, this piece of writing is fastidious, my sister is analyzing these kinds of things, thus I am going to tell her.

  11. #11 by cheap nike roshe run on 16 de julho de 2017 - 6:41 am

    This particular goods ended up being at that awesome price I never believed ones premium would be hence excellent. Its breathtaking. This mama may like this upon Holiday morning whenever she starts gift therefore seems like I devoted significantly more, and yet prices is exclusively great!!

  12. #12 by http://www.alvtank.se/redbottom.asp on 16 de julho de 2017 - 6:41 am

    The device had been in that very good pricing I never believed the particular premium is therefore exceptional. It is striking. It mom may love that it in Christmas early morning where she opens up present therefore looks as though I invested way more, then again rates ended up being really awesome!!

  13. #13 by parajumpers outlet online on 16 de julho de 2017 - 7:26 am

    It is rare for me to discover something on the internet that is as entertaining and fascinating as what you have got here. Your page is lovely, your graphics are outstanding, and whats more, you use source that are relevant to what youre talking about. Youre definitely one in a million, well done!
    parajumpers outlet online

  14. #14 by replica christian louboutin sale on 16 de julho de 2017 - 12:28 pm

    That device is in such a ideal price I do not idea that excellent is so excellent. Its striking. Our mom may prefer they concerning Christmas morning where she opens present and it looks as though I spent much more, however pricing had been really great!!

  15. #15 by http://www.marsgatan.com/list/womenshoes.asp on 16 de julho de 2017 - 12:28 pm

    Your item is with such a great pricing I never believe your high quality is so that excellent. It is gorgeous. This particular mom will adore it regarding Christmas morning when she opens gifts therefore appearance as though I spent a great deal more, but pricing is really ideal!!

  16. #16 by this article on 16 de julho de 2017 - 12:44 pm

    the time to read or go to the content or sites we have linked to below the

  17. #17 by Bat dong san Nha Viet on 16 de julho de 2017 - 12:47 pm

    Way cool, some valid points! I appreciate you making this article available, the rest of the site is also high quality. Have a fun.

  18. #18 by JgDOG on 16 de julho de 2017 - 1:08 pm

    539202 51504Some really nice stuff on this web site , I like it. 243957

  19. #19 by nfl jerseys online on 16 de julho de 2017 - 1:58 pm

    Thanks for spreading this specific good content on your web-site. I came across it on the search engines. I will check back again when you post more aricles.
    nfl jerseys online

  20. #20 by نصب دوربین مداربسته ip on 16 de julho de 2017 - 2:09 pm

    usually posts some incredibly fascinating stuff like this. If you?re new to this site

  21. #21 by Bat dong san Nha Viet on 16 de julho de 2017 - 2:14 pm

    Very interesting points you have mentioned, regards for putting up. “Custom is the great guide of human life.” by David Hume.

  22. #22 by Bat dong san TPHCM on 16 de julho de 2017 - 2:31 pm

    It’s really a nice and helpful piece of information. I’m glad that you shared this useful information with us. Please keep us up to date like this. Thanks for sharing.

  23. #23 by look here on 16 de julho de 2017 - 2:49 pm

    here are some hyperlinks to websites that we link to for the reason that we assume they may be worth visiting

  24. #24 by Fashion on 16 de julho de 2017 - 2:58 pm

    Hey, you used to write excellent, but the last few posts have been kinda boring… I miss your super writings. Past few posts are just a little out of track! come on!

  25. #25 by true religion jeans outlet on 16 de julho de 2017 - 3:46 pm

    hooker flibbertigibbet kamakiriad colquhoun medicate shells mailes olds compress
    true religion jeans outlet

  26. #26 by Birkin Hermes Replica on 16 de julho de 2017 - 4:22 pm

    This item is in that ideal pricing I did not believed that the top quality is therefore exceptional. Its gorgeous. That mama will really like it in Christmas time day once she opens up gift therefore appearances like I devoted much more, although pricing was really great!!

  27. #27 by Moncler Outlet Online on 16 de julho de 2017 - 4:22 pm

    Your item is at such a awesome amount I by no means consideration some sort of top quality would be hence exceptional. Its striking. It mama does appreciate information technology on top of Christmas time morning when she opens present therefore appears like I devoted more, although cost ended up being simply great!!

  28. #28 by keen store online on 16 de julho de 2017 - 4:44 pm

    My sister saved this blog for me and I have been reading through it for the past couple hours. This is really going to help me and my classmates for our class project. By the way, I enjoy the way you write.
    keen store online

  29. #29 by Cecil Huggard on 16 de julho de 2017 - 5:16 pm

    I think other web-site proprietors should take this site as an model, very clean and excellent user friendly style and design, as well as the content. You are an expert in this topic!

  30. #30 by vive sin ansiedad on 16 de julho de 2017 - 5:22 pm

    This is my first time pay a visit at here and i am truly impressed to read all at alone place.|

  31. #31 by click to find out more on 16 de julho de 2017 - 5:32 pm

    Wonderful story, reckoned we could combine some unrelated data, nonetheless definitely worth taking a appear, whoa did one find out about Mid East has got more problerms as well

  32. #32 by http://www.assurance-decottignies.fr/boots.asp on 16 de julho de 2017 - 5:48 pm

    I’ve purchased that brand name out of bracelet some days. Every one is extremely adorable, manufactured well, does not tarnish to important depending on which any you buy and whom a person give things on.

  33. #33 by Psycholog Gorzow Anna Hajduk on 16 de julho de 2017 - 7:33 pm

    Enjoyed reading through this, very good stuff, appreciate it. “Shared joys make a friend, not shared sufferings.” by Friedrich Wilhelm Nietzsche.

  34. #34 by gold coin investing on 16 de julho de 2017 - 7:43 pm

    Hi Dear, are you in fact visiting this site daily, if so afterward you will definitely take good know-how.

  35. #35 by read this on 16 de julho de 2017 - 7:48 pm

    please pay a visit to the websites we follow, like this 1, because it represents our picks through the web

  36. #36 by Christian Louboutin Replica on 16 de julho de 2017 - 8:00 pm

    We have purchased this one brand name regarding bracelet a few circumstances. Every single a person is ultra cute, done very well, does not tarnish to meaningful based on which kind of a single you buy to whom one award that at.

  37. #37 by find here on 16 de julho de 2017 - 8:36 pm

    here are some links to websites that we link to mainly because we consider they are worth visiting

  38. #38 by replica christian louboutin sale on 16 de julho de 2017 - 8:36 pm

    This particular goods was in that awesome rate I by no means idea that grade would be so that excellent. It’s pretty. That mom might appreciate this on Christmas morning where she opens present and it looks like I devoted way more, and yet prices is only awesome!!

  39. #39 by Technology on 16 de julho de 2017 - 10:49 pm

    Some truly good blog posts on this site, thanks for contribution.

  40. #40 by ray ban sunglasses on 16 de julho de 2017 - 11:02 pm

  41. #42 by cat on 17 de julho de 2017 - 4:52 am

    I think this website has very good composed subject matter content.

  42. #43 by Pets on 17 de julho de 2017 - 5:23 am

    Magnificent site. Lots of helpful info here. I’m sending it to several buddies ans also sharing in delicious. And certainly, thanks for your effort!

(não será publicado)