3 Replies Latest reply on Oct 13, 2010 12:36 PM by Illusio

    Bit operations in OpenCL Kernel

    spectral

       

       

      Hi,

       

      In my OpenCL kernel I use the following code :

       

      #define BxDFType uint
      #define    BSDF_UNDEFINED     0
      #define    BSDF_REFLECTION    1<<0
      #define    BSDF_TRANSMISSION  1<<1
      #define    BSDF_DIFFUSE       1<<2
      #define    BSDF_GLOSSY        1<<3
      #define    BSDF_SPECULAR      1<<4
      #define    BSDF_ALL_TYPES         BSDF_DIFFUSE | BSDF_GLOSSY | BSDF_SPECULAR
      #define    BSDF_ALL_REFLECTION    BSDF_REFLECTION | BSDF_ALL_TYPES
      #define    BSDF_ALL_TRANSMISSION  BSDF_TRANSMISSION | BSDF_ALL_TYPES
      #define    BSDF_ALL               BSDF_ALL_REFLECTION | BSDF_ALL_TRANSMISSION

      #define    BSDF_CONDUCTOR 1<<5
      #define    BSDF_DIELECTRIC 1<<6
      #define    BSDF_MICROFACET_BLINN 1<<8
      #define    BSDF_MICROFACET_ANISOTROPIC 1<<9

       

      And I do some operations like this :

       

      BxDFType type;

       

      1) Set a specific flag : type = (type | BSDF_CONDUCTOR)

      2) Remove a specific flag : type = (type & ~ BSDF_CONDUCTOR)

      3) Check if a flag is set : if (type & BSDF_CONDUCTOR) ...

      4) Check if a flag is not set : if (!(type & BSDF_CONDUCTOR)) ...

      5) Check that a flag is set : if (type == BSDF_UNDEFINED)... nothing defined !

       

      But it sounds that there are some of theses operations that doesn't work well !

      Because I'm unable to debug I can find where is my error. So, I just would like to check with you if

      we can do theses operations in OpenCL ?



       

       

       

        • Bit operations in OpenCL Kernel
          himanshu.gautam

          viewon01,

          BSDF_CONDUCTOR has a effective value of 32(1<<5).

          so we are doing all operation on flags reletive to this 5th bit only.But actually flag is a 32 bit uint and its value depends on the values at other bit positions.

          in set flag you do type|BSDF_CONDUCTOR which definitely sets 5th bit of flag but other bits remain unchanged,resulting in a random number.

          I hope it is clear.

            • Bit operations in OpenCL Kernel
              spectral

              Thanks,

              Yes, I understand, maybe I was not clear enough.

              Set a flag : type = type|BSDF_CONDUCTOR

              Reset all and set a specific flag : type = BSDF_CONDUCTOR

                • Bit operations in OpenCL Kernel
                  Illusio

                  In general, it's a good idea to wrap such defines with a paranthesis, like

                  #define    BSDF_TRANSMISSION  (1<<1)

                  #define    BSDF_ALL_TYPES         (BSDF_DIFFUSE | BSDF_GLOSSY | BSDF_SPECULAR)

                  Although I don't think that's the problem in your code, assuming you listed all the operations you use.

                  However, there is a potential for operator precedence effects to create really obscure bugs if you don't make sure the define is evaluated in its entirety before any outside operations can operate on the content.