2 Replies Latest reply on Oct 14, 2010 1:46 PM by MicahVillmow

    Vector type, variable component choice

    eklund.n

      Hi.

      Is there a way to dynamically chose which vector component to use? ie. code alternative 1 vs. alternative 2.

      I have a function that does swizzling on a char4 depending on a value. I calculate the different components to use (i.xyzw) and would like to write something like the //swizzling part.

      uint i, j; uint2 m; //alternative 1 if (i==0) j = m.s0; else j = m.s1; //alternative 2 j = m.s[i]; or j = m[i]; uchar4 obj; uint4 i; //swizzle obj = obj.s[i.x][i.y][i.z][i.w]; or obj = (uchar4)(obj[i.x],obj[i.y],obj[i.z],obj[i.w]);

        • Vector type, variable component choice
          LeeHowes

          A vector is explicitly not an array. It's an opaque containter, that's a very important design decision. Any approach that would allow you to do what you want would have to compile on most hardware to either accesses in a memory buffer or exactly what you would do as a work around. I'd be prone to say it should stay as a workaround in code rather than pretending it's a more efficient operation than it really is.

          • Vector type, variable component choice
            MicahVillmow
            You can use the shuffle function which is provided by OpenCL to do this for you. Dynamic indexing into a vector is not allowed because a vector is a single unit and not an array.