ENABLE_SHADERS_MANAGER -> Unified client code of new GLShadersManager and GLShaderProgram classes

This commit is contained in:
enricoturri1966 2020-05-21 10:15:00 +02:00
parent cbfb09a241
commit 5aa8cc5779
10 changed files with 123 additions and 37 deletions

View File

@ -196,15 +196,18 @@ void Bed3D::Axes::render() const
shader->start_using();
// x axis
shader->set_uniform("uniform_color", { 0.75f, 0.0f, 0.0f, 1.0f });
std::array<float, 4> color = { 0.75f, 0.0f, 0.0f, 1.0f };
shader->set_uniform("uniform_color", color);
render_axis(Geometry::assemble_transform(m_origin, { 0.0, 0.5 * M_PI, 0.0f }).cast<float>());
// y axis
shader->set_uniform("uniform_color", { 0.0f, 0.75f, 0.0f, 1.0f });
color = { 0.0f, 0.75f, 0.0f, 1.0f };
shader->set_uniform("uniform_color", color);
render_axis(Geometry::assemble_transform(m_origin, { -0.5 * M_PI, 0.0, 0.0f }).cast<float>());
// z axis
shader->set_uniform("uniform_color", { 0.0f, 0.0f, 0.75f, 1.0f });
color = { 0.0f, 0.0f, 0.75f, 1.0f };
shader->set_uniform("uniform_color", color);
render_axis(Geometry::assemble_transform(m_origin).cast<float>());
shader->stop_using();
@ -676,9 +679,19 @@ void Bed3D::render_model() const
if (!m_model.get_filename().empty())
{
#if ENABLE_SHADERS_MANAGER
GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light");
if (shader != nullptr)
{
shader->start_using();
m_model.render();
shader->stop_using();
}
#else
glsafe(::glEnable(GL_LIGHTING));
m_model.render();
glsafe(::glDisable(GL_LIGHTING));
#endif // ENABLE_SHADERS_MANAGER
}
}

View File

@ -1,6 +1,10 @@
#include <GL/glew.h>
#include "3DScene.hpp"
#if ENABLE_SHADERS_MANAGER
#include "GLShader.hpp"
#include "GUI_App.hpp"
#endif // ENABLE_SHADERS_MANAGER
#include "libslic3r/ExtrusionEntity.hpp"
#include "libslic3r/ExtrusionEntityCollection.hpp"
@ -640,6 +644,12 @@ GLVolumeWithIdAndZList volumes_to_render(const GLVolumePtrs& volumes, GLVolumeCo
void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disable_cullface, const Transform3d& view_matrix, std::function<bool(const GLVolume&)> filter_func) const
{
#if ENABLE_SHADERS_MANAGER
GLShaderProgram* shader = GUI::wxGetApp().get_current_shader();
if (shader == nullptr)
return;
#endif // ENABLE_SHADERS_MANAGER
glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
@ -650,6 +660,15 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
glsafe(::glEnableClientState(GL_NORMAL_ARRAY));
#if ENABLE_SHADERS_MANAGER
shader->set_uniform("print_box.min", m_print_box_min, 3);
shader->set_uniform("print_box.max", m_print_box_max, 3);
shader->set_uniform("z_range", m_z_range, 2);
shader->set_uniform("clipping_plane", m_clipping_plane, 4);
#if ENABLE_SLOPE_RENDERING
shader->set_uniform("slope.z_range", m_slope.z_range);
#endif // ENABLE_SLOPE_RENDERING
#else
GLint current_program_id;
glsafe(::glGetIntegerv(GL_CURRENT_PROGRAM, &current_program_id));
GLint color_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "uniform_color") : -1;
@ -684,11 +703,19 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
if (slope_z_range_id != -1)
glsafe(::glUniform2fv(slope_z_range_id, 1, (const GLfloat*)m_slope.z_range.data()));
#endif // ENABLE_SLOPE_RENDERING
#endif // ENABLE_SHADERS_MANAGER
GLVolumeWithIdAndZList to_render = volumes_to_render(this->volumes, type, view_matrix, filter_func);
for (GLVolumeWithIdAndZ& volume : to_render) {
volume.first->set_render_color();
#if ENABLE_SLOPE_RENDERING
#if ENABLE_SHADERS_MANAGER
shader->set_uniform("uniform_color", volume.first->render_color, 4);
shader->set_uniform("print_box.actived", volume.first->shader_outside_printer_detection_enabled);
shader->set_uniform("print_box.volume_world_matrix", volume.first->world_matrix());
shader->set_uniform("slope.actived", m_slope.active && !volume.first->is_modifier && !volume.first->is_wipe_tower);
shader->set_uniform("slope.volume_world_normal_matrix", volume.first->world_matrix().matrix().block(0, 0, 3, 3).inverse().transpose().cast<float>());
#else
if (color_id >= 0)
glsafe(::glUniform4fv(color_id, 1, (const GLfloat*)volume.first->render_color));
else
@ -708,6 +735,7 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
Matrix3f normal_matrix = volume.first->world_matrix().matrix().block(0, 0, 3, 3).inverse().transpose().cast<float>();
glsafe(::glUniformMatrix3fv(slope_normal_matrix_id, 1, GL_FALSE, (const GLfloat*)normal_matrix.data()));
}
#endif // ENABLE_SHADERS_MANAGER
volume.first->render();
#else
@ -1912,6 +1940,12 @@ void GLModel::reset()
void GLModel::render() const
{
#if ENABLE_SHADERS_MANAGER
GLShaderProgram* shader = GUI::wxGetApp().get_current_shader();
if (shader == nullptr)
return;
#endif // ENABLE_SHADERS_MANAGER
glsafe(::glEnable(GL_BLEND));
glsafe(::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
@ -1919,16 +1953,22 @@ void GLModel::render() const
glsafe(::glEnableClientState(GL_VERTEX_ARRAY));
glsafe(::glEnableClientState(GL_NORMAL_ARRAY));
#if !ENABLE_SHADERS_MANAGER
GLint current_program_id;
glsafe(::glGetIntegerv(GL_CURRENT_PROGRAM, &current_program_id));
GLint color_id = (current_program_id > 0) ? ::glGetUniformLocation(current_program_id, "uniform_color") : -1;
glcheck();
#endif // !ENABLE_SHADERS_MANAGER
#if ENABLE_SLOPE_RENDERING
#if ENABLE_SHADERS_MANAGER
shader->set_uniform("uniform_color", m_volume.render_color, 4);
#else
if (color_id >= 0)
glsafe(::glUniform4fv(color_id, 1, (const GLfloat*)m_volume.render_color));
else
glsafe(::glColor4fv(m_volume.render_color));
#endif // ENABLE_SHADERS_MANAGER
m_volume.render();
#else

View File

@ -26,12 +26,6 @@ inline void glAssertRecentCall() { }
#endif
namespace Slic3r {
namespace GUI {
class Bed3D;
struct Camera;
class GLToolbar;
} // namespace GUI
class SLAPrintObject;
enum SLAPrintObjectStep : unsigned int;
class DynamicPrintConfig;

View File

@ -490,16 +490,16 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G
assert(this->last_object_id != -1);
#if ENABLE_SHADERS_MANAGER
GLShaderProgram* shader = wxGetApp().get_shader("variable_layer_height");
assert(shader != nullptr);
if (shader == nullptr)
return;
GLint current_program_id = 0;
glsafe(::glGetIntegerv(GL_CURRENT_PROGRAM, &current_program_id));
if (shader->get_id() != static_cast<GLuint>(current_program_id))
GLShaderProgram* current_shader = wxGetApp().get_current_shader();
if (shader->get_id() != current_shader->get_id())
// The layer editing shader is not yet active. Activate it.
shader->start_using();
else
// The layer editing shader was already active.
current_program_id = 0;
current_shader = nullptr;
const_cast<LayersEditing*>(this)->generate_layer_height_texture();
@ -529,7 +529,6 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G
GLint object_max_z_id = ::glGetUniformLocation(shader_id, "object_max_z");
glcheck();
if (z_to_texture_row_id != -1 && z_texture_row_to_normalized_id != -1 && z_cursor_id != -1 && z_cursor_band_width_id != -1 && world_matrix_id != -1)
{
const_cast<LayersEditing*>(this)->generate_layer_height_texture();
@ -568,9 +567,12 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G
}
// Revert back to the previous shader.
glBindTexture(GL_TEXTURE_2D, 0);
#if ENABLE_SHADERS_MANAGER
if (current_shader != nullptr)
current_shader->start_using();
#else
if (current_program_id > 0)
glsafe(::glUseProgram(current_program_id));
#if !ENABLE_SHADERS_MANAGER
}
else
{
@ -584,7 +586,7 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G
glvolume->render();
}
}
#endif // !ENABLE_SHADERS_MANAGER
#endif // ENABLE_SHADERS_MANAGER
}
void GLCanvas3D::LayersEditing::adjust_layer_height_profile()

View File

@ -6,6 +6,7 @@
#include <boost/nowide/fstream.hpp>
#include <GL/glew.h>
#include <cassert>
#if ENABLE_SHADERS_MANAGER
#include <boost/log/trivial.hpp>
@ -23,8 +24,7 @@ bool GLShaderProgram::init_from_files(const std::string& name, const ShaderFilen
auto load_from_file = [](const std::string& filename) {
std::string path = resources_dir() + "/shaders/" + filename;
boost::nowide::ifstream s(path, boost::nowide::ifstream::binary);
if (!s.good())
{
if (!s.good()) {
BOOST_LOG_TRIVIAL(error) << "Couldn't open file: '" << path << "'";
return std::string();
}
@ -34,8 +34,7 @@ bool GLShaderProgram::init_from_files(const std::string& name, const ShaderFilen
s.seekg(0, s.beg);
std::string source(file_length, '\0');
s.read(source.data(), file_length);
if (!s.good())
{
if (!s.good()) {
BOOST_LOG_TRIVIAL(error) << "Error while loading file: '" << path << "'";
return std::string();
}
@ -49,8 +48,9 @@ bool GLShaderProgram::init_from_files(const std::string& name, const ShaderFilen
sources[i] = filenames[i].empty() ? std::string() : load_from_file(filenames[i]);
}
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();
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();
valid |= !sources[static_cast<size_t>(EShaderType::Compute)].empty() && sources[static_cast<size_t>(EShaderType::Vertex)].empty() && sources[static_cast<size_t>(EShaderType::Fragment)].empty() &&
sources[static_cast<size_t>(EShaderType::Geometry)].empty() && sources[static_cast<size_t>(EShaderType::TessEvaluation)].empty() && sources[static_cast<size_t>(EShaderType::TessControl)].empty();
return valid ? init_from_texts(name, sources) : false;
}
@ -107,9 +107,11 @@ bool GLShaderProgram::init_from_texts(const std::string& name, const ShaderSourc
auto [result, id] = create_shader(type);
if (result)
shader_ids[i] = id;
else
{
else {
BOOST_LOG_TRIVIAL(error) << "glCreateShader() failed for " << shader_type_as_string(type) << " shader of shader program '" << name << "'";
// release shaders
release_shaders(shader_ids);
return false;
}
@ -125,7 +127,7 @@ bool GLShaderProgram::init_from_texts(const std::string& name, const ShaderSourc
glsafe(::glGetShaderInfoLog(id, params, &params, msg.data()));
BOOST_LOG_TRIVIAL(error) << "Unable to compile " << shader_type_as_string(type) << " shader of shader program '" << name << "':\n" << msg.data();
// release shader
// release shaders
release_shaders(shader_ids);
return false;
}
@ -173,13 +175,10 @@ bool GLShaderProgram::init_from_texts(const std::string& name, const ShaderSourc
return true;
}
bool GLShaderProgram::start_using() const
void GLShaderProgram::start_using() const
{
if (m_id == 0)
return false;
assert(m_id > 0);
glsafe(::glUseProgram(m_id));
return true;
}
void GLShaderProgram::stop_using() const
@ -212,6 +211,16 @@ bool GLShaderProgram::set_uniform(const char* name, float value) const
return false;
}
bool GLShaderProgram::set_uniform(const char* name, const std::array<float, 2>& value) const
{
int id = get_uniform_location(name);
if (id >= 0) {
glsafe(::glUniform2fv(id, 1, static_cast<const GLfloat*>(value.data())));
return true;
}
return false;
}
bool GLShaderProgram::set_uniform(const char* name, const std::array<float, 3>& value) const
{
int id = get_uniform_location(name);
@ -236,8 +245,7 @@ bool GLShaderProgram::set_uniform(const char* name, const float* value, size_t s
{
if (size == 1)
return set_uniform(name, value[0]);
else if (size < 5)
{
else if (size < 5) {
int id = get_uniform_location(name);
if (id >= 0) {
if (size == 2)
@ -268,6 +276,16 @@ bool GLShaderProgram::set_uniform(const char* name, const Transform3d& value) co
return set_uniform(name, value.cast<float>());
}
bool GLShaderProgram::set_uniform(const char* name, const Matrix3f& value) const
{
int id = get_uniform_location(name);
if (id >= 0) {
glsafe(::glUniformMatrix3fv(id, 1, GL_FALSE, static_cast<const GLfloat*>(value.data())));
return true;
}
return false;
}
int GLShaderProgram::get_attrib_location(const char* name) const
{
return (m_id > 0) ? ::glGetAttribLocation(m_id, name) : -1;

View File

@ -37,17 +37,19 @@ public:
const std::string& get_name() const { return m_name; }
unsigned int get_id() const { return m_id; }
bool start_using() const;
void start_using() const;
void stop_using() const;
bool set_uniform(const char* name, int value) const;
bool set_uniform(const char* name, bool value) const;
bool set_uniform(const char* name, float value) const;
bool set_uniform(const char* name, const std::array<float, 2>& value) const;
bool set_uniform(const char* name, const std::array<float, 3>& value) const;
bool set_uniform(const char* name, const std::array<float, 4>& value) const;
bool set_uniform(const char* name, const float* value, size_t size) const;
bool set_uniform(const char* name, const Transform3f& value) const;
bool set_uniform(const char* name, const Transform3d& value) const;
bool set_uniform(const char* name, const Matrix3f& value) const;
// returns -1 if not found
int get_attrib_location(const char* name) const;

View File

@ -1,9 +1,12 @@
#include "libslic3r/libslic3r.h"
#include "GLShadersManager.hpp"
#include "3DScene.hpp"
#include <cassert>
#include <algorithm>
#include <GL/glew.h>
#if ENABLE_SHADERS_MANAGER
namespace Slic3r {
@ -71,6 +74,17 @@ GLShaderProgram* GLShadersManager::get_shader(const std::string& shader_name)
return (it != m_shaders.end()) ? it->get() : nullptr;
}
GLShaderProgram* GLShadersManager::get_current_shader()
{
GLint id = 0;
glsafe(::glGetIntegerv(GL_CURRENT_PROGRAM, &id));
if (id == 0)
return false;
auto it = std::find_if(m_shaders.begin(), m_shaders.end(), [id](std::unique_ptr<GLShaderProgram>& p) { return p->get_id() == id; });
return (it != m_shaders.end()) ? it->get() : nullptr;
}
} // namespace Slic3r
#endif // ENABLE_SHADERS_MANAGER

View File

@ -22,6 +22,9 @@ public:
// returns nullptr if not found
GLShaderProgram* get_shader(const std::string& shader_name);
// returns currently active shader, nullptr if none
GLShaderProgram* get_current_shader();
};
} // namespace Slic3r

View File

@ -218,8 +218,8 @@ public:
#endif // ENABLE_THUMBNAIL_GENERATOR_DEBUG
#if ENABLE_SHADERS_MANAGER
// returns nullptr if not found
GLShaderProgram* get_shader(const std::string& shader_name) { return m_opengl_mgr.get_shader(shader_name); }
GLShaderProgram* get_current_shader() { return m_opengl_mgr.get_current_shader(); }
#endif // ENABLE_SHADERS_MANAGER
private:

View File

@ -96,8 +96,8 @@ public:
wxGLContext* init_glcontext(wxGLCanvas& canvas);
#if ENABLE_SHADERS_MANAGER
// returns nullptr if not found
GLShaderProgram* get_shader(const std::string& shader_name) { return m_shaders_manager.get_shader(shader_name); }
GLShaderProgram* get_current_shader() { return m_shaders_manager.get_current_shader(); }
#endif // ENABLE_SHADERS_MANAGER
static bool are_compressed_textures_supported() { return s_compressed_textures_supported; }