1) Shader manager: Allow to override #defines programatically

when loading a shader file.
2) Conditional compilation of environmental mapping in gouraud.fs
   using 1)
This commit is contained in:
Vojtech Bubnik 2020-12-10 13:20:18 +01:00
parent b589b9ce9c
commit 8d644fbcb6
5 changed files with 47 additions and 17 deletions

View file

@ -16,8 +16,10 @@ struct SlopeDetection
uniform vec4 uniform_color;
uniform SlopeDetection slope;
uniform sampler2D environment_tex;
uniform bool use_environment_tex;
#if ENABLE_ENVIRONMENT_MAP
uniform sampler2D environment_tex;
uniform bool use_environment_tex;
#endif // ENABLE_ENVIRONMENT_MAP
varying vec3 clipping_planes_dots;
@ -42,8 +44,10 @@ void main()
vec3 color = slope.actived ? slope_color() : uniform_color.rgb;
// if the fragment is outside the print volume -> use darker color
color = (any(lessThan(delta_box_min, ZERO)) || any(greaterThan(delta_box_max, ZERO))) ? mix(color, ZERO, 0.3333) : color;
#if ENABLE_ENVIRONMENT_MAP
if (use_environment_tex)
gl_FragColor = vec4(0.45 * texture2D(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity.x, uniform_color.a);
else
#endif
gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, uniform_color.a);
}

View file

@ -611,7 +611,7 @@ struct _3DScene
static void point3_to_verts(const Vec3crd& point, double width, double height, GLVolume& volume);
};
static constexpr float BedEpsilon = EPSILON;
static constexpr float BedEpsilon = float(EPSILON);
}

View file

@ -18,9 +18,10 @@ GLShaderProgram::~GLShaderProgram()
glsafe(::glDeleteProgram(m_id));
}
bool GLShaderProgram::init_from_files(const std::string& name, const ShaderFilenames& filenames)
bool GLShaderProgram::init_from_files(const std::string& name, const ShaderFilenames& filenames, const std::initializer_list<std::string_view> &defines)
{
auto load_from_file = [](const std::string& filename) {
// Load a shader program from file, prepend defs block.
auto load_from_file = [](const std::string& filename, const std::string &defs) {
std::string path = resources_dir() + "/shaders/" + filename;
boost::nowide::ifstream s(path, boost::nowide::ifstream::binary);
if (!s.good()) {
@ -31,20 +32,39 @@ bool GLShaderProgram::init_from_files(const std::string& name, const ShaderFilen
s.seekg(0, s.end);
int file_length = static_cast<int>(s.tellg());
s.seekg(0, s.beg);
std::string source(file_length, '\0');
s.read(source.data(), file_length);
std::string source(defs.size() + file_length, '\0');
memcpy(source.data(), defs.c_str(), defs.size());
s.read(source.data() + defs.size(), file_length);
if (!s.good()) {
BOOST_LOG_TRIVIAL(error) << "Error while loading file: '" << path << "'";
return std::string();
}
s.close();
if (! defs.empty()) {
// Extract the version and flip the order of "defines" and version in the source block.
size_t idx = source.find("\n", defs.size());
if (idx != std::string::npos && strncmp(source.c_str() + defs.size(), "#version", 8) == 0) {
// Swap the version line with the defines.
size_t len = idx - defs.size() + 1;
memmove(source.data(), source.c_str() + defs.size(), len);
memcpy(source.data() + len, defs.c_str(), defs.size());
}
}
return source;
};
// Create a block of C "defines" from list of symbols.
std::string defines_program;
for (std::string_view def : defines)
// Our shaders are stored with "\r\n", thus replicate the same here for consistency. Likely "\n" would suffice,
// but we don't know all the OpenGL shader compilers around.
defines_program += format("#define %s 1\r\n", def);
ShaderSources sources = {};
for (size_t i = 0; i < static_cast<size_t>(EShaderType::Count); ++i) {
sources[i] = filenames[i].empty() ? std::string() : load_from_file(filenames[i]);
sources[i] = filenames[i].empty() ? std::string() : load_from_file(filenames[i], defines_program);
}
bool valid = !sources[static_cast<size_t>(EShaderType::Vertex)].empty() && !sources[static_cast<size_t>(EShaderType::Fragment)].empty() && sources[static_cast<size_t>(EShaderType::Compute)].empty();

View file

@ -3,6 +3,7 @@
#include <array>
#include <string>
#include <string_view>
#include "libslic3r/Point.hpp"
@ -32,7 +33,7 @@ private:
public:
~GLShaderProgram();
bool init_from_files(const std::string& name, const ShaderFilenames& filenames);
bool init_from_files(const std::string& name, const ShaderFilenames& filenames, const std::initializer_list<std::string_view> &defines = {});
bool init_from_texts(const std::string& name, const ShaderSources& sources);
const std::string& get_name() const { return m_name; }

View file

@ -5,6 +5,8 @@
#include <cassert>
#include <algorithm>
#include <string_view>
using namespace std::literals;
#include <GL/glew.h>
@ -14,9 +16,10 @@ std::pair<bool, std::string> GLShadersManager::init()
{
std::string error;
auto append_shader = [this, &error](const std::string& name, const GLShaderProgram::ShaderFilenames& filenames) {
auto append_shader = [this, &error](const std::string& name, const GLShaderProgram::ShaderFilenames& filenames,
const std::initializer_list<std::string_view> &defines = {}) {
m_shaders.push_back(std::make_unique<GLShaderProgram>());
if (!m_shaders.back()->init_from_files(name, filenames)) {
if (!m_shaders.back()->init_from_files(name, filenames, defines)) {
error += name + "\n";
// if any error happens while initializating the shader, we remove it from the list
m_shaders.pop_back();
@ -40,7 +43,11 @@ std::pair<bool, std::string> GLShadersManager::init()
// used to render extrusion and travel paths as lines in gcode preview
valid &= append_shader("toolpaths_lines", { "toolpaths_lines.vs", "toolpaths_lines.fs" });
// used to render objects in 3d editor
valid &= append_shader("gouraud", { "gouraud.vs", "gouraud.fs" });
valid &= append_shader("gouraud", { "gouraud.vs", "gouraud.fs" }
#if ENABLE_ENVIRONMENT_MAP
, { "ENABLE_ENVIRONMENT_MAP"sv }
#endif
);
// used to render variable layers heights in 3d editor
valid &= append_shader("variable_layer_height", { "variable_layer_height.vs", "variable_layer_height.fs" });
@ -49,14 +56,12 @@ std::pair<bool, std::string> GLShadersManager::init()
void GLShadersManager::shutdown()
{
for (std::unique_ptr<GLShaderProgram>& shader : m_shaders) {
shader.reset();
}
m_shaders.clear();
}
GLShaderProgram* GLShadersManager::get_shader(const std::string& shader_name)
{
auto it = std::find_if(m_shaders.begin(), m_shaders.end(), [shader_name](std::unique_ptr<GLShaderProgram>& p) { return p->get_name() == shader_name; });
auto it = std::find_if(m_shaders.begin(), m_shaders.end(), [&shader_name](std::unique_ptr<GLShaderProgram>& p) { return p->get_name() == shader_name; });
return (it != m_shaders.end()) ? it->get() : nullptr;
}