diff --git a/resources/shaders/gouraud.fs b/resources/shaders/gouraud.fs index b1a8d6ac2..bfb5e1cac 100644 --- a/resources/shaders/gouraud.fs +++ b/resources/shaders/gouraud.fs @@ -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); } diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index ffca86077..eeaa87739 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -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); } diff --git a/src/slic3r/GUI/GLShader.cpp b/src/slic3r/GUI/GLShader.cpp index 3c2612b45..02c01d104 100644 --- a/src/slic3r/GUI/GLShader.cpp +++ b/src/slic3r/GUI/GLShader.cpp @@ -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 &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(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(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(EShaderType::Vertex)].empty() && !sources[static_cast(EShaderType::Fragment)].empty() && sources[static_cast(EShaderType::Compute)].empty(); diff --git a/src/slic3r/GUI/GLShader.hpp b/src/slic3r/GUI/GLShader.hpp index 84fdf5eba..c92ea274a 100644 --- a/src/slic3r/GUI/GLShader.hpp +++ b/src/slic3r/GUI/GLShader.hpp @@ -3,6 +3,7 @@ #include #include +#include #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 &defines = {}); bool init_from_texts(const std::string& name, const ShaderSources& sources); const std::string& get_name() const { return m_name; } diff --git a/src/slic3r/GUI/GLShadersManager.cpp b/src/slic3r/GUI/GLShadersManager.cpp index 1041faa3d..5ee14c526 100644 --- a/src/slic3r/GUI/GLShadersManager.cpp +++ b/src/slic3r/GUI/GLShadersManager.cpp @@ -5,6 +5,8 @@ #include #include +#include +using namespace std::literals; #include @@ -14,9 +16,10 @@ std::pair 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 &defines = {}) { m_shaders.push_back(std::make_unique()); - 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 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 GLShadersManager::init() void GLShadersManager::shutdown() { - for (std::unique_ptr& 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& p) { return p->get_name() == shader_name; }); + auto it = std::find_if(m_shaders.begin(), m_shaders.end(), [&shader_name](std::unique_ptr& p) { return p->get_name() == shader_name; }); return (it != m_shaders.end()) ? it->get() : nullptr; }