metaforce/hecl/shaderc/shaderc.cpp

818 lines
31 KiB
C++
Raw Normal View History

2018-10-07 02:53:57 +00:00
#include "shaderc.hpp"
#include "athena/FileReader.hpp"
#include "logvisor/logvisor.hpp"
#include "hecl/hecl.hpp"
#include "hecl/PipelineBase.hpp"
#include <algorithm>
#include <regex>
#include <unordered_map>
#include <set>
2018-10-14 20:09:15 +00:00
#include <bitset>
2018-10-16 03:15:05 +00:00
#include <memory>
#include <cstdint>
2019-07-20 04:22:58 +00:00
#include <fmt/ostream.h>
#include <sstream>
2018-10-07 02:53:57 +00:00
using namespace std::literals;
2018-12-08 05:18:42 +00:00
namespace hecl::shaderc {
2018-10-07 02:53:57 +00:00
static logvisor::Module Log("shaderc");
2019-07-20 04:22:58 +00:00
constexpr std::regex::flag_type RegexFlags = std::regex::ECMAScript | std::regex::optimize;
2018-10-07 02:53:57 +00:00
2019-06-06 00:07:29 +00:00
static const char* StageNames[] = {
"hecl::PipelineStage::Vertex",
"hecl::PipelineStage::Fragment",
"hecl::PipelineStage::Geometry",
"hecl::PipelineStage::Control",
"hecl::PipelineStage::Evaluation"
};
const std::string* Compiler::getFileContents(std::string_view path) {
2020-04-11 22:48:11 +00:00
// TODO: Heterogeneous lookup when C++20 available
2018-12-08 05:18:42 +00:00
auto search = m_fileContents.find(path.data());
if (search == m_fileContents.end()) {
athena::io::FileReader r(path);
if (r.hasError())
return nullptr;
auto len = r.length();
auto data = r.readBytes(len);
search = m_fileContents.insert(std::make_pair(path.data(), std::string((char*)data.get(), len))).first;
}
return &search->second;
2018-10-07 02:53:57 +00:00
}
void Compiler::addInputFile(std::string_view file) {
2018-12-08 05:18:42 +00:00
if (std::find(m_inputFiles.begin(), m_inputFiles.end(), file) == m_inputFiles.end())
m_inputFiles.emplace_back(file);
2018-10-07 02:53:57 +00:00
}
2018-12-08 05:18:42 +00:00
void Compiler::addDefine(std::string_view var, std::string_view val) { m_defines[var.data()] = val; }
2018-10-07 02:53:57 +00:00
2020-04-11 22:48:11 +00:00
constexpr auto ShaderHeaderTemplate = FMT_STRING(
2019-07-20 04:22:58 +00:00
"class Shader_{} : public hecl::GeneralShader\n"
"{{\n"
2018-12-08 05:18:42 +00:00
"public:\n"
" static const boo::VertexFormatInfo VtxFmt;\n"
" static const boo::AdditionalPipelineInfo PipelineInfo;\n"
" static constexpr bool HasHash = true;\n"
2019-07-20 04:22:58 +00:00
" static constexpr uint64_t Hash() {{ return 0x{:016X}; }}\n"
2019-06-06 00:07:29 +00:00
" static constexpr bool HasStageHash = true;\n"
" template <typename S>\n"
" static constexpr uint64_t StageHash();\n"
2019-07-20 04:22:58 +00:00
"}};\n\n");
2018-10-07 02:53:57 +00:00
2020-04-11 22:48:11 +00:00
constexpr auto StageObjectHeaderTemplate = FMT_STRING(
2018-12-08 05:18:42 +00:00
"template<typename P, typename S>\n"
2019-07-20 04:22:58 +00:00
"class StageObject_{} : public hecl::StageBinary<P, S>\n"
"{{\n"
2018-12-08 05:18:42 +00:00
" static const hecl::StageBinary<P, S> Prototype;\n"
"public:\n"
2019-07-20 04:22:58 +00:00
" StageObject_{}(hecl::StageConverter<P, S>& conv, hecl::FactoryCtx& ctx, const Shader_{}& in)\n"
" : hecl::StageBinary<P, S>(Prototype) {{}}\n"
"}};\n"
"STAGEOBJECT_PROTOTYPE_DECLARATIONS(StageObject_{})\n\n");
2018-10-07 02:53:57 +00:00
2020-04-11 22:48:11 +00:00
constexpr auto StageObjectImplTemplate = FMT_STRING(
2018-12-08 05:18:42 +00:00
"template<>\n"
2019-07-20 04:22:58 +00:00
"const hecl::StageBinary<hecl::PlatformType::{}, hecl::PipelineStage::{}>\n"
"StageObject_{}<hecl::PlatformType::{}, hecl::PipelineStage::{}>::Prototype = \n"
"{{{}_{}_{}_data, sizeof({}_{}_{}_data)}};\n\n");
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
struct CompileSubStageAction {
template <typename P, typename S>
2019-07-20 04:22:58 +00:00
static bool Do(const std::string& name, const std::string& basename, const std::string& stage, std::stringstream& out) {
fmt::print(out, StageObjectImplTemplate, P::Name, S::Name, name, P::Name, S::Name, basename,
P::Name, S::Name, basename, P::Name, S::Name);
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
return true;
}
2018-10-07 02:53:57 +00:00
};
2018-12-08 05:18:42 +00:00
struct CompileStageAction {
template <typename P, typename S>
2019-07-20 04:22:58 +00:00
static bool Do(const std::string& name, const std::string& basename, const std::string& stage, std::stringstream& out) {
2018-12-08 05:18:42 +00:00
std::pair<StageBinaryData, size_t> data = CompileShader<P, S>(stage);
if (data.second == 0)
return false;
2020-04-11 22:48:11 +00:00
fmt::print(out, FMT_STRING("static const uint8_t {}_{}_{}_data[] = {{\n"), name, P::Name, S::Name);
2018-12-08 05:18:42 +00:00
for (size_t i = 0; i < data.second;) {
2019-07-20 04:22:58 +00:00
out << " ";
2018-12-08 05:18:42 +00:00
for (int j = 0; j < 10 && i < data.second; ++i, ++j)
2020-04-11 22:48:11 +00:00
fmt::print(out, FMT_STRING("0x{:02X}, "), data.first.get()[i]);
2019-07-20 04:22:58 +00:00
out << "\n";
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
out << "};\n\n";
fmt::print(out, StageObjectImplTemplate, P::Name, S::Name, name, P::Name, S::Name, name,
P::Name, S::Name, name, P::Name, S::Name);
2018-12-08 05:18:42 +00:00
return true;
}
};
template <typename Action, typename P>
bool Compiler::StageAction(StageType type, const std::string& name, const std::string& basename,
2019-07-20 04:22:58 +00:00
const std::string& stage, std::stringstream& implOut) {
2018-12-08 05:18:42 +00:00
switch (type) {
case StageType::Vertex:
return Action::template Do<P, PipelineStage::Vertex>(name, basename, stage, implOut);
case StageType::Fragment:
return Action::template Do<P, PipelineStage::Fragment>(name, basename, stage, implOut);
case StageType::Geometry:
return Action::template Do<P, PipelineStage::Geometry>(name, basename, stage, implOut);
case StageType::Control:
return Action::template Do<P, PipelineStage::Control>(name, basename, stage, implOut);
case StageType::Evaluation:
return Action::template Do<P, PipelineStage::Evaluation>(name, basename, stage, implOut);
default:
break;
}
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Unknown stage type"));
2018-12-08 05:18:42 +00:00
return false;
2018-10-07 02:53:57 +00:00
}
static const std::regex regWord(R"((\w+))", RegexFlags);
2018-12-08 05:18:42 +00:00
template <typename Action>
bool Compiler::StageAction(const std::string& platforms, StageType type, const std::string& name,
2019-07-20 04:22:58 +00:00
const std::string& basename, const std::string& stage, std::stringstream& implOut) {
2018-12-08 05:18:42 +00:00
std::smatch match;
auto begin = platforms.cbegin();
auto end = platforms.cend();
while (std::regex_search(begin, end, match, regWord)) {
std::string plat = match[1].str();
std::transform(plat.begin(), plat.end(), plat.begin(), ::tolower);
if (plat == "glsl") {
if (!StageAction<Action, PlatformType::OpenGL>(type, name, basename, stage, implOut) ||
!StageAction<Action, PlatformType::Vulkan>(type, name, basename, stage, implOut)
2018-10-07 02:53:57 +00:00
#if HECL_NOUVEAU_NX
2018-12-08 05:18:42 +00:00
|| !StageAction<Action, PlatformType::NX>(type, name, basename, stage, implOut)
2018-10-07 02:53:57 +00:00
#endif
2018-12-08 05:18:42 +00:00
)
return false;
} else if (plat == "opengl") {
if (!StageAction<Action, PlatformType::OpenGL>(type, name, basename, stage, implOut))
return false;
} else if (plat == "vulkan") {
if (!StageAction<Action, PlatformType::Vulkan>(type, name, basename, stage, implOut))
return false;
} else if (plat == "nx") {
2018-10-07 02:53:57 +00:00
#if HECL_NOUVEAU_NX
2018-12-08 05:18:42 +00:00
if (!StageAction<Action, PlatformType::NX>(type, name, basename, stage, implOut))
return false;
2018-10-07 02:53:57 +00:00
#endif
2018-12-08 05:18:42 +00:00
} else if (plat == "d3d11" || plat == "hlsl") {
2018-10-07 02:53:57 +00:00
#if _WIN32
2018-12-08 05:18:42 +00:00
if (!StageAction<Action, PlatformType::D3D11>(type, name, basename, stage, implOut))
return false;
2018-10-07 02:53:57 +00:00
#endif
2018-12-08 05:18:42 +00:00
} else if (plat == "metal") {
2018-10-07 02:53:57 +00:00
#if __APPLE__
2018-12-08 05:18:42 +00:00
if (!StageAction<Action, PlatformType::Metal>(type, name, basename, stage, implOut))
return false;
2018-10-07 02:53:57 +00:00
#endif
2018-12-08 05:18:42 +00:00
} else {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Unknown platform '{}'"), plat);
2018-12-08 05:18:42 +00:00
return false;
2018-10-07 02:53:57 +00:00
}
2018-12-08 05:18:42 +00:00
begin = match.suffix().first;
}
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
return true;
2018-10-07 02:53:57 +00:00
}
static const std::regex regInclude(R"(#\s*include\s+\"(.*)\")", RegexFlags);
static const std::regex regDefine(R"(#\s*define\s+(\w+)\s*(.*))", RegexFlags);
static const std::regex regShaderEx(R"(#\s*shader\s+(\w+)\s*:\s*(\w+))", RegexFlags);
static const std::regex regShader(R"(#\s*shader\s+(\w+))", RegexFlags);
static const std::regex regAttributeEx(R"(#\s*attribute\s+(\w+)\s+([0-9]+))", RegexFlags);
static const std::regex regAttribute(R"(#\s*attribute\s+(\w+))", RegexFlags);
static const std::regex regInstAttributeEx(R"(#\s*instattribute\s+(\w+)\s+([0-9]+))", RegexFlags);
static const std::regex regInstAttribute(R"(#\s*instattribute\s+(\w+))", RegexFlags);
static const std::regex regSrcFac(R"(#\s*srcfac\s+(\w+))", RegexFlags);
static const std::regex regDstFac(R"(#\s*dstfac\s+(\w+))", RegexFlags);
static const std::regex regPrim(R"(#\s*primitive\s+(\w+))", RegexFlags);
static const std::regex regZTest(R"(#\s*depthtest\s+(\w+))", RegexFlags);
static const std::regex regDepthWrite(R"(#\s*depthwrite\s+(\w+))", RegexFlags);
static const std::regex regColorWrite(R"(#\s*colorwrite\s+(\w+))", RegexFlags);
static const std::regex regAlphaWrite(R"(#\s*alphawrite\s+(\w+))", RegexFlags);
static const std::regex regCulling(R"(#\s*culling\s+(\w+))", RegexFlags);
static const std::regex regPatchSize(R"(#\s*patchsize\s+(\w+))", RegexFlags);
static const std::regex regOverwriteAlpha(R"(#\s*overwritealpha\s+(\w+))", RegexFlags);
static const std::regex regDepthAttachment(R"(#\s*depthattachment\s+(\w+))", RegexFlags);
static const std::regex regVertex(R"(#\s*vertex\s+(.*))", RegexFlags);
static const std::regex regFragment(R"(#\s*fragment\s+(.*))", RegexFlags);
static const std::regex regGeometry(R"(#\s*geometry\s+(.*))", RegexFlags);
static const std::regex regControl(R"(#\s*control\s+(.*))", RegexFlags);
static const std::regex regEvaluation(R"(#\s*evaluation\s+(.*))", RegexFlags);
bool Compiler::includeFile(std::string_view file, std::string& out, int depth) {
2018-12-08 05:18:42 +00:00
if (depth > 32) {
Log.report(logvisor::Error, FMT_STRING("Too many levels of includes (>32) at '{}'"), file);
2018-12-08 05:18:42 +00:00
return false;
}
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
const std::string* data = getFileContents(file);
if (!data) {
Log.report(logvisor::Error, FMT_STRING("Unable to access '{}'"), file);
2018-12-08 05:18:42 +00:00
return false;
}
const std::string& sdata = *data;
std::string directory;
auto slashPos = file.find_last_of("/\\");
if (slashPos != std::string::npos)
directory = std::string(file.data(), slashPos);
2018-12-08 05:18:42 +00:00
else
directory = ".";
2018-12-08 05:18:42 +00:00
auto begin = sdata.cbegin();
auto end = sdata.cend();
while (begin != end) {
std::string::const_iterator nextBegin;
auto findPos = sdata.find('\n', begin - sdata.begin());
if (findPos == std::string::npos)
nextBegin = end;
2018-10-07 02:53:57 +00:00
else
2018-12-08 05:18:42 +00:00
nextBegin = sdata.begin() + findPos + 1;
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
std::smatch subMatch;
if (std::regex_search(begin, nextBegin, subMatch, regInclude)) {
std::string path = subMatch[1].str();
if (path.empty()) {
Log.report(logvisor::Error, FMT_STRING("Empty path provided to include in '{}'"), file);
2018-12-08 05:18:42 +00:00
return false;
}
2018-10-07 02:53:57 +00:00
if (!hecl::IsAbsolute(path))
path = directory + '/' + path;
if (!includeFile(path, out, depth + 1))
2018-10-07 02:53:57 +00:00
return false;
2018-12-08 05:18:42 +00:00
} else {
out.insert(out.end(), begin, nextBegin);
2018-10-07 02:53:57 +00:00
}
2018-12-08 05:18:42 +00:00
begin = nextBegin;
}
return true;
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
constexpr std::string_view BlendFactorToStr(boo::BlendFactor fac) {
2018-12-08 05:18:42 +00:00
switch (fac) {
case boo::BlendFactor::Zero:
default:
return "boo::BlendFactor::Zero"sv;
case boo::BlendFactor::One:
return "boo::BlendFactor::One"sv;
case boo::BlendFactor::SrcColor:
return "boo::BlendFactor::SrcColor"sv;
case boo::BlendFactor::InvSrcColor:
return "boo::BlendFactor::InvSrcColor"sv;
case boo::BlendFactor::DstColor:
return "boo::BlendFactor::DstColor"sv;
case boo::BlendFactor::InvDstColor:
return "boo::BlendFactor::InvDstColor"sv;
case boo::BlendFactor::SrcAlpha:
return "boo::BlendFactor::SrcAlpha"sv;
case boo::BlendFactor::InvSrcAlpha:
return "boo::BlendFactor::InvSrcAlpha"sv;
case boo::BlendFactor::DstAlpha:
return "boo::BlendFactor::DstAlpha"sv;
case boo::BlendFactor::InvDstAlpha:
return "boo::BlendFactor::InvDstAlpha"sv;
case boo::BlendFactor::SrcColor1:
return "boo::BlendFactor::SrcColor1"sv;
case boo::BlendFactor::InvSrcColor1:
return "boo::BlendFactor::InvSrcColor1"sv;
case boo::BlendFactor::Subtract:
return "boo::BlendFactor::Subtract"sv;
}
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
inline bool StrToBlendFactor(std::string str, boo::BlendFactor& fac) {
2018-12-08 05:18:42 +00:00
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
if (str == "zero")
fac = boo::BlendFactor::Zero;
else if (str == "one")
fac = boo::BlendFactor::One;
else if (str == "srccolor")
fac = boo::BlendFactor::SrcColor;
else if (str == "invsrccolor")
fac = boo::BlendFactor::InvSrcColor;
else if (str == "dstcolor")
fac = boo::BlendFactor::DstColor;
else if (str == "invdstcolor")
fac = boo::BlendFactor::InvDstColor;
else if (str == "srcalpha")
fac = boo::BlendFactor::SrcAlpha;
else if (str == "invsrcalpha")
fac = boo::BlendFactor::InvSrcAlpha;
else if (str == "dstalpha")
fac = boo::BlendFactor::DstAlpha;
else if (str == "invdstalpha")
fac = boo::BlendFactor::InvDstAlpha;
else if (str == "srccolor1")
fac = boo::BlendFactor::SrcColor1;
else if (str == "invsrccolor1")
fac = boo::BlendFactor::InvSrcColor1;
else if (str == "subtract")
fac = boo::BlendFactor::Subtract;
else {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Unrecognized blend mode '{}'"), str);
2018-12-08 05:18:42 +00:00
return false;
}
return true;
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
constexpr std::string_view PrimitiveToStr(boo::Primitive prim) {
2018-12-08 05:18:42 +00:00
switch (prim) {
case boo::Primitive::Triangles:
default:
return "boo::Primitive::Triangles"sv;
case boo::Primitive::TriStrips:
return "boo::Primitive::TriStrips"sv;
case boo::Primitive::Patches:
return "boo::Primitive::Patches"sv;
}
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
inline bool StrToPrimitive(std::string str, boo::Primitive& prim) {
2018-12-08 05:18:42 +00:00
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
if (str == "triangles")
prim = boo::Primitive::Triangles;
else if (str == "tristrips")
prim = boo::Primitive::TriStrips;
else if (str == "patches")
prim = boo::Primitive::Patches;
else {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Unrecognized primitive '{}'"), str);
2018-12-08 05:18:42 +00:00
return false;
}
return true;
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
constexpr std::string_view ZTestToStr(boo::ZTest ztest) {
2018-12-08 05:18:42 +00:00
switch (ztest) {
case boo::ZTest::None:
default:
return "boo::ZTest::None"sv;
case boo::ZTest::LEqual:
return "boo::ZTest::LEqual"sv;
case boo::ZTest::Greater:
return "boo::ZTest::Greater"sv;
case boo::ZTest::GEqual:
return "boo::ZTest::GEqual"sv;
case boo::ZTest::Equal:
return "boo::ZTest::Equal"sv;
}
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
inline bool StrToZTest(std::string str, boo::ZTest& ztest) {
2018-12-08 05:18:42 +00:00
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
if (str == "none")
ztest = boo::ZTest::None;
else if (str == "lequal")
ztest = boo::ZTest::LEqual;
else if (str == "greater")
ztest = boo::ZTest::Greater;
else if (str == "gequal")
ztest = boo::ZTest::GEqual;
else if (str == "equal")
ztest = boo::ZTest::Equal;
else {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Unrecognized ztest '{}'"), str);
2018-12-08 05:18:42 +00:00
return false;
}
return true;
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
constexpr std::string_view CullModeToStr(boo::CullMode cull) {
2018-12-08 05:18:42 +00:00
switch (cull) {
case boo::CullMode::None:
default:
return "boo::CullMode::None"sv;
case boo::CullMode::Backface:
return "boo::CullMode::Backface"sv;
case boo::CullMode::Frontface:
return "boo::CullMode::Frontface"sv;
}
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
inline bool StrToCullMode(std::string str, boo::CullMode& cull) {
2018-12-08 05:18:42 +00:00
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
if (str == "none")
cull = boo::CullMode::None;
else if (str == "backface")
cull = boo::CullMode::Backface;
else if (str == "frontface")
cull = boo::CullMode::Frontface;
else {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Unrecognized cull mode '{}'"), str);
2018-12-08 05:18:42 +00:00
return false;
}
return true;
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
constexpr std::string_view BoolToStr(bool b) { return b ? "true"sv : "false"sv; }
2018-12-08 05:18:42 +00:00
2019-07-20 04:22:58 +00:00
inline bool StrToBool(std::string str, bool& b) {
2018-12-08 05:18:42 +00:00
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
if (strtol(str.c_str(), nullptr, 0))
b = true;
else if (str == "true")
b = true;
else if (str == "false")
b = false;
else {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Unrecognized bool '{}'"), str);
2018-12-08 05:18:42 +00:00
return false;
}
return true;
}
2018-10-07 02:53:57 +00:00
2019-07-20 04:22:58 +00:00
template <typename Format>
constexpr void SemanticOut(std::stringstream& out,
const std::pair<boo::VertexSemantic, int>& attr, const Format& fmtstr) {
fmt::print(out, fmtstr,
True(attr.first & boo::VertexSemantic::Instanced)
? " | boo::VertexSemantic::Instanced"
: "",
attr.second);
}
bool Compiler::compileFile(std::string_view file, std::string_view baseName,
2019-07-20 04:22:58 +00:00
std::pair<std::stringstream, std::stringstream>& out) {
2018-12-08 05:18:42 +00:00
std::string includesPass;
if (!includeFile(file, includesPass))
return false;
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
std::string shaderName(baseName);
std::string shaderBase;
std::vector<std::pair<boo::VertexSemantic, int>> shaderAttributes;
bool shaderAttributesReset = false;
boo::AdditionalPipelineInfo shaderInfo = {};
std::string stagePlatforms;
StageType stageType;
auto stageBegin = includesPass.cend();
auto stageEnd = includesPass.cend();
2019-06-06 00:07:29 +00:00
struct ShaderStageUse {
std::bitset<6> stages;
std::array<unsigned long long, 6> stageHashes = {};
std::set<std::string> platforms;
};
std::unordered_map<std::string, ShaderStageUse> shaderStageUses;
2018-12-08 05:18:42 +00:00
std::unordered_map<std::string, std::string> shaderBases;
auto _DoCompile = [&]() {
if (stageBegin == includesPass.end())
return true;
if (shaderName.empty()) {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("`#shader <name>` must be issued before stages"));
2018-12-08 05:18:42 +00:00
return false;
}
std::string stage(stageBegin, stageEnd);
for (const auto& define : m_defines) {
std::string::size_type pos = 0;
while ((pos = stage.find(define.first, pos)) != std::string::npos) {
stage = std::string(stage.begin(), stage.begin() + pos) + define.second +
std::string(stage.begin() + pos + define.first.size(), stage.end());
}
}
stageBegin = includesPass.end();
2019-06-06 00:07:29 +00:00
ShaderStageUse& uses = shaderStageUses[shaderName];
uses.stages.set(size_t(stageType));
uses.stageHashes[size_t(stageType)] = XXH64(stage.c_str(), stage.size(), 0);
uses.platforms.insert(stagePlatforms);
2018-12-08 05:18:42 +00:00
return StageAction<CompileStageAction>(stagePlatforms, stageType, shaderName, shaderBase, stage, out.second);
};
auto DoCompile = [&](std::string platform, StageType type, std::string::const_iterator end,
std::string::const_iterator begin) {
stageEnd = end;
bool ret = _DoCompile();
stagePlatforms = std::move(platform);
stageType = type;
stageBegin = begin;
return ret;
};
auto DoShader = [&]() {
if (shaderBase.empty() && shaderStageUses.find(shaderName) == shaderStageUses.cend())
return true;
2019-06-06 00:07:29 +00:00
ShaderStageUse& uses = shaderStageUses[shaderName];
if (uses.stages.test(5))
2018-12-08 05:18:42 +00:00
return true;
2019-07-20 04:22:58 +00:00
fmt::print(out.first, ShaderHeaderTemplate, shaderName, XXH64(shaderName.c_str(), shaderName.size(), 0));
fmt::print(out.first, StageObjectHeaderTemplate, shaderName, shaderName, shaderName, shaderName);
2018-12-08 05:18:42 +00:00
if (!shaderBase.empty()) {
shaderBases[shaderName] = shaderBase;
for (int i = 0; i < 5; ++i) {
2019-06-06 00:07:29 +00:00
if (uses.stages.test(size_t(i)))
2018-12-08 05:18:42 +00:00
continue;
std::string useBase = shaderBase;
2019-06-06 00:07:29 +00:00
std::unordered_map<std::string, ShaderStageUse>::const_iterator baseUses =
2018-12-08 05:18:42 +00:00
shaderStageUses.find(useBase);
2019-06-06 00:07:29 +00:00
while (baseUses == shaderStageUses.cend() || !baseUses->second.stages.test(size_t(i))) {
2018-12-08 05:18:42 +00:00
auto search = shaderBases.find(useBase);
if (search == shaderBases.cend())
break;
useBase = search->second;
baseUses = shaderStageUses.find(useBase);
}
2019-06-06 00:07:29 +00:00
if (baseUses == shaderStageUses.cend() || !baseUses->second.stages.test(size_t(i)))
2018-12-08 05:18:42 +00:00
continue;
2019-06-06 00:07:29 +00:00
for (const std::string& basePlatforms : baseUses->second.platforms) {
uses.stageHashes[size_t(i)] = baseUses->second.stageHashes[size_t(i)];
2018-12-08 05:18:42 +00:00
StageAction<CompileSubStageAction>(basePlatforms, StageType(i), shaderName, useBase, {}, out.second);
}
}
shaderBase.clear();
2018-10-07 02:53:57 +00:00
}
2020-04-11 22:48:11 +00:00
fmt::print(out.second, FMT_STRING("static const boo::VertexElementDescriptor {}_vtxfmtelems[] = {{\n"), shaderName);
2018-12-08 05:18:42 +00:00
for (const auto& attr : shaderAttributes) {
switch (attr.first & boo::VertexSemantic::SemanticMask) {
2021-05-24 21:25:31 +00:00
case boo::VertexSemantic::Position2:
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::Position2{}, {}}},\n"));
break;
2018-12-08 05:18:42 +00:00
case boo::VertexSemantic::Position3:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::Position3{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::Position4:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::Position4{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::Normal3:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::Normal3{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::Normal4:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::Normal4{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::Color:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::Color{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::ColorUNorm:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::ColorUNorm{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::UV2:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::UV2{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::UV4:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::UV4{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::Weight:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::Weight{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
case boo::VertexSemantic::ModelView:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::ModelView{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
default:
2020-04-11 22:48:11 +00:00
SemanticOut(out.second, attr, FMT_STRING("{{boo::VertexSemantic::None{}, {}}},\n"));
2018-12-08 05:18:42 +00:00
break;
}
}
2019-07-20 04:22:58 +00:00
out.second << "};\n";
2020-04-11 22:48:11 +00:00
fmt::print(out.second, FMT_STRING("const boo::VertexFormatInfo Shader_{}::VtxFmt = {{ {}_vtxfmtelems }};\n\n"),
2019-07-20 04:22:58 +00:00
shaderName, shaderName);
2020-04-11 22:48:11 +00:00
fmt::print(out.second, FMT_STRING("const boo::AdditionalPipelineInfo Shader_{}::PipelineInfo = {{\n"), shaderName);
2019-07-20 04:22:58 +00:00
out.second << BlendFactorToStr(shaderInfo.srcFac);
out.second << ", ";
out.second << BlendFactorToStr(shaderInfo.dstFac);
out.second << ", ";
out.second << PrimitiveToStr(shaderInfo.prim);
out.second << ", ";
out.second << ZTestToStr(shaderInfo.depthTest);
out.second << ",\n";
out.second << BoolToStr(shaderInfo.depthWrite);
out.second << ", ";
out.second << BoolToStr(shaderInfo.colorWrite);
out.second << ", ";
out.second << BoolToStr(shaderInfo.alphaWrite);
out.second << ", ";
out.second << CullModeToStr(shaderInfo.culling);
out.second << ", ";
2020-04-11 22:48:11 +00:00
fmt::print(out.second, FMT_STRING("{}, "), shaderInfo.patchSize);
2019-07-20 04:22:58 +00:00
out.second << BoolToStr(shaderInfo.overwriteAlpha);
out.second << ", ";
out.second << BoolToStr(shaderInfo.depthAttachment);
out.second << ", ";
out.second << "};\n\n";
2018-12-08 05:18:42 +00:00
2019-06-06 00:07:29 +00:00
uses.stages.set(5);
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
return true;
};
auto AddAttribute = [&](std::string semantic, std::string idx, bool inst) {
if (shaderAttributesReset) {
shaderAttributes.clear();
shaderAttributesReset = false;
2018-10-07 02:53:57 +00:00
}
2018-12-08 05:18:42 +00:00
boo::VertexSemantic orsem = inst ? boo::VertexSemantic::Instanced : boo::VertexSemantic::None;
int idxNum = int(strtoul(idx.c_str(), nullptr, 10));
std::transform(semantic.begin(), semantic.end(), semantic.begin(), ::tolower);
2021-05-24 21:25:31 +00:00
if (semantic == "position2")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::Position2 | orsem, idxNum));
else if (semantic == "position3")
2018-12-08 05:18:42 +00:00
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::Position3 | orsem, idxNum));
else if (semantic == "position4")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::Position4 | orsem, idxNum));
else if (semantic == "normal3")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::Normal3 | orsem, idxNum));
else if (semantic == "normal4")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::Normal4 | orsem, idxNum));
else if (semantic == "color")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::Color | orsem, idxNum));
else if (semantic == "colorunorm")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::ColorUNorm | orsem, idxNum));
else if (semantic == "uv2")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::UV2 | orsem, idxNum));
else if (semantic == "uv4")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::UV4 | orsem, idxNum));
else if (semantic == "weight")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::Weight | orsem, idxNum));
else if (semantic == "modelview")
shaderAttributes.push_back(std::make_pair(boo::VertexSemantic::ModelView | orsem, idxNum));
else {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Unrecognized vertex semantic '{}'"), semantic);
2018-12-08 05:18:42 +00:00
return false;
2018-10-07 02:53:57 +00:00
}
return true;
2018-12-08 05:18:42 +00:00
};
auto begin = includesPass.cbegin();
auto end = includesPass.cend();
std::string* defineContinue = nullptr;
while (begin != end) {
std::string::const_iterator nextBegin;
auto findPos = includesPass.find('\n', begin - includesPass.cbegin());
if (findPos == std::string::npos)
nextBegin = end;
else
nextBegin = includesPass.cbegin() + findPos + 1;
std::smatch subMatch;
if (defineContinue) {
std::string extraLine;
if (findPos == std::string::npos)
extraLine = std::string(begin, end);
else
extraLine = std::string(begin, includesPass.cbegin() + findPos);
*defineContinue += extraLine;
if (!defineContinue->empty() && defineContinue->back() == '\r')
defineContinue->pop_back();
if (!defineContinue->empty() && defineContinue->back() == '\\')
defineContinue->pop_back();
else
defineContinue = nullptr;
} else if (std::regex_search(begin, nextBegin, subMatch, regDefine)) {
std::string& defOut = m_defines[subMatch[1].str()];
defOut = subMatch[2].str();
if (!defOut.empty() && defOut.back() == '\r')
defOut.pop_back();
if (!defOut.empty() && defOut.back() == '\\') {
defOut.pop_back();
defineContinue = &defOut;
}
} else if (std::regex_search(begin, nextBegin, subMatch, regShaderEx)) {
stageEnd = begin;
if (!_DoCompile() || !DoShader())
return false;
shaderName = subMatch[1].str();
shaderBase = subMatch[2].str();
shaderAttributesReset = true;
// shaderAttributes.clear();
// shaderInfo = boo::AdditionalPipelineInfo();
} else if (std::regex_search(begin, nextBegin, subMatch, regShader)) {
stageEnd = begin;
if (!_DoCompile() || !DoShader())
return false;
shaderName = subMatch[1].str();
shaderAttributesReset = true;
// shaderAttributes.clear();
// shaderInfo = boo::AdditionalPipelineInfo();
} else if (std::regex_search(begin, nextBegin, subMatch, regAttributeEx)) {
if (!AddAttribute(subMatch[1].str(), subMatch[2].str(), false))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regAttribute)) {
if (!AddAttribute(subMatch[1].str(), "0", false))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regInstAttributeEx)) {
if (!AddAttribute(subMatch[1].str(), subMatch[2].str(), true))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regInstAttribute)) {
if (!AddAttribute(subMatch[1].str(), "0", true))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regSrcFac)) {
if (!StrToBlendFactor(subMatch[1].str(), shaderInfo.srcFac))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regDstFac)) {
if (!StrToBlendFactor(subMatch[1].str(), shaderInfo.dstFac))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regPrim)) {
if (!StrToPrimitive(subMatch[1].str(), shaderInfo.prim))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regZTest)) {
if (!StrToZTest(subMatch[1].str(), shaderInfo.depthTest))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regDepthWrite)) {
if (!StrToBool(subMatch[1].str(), shaderInfo.depthWrite))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regColorWrite)) {
if (!StrToBool(subMatch[1].str(), shaderInfo.colorWrite))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regAlphaWrite)) {
if (!StrToBool(subMatch[1].str(), shaderInfo.alphaWrite))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regCulling)) {
if (!StrToCullMode(subMatch[1].str(), shaderInfo.culling))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regPatchSize)) {
auto str = subMatch[1].str();
char* endptr;
shaderInfo.patchSize = uint32_t(strtoul(str.c_str(), &endptr, 0));
if (endptr == str.c_str()) {
2020-04-11 22:48:11 +00:00
Log.report(logvisor::Error, FMT_STRING("Non-unsigned-integer value for #patchsize directive"));
2018-12-08 05:18:42 +00:00
return false;
}
} else if (std::regex_search(begin, nextBegin, subMatch, regOverwriteAlpha)) {
if (!StrToBool(subMatch[1].str(), shaderInfo.overwriteAlpha))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regDepthAttachment)) {
if (!StrToBool(subMatch[1].str(), shaderInfo.depthAttachment))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regVertex)) {
if (!DoCompile(subMatch[1].str(), StageType::Vertex, begin, nextBegin))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regFragment)) {
if (!DoCompile(subMatch[1].str(), StageType::Fragment, begin, nextBegin))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regGeometry)) {
if (!DoCompile(subMatch[1].str(), StageType::Geometry, begin, nextBegin))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regControl)) {
if (!DoCompile(subMatch[1].str(), StageType::Control, begin, nextBegin))
return false;
} else if (std::regex_search(begin, nextBegin, subMatch, regEvaluation)) {
if (!DoCompile(subMatch[1].str(), StageType::Evaluation, begin, nextBegin))
return false;
}
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
begin = nextBegin;
}
2018-10-07 02:53:57 +00:00
2018-12-08 05:18:42 +00:00
stageEnd = begin;
if (!_DoCompile() || !DoShader())
return false;
2018-10-07 02:53:57 +00:00
2019-06-06 00:07:29 +00:00
for (const auto& shader : shaderStageUses) {
for (int i = 0; i < 5; ++i) {
if (shader.second.stageHashes[i]) {
2019-07-20 04:22:58 +00:00
out.first << "template <> constexpr uint64_t Shader_";
out.first << shader.first;
2020-04-11 22:48:11 +00:00
fmt::print(out.first, FMT_STRING("::StageHash<{}>() {{ return 0x{:016X}; }}\n"),
2019-07-20 04:22:58 +00:00
StageNames[i], shader.second.stageHashes[i]);
2019-06-06 00:07:29 +00:00
}
}
}
2019-07-20 04:22:58 +00:00
out.first << "\n";
2019-06-06 00:07:29 +00:00
2019-07-20 04:22:58 +00:00
out.first << "#define UNIVERSAL_PIPELINES_";
out.first << baseName;
2018-12-08 05:18:42 +00:00
for (const auto& shader : shaderStageUses) {
2019-07-20 04:22:58 +00:00
out.first << " \\\n";
out.first << "::Shader_";
out.first << shader.first;
2018-12-08 05:18:42 +00:00
}
2019-07-20 04:22:58 +00:00
out.first << "\n";
2018-12-08 05:18:42 +00:00
2019-07-20 04:22:58 +00:00
out.first << "#define STAGES_";
out.first << baseName;
out.first << "(P, S)";
2018-12-08 05:18:42 +00:00
for (const auto& shader : shaderStageUses) {
2019-07-20 04:22:58 +00:00
out.first << " \\\n";
out.first << "::StageObject_";
out.first << shader.first;
out.first << "<P, S>,";
2018-12-08 05:18:42 +00:00
}
2019-07-20 04:22:58 +00:00
out.first << "\n";
2021-05-24 21:25:31 +00:00
2018-12-08 05:18:42 +00:00
return true;
2018-10-07 02:53:57 +00:00
}
2019-07-20 04:22:58 +00:00
bool Compiler::compile(std::string_view baseName, std::pair<std::stringstream, std::stringstream>& out) {
out.first << "#pragma once\n"
"#include \"hecl/PipelineBase.hpp\"\n\n";
2020-04-11 22:48:11 +00:00
fmt::print(out.second, FMT_STRING("#include \"{}.hpp\"\n\n"), baseName);
2018-12-08 05:18:42 +00:00
for (const auto& file : m_inputFiles)
if (!compileFile(file, baseName, out))
return false;
return true;
2018-10-07 02:53:57 +00:00
}
2018-12-08 05:18:42 +00:00
} // namespace hecl::shaderc