4 #include <core/Logger.h>
5 #include <resource/StringResource.h>
6 #include <utility/String.h>
11 CHIRA_CREATE_LOG(SHADER);
13 Shader::Shader(std::string identifier_)
14 :
Resource(std::move(identifier_)) {}
16 void Shader::compile(
const byte buffer[], std::size_t bufferLength) {
17 Serial::loadFromBuffer(
this, buffer, bufferLength);
19 const auto shaderModuleVertString = Resource::getUniqueUncachedResource<StringResource>(this->vertexPath);
20 const auto shaderModuleVertData = replaceMacros(shaderModuleVertString->getIdentifier().data(), shaderModuleVertString->getString());
21 const auto shaderModuleFragString = Resource::getUniqueUncachedResource<StringResource>(this->fragmentPath);
22 const auto shaderModuleFragData = replaceMacros(shaderModuleFragString->getIdentifier().data(), shaderModuleFragString->getString());
23 this->handle = Renderer::createShader(shaderModuleVertData, shaderModuleFragData);
26 PerspectiveViewUBO::get().bindToShader(this->handle);
29 LightsUBO::get().bindToShader(this->handle);
33 void Shader::use()
const {
34 Renderer::useShader(this->handle);
38 Renderer::destroyShader(this->handle);
41 void Shader::addPreprocessorSymbol(
const std::string& name,
const std::string& value) {
42 Shader::preprocessorSymbols[name] = value;
45 void Shader::setPreprocessorPrefix(
const std::string& prefix) {
46 Shader::preprocessorPrefix = prefix;
49 void Shader::setPreprocessorSuffix(
const std::string& suffix) {
50 Shader::preprocessorSuffix = suffix;
53 std::string Shader::replaceMacros(
const std::string& ignoredInclude,
const std::string& data) {
59 static const std::regex includes{Shader::preprocessorPrefix +
"(include[ \t]+([a-z:\\/.]+))" + Shader::preprocessorSuffix,
60 std::regex_constants::icase | std::regex_constants::optimize};
63 for (std::sregex_iterator it{data.begin(), data.end(), includes}; it != std::sregex_iterator{}; ++it) {
64 if (it->str(2) != ignoredInclude && !Shader::preprocessorSymbols.count(it->str(2))) {
65 auto contents = Resource::getUniqueUncachedResource<StringResource>(it->str(2));
66 Shader::addPreprocessorSymbol(it->str(1), replaceMacros(it->str(2), contents->getString()));
71 std::string out = data;
72 for (
const auto& [macro, contents] : Shader::preprocessorSymbols) {
73 std::string fullKey = Shader::preprocessorPrefix;
75 fullKey += Shader::preprocessorSuffix;
76 String::replace(out, fullKey, contents);
A chunk of data, usually a file. Is typically cached and shared.