2018-10-07 02:53:57 +00:00
|
|
|
#include "hecl/Compilers.hpp"
|
2019-08-23 15:49:53 +00:00
|
|
|
#include <boo/graphicsdev/GLSLMacros.hpp>
|
|
|
|
#include <logvisor/logvisor.hpp>
|
|
|
|
|
2018-10-07 02:53:57 +00:00
|
|
|
#include <glslang/Public/ShaderLang.h>
|
|
|
|
#include <StandAlone/ResourceLimits.h>
|
|
|
|
#include <SPIRV/GlslangToSpv.h>
|
|
|
|
#include <SPIRV/disassemble.h>
|
2019-08-23 15:49:53 +00:00
|
|
|
|
2018-10-14 20:09:15 +00:00
|
|
|
#if _WIN32
|
|
|
|
#include <d3dcompiler.h>
|
|
|
|
extern pD3DCompile D3DCompilePROC;
|
|
|
|
#endif
|
2019-08-23 15:49:53 +00:00
|
|
|
|
2018-10-16 03:15:05 +00:00
|
|
|
#if __APPLE__
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <memory>
|
|
|
|
#endif
|
2018-10-07 02:53:57 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
namespace hecl {
|
2018-10-07 02:53:57 +00:00
|
|
|
logvisor::Module Log("hecl::Compilers");
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
template <typename P>
|
|
|
|
struct ShaderCompiler {};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ShaderCompiler<PlatformType::OpenGL> {
|
|
|
|
template <typename S>
|
|
|
|
static std::pair<StageBinaryData, size_t> Compile(std::string_view text) {
|
|
|
|
std::string str = "#version 330\n";
|
|
|
|
str += BOO_GLSL_BINDING_HEAD;
|
|
|
|
str += text;
|
|
|
|
std::pair<StageBinaryData, size_t> ret(MakeStageBinaryData(str.size() + 1), str.size() + 1);
|
|
|
|
memcpy(ret.first.get(), str.data(), ret.second);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-10-07 02:53:57 +00:00
|
|
|
};
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
template <>
|
|
|
|
struct ShaderCompiler<PlatformType::Vulkan> {
|
|
|
|
static constexpr EShLanguage ShaderTypes[] = {EShLangVertex, /* Invalid */
|
|
|
|
EShLangVertex, EShLangFragment, EShLangGeometry,
|
|
|
|
EShLangTessControl, EShLangTessEvaluation};
|
|
|
|
|
|
|
|
template <typename S>
|
|
|
|
static std::pair<StageBinaryData, size_t> Compile(std::string_view text) {
|
|
|
|
EShLanguage lang = ShaderTypes[int(S::Enum)];
|
|
|
|
const EShMessages messages = EShMessages(EShMsgSpvRules | EShMsgVulkanRules);
|
|
|
|
glslang::TShader shader(lang);
|
|
|
|
const char* strings[] = {"#version 330\n", BOO_GLSL_BINDING_HEAD, text.data()};
|
|
|
|
shader.setStrings(strings, 3);
|
|
|
|
if (!shader.parse(&glslang::DefaultTBuiltInResource, 110, false, messages)) {
|
2019-07-20 04:22:58 +00:00
|
|
|
fmt::print(fmt("{}\n"), text);
|
|
|
|
Log.report(logvisor::Fatal, fmt("unable to compile shader\n{}"), shader.getInfoLog());
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
2018-10-07 02:53:57 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
glslang::TProgram prog;
|
|
|
|
prog.addShader(&shader);
|
|
|
|
if (!prog.link(messages)) {
|
2019-07-20 04:22:58 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to link shader program\n{}"), prog.getInfoLog());
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
2018-10-07 02:53:57 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
|
|
|
|
std::vector<unsigned int> out;
|
|
|
|
glslang::GlslangToSpv(*prog.getIntermediate(lang), out);
|
|
|
|
std::pair<StageBinaryData, size_t> ret(MakeStageBinaryData(out.size() * 4), out.size() * 4);
|
|
|
|
memcpy(ret.first.get(), out.data(), ret.second);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-10-07 02:53:57 +00:00
|
|
|
};
|
|
|
|
|
2018-10-14 20:09:15 +00:00
|
|
|
#if _WIN32
|
2018-12-08 05:18:42 +00:00
|
|
|
static const char* D3DShaderTypes[] = {nullptr, "vs_5_0", "ps_5_0", "gs_5_0", "hs_5_0", "ds_5_0"};
|
|
|
|
template <>
|
|
|
|
struct ShaderCompiler<PlatformType::D3D11> {
|
2018-10-14 20:09:15 +00:00
|
|
|
#if _DEBUG && 0
|
|
|
|
#define BOO_D3DCOMPILE_FLAG D3DCOMPILE_DEBUG | D3DCOMPILE_OPTIMIZATION_LEVEL0
|
|
|
|
#else
|
|
|
|
#define BOO_D3DCOMPILE_FLAG D3DCOMPILE_OPTIMIZATION_LEVEL3
|
|
|
|
#endif
|
2018-12-08 05:18:42 +00:00
|
|
|
template <typename S>
|
|
|
|
static std::pair<StageBinaryData, size_t> Compile(std::string_view text) {
|
|
|
|
ComPtr<ID3DBlob> errBlob;
|
|
|
|
ComPtr<ID3DBlob> blobOut;
|
|
|
|
if (FAILED(D3DCompilePROC(text.data(), text.size(), "Boo HLSL Source", nullptr, nullptr, "main",
|
|
|
|
D3DShaderTypes[int(S::Enum)], BOO_D3DCOMPILE_FLAG, 0, &blobOut, &errBlob))) {
|
2019-08-23 15:49:53 +00:00
|
|
|
fmt::print(fmt("{}\n"), text);
|
2019-07-28 01:19:48 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("error compiling shader: {}"), (char*)errBlob->GetBufferPointer());
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
2018-10-14 20:09:15 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
std::pair<StageBinaryData, size_t> ret(MakeStageBinaryData(blobOut->GetBufferSize()), blobOut->GetBufferSize());
|
|
|
|
memcpy(ret.first.get(), blobOut->GetBufferPointer(), blobOut->GetBufferSize());
|
|
|
|
return ret;
|
|
|
|
}
|
2018-10-14 20:09:15 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-10-16 03:15:05 +00:00
|
|
|
#if __APPLE__
|
2018-12-08 05:18:42 +00:00
|
|
|
template <>
|
|
|
|
struct ShaderCompiler<PlatformType::Metal> {
|
|
|
|
static bool m_didCompilerSearch;
|
|
|
|
static bool m_hasCompiler;
|
|
|
|
|
|
|
|
static bool SearchForCompiler() {
|
|
|
|
m_didCompilerSearch = true;
|
|
|
|
const char* no_metal_compiler = getenv("HECL_NO_METAL_COMPILER");
|
|
|
|
if (no_metal_compiler && atoi(no_metal_compiler))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (!pid) {
|
2019-08-23 15:56:34 +00:00
|
|
|
execlp("xcrun", "xcrun", "-sdk", "macosx", "metal", "--version", nullptr);
|
2018-12-08 05:18:42 +00:00
|
|
|
/* xcrun returns 72 if metal command not found;
|
|
|
|
* emulate that if xcrun not found */
|
|
|
|
exit(72);
|
2018-10-16 03:15:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
int status, ret;
|
|
|
|
while ((ret = waitpid(pid, &status, 0)) < 0 && errno == EINTR) {}
|
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
2019-07-01 08:26:42 +00:00
|
|
|
return WEXITSTATUS(status) == 0;
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename S>
|
|
|
|
static std::pair<StageBinaryData, size_t> Compile(std::string_view text) {
|
|
|
|
if (!m_didCompilerSearch)
|
|
|
|
m_hasCompiler = SearchForCompiler();
|
|
|
|
|
|
|
|
std::string str =
|
|
|
|
"#include <metal_stdlib>\n"
|
|
|
|
"using namespace metal;\n";
|
|
|
|
str += text;
|
|
|
|
std::pair<StageBinaryData, size_t> ret;
|
|
|
|
|
|
|
|
if (!m_hasCompiler) {
|
|
|
|
/* First byte unset to indicate source data */
|
|
|
|
ret.first = MakeStageBinaryData(str.size() + 2);
|
|
|
|
ret.first.get()[0] = 0;
|
|
|
|
ret.second = str.size() + 2;
|
|
|
|
memcpy(&ret.first.get()[1], str.data(), str.size() + 1);
|
|
|
|
} else {
|
|
|
|
int compilerOut[2];
|
|
|
|
int compilerIn[2];
|
|
|
|
pipe(compilerOut);
|
|
|
|
pipe(compilerIn);
|
|
|
|
|
|
|
|
pid_t pid = getpid();
|
|
|
|
const char* tmpdir = getenv("TMPDIR");
|
2019-07-20 04:22:58 +00:00
|
|
|
std::string libFile = fmt::format(fmt("{}boo_metal_shader{}.metallib"), tmpdir, pid);
|
2018-12-08 05:18:42 +00:00
|
|
|
|
|
|
|
/* Pipe source write to compiler */
|
|
|
|
pid_t compilerPid = fork();
|
|
|
|
if (!compilerPid) {
|
|
|
|
dup2(compilerIn[0], STDIN_FILENO);
|
|
|
|
dup2(compilerOut[1], STDOUT_FILENO);
|
|
|
|
|
|
|
|
close(compilerOut[0]);
|
|
|
|
close(compilerOut[1]);
|
|
|
|
close(compilerIn[0]);
|
|
|
|
close(compilerIn[1]);
|
|
|
|
|
|
|
|
execlp("xcrun", "xcrun", "-sdk", "macosx", "metal", "-o", "/dev/stdout", "-Wno-unused-variable",
|
2019-02-04 01:38:14 +00:00
|
|
|
"-Wno-unused-const-variable", "-Wno-unused-function", "-c", "-x", "metal",
|
|
|
|
#ifndef NDEBUG
|
|
|
|
"-gline-tables-only", "-MO",
|
|
|
|
#endif
|
2019-08-23 15:56:34 +00:00
|
|
|
"-", nullptr);
|
2019-08-23 15:53:35 +00:00
|
|
|
fmt::print(stderr, fmt("execlp fail {}\n"), strerror(errno));
|
2018-12-08 05:18:42 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(compilerIn[0]);
|
|
|
|
close(compilerOut[1]);
|
|
|
|
|
|
|
|
/* Pipe compiler to linker */
|
|
|
|
pid_t linkerPid = fork();
|
|
|
|
if (!linkerPid) {
|
|
|
|
dup2(compilerOut[0], STDIN_FILENO);
|
|
|
|
|
|
|
|
close(compilerOut[0]);
|
|
|
|
close(compilerIn[1]);
|
|
|
|
|
|
|
|
/* metallib doesn't like outputting to a pipe, so temp file will have to do */
|
2019-08-23 15:56:34 +00:00
|
|
|
execlp("xcrun", "xcrun", "-sdk", "macosx", "metallib", "-", "-o", libFile.c_str(), nullptr);
|
2019-08-23 15:53:35 +00:00
|
|
|
fmt::print(stderr, fmt("execlp fail {}\n"), strerror(errno));
|
2018-12-08 05:18:42 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(compilerOut[0]);
|
|
|
|
|
|
|
|
/* Stream in source */
|
|
|
|
const char* inPtr = str.data();
|
|
|
|
size_t inRem = str.size();
|
|
|
|
while (inRem) {
|
|
|
|
ssize_t writeRes = write(compilerIn[1], inPtr, inRem);
|
|
|
|
if (writeRes < 0) {
|
2019-08-23 15:53:35 +00:00
|
|
|
fmt::print(stderr, fmt("write fail {}\n"), strerror(errno));
|
2018-12-08 05:18:42 +00:00
|
|
|
break;
|
2018-10-16 03:15:05 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
inPtr += writeRes;
|
|
|
|
inRem -= writeRes;
|
|
|
|
}
|
|
|
|
close(compilerIn[1]);
|
|
|
|
|
|
|
|
/* Wait for completion */
|
|
|
|
int compilerStat, linkerStat;
|
|
|
|
while (waitpid(compilerPid, &compilerStat, 0) < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2019-08-23 15:53:35 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("waitpid fail {}"), strerror(errno));
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WEXITSTATUS(compilerStat)) {
|
2019-07-20 04:22:58 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("compile fail"));
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
while (waitpid(linkerPid, &linkerStat, 0) < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2019-08-23 15:53:35 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("waitpid fail {}"), strerror(errno));
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WEXITSTATUS(linkerStat)) {
|
2019-07-20 04:22:58 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("link fail"));
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy temp file into buffer with first byte set to indicate binary data */
|
2019-07-20 04:22:58 +00:00
|
|
|
FILE* fin = fopen(libFile.c_str(), "rb");
|
2018-12-08 05:18:42 +00:00
|
|
|
fseek(fin, 0, SEEK_END);
|
|
|
|
long libLen = ftell(fin);
|
|
|
|
fseek(fin, 0, SEEK_SET);
|
|
|
|
ret.first = MakeStageBinaryData(libLen + 1);
|
|
|
|
ret.first.get()[0] = 1;
|
|
|
|
ret.second = libLen + 1;
|
|
|
|
fread(&ret.first.get()[1], 1, libLen, fin);
|
|
|
|
fclose(fin);
|
|
|
|
unlink(libFile);
|
2018-10-16 03:15:05 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-10-16 03:15:05 +00:00
|
|
|
};
|
|
|
|
bool ShaderCompiler<PlatformType::Metal>::m_didCompilerSearch = false;
|
|
|
|
bool ShaderCompiler<PlatformType::Metal>::m_hasCompiler = false;
|
|
|
|
#endif
|
|
|
|
|
2018-10-14 20:09:15 +00:00
|
|
|
#if HECL_NOUVEAU_NX
|
2018-12-08 05:18:42 +00:00
|
|
|
template <>
|
|
|
|
struct ShaderCompiler<PlatformType::NX> {
|
|
|
|
template <typename S>
|
|
|
|
static std::pair<std::shared_ptr<uint8_t[]>, size_t> Compile(std::string_view text) {
|
|
|
|
std::string str = "#version 330\n";
|
|
|
|
str += BOO_GLSL_BINDING_HEAD;
|
|
|
|
str += text;
|
|
|
|
std::pair<std::shared_ptr<uint8_t[]>, size_t> ret(new uint8_t[str.size() + 1], str.size() + 1);
|
|
|
|
memcpy(ret.first.get(), str.data(), str.size() + 1);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-10-07 02:53:57 +00:00
|
|
|
};
|
2018-10-14 20:09:15 +00:00
|
|
|
#endif
|
2018-10-07 02:53:57 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
template <typename P, typename S>
|
|
|
|
std::pair<StageBinaryData, size_t> CompileShader(std::string_view text) {
|
|
|
|
return ShaderCompiler<P>::template Compile<S>(text);
|
2018-10-07 02:53:57 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
#define SPECIALIZE_COMPILE_SHADER(P) \
|
|
|
|
template std::pair<StageBinaryData, size_t> CompileShader<P, PipelineStage::Vertex>(std::string_view text); \
|
|
|
|
template std::pair<StageBinaryData, size_t> CompileShader<P, PipelineStage::Fragment>(std::string_view text); \
|
|
|
|
template std::pair<StageBinaryData, size_t> CompileShader<P, PipelineStage::Geometry>(std::string_view text); \
|
|
|
|
template std::pair<StageBinaryData, size_t> CompileShader<P, PipelineStage::Control>(std::string_view text); \
|
|
|
|
template std::pair<StageBinaryData, size_t> CompileShader<P, PipelineStage::Evaluation>(std::string_view text);
|
2018-10-07 02:53:57 +00:00
|
|
|
SPECIALIZE_COMPILE_SHADER(PlatformType::OpenGL)
|
|
|
|
SPECIALIZE_COMPILE_SHADER(PlatformType::Vulkan)
|
|
|
|
#if _WIN32
|
|
|
|
SPECIALIZE_COMPILE_SHADER(PlatformType::D3D11)
|
|
|
|
#endif
|
|
|
|
#if __APPLE__
|
|
|
|
SPECIALIZE_COMPILE_SHADER(PlatformType::Metal)
|
|
|
|
#endif
|
|
|
|
#if HECL_NOUVEAU_NX
|
|
|
|
SPECIALIZE_COMPILE_SHADER(PlatformType::NX)
|
|
|
|
#endif
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
} // namespace hecl
|