Commit 118706f3 authored by Johannes Braun's avatar Johannes Braun
Browse files

Moved all C-style code from core into mcpp_glsl.h

parent f61de7d5
......@@ -4,51 +4,12 @@
namespace glare::core
{
std::string GLSLParser::preprocess(const fs::path& shader, const std::vector<std::string>& include_paths)
std::string GLSLParser::preprocess(const fs::path& shader, const std::vector<fs::path>& include_paths)
{
mcpp::mcpp_use_mem_buffers(1);
std::vector<const char*> args;
args.push_back("mcpp");
auto path = shader.string();
args.push_back(path.c_str());
for (const auto& inc : include_paths)
{
args.push_back("-I");
args.push_back(inc.c_str());
}
mcpp::mcpp_lib_main(static_cast<int>(args.size()), const_cast<char**>(args.data()));
char* result = mcpp::mcpp_get_mem_buffer(mcpp::OUT);
char* version = "#version";
std::string line;
if (strncmp(result, version, strlen(version)) != 0)
{
// Now we just have to revert the order of #version and the first #line
bool ln_name = false;
while (*(++result) != '\n')
{
if (*result == '"')
ln_name = true;
if (ln_name)
line += *result;
}
while (*(result) != '#') ++result;
}
else
{
line = "\"" + path + "\"";
}
std::string src(result);
if (src[0] == '\n')
Log_Info << "di";
auto it = src.begin();
for (; *it != '\n'; ++it)
{
}
line = "\n#line 2 " + line;
src.insert(it, line.begin(), line.end());
std::vector<std::string> include_paths_strings;
for (const auto& path : include_paths)
include_paths_strings.push_back(path.string());
auto src = mcpp::preprocess(shader, include_paths_strings);
format(src);
return src;
}
......
......@@ -11,9 +11,7 @@ namespace glare::core
class GLSLParser
{
public:
static std::string preprocess(const fs::path& shader, const std::vector<std::string>& include_paths);
private:
static std::string preprocess(const fs::path& shader, const std::vector<fs::path>& include_paths);
static void format(std::string& src);
};
}
......
......@@ -14,7 +14,7 @@ namespace glare::core
{}
Shader::Shader(gl::ShaderType shader_type, const fs::path& path)
: Shader(shader_type, GLSLParser::preprocess(path, std::vector<std::string>{ files::asset("shaders").string() }), path.string())
: Shader(shader_type, GLSLParser::preprocess(path, { files::asset("shaders") }), path.string())
{}
Shader::Shader(gl::ShaderType shader_type, const std::string &source, const std::string& identifier)
......@@ -33,23 +33,12 @@ namespace glare::core
m_identifier = identifier;
std::string precompiled = source;
GLSLParser::format(precompiled);
const char *source_chars = precompiled.c_str();
gl::shaderSource(m_handle, 1, &source_chars, nullptr);
//const char* root[2]{ "/.", "/screenshader/gbuffer" };
/* auto root = files::asset("shaders").string();
std::replace(root.begin(), root.end(), '\\', '/');
root = "/" + root;
const char* ptr = root.c_str();
if (glfwExtensionSupported("GL_ARB_shading_language_include")) {
gl::compileShaderInclude(m_handle, 1, &ptr, nullptr);
}
else
{*/
gl::compileShader(m_handle);
// }
gl::compileShader(m_handle);
int success = 0;
gl::getShaderiv(m_handle, gl::GetShaderParameter::eCompileStatus, &success);
......
#ifndef INCLUDE_MCPP_GLSL_H
#define INCLUDE_MCPP_GLSL_H
#include <string>
#include <vector>
#include <filesystem>
// This is only a wrapping header to put it all into a separate namespace and have it as extern "C".
// For opengl interactions and system settings please have a look at noconfig.H
......@@ -10,6 +14,54 @@ namespace mcpp
{
#include "mcpp_lib.h"
}
namespace fs = std::experimental::filesystem;
inline std::string preprocess(const fs::path& shader, const std::vector<std::string>& include_paths)
{
mcpp::mcpp_use_mem_buffers(1);
std::vector<const char*> args;
args.push_back("mcpp");
auto path = shader.string();
args.push_back(path.c_str());
for (const auto& inc : include_paths)
{
args.push_back("-I");
args.push_back(inc.c_str());
}
mcpp::mcpp_lib_main(static_cast<int>(args.size()), const_cast<char**>(args.data()));
char* result = mcpp::mcpp_get_mem_buffer(mcpp::OUT);
char* version = "#version";
std::string line;
if (strncmp(result, version, strlen(version)) != 0)
{
// Now we just have to revert the order of #version and the first #line
bool ln_name = false;
while (*(++result) != '\n')
{
if (*result == '"')
ln_name = true;
if (ln_name)
line += *result;
}
while (*(result) != '#') ++result;
}
else
{
line = "\"" + path + "\"";
}
std::string src(result);
auto it = src.begin();
for (; *it != '\n'; ++it)
{
}
line = "\n#line 2 " + line;
src.insert(it, line.begin(), line.end());
return src;
}
}
#endif //INCLUDE_MCPP_GLSL_H;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment