First off, I know there have been many topic at this forum regarding replicating GLSL/HLSL vector "swizzling" in C++, most notably:
None of them provides a solution good enough to "run" GLSL/HLSL code. They are all locked now, so I'll start the topic again. Maybe OPs and newcommers will find this infofmation useful. Also, I wanted to set the bar higher - to be able run an actual shader as C++ code, for fun and science.
What we wan't is this code to be valid:
vec4 foo(0); // 0,0,0,0 foo.yx = vec2(2, 1); // 1,2,0,0 foo.zw = foo.xy * 2; // 1,2,2,4 vec2 bar = max(foo.xw, foo.yz).yx; // 4,2 bar = clamp(foo.xw, 0, 2); // 1,2 mat2 m(foo.xyz, 1); // 1,2,2,1 bar *= m; // 5,2
I already mentioned previous thread. There also is the GLM library (http://glm.g-truc.net/0.9.4/index.html), which almost gets it right. Swizzling is pretty much replicated from GLSL, along with matrices and math functions. The problem is... you still just can't take any GLSL shader (these days I get ones for tests from http://glsl.heroku.com/ nad https://www.shadertoy.com/) and treat it as C++ code. You'll get ambiguity errors, mostly because in GLSL you don't have to suffix float literal with 'f' for it to be a float (0.5 vs 0.5f), it's more contextual. Also it seems that GLM is simply missing some GLSL features, e.g. it has incomplete vector/matrix constructors list. Apart from that, GLM suffers from what I call "code diarrhea", i.e. there's just to much code repetition and macros all over the place. I appreciate the amount of work that went into GLM, though.
One should also know about Clang's extension (http://clang.llvm.org/docs/LanguageExtensions.html#vectors-and-extended-vectors) that enables vector swizzling *as a compiler feature*. It is very neat and the output assembly looks good, but the syntax of constructing a vector, man...
float4 vf = (float4)((float2)(1.0f, 2.0f), (float2)(3.0f, 4.0f));
A big no-no, no way this can be used to "run" valid GLSL. Also, it binds you to a one compiler, which is never good.
Here steps in the CxxSwizzle (https://github.com/gwiazdorrr/CxxSwizzle), which I myself wrote in my free time. Bottom line, you can take almost any GLSL fragment shader and run it as C++ code, without any changes. Just like that. Conditional breakpoints, assertions, logs - there simply isn't a shader debugging solution (to my knowledge) that enables you to do that.
Feature list is still incomplete, but missing are mostly functions, which are the least difficult to implement (it's just a chore). Also, math implementation is extremely naive and slow, but due to the component structure of the code the "backend" can be easily moved to SSE based one, for instance.
Anyway, the syntactic compability with GLSL has been achieved, with little or no code repetition, using industry-available C++ features (subset of MSVC 2010, also tested on g++4.8.1). If you ever wanted to step through a shader and see "how it works" you now can.
I am extremely interested in (constructive) criticism of the library and, perhaps, discovering a better way of rich shader debugging. This one is slow as hell, but, well.. works. So?