Register
4coder»Forums»Built in way to tell 4coder to format shader code?
Jason
176 posts
Built in way to tell 4coder to format shader code?
3 months, 3 weeks ago Edited by Jason on May 25, 2020, 6:04 p.m. Reason: Initial post
I'm writing some inline shaders like Casey does in handmade hero and was wondering if there was ever a feature put in so you could have 4coder indent shader code inside strings? Right now I have the C++ heredoc thing working at least:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const char* fragmentShaderCode =
R"HereDoc(

#version 430

out vec4 color;
in vec3 fragColor;

void main()
{
color = vec4(fragColor, 1.0f);
};

)HereDoc";


Simon Anciaux
881 posts
Built in way to tell 4coder to format shader code?
3 months, 3 weeks ago Edited by Simon Anciaux on May 25, 2020, 9:39 p.m.
There isn't a way to do that out of the box. But I imagine you could modify the layout function to indent heredoc that would start with a specific string. For example if you used:
1
2
3
4
const char* fragmentShaderCode =
R"ShaderCode(
...
)ShaderCode";

You could probably (I haven't done it) detect the "ShaderCode(" and indent the string.

I use a different method: I added a meta programming step in my build system, that will parse shader files and produce a header that contains the shaders as C strings.

For example, the shader file I edit look like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[name: vs_default]

in vec2 position;
in vec2 in_uv;
out vec2 out_uv;

void main( ) {
    gl_Position = vec4( position, 0.0, 1.0 );
    out_uv = in_uv;
}


The [name: vs_default] defines the name the string will have in the C header.

This will output something like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
u8 vs_default[ ] =
"in vec2 position;\n"
"in vec2 in_uv;\n"
"out vec2 out_uv;\n"
"\n"
"void main( ) {\n"
"    gl_Position = vec4( position, 0.0, 1.0 );\n"
"    out_uv = in_uv;\n"
"}\n"
"\n";


The parser is really simple, it just a text search for [name: , extract the name, and copy the bytes to the next [name: or the end of the file. I added the \n in the strings so that OpenGL shader compiler error will be easier to find.
Jason
176 posts
Built in way to tell 4coder to format shader code?
3 months, 3 weeks ago
I've played around with the idea of doing some meta stuff but haven't got around to it yet.

mrmixer
You could probably (I haven't done it) detect the "ShaderCode(" and indent the string.


Ya, I guess I'll eventually just create some custom way to do this in 4coder. The only reason I asked was because I just watched a video on handmade hero where a listener told Casey that if you updated to latest version of 4coder (this was a while back some I'm assuming the version was an alpha version) then you could indent your shader string (since Casey was unable to do this with a previous version of 4coder). Wasn't sure if this meant there was some built in way to do so.