Handmade Hero » Forums » Code » C++11 raw string literal & GLSL shaders indentation in 4coder
mmozeiko
Mārtiņš Možeiko
1518 posts
1 project
#12482 C++11 raw string literal & GLSL shaders indentation in 4coder
5 months ago Edited by Mārtiņš Možeiko on July 15, 2017, 9:26 a.m.

Maybe this will fix 4coder indentation issues in Handmade Hero code with C++11 raw string literals:

1
2
3
4
5
6
7
char* ShaderCode = R"FOO"(
    void main()
    {
        v2 foo = v2(1,2);
        ....
    }
)FOO"";

Instead of using FOO as begin/end marker try using FOO" as begin/end marker. This is legal in C++11, and VS2013 supports this. My assumption is that 4coder will see "FOO" just as a normal string, then it will see GLSL code outside of string literal, and at the end it will see just an empty "" string literal. Nothing will be inside string literal from 4coder's point of view.

I don't have 4coder myself so I cannot try if this will really work. Maybe 4coder now will be confused about parenthesis around GLSL code which are outside of string literal.
mrmixer
Simon Anciaux
289 posts
#12487 C++11 raw string literal & GLSL shaders indentation in 4coder
5 months ago

It doesn't work.

mmozeiko
Mārtiņš Možeiko
1518 posts
1 project
#12492 C++11 raw string literal & GLSL shaders indentation in 4coder
5 months ago Edited by Mārtiņš Možeiko on July 15, 2017, 8:04 p.m.

Right, it doesn't :(
4coder is too smart, I guess it recognizes C++11 raw string literals.

Maybe alternative is to do this:
1
2
3
4
5
6
#define SHADER_CODE2(x) #x
#define SHADER_CODE(x) SHADER_CODE2(x)

const char* foo =
#include "shader.h"
;

And in shader.h this:
1
2
3
4
5
6
7
8
SHADER_CODE(

void main()
{
  v2 foo = v2(1,2);
}

)

But this will loose newlines in GLSL source.

mrmixer
Simon Anciaux
289 posts
#12504 C++11 raw string literal & GLSL shaders indentation in 4coder
5 months ago Edited by Simon Anciaux on July 16, 2017, 2:30 p.m. Reason: typo

But at that point wouldn't it be simpler to use shader files and load them at run time ?

Edit: This solution doesn't indent properly
mmozeiko
Mārtiņš Možeiko
1518 posts
1 project
#12518 C++11 raw string literal & GLSL shaders indentation in 4coder
5 months ago

Ok, then I'm out of ideas :)

At this point loading shaders at runtime or doing meta-programming for converting glsl files to C strings at compile time is more reasonable.