mirror of
https://github.com/AxioDL/metaforce.git
synced 2025-12-08 12:24:56 +00:00
New code style refactor
This commit is contained in:
@@ -11,31 +11,25 @@ static logvisor::Module Log("shaderc");
|
||||
extern pD3DCompile D3DCompilePROC;
|
||||
pD3DCompile D3DCompilePROC = nullptr;
|
||||
|
||||
static bool FindBestD3DCompile()
|
||||
{
|
||||
HMODULE d3dCompilelib = LoadLibraryW(L"D3DCompiler_47.dll");
|
||||
if (!d3dCompilelib)
|
||||
{
|
||||
d3dCompilelib = LoadLibraryW(L"D3DCompiler_46.dll");
|
||||
if (!d3dCompilelib)
|
||||
{
|
||||
d3dCompilelib = LoadLibraryW(L"D3DCompiler_45.dll");
|
||||
if (!d3dCompilelib)
|
||||
{
|
||||
d3dCompilelib = LoadLibraryW(L"D3DCompiler_44.dll");
|
||||
if (!d3dCompilelib)
|
||||
{
|
||||
d3dCompilelib = LoadLibraryW(L"D3DCompiler_43.dll");
|
||||
}
|
||||
}
|
||||
static bool FindBestD3DCompile() {
|
||||
HMODULE d3dCompilelib = LoadLibraryW(L"D3DCompiler_47.dll");
|
||||
if (!d3dCompilelib) {
|
||||
d3dCompilelib = LoadLibraryW(L"D3DCompiler_46.dll");
|
||||
if (!d3dCompilelib) {
|
||||
d3dCompilelib = LoadLibraryW(L"D3DCompiler_45.dll");
|
||||
if (!d3dCompilelib) {
|
||||
d3dCompilelib = LoadLibraryW(L"D3DCompiler_44.dll");
|
||||
if (!d3dCompilelib) {
|
||||
d3dCompilelib = LoadLibraryW(L"D3DCompiler_43.dll");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (d3dCompilelib)
|
||||
{
|
||||
D3DCompilePROC = (pD3DCompile)GetProcAddress(d3dCompilelib, "D3DCompile");
|
||||
return D3DCompilePROC != nullptr;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (d3dCompilelib) {
|
||||
D3DCompilePROC = (pD3DCompile)GetProcAddress(d3dCompilelib, "D3DCompile");
|
||||
return D3DCompilePROC != nullptr;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int wmain(int argc, const hecl::SystemChar** argv)
|
||||
@@ -43,127 +37,102 @@ int wmain(int argc, const hecl::SystemChar** argv)
|
||||
int main(int argc, const hecl::SystemChar** argv)
|
||||
#endif
|
||||
{
|
||||
logvisor::RegisterConsoleLogger();
|
||||
logvisor::RegisterStandardExceptions();
|
||||
logvisor::RegisterConsoleLogger();
|
||||
logvisor::RegisterStandardExceptions();
|
||||
|
||||
#if _WIN32
|
||||
if (!FindBestD3DCompile())
|
||||
{
|
||||
Log.report(logvisor::Info, "Unable to find D3DCompiler dll");
|
||||
return 1;
|
||||
}
|
||||
if (!FindBestD3DCompile()) {
|
||||
Log.report(logvisor::Info, "Unable to find D3DCompiler dll");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
Log.report(logvisor::Info, "Usage: shaderc -o <out-base> [-D definevar=defineval]... <in-files>...");
|
||||
return 0;
|
||||
}
|
||||
|
||||
hecl::SystemString outPath;
|
||||
hecl::shaderc::Compiler c;
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
if (argv[i][0] == '-')
|
||||
{
|
||||
if (argv[i][1] == 'o')
|
||||
{
|
||||
if (argv[i][2])
|
||||
{
|
||||
outPath = &argv[i][2];
|
||||
}
|
||||
else if (i + 1 < argc)
|
||||
{
|
||||
++i;
|
||||
outPath = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.report(logvisor::Error, "Invalid -o argument");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (argv[i][1] == 'D')
|
||||
{
|
||||
const hecl::SystemChar* define;
|
||||
if (argv[i][2])
|
||||
{
|
||||
define = &argv[i][2];
|
||||
}
|
||||
else if (i + 1 < argc)
|
||||
{
|
||||
++i;
|
||||
define = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.report(logvisor::Error, "Invalid -D argument");
|
||||
return 1;
|
||||
}
|
||||
hecl::SystemUTF8Conv conv(define);
|
||||
const char* defineU8 = conv.c_str();
|
||||
if (const char* equals = strchr(defineU8, '='))
|
||||
c.addDefine(std::string(defineU8, equals - defineU8), equals + 1);
|
||||
else
|
||||
c.addDefine(defineU8, "");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.report(logvisor::Error, "Unrecognized flag option '%c'", argv[i][1]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
c.addInputFile(argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (outPath.empty())
|
||||
{
|
||||
Log.report(logvisor::Error, "-o option is required");
|
||||
return 1;
|
||||
}
|
||||
|
||||
hecl::SystemStringView baseName;
|
||||
auto slashPos = outPath.find_last_of(_SYS_STR("/\\"));
|
||||
if (slashPos != hecl::SystemString::npos)
|
||||
baseName = outPath.data() + slashPos + 1;
|
||||
else
|
||||
baseName = outPath;
|
||||
|
||||
if (!glslang::InitializeProcess())
|
||||
{
|
||||
Log.report(logvisor::Error, "Unable to initialize glslang");
|
||||
return 1;
|
||||
}
|
||||
|
||||
hecl::SystemUTF8Conv conv(baseName);
|
||||
std::pair<std::string, std::string> ret;
|
||||
if (!c.compile(conv.str(), ret))
|
||||
return 1;
|
||||
|
||||
{
|
||||
hecl::SystemString headerPath = outPath + _SYS_STR(".hpp");
|
||||
athena::io::FileWriter w(headerPath);
|
||||
if (w.hasError())
|
||||
{
|
||||
Log.report(logvisor::Error, _SYS_STR("Error opening '%s' for writing"), headerPath.c_str());
|
||||
return 1;
|
||||
}
|
||||
w.writeBytes(ret.first.data(), ret.first.size());
|
||||
}
|
||||
|
||||
{
|
||||
hecl::SystemString impPath = outPath + _SYS_STR(".cpp");
|
||||
athena::io::FileWriter w(impPath);
|
||||
if (w.hasError())
|
||||
{
|
||||
Log.report(logvisor::Error, _SYS_STR("Error opening '%s' for writing"), impPath.c_str());
|
||||
return 1;
|
||||
}
|
||||
w.writeBytes(ret.second.data(), ret.second.size());
|
||||
}
|
||||
|
||||
if (argc == 1) {
|
||||
Log.report(logvisor::Info, "Usage: shaderc -o <out-base> [-D definevar=defineval]... <in-files>...");
|
||||
return 0;
|
||||
}
|
||||
|
||||
hecl::SystemString outPath;
|
||||
hecl::shaderc::Compiler c;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (argv[i][0] == '-') {
|
||||
if (argv[i][1] == 'o') {
|
||||
if (argv[i][2]) {
|
||||
outPath = &argv[i][2];
|
||||
} else if (i + 1 < argc) {
|
||||
++i;
|
||||
outPath = argv[i];
|
||||
} else {
|
||||
Log.report(logvisor::Error, "Invalid -o argument");
|
||||
return 1;
|
||||
}
|
||||
} else if (argv[i][1] == 'D') {
|
||||
const hecl::SystemChar* define;
|
||||
if (argv[i][2]) {
|
||||
define = &argv[i][2];
|
||||
} else if (i + 1 < argc) {
|
||||
++i;
|
||||
define = argv[i];
|
||||
} else {
|
||||
Log.report(logvisor::Error, "Invalid -D argument");
|
||||
return 1;
|
||||
}
|
||||
hecl::SystemUTF8Conv conv(define);
|
||||
const char* defineU8 = conv.c_str();
|
||||
if (const char* equals = strchr(defineU8, '='))
|
||||
c.addDefine(std::string(defineU8, equals - defineU8), equals + 1);
|
||||
else
|
||||
c.addDefine(defineU8, "");
|
||||
} else {
|
||||
Log.report(logvisor::Error, "Unrecognized flag option '%c'", argv[i][1]);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
c.addInputFile(argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (outPath.empty()) {
|
||||
Log.report(logvisor::Error, "-o option is required");
|
||||
return 1;
|
||||
}
|
||||
|
||||
hecl::SystemStringView baseName;
|
||||
auto slashPos = outPath.find_last_of(_SYS_STR("/\\"));
|
||||
if (slashPos != hecl::SystemString::npos)
|
||||
baseName = outPath.data() + slashPos + 1;
|
||||
else
|
||||
baseName = outPath;
|
||||
|
||||
if (!glslang::InitializeProcess()) {
|
||||
Log.report(logvisor::Error, "Unable to initialize glslang");
|
||||
return 1;
|
||||
}
|
||||
|
||||
hecl::SystemUTF8Conv conv(baseName);
|
||||
std::pair<std::string, std::string> ret;
|
||||
if (!c.compile(conv.str(), ret))
|
||||
return 1;
|
||||
|
||||
{
|
||||
hecl::SystemString headerPath = outPath + _SYS_STR(".hpp");
|
||||
athena::io::FileWriter w(headerPath);
|
||||
if (w.hasError()) {
|
||||
Log.report(logvisor::Error, _SYS_STR("Error opening '%s' for writing"), headerPath.c_str());
|
||||
return 1;
|
||||
}
|
||||
w.writeBytes(ret.first.data(), ret.first.size());
|
||||
}
|
||||
|
||||
{
|
||||
hecl::SystemString impPath = outPath + _SYS_STR(".cpp");
|
||||
athena::io::FileWriter w(impPath);
|
||||
if (w.hasError()) {
|
||||
Log.report(logvisor::Error, _SYS_STR("Error opening '%s' for writing"), impPath.c_str());
|
||||
return 1;
|
||||
}
|
||||
w.writeBytes(ret.second.data(), ret.second.size());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,38 +4,28 @@
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace hecl::shaderc
|
||||
{
|
||||
namespace hecl::shaderc {
|
||||
|
||||
class Compiler
|
||||
{
|
||||
enum class StageType
|
||||
{
|
||||
Vertex,
|
||||
Fragment,
|
||||
Geometry,
|
||||
Control,
|
||||
Evaluation
|
||||
};
|
||||
class Compiler {
|
||||
enum class StageType { Vertex, Fragment, Geometry, Control, Evaluation };
|
||||
|
||||
std::vector<SystemString> m_inputFiles;
|
||||
std::unordered_map<SystemString, std::string> m_fileContents;
|
||||
const std::string* getFileContents(SystemStringView path);
|
||||
std::unordered_map<std::string, std::string> m_defines;
|
||||
template <typename Action, typename P>
|
||||
static bool StageAction(StageType type, const std::string& name, const std::string& basename,
|
||||
const std::string& stage, std::string& implOut);
|
||||
template <typename Action>
|
||||
static bool StageAction(const std::string& platforms, StageType type, const std::string& name,
|
||||
const std::string& basename, const std::string& stage, std::string& implOut);
|
||||
bool includeFile(SystemStringView file, std::string& out, int depth = 0);
|
||||
bool compileFile(SystemStringView file, std::string_view baseName, std::pair<std::string, std::string>& out);
|
||||
|
||||
std::vector<SystemString> m_inputFiles;
|
||||
std::unordered_map<SystemString, std::string> m_fileContents;
|
||||
const std::string* getFileContents(SystemStringView path);
|
||||
std::unordered_map<std::string, std::string> m_defines;
|
||||
template<typename Action, typename P>
|
||||
static bool StageAction(StageType type,
|
||||
const std::string& name, const std::string& basename, const std::string& stage,
|
||||
std::string& implOut);
|
||||
template<typename Action>
|
||||
static bool StageAction(const std::string& platforms, StageType type,
|
||||
const std::string& name, const std::string& basename, const std::string& stage,
|
||||
std::string& implOut);
|
||||
bool includeFile(SystemStringView file, std::string& out, int depth = 0);
|
||||
bool compileFile(SystemStringView file, std::string_view baseName, std::pair<std::string, std::string>& out);
|
||||
public:
|
||||
void addInputFile(SystemStringView file);
|
||||
void addDefine(std::string_view var, std::string_view val);
|
||||
bool compile(std::string_view baseName, std::pair<std::string, std::string>& out);
|
||||
void addInputFile(SystemStringView file);
|
||||
void addDefine(std::string_view var, std::string_view val);
|
||||
bool compile(std::string_view baseName, std::pair<std::string, std::string>& out);
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace hecl::shaderc
|
||||
|
||||
Reference in New Issue
Block a user