Skip to content

Instantly share code, notes, and snippets.

@Journeyman1337
Last active April 1, 2021 02:59
Show Gist options
  • Save Journeyman1337/01df13c01272393a893d06d59b45bf80 to your computer and use it in GitHub Desktop.
Save Journeyman1337/01df13c01272393a893d06d59b45bf80 to your computer and use it in GitHub Desktop.
shader preprocessor wip
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec2 aTexCoord;
out vec2 texCoord;
uniform mat4 _MODEL;
uniform mat4 _VIEW;
uniform mat4 _PROJECTION;
vec4 MVP(vec4 position, mat4 model, mat4 view, mat4 projection)
{
return position * model * view * projection;
}
void main(void)
{
texCoord = aTexCoord;
gl_Position = MVP(vec4(aPosition, 1.0), _MODEL, _VIEW, _PROJECTION);
}
#export mvp_multiplication
vec4 MVP(vec4 position, mat4 model, mat4 view, mat4 projection)
{
return position * model * view * projection;
}
#program single texture
#vertex
#version 330 core
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec2 aTexCoord;
out vec2 texCoord;
uniform mat4 _MODEL;
uniform mat4 _VIEW;
uniform mat4 _PROJECTION;
#include mvp_multiplication
void main(void)
{
texCoord = aTexCoord;
gl_Position = MVP(vec4(aPosition, 1.0), _MODEL, _VIEW, _PROJECTION);
}
#fragment
#version 330
out vec4 outputColor;
in vec2 texCoord;
uniform sampler2D Texture0;
void main()
{
outputColor = texture(Texture0, texCoord);
}
#include <jmk/shader_source.h>
#include <iostream>
int main()
{
auto ssl = jmk::ShaderSourceLibrary::MakeUnique();
ssl->AddAllFromFolder("shaders");
ssl->PreprocessAll();
std::cout << ssl->GetSource("single texture").value()->GetVertexShaderSource() << '\n';
}
/*
The MIT License (MIT)
Copyright (c) 2020 Daniel Valcour
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <jmk/shader_source.h>
#include <filesystem>
#include <fstream>
#include <sstream>
void jmk::ShaderSourceLibrary::AddAllFromFolder(const std::string& directory_path)
{
if (std::filesystem::exists(directory_path) && std::filesystem::is_directory(directory_path))
{
for (auto& p : std::filesystem::recursive_directory_iterator(directory_path))
{
auto ext = p.path().extension();
auto this_path = p.path().u8string();
if (ext == ".glsl")
{
std::ifstream infile{ this_path };
std::string file_contents{ std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>() };
raw_shader_source.push_back(RawShaderFile(this_path, file_contents));
}
else if (ext == ".glinc")
{
std::ifstream infile{ this_path };
std::string file_contents{ std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>() };
raw_shader_include.push_back(RawShaderFile(this_path, file_contents));
}
}
}
}
void jmk::ShaderSourceLibrary::AddFromCode(const std::string& path, const std::string& raw_text)
{
auto p = std::filesystem::path(path);
auto ext = p.extension();
if (ext == ".glsl")
{
raw_shader_source.push_back(RawShaderFile(path, raw_text));
}
else if (ext == ".glinc")
{
raw_shader_include.push_back(RawShaderFile(path, raw_text));
}
}
void jmk::ShaderSourceLibrary::PreprocessAll()
{
for (size_t i = 0; i < this->raw_shader_include.size(); i++)
{
auto& this_include = this->raw_shader_include[i];
int identifier_macro_index = this_include.text.find("#export ");
int macro_end_index = this_include.text.find('\n', identifier_macro_index);
int identifier_size = macro_end_index - identifier_macro_index - 8;
if (identifier_macro_index == -1 || macro_end_index == -1 || identifier_size == 0)
{
//TODO log no identifier in glinc
continue;
}
auto identifier = this_include.text.substr(identifier_macro_index + 8, identifier_size);
if (this->include_map.count(identifier) > 0)
{
//TODO log multiple shader includes same identifier
}
include_map[identifier] = this_include.text.substr(0, static_cast<size_t>(identifier_macro_index)) + this_include.text.substr(static_cast<size_t>(macro_end_index) + 1) + "\n";
}
this->raw_shader_include.clear();
for (size_t s = 0; s < this->raw_shader_source.size(); s++)
{
auto& this_source = this->raw_shader_source[s];
int identifier_macro_index = this_source.text.find("#program ");
int macro_end_index = this_source.text.find('\n', identifier_macro_index);
int identifier_size = macro_end_index - identifier_macro_index - 9;
if (identifier_macro_index == -1 || macro_end_index == -1 || identifier_size == 0)
{
//TODO log no identifier in glsl
continue;
}
auto identifier = this_source.text.substr(identifier_macro_index + 9, identifier_size);
std::string vertex_source = "";
std::string fragment_source = "";
std::string* current_source = nullptr;
std::istringstream f(this_source.text);
std::string line;
while (std::getline(f, line)) {
if (line == "#vertex")
{
current_source = &vertex_source;
}
else if (line == "#fragment")
{
current_source = &fragment_source;
}
else if (line.find("#include ") == 0 && current_source)
{
current_source->append(this->include_map[line.substr(9)]);
}
else if (current_source)
{
line.append("\n");
current_source->append(line);
}
}
if (vertex_source.size() == 0 || fragment_source.size() == 0)
{
//TODO log missing shader
continue;
}
if (this->shader_map.count(identifier) > 0)
{
//TODO log multiple shaders same identifier
}
this->shader_map[identifier] = jmk::ShaderSource(fragment_source, vertex_source);
}
this->raw_shader_source.clear();
}
void jmk::ShaderSourceLibrary::CopyAll(const ShaderSourceLibrary& other)
{
raw_shader_source.reserve(other.raw_shader_source.size());
{
size_t offset = raw_shader_source.size();
for (size_t s = 0; s < other.raw_shader_source.size(); s++)
{
raw_shader_source[offset + s] = other.raw_shader_source[s];
}
}
raw_shader_include.reserve(other.raw_shader_include.size());
{
size_t offset = raw_shader_include.size();
for (size_t i = 0; i < other.raw_shader_include.size(); i++)
{
raw_shader_include[offset + i] = other.raw_shader_include[i];
}
}
for (const auto& [key, value] : other.shader_map) {
shader_map[key] = value;
}
for (const auto& [key, value] : other.include_map) {
include_map[key] = value;
}
}
std::optional<jmk::ShaderSource*> jmk::ShaderSourceLibrary::GetSource(const std::string& identifier)
{
return &shader_map[identifier];
}
jmk::shdsrclib_sptr_t jmk::ShaderSourceLibrary::MakeShared()
{
return std::make_shared<ShaderSourceLibrary>();
}
jmk::shdsrclib_uptr_t jmk::ShaderSourceLibrary::MakeUnique()
{
return std::make_unique<ShaderSourceLibrary>();
}
jmk::RawShaderFile::RawShaderFile(const std::string& path, const std::string& text) :
path(path),
text(text)
{
}
jmk::ShaderSource::ShaderSource(const std::string& fragment_source, const std::string& vertex_source) :
fragment_source(fragment_source),
vertex_source(vertex_source)
{
}
std::string& jmk::ShaderSource::GetFragmentShaderSource()
{
return fragment_source;
}
std::string& jmk::ShaderSource::GetVertexShaderSource()
{
return vertex_source;
}
/*
The MIT License (MIT)
Copyright (c) 2020 Daniel Valcour
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <jmk/types.h>
#include <string>
#include <map>
#include <vector>
#include <memory>
#include <optional>
namespace jmk
{
struct RawShaderFile
{
std::string path;
std::string text;
RawShaderFile(const std::string& path, const std::string& text);
};
class ShaderSource
{
private:
std::string fragment_source;
std::string vertex_source;
public:
ShaderSource() = default;
ShaderSource(const std::string& fragment_source, const std::string& vertex_source);
std::string& GetFragmentShaderSource();
std::string& GetVertexShaderSource();
};
class ShaderSourceLibrary
{
private:
std::vector<RawShaderFile> raw_shader_source;
std::vector<RawShaderFile> raw_shader_include;
std::map<std::string, std::string> include_map;
std::map<std::string, ShaderSource> shader_map;
public:
ShaderSourceLibrary() = default;
~ShaderSourceLibrary() = default;
void AddAllFromFolder(const std::string& directory_path);
void AddFromCode(const std::string& path, const std::string& raw_text);
//void AddAllEmbeded(); //TODO shaders embeded in executable
void PreprocessAll();
void CopyAll(const ShaderSourceLibrary& other);
std::optional<ShaderSource*> GetSource(const std::string& identifier);
static jmk::shdsrclib_sptr_t MakeShared();
static jmk::shdsrclib_uptr_t MakeUnique();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment