mirror of
https://github.com/bkaradzic/bgfx.git
synced 2026-02-21 06:13:07 +01:00
Adding an API to shaderc (#1280)
This commit is contained in:
committed by
Branimir Karadžić
parent
88f6d1b0c0
commit
b8886a3531
File diff suppressed because it is too large
Load Diff
@@ -122,6 +122,41 @@ namespace bgfx
|
||||
uint16_t regCount;
|
||||
};
|
||||
|
||||
struct Options
|
||||
{
|
||||
Options();
|
||||
|
||||
void dump();
|
||||
|
||||
char shaderType;
|
||||
std::string platform;
|
||||
std::string profile;
|
||||
|
||||
std::string inputFilePath;
|
||||
std::string outputFilePath;
|
||||
|
||||
std::vector<std::string> includeDirs;
|
||||
std::vector<std::string> defines;
|
||||
std::vector<std::string> dependencies;
|
||||
|
||||
bool disasm;
|
||||
bool raw;
|
||||
bool preprocessOnly;
|
||||
bool depends;
|
||||
|
||||
bool debugInformation;
|
||||
|
||||
bool avoidFlowControl;
|
||||
bool noPreshader;
|
||||
bool partialPrecision;
|
||||
bool preferFlowControl;
|
||||
bool backwardsCompatibility;
|
||||
bool warningsAreErrors;
|
||||
|
||||
bool optimize;
|
||||
uint32_t optimizationLevel;
|
||||
};
|
||||
|
||||
typedef std::vector<Uniform> UniformArray;
|
||||
|
||||
void printCode(const char* _code, int32_t _line = 0, int32_t _start = 0, int32_t _end = INT32_MAX, int32_t _column = -1);
|
||||
@@ -129,10 +164,10 @@ namespace bgfx
|
||||
int32_t writef(bx::WriterI* _writer, const char* _format, ...);
|
||||
void writeFile(const char* _filePath, const void* _data, int32_t _size);
|
||||
|
||||
bool compileGLSLShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer);
|
||||
bool compileHLSLShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer);
|
||||
bool compilePSSLShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer);
|
||||
bool compileSPIRVShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer);
|
||||
bool compileGLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer);
|
||||
bool compileHLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer);
|
||||
bool compilePSSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer);
|
||||
bool compileSPIRVShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer);
|
||||
|
||||
} // namespace bgfx
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
namespace bgfx { namespace glsl
|
||||
{
|
||||
static bool compile(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
{
|
||||
char ch = char(tolower(_cmdLine.findOption('\0', "type")[0]) );
|
||||
char ch = _options.shaderType;
|
||||
const glslopt_shader_type type = ch == 'f'
|
||||
? kGlslOptShaderFragment
|
||||
: (ch == 'c' ? kGlslOptShaderCompute : kGlslOptShaderVertex);
|
||||
@@ -292,10 +292,9 @@ namespace bgfx { namespace glsl
|
||||
uint8_t nul = 0;
|
||||
bx::write(_writer, nul);
|
||||
|
||||
if (_cmdLine.hasArg('\0', "disasm") )
|
||||
if (_options.disasm )
|
||||
{
|
||||
std::string disasmfp = _cmdLine.findOption('o');
|
||||
disasmfp += ".disasm";
|
||||
std::string disasmfp = _options.outputFilePath + ".disasm";
|
||||
writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
|
||||
}
|
||||
|
||||
@@ -306,9 +305,9 @@ namespace bgfx { namespace glsl
|
||||
|
||||
} // namespace glsl
|
||||
|
||||
bool compileGLSLShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
bool compileGLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
{
|
||||
return glsl::compile(_cmdLine, _version, _code, _writer);
|
||||
return glsl::compile(_options, _version, _code, _writer);
|
||||
}
|
||||
|
||||
} // namespace bgfx
|
||||
|
||||
@@ -545,10 +545,11 @@ namespace bgfx { namespace hlsl
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool compile(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
|
||||
static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
|
||||
{
|
||||
const char* profile = _cmdLine.findOption('p', "profile");
|
||||
if (NULL == profile)
|
||||
const char* profile = _options.profile.c_str();
|
||||
|
||||
if (profile[0] == '\0')
|
||||
{
|
||||
fprintf(stderr, "Error: Shader profile must be specified.\n");
|
||||
return false;
|
||||
@@ -557,27 +558,26 @@ namespace bgfx { namespace hlsl
|
||||
s_compiler = load();
|
||||
|
||||
bool result = false;
|
||||
bool debug = _cmdLine.hasArg('\0', "debug");
|
||||
bool debug = _options.debugInformation;
|
||||
|
||||
uint32_t flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
|
||||
flags |= debug ? D3DCOMPILE_DEBUG : 0;
|
||||
flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
|
||||
flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DCOMPILE_NO_PRESHADER : 0;
|
||||
flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DCOMPILE_PARTIAL_PRECISION : 0;
|
||||
flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
|
||||
flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
|
||||
flags |= _options.avoidFlowControl ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
|
||||
flags |= _options.noPreshader ? D3DCOMPILE_NO_PRESHADER : 0;
|
||||
flags |= _options.partialPrecision ? D3DCOMPILE_PARTIAL_PRECISION : 0;
|
||||
flags |= _options.preferFlowControl ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
|
||||
flags |= _options.backwardsCompatibility ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
|
||||
|
||||
bool werror = _cmdLine.hasArg('\0', "Werror");
|
||||
bool werror = _options.warningsAreErrors;
|
||||
|
||||
if (werror)
|
||||
{
|
||||
flags |= D3DCOMPILE_WARNINGS_ARE_ERRORS;
|
||||
}
|
||||
|
||||
uint32_t optimization = 3;
|
||||
if (_cmdLine.hasArg(optimization, 'O') )
|
||||
if (_options.optimize )
|
||||
{
|
||||
optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelD3D11) - 1);
|
||||
uint32_t optimization = bx::uint32_min(_options.optimizationLevel, BX_COUNTOF(s_optimizationLevelD3D11) - 1);
|
||||
flags |= s_optimizationLevelD3D11[optimization];
|
||||
}
|
||||
else
|
||||
@@ -598,8 +598,7 @@ namespace bgfx { namespace hlsl
|
||||
|
||||
if (debug)
|
||||
{
|
||||
hlslfp = _cmdLine.findOption('o');
|
||||
hlslfp += ".hlsl";
|
||||
hlslfp = _options.outputFilePath + ".hlsl";
|
||||
writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
|
||||
}
|
||||
|
||||
@@ -708,7 +707,7 @@ namespace bgfx { namespace hlsl
|
||||
}
|
||||
|
||||
// recompile with the unused uniforms converted to statics
|
||||
return compile(_cmdLine, _version, output.c_str(), _writer, false);
|
||||
return compile(_options, _version, output.c_str(), _writer, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -771,7 +770,7 @@ namespace bgfx { namespace hlsl
|
||||
bx::write(_writer, size);
|
||||
}
|
||||
|
||||
if (_cmdLine.hasArg('\0', "disasm") )
|
||||
if (_options.disasm )
|
||||
{
|
||||
ID3DBlob* disasm;
|
||||
D3DDisassemble(code->GetBufferPointer()
|
||||
@@ -783,8 +782,7 @@ namespace bgfx { namespace hlsl
|
||||
|
||||
if (NULL != disasm)
|
||||
{
|
||||
std::string disasmfp = _cmdLine.findOption('o');
|
||||
disasmfp += ".disasm";
|
||||
std::string disasmfp = _options.outputFilePath + ".disasm";
|
||||
|
||||
writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), (uint32_t)disasm->GetBufferSize() );
|
||||
disasm->Release();
|
||||
@@ -806,9 +804,9 @@ namespace bgfx { namespace hlsl
|
||||
|
||||
} // namespace hlsl
|
||||
|
||||
bool compileHLSLShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
bool compileHLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
{
|
||||
return hlsl::compile(_cmdLine, _version, _code, _writer, true);
|
||||
return hlsl::compile(_options, _version, _code, _writer, true);
|
||||
}
|
||||
|
||||
} // namespace bgfx
|
||||
@@ -817,9 +815,9 @@ namespace bgfx { namespace hlsl
|
||||
|
||||
namespace bgfx
|
||||
{
|
||||
bool compileHLSLShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
bool compileHLSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
{
|
||||
BX_UNUSED(_cmdLine, _version, _code, _writer);
|
||||
BX_UNUSED(_options, _version, _code, _writer);
|
||||
fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
namespace bgfx
|
||||
{
|
||||
bool compilePSSLShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
bool compilePSSLShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
{
|
||||
BX_UNUSED(_cmdLine, _version, _code, _writer);
|
||||
BX_UNUSED(_options, _version, _code, _writer);
|
||||
fprintf(stderr, "PSSL compiler is not supported.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -545,25 +545,18 @@ namespace bgfx { namespace spirv
|
||||
// fprintf(stderr, "%s\n", _message);
|
||||
// }
|
||||
|
||||
static bool compile(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
{
|
||||
BX_UNUSED(_cmdLine, _version, _code, _writer);
|
||||
|
||||
const char* type = _cmdLine.findOption('\0', "type");
|
||||
if (NULL == type)
|
||||
{
|
||||
fprintf(stderr, "Error: Shader type must be specified.\n");
|
||||
return false;
|
||||
}
|
||||
BX_UNUSED(_version);
|
||||
|
||||
glslang::InitializeProcess();
|
||||
|
||||
glslang::TProgram* program = new glslang::TProgram;
|
||||
|
||||
EShLanguage stage = getLang(type[0]);
|
||||
EShLanguage stage = getLang(_options.shaderType);
|
||||
if (EShLangCount == stage)
|
||||
{
|
||||
fprintf(stderr, "Error: Unknown shader type %s.\n", type);
|
||||
fprintf(stderr, "Error: Unknown shader type %s.\n", _options.shaderType);
|
||||
return false;
|
||||
}
|
||||
glslang::TShader* shader = new glslang::TShader(stage);
|
||||
@@ -649,7 +642,7 @@ namespace bgfx { namespace spirv
|
||||
uint16_t count = (uint16_t)program->getNumLiveUniformVariables();
|
||||
bx::write(_writer, count);
|
||||
|
||||
uint32_t fragmentBit = type[0] == 'f' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
|
||||
uint32_t fragmentBit = _options.shaderType == 'f' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
|
||||
for (uint16_t ii = 0; ii < count; ++ii)
|
||||
{
|
||||
Uniform un;
|
||||
@@ -767,9 +760,9 @@ namespace bgfx { namespace spirv
|
||||
|
||||
} // namespace spirv
|
||||
|
||||
bool compileSPIRVShader(const bx::CommandLine& _cmdLine, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
bool compileSPIRVShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer)
|
||||
{
|
||||
return spirv::compile(_cmdLine, _version, _code, _writer);
|
||||
return spirv::compile(_options, _version, _code, _writer);
|
||||
}
|
||||
|
||||
} // namespace bgfx
|
||||
|
||||
Reference in New Issue
Block a user