Skip to content

Commit df22b08

Browse files
committed
Determine shader output type
Signed-off-by: Sam Hellawell <[email protected]>
1 parent e60dab3 commit df22b08

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

tools/shaderc/shaderc.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ namespace bgfx
954954
return hash;
955955
}
956956

957-
void addFragData(Preprocessor& _preprocessor, char* _data, uint32_t _idx, bool _comma)
957+
void addFragData(Preprocessor& _preprocessor, char* _data, uint32_t _idx, bool _comma, std::string& outputType)
958958
{
959959
char find[32];
960960
bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
@@ -965,8 +965,9 @@ namespace bgfx
965965
strReplace(_data, find, replace);
966966

967967
_preprocessor.writef(
968-
" \\\n\t%sout vec4 bgfx_FragData%d : SV_TARGET%d"
968+
" \\\n\t%sout %s bgfx_FragData%d : SV_TARGET%d"
969969
, _comma ? ", " : " "
970+
, outputType.c_str()
970971
, _idx
971972
, _idx
972973
);
@@ -1710,7 +1711,16 @@ namespace bgfx
17101711
}
17111712
else // Vertex/Fragment
17121713
{
1714+
std::string outputType = "vec4";
17131715
bx::StringView shader(input);
1716+
bx::StringView entryTest = bx::strFind(shader, "BGFX_FRAG_OUTPUT_TYPE");
1717+
if (!entryTest.isEmpty() ) {
1718+
bx::StringView insert = bx::strFind(bx::StringView(entryTest.getPtr(), shader.getTerm() ), "\n");
1719+
if (!insert.isEmpty() ) {
1720+
outputType.assign(entryTest.getPtr() + 22, insert.getPtr() );
1721+
}
1722+
}
1723+
17141724
bx::StringView entry = bx::strFind(shader, "void main()");
17151725
if (entry.isEmpty() )
17161726
{
@@ -1748,12 +1758,12 @@ namespace bgfx
17481758
if (hasFragColor)
17491759
{
17501760
preprocessor.writef("#define gl_FragColor bgfx_FragColor\n");
1751-
preprocessor.writef("out mediump vec4 bgfx_FragColor;\n");
1761+
preprocessor.writef("out mediump %s bgfx_FragColor;\n", outputType.c_str());
17521762
}
17531763
else if (numFragData)
17541764
{
17551765
preprocessor.writef("#define gl_FragData bgfx_FragData\n");
1756-
preprocessor.writef("out mediump vec4 bgfx_FragData[gl_MaxDrawBuffers];\n");
1766+
preprocessor.writef("out mediump %s bgfx_FragData[gl_MaxDrawBuffers];\n", outputType.c_str());
17571767
}
17581768
}
17591769

@@ -1896,7 +1906,7 @@ namespace bgfx
18961906

18971907
if (hasFragCoord)
18981908
{
1899-
preprocessor.writef(" \\\n\tvec4 gl_FragCoord : SV_POSITION");
1909+
preprocessor.writef(" \\\n\t%s gl_FragCoord : SV_POSITION", outputType.c_str());
19001910
++arg;
19011911
}
19021912

@@ -1924,7 +1934,7 @@ namespace bgfx
19241934
{
19251935
if (hasFragData[ii])
19261936
{
1927-
addFragData(preprocessor, input, ii, arg++ > 0);
1937+
addFragData(preprocessor, input, ii, arg++ > 0, outputType);
19281938
}
19291939
}
19301940
else
@@ -2515,8 +2525,9 @@ namespace bgfx
25152525
if (!bx::findIdentifierMatch(input, "gl_FragColor").isEmpty() )
25162526
{
25172527
bx::stringPrintf(code
2518-
, "out vec4 bgfx_FragColor;\n"
2528+
, "out %s bgfx_FragColor;\n"
25192529
"#define gl_FragColor bgfx_FragColor\n"
2530+
, outputType.c_str()
25202531
);
25212532
}
25222533
}

0 commit comments

Comments
 (0)