Define swizzling programmatically (as in GLSL) -


how 1 write swizzling defined behaviour in programming language? (swizzling members matrices , vectors in glsl) if wanted make programming language allow definition of swizzling on members, way it? example this:

struct {     swizzable     {         float x, float y, float z, float w     } } 

but missing lot. example not define sould return when swizzle more or less elements or assign subset or elements backwards. in glsl can v.xyz create vec3 vec4 called v. or assign subset of members: v.zyx = ... in order.

so swizzable substruct not solution (or @ least limited). way return array of swizzled members , implicit cast (with constructor) generate wanted element:

struct vec2 {     swizzable { float x, float y }     vec2(float[2] elements)     { x = elements[0]; y = elements[1]; } }  struct vec3 {     swizzable { float x, float y, float z } } 

so if accessed vec3's x , y via swizzling, float[2] , because have constructor vec2, can assign array (and implicitly instantiating vec2).

this looks better solution still: how 1 better?

edit: sorry didn't specify question: want implement programming language supports kind of thing.

i'm not sure how give good, detailed answer, here 1 idea.

if understand right, swizzling syntactic convenience. page https://www.opengl.org/wiki/glsl_optimizations gives following example of swizzling:

gl_fragcolor = mycolor.xyzw * constantlist.xxxy + constantlist.yyyx; 

this syntactic shorthand like:

gl_fragcolor = vector(mycolor.x, mycolor.y, mycolor.z, mycolor.w)     * vector(constantlist.x, constantlist.x, constantlist.x, constantlist.y)     + vector(constantlist.y, constantlist.y, constantlist.y, constantlist.x); 

so, 1 step may figure out how parse shorter syntax , interpret meaning similar longer syntax.

i don't see why necessary declare struct more complicated struct mystruct { float x, float y, float z, float w }. language should able handle details of how implement swizzling.


Comments