I'm on Linux and I am trying to compile basic shaders for bgfx using shaderc.
My Compile Lines:
shaderc -f ./vs_triangle.sc -o vs_triangle.bin --type vertex --platform linux --profile spirv --varyingdef ./varying.def.sc
shaderc -f ./fs_triangle.sc -o fs_triangle.bin --type fragment --platform linux --profile spirv --varyingdef ./varying.def.sc
I am very new to shader programming and I am just trying to set up a project to learn shaders.
Here is my varying.def.sc:
vec3 a_position : POSITION;
vec4 a_color0 : COLOR0;
vec4 v_color0 : COLOR0;
Here is my vertex shader:
$input a_position, a_color0
$output v_color0
void main()
{
gl_Position = vec4(a_position, 1.0);
v_color0 = a_color0;
}
Here is my fragment shader:
$input v_color0
$output o_color
void main()
{
o_color = v_color0;
}
When I try to compile my fragment shader I get this:
Code:
---
1: // shaderc command line:
2: // shaderc -f ./fs_triangle.sc -o fs_triangle.bin --type fragment --platform linux --profile spirv --varyingdef ./varying.def.sc
3:
4: void main( float4 gl_FragCoord : SV_POSITION , float4 v_color0 : COLOR0 )
5: {
>>> 6: float4 bgfx_VoidFrag = vec4_splat(0.0);
>>> 0: ^
7: o_color = v_color0;
8: }
---
ERROR: 0:6: 'vec4_splat' : no matching overloaded function found
ERROR: 0:6: 'declaration' : Expected
(6): error at column 39, HLSL parsing failed.
ERROR: 3 compilation errors. No code generated.
Failed to build shader.
Why is it throwing an error with code that's not even in my shader code?
I figured it out. I needed to organized my project. I made a dedicated folder containing all of the files that needed to be included then wrote the compile script below. I also needed to include necessary files in my shaders with:
#include<common.sh>
I've done this before, but it was needed in conjunction with specifying the include paths in the compile script and having everything that is needed in the include path. The big issue was I didn't know how everything connected since I am very new to shader development and bgfx so I wasn't sure what files needed to even be included and how to organize it all.
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
INCLUDE_DIR="$SCRIPT_DIR/../bgfx/include"
SHADERC="$HOME/.desktop-system/programs/shaderc"
PLATFORM=linux
INCLUDES=(
-i "$INCLUDE_DIR/common"
-i "$SCRIPT_DIR"
)
"$SHADERC" -f "$SCRIPT_DIR/vs_triangle.sc" -o "$SCRIPT_DIR/vs_triangle.bin" --type vertex --platform "$PLATFORM" --profile spirv "${INCLUDES[@]}" --varyingdef "$INCLUDE_DIR/common/varying.def.sc" --verbose
"$SHADERC" -f "$SCRIPT_DIR/fs_triangle.sc" -o "$SCRIPT_DIR/fs_triangle.bin" --type fragment --platform "$PLATFORM" --profile spirv "${INCLUDES[@]}" --varyingdef "$INCLUDE_DIR/common/varying.def.sc" --verbose
I saw another post that mentioned another issue that further complicated my attempt to compile the shaders (I changed that file a lot with and without comments):
BGFX shader compilation using shaderc
If you have comments in varying.def.sc the parser will parse that particular file incorrectly which will lead to a compilation error. You can bypass this and still have comments if you end your comment lines with a semi-colon ';'. I tested this. Also, I should have been using gl_FragColor instead of o_color.