Skip to content

Commit

Permalink
Start improved compiler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LDAP committed Nov 5, 2024
1 parent 174f76c commit 9536f4d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 15 deletions.
11 changes: 10 additions & 1 deletion include/merian/vk/extension/extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Extension {
friend Context;

public:
Extension(std::string name) : name(name) {}
Extension(const std::string& name) : name(name) {}

virtual ~Extension() = 0;

Expand Down Expand Up @@ -109,6 +109,15 @@ class Extension {
spdlog::warn("extension {} not supported ({})", name, reason);
}

// OTHER

// return strings that should be defined when compiling shaders with Merians shader compiler.
// Note that device and instance extensions are automatically defined as
// MERIAN_DEVICE_EXT_ENABLE_<NAME> and MERIAN_INSTANCE_EXT_ENABLE_<NAME>
virtual std::vector<std::string> shader_defines() {
return {};
}

public:
const std::string name;
};
Expand Down
46 changes: 36 additions & 10 deletions include/merian/vk/shader/shader_compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace merian {

// A compiler for shaders.
//
// Include paths for the merian-nodes library and context extensions must be automatically added.
class ShaderCompiler {
public:
class compilation_failed : public std::runtime_error {
Expand All @@ -19,7 +22,13 @@ class ShaderCompiler {
};

public:
ShaderCompiler() {}
ShaderCompiler(const std::vector<std::string>& user_include_paths = {},
const std::map<std::string, std::string>& user_macro_definitions = {})
: include_paths(user_include_paths), macro_definitions(user_macro_definitions) {
// search merian-shaders

// add macro definitions from context extensions and enabled instance and device extensions.
}

virtual ~ShaderCompiler() = 0;

Expand Down Expand Up @@ -55,8 +64,16 @@ class ShaderCompiler {
const std::string& source_name,
const vk::ShaderStageFlagBits shader_kind) = 0;

const std::vector<std::string>& get_include_paths() const {
return include_paths;
}

const std::map<std::string, std::string>& get_macro_definitions() const {
return macro_definitions;
}

private:
vk::ShaderStageFlagBits guess_kind(const std::filesystem::path& path) {
static vk::ShaderStageFlagBits guess_kind(const std::filesystem::path& path) {
std::string extension;
if (path.extension().string() == ".glsl") {
extension = std::filesystem::path(path.string().substr(0, path.string().size() - 5))
Expand All @@ -65,23 +82,32 @@ class ShaderCompiler {
} else {
extension = path.extension().string();
}

if (extension == ".vert") {
return vk::ShaderStageFlagBits::eVertex;
} else if (extension == ".tesc") {
}
if (extension == ".tesc") {
return vk::ShaderStageFlagBits::eTessellationControl;
} else if (extension == ".tese") {
}
if (extension == ".tese") {
return vk::ShaderStageFlagBits::eTessellationEvaluation;
} else if (extension == ".geom") {
}
if (extension == ".geom") {
return vk::ShaderStageFlagBits::eGeometry;
} else if (extension == ".frag") {
}
if (extension == ".frag") {
return vk::ShaderStageFlagBits::eFragment;
} else if (extension == ".comp") {
}
if (extension == ".comp") {
return vk::ShaderStageFlagBits::eCompute;
} else {
throw compilation_failed{
fmt::format("Shader kind could not be determined for path {}", path.string())};
}

throw compilation_failed{
fmt::format("Shader kind could not be determined for path {}", path.string())};
}

std::vector<std::string> include_paths;
std::map<std::string, std::string> macro_definitions;
};
using ShaderCompilerHandle = std::shared_ptr<ShaderCompiler>;

Expand Down
22 changes: 22 additions & 0 deletions include/merian/vk/shader/shader_compiler_glsllangValidator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "merian/vk/shader/shader_compiler.hpp"
#include <map>

namespace merian {

// Uses glslangValidator executable to compile shaders.
class GLSLLangValidatorCompiler : public ShaderCompiler {
public:
// Include paths for the merian-nodes library are automatically added
GLSLLangValidatorCompiler(const std::vector<std::string>& include_paths = {},
const std::map<std::string, std::string>& macro_definitions = {});

~GLSLLangValidatorCompiler();

std::vector<uint32_t> compile_glsl(const std::string& source,
const std::string& source_name,
const vk::ShaderStageFlagBits shader_kind) override;
};

} // namespace merian
1 change: 0 additions & 1 deletion include/merian/vk/shader/shader_compiler_shaderc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace merian {

class ShadercCompiler : public ShaderCompiler {
public:
// Include paths for the merian-nodes library are automatically added
ShadercCompiler(const std::vector<std::string>& include_paths = {},
const std::map<std::string, std::string>& macro_definitions = {});

Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,5 @@ merian_dep = declare_dependency(
vulkan,
]
)

install_subdir('include', install_dir: get_option('includedir'), strip_directory: true)
7 changes: 4 additions & 3 deletions src/merian/vk/shader/shader_compiler_shaderc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ shaderc_shader_kind_for_stage_flag_bit(const vk::ShaderStageFlagBits shader_kind
}

ShadercCompiler::ShadercCompiler(const std::vector<std::string>& include_paths,
const std::map<std::string, std::string>& macro_definitions) {
const std::map<std::string, std::string>& macro_definitions)
: ShaderCompiler(include_paths, macro_definitions) {

for (const auto& [key, value] : macro_definitions) {
for (const auto& [key, value] : get_macro_definitions()) {
compile_options.AddMacroDefinition(key, value);
}

auto includer = std::make_unique<FileIncluder>();
for (const auto& include_path : include_paths) {
for (const auto& include_path : get_include_paths()) {
includer->get_file_loader().add_search_path(include_path);
}
compile_options.SetIncluder(std::move(includer));
Expand Down

0 comments on commit 9536f4d

Please sign in to comment.