Fix opengl core using -- remove GL_ALPHA to use texture with GL_RGBA

-- take more memory from GPU but works universaly(independent on opengl version)
This commit is contained in:
Filip Sykala - NTB T15p 2022-08-31 15:28:23 +02:00
parent c549c6afbe
commit ac6a91debe
5 changed files with 45 additions and 37 deletions

View File

@ -1240,32 +1240,35 @@ void GLGizmoEmboss::init_face_names() {
std::sort(m_face_names.names.begin(), m_face_names.names.end()); std::sort(m_face_names.names.begin(), m_face_names.names.end());
} }
#include "slic3r/GUI/Jobs/CreateFontNameImageJob.hpp" // create texture for visualization font face
void GLGizmoEmboss::draw_font_list() void GLGizmoEmboss::init_font_name_texture() {
{
// Create of texture for font name
auto init_texture = [&face_names = m_face_names, size = m_gui_cfg->face_name_size]() {
// check if already exists // check if already exists
GLuint &id = face_names.texture_id; GLuint &id = m_face_names.texture_id;
if (id != 0) return; if (id != 0) return;
// create texture for font // create texture for font
GLenum target = GL_TEXTURE_2D, format = GL_ALPHA, GLenum target = GL_TEXTURE_2D;
type = GL_UNSIGNED_BYTE;
GLint level = 0, border = 0;
glsafe(::glGenTextures(1, &id)); glsafe(::glGenTextures(1, &id));
glsafe(::glBindTexture(target, id)); glsafe(::glBindTexture(target, id));
glsafe(::glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); glsafe(::glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
glsafe(::glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); glsafe(::glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GLint w = size.x(), h = face_names.names.size() * size.y(); const Vec2i &size = m_gui_cfg->face_name_size;
std::vector<unsigned char> data(w * h, {0}); GLint w = size.x(), h = m_face_names.names.size() * size.y();
glsafe(::glTexImage2D(target, level, GL_ALPHA, w, h, border, format, type, data.data())); std::vector<unsigned char> data(4*w * h, {0});
const GLenum format = GL_RGBA, type = GL_UNSIGNED_BYTE;
const GLint level = 0, internal_format = GL_RGBA, border = 0;
glsafe(::glTexImage2D(target, level, internal_format, w, h, border, format, type, data.data()));
// bind default texture // bind default texture
GLuint no_texture_id = 0; GLuint no_texture_id = 0;
glsafe(::glBindTexture(target, no_texture_id)); glsafe(::glBindTexture(target, no_texture_id));
// no one is initialized yet // no one is initialized yet
face_names.exist_textures = std::vector<bool>(face_names.names.size(), {false}); m_face_names.exist_textures = std::vector<bool>(m_face_names.names.size(), {false});
}; }
#include "slic3r/GUI/Jobs/CreateFontNameImageJob.hpp"
void GLGizmoEmboss::draw_font_list()
{
auto init_trancated_names = [&face_names = m_face_names, auto init_trancated_names = [&face_names = m_face_names,
width = m_gui_cfg->face_name_max_width]() { width = m_gui_cfg->face_name_max_width]() {
if (!face_names.names_truncated.empty()) return; if (!face_names.names_truncated.empty()) return;
@ -1288,7 +1291,7 @@ void GLGizmoEmboss::draw_font_list()
wxString del_facename; wxString del_facename;
if (ImGui::BeginCombo("##font_selector", selected)) { if (ImGui::BeginCombo("##font_selector", selected)) {
if (!m_face_names.is_init) init_face_names(); if (!m_face_names.is_init) init_face_names();
init_texture(); init_font_name_texture();
if (m_face_names.names_truncated.empty()) init_trancated_names(); if (m_face_names.names_truncated.empty()) init_trancated_names();
ImTextureID tex_id = (void *) (intptr_t) m_face_names.texture_id; ImTextureID tex_id = (void *) (intptr_t) m_face_names.texture_id;
for (const wxString &face_name : m_face_names.names) { for (const wxString &face_name : m_face_names.names) {
@ -1309,6 +1312,11 @@ void GLGizmoEmboss::draw_font_list()
ImGui::IsItemVisible()) { ImGui::IsItemVisible()) {
m_face_names.exist_textures[index] = true; m_face_names.exist_textures[index] = true;
std::string text = m_text.empty() ? "AaBbCc" : m_text; std::string text = m_text.empty() ? "AaBbCc" : m_text;
const unsigned char gray_level = 5;
// format type and level must match to texture data
const GLenum format = GL_RGBA, type = GL_UNSIGNED_BYTE;
const GLint level = 0;
// render text to texture // render text to texture
FontImageData data{text, FontImageData data{text,
face_name, face_name,
@ -1316,8 +1324,11 @@ void GLGizmoEmboss::draw_font_list()
m_face_names.texture_id, m_face_names.texture_id,
index, index,
m_gui_cfg->face_name_size, m_gui_cfg->face_name_size,
&m_allow_update_rendered_font &m_allow_update_rendered_font,
}; gray_level,
format,
type,
level};
auto job = std::make_unique<CreateFontImageJob>(std::move(data)); auto job = std::make_unique<CreateFontImageJob>(std::move(data));
auto& worker = wxGetApp().plater()->get_ui_job_worker(); auto& worker = wxGetApp().plater()->get_ui_job_worker();
queue_job(worker, std::move(job)); queue_job(worker, std::move(job));
@ -1767,7 +1778,7 @@ void GLGizmoEmboss::draw_style_list() {
bool is_selected = (index == m_style_manager.get_style_index()); bool is_selected = (index == m_style_manager.get_style_index());
ImVec2 select_size(0,m_gui_cfg->max_style_image_size.y()); // 0,0 --> calculate in draw ImVec2 select_size(0,m_gui_cfg->max_style_image_size.y()); // 0,0 --> calculate in draw
const std::optional<EmbossStyleManager::StyleImage> img = item.image; const std::optional<EmbossStyleManager::StyleImage> &img = item.image;
// allow click delete button // allow click delete button
ImGuiSelectableFlags_ flags = ImGuiSelectableFlags_AllowItemOverlap; ImGuiSelectableFlags_ flags = ImGuiSelectableFlags_AllowItemOverlap;
if (ImGui::Selectable(item.truncated_name.c_str(), is_selected, flags, select_size)) { if (ImGui::Selectable(item.truncated_name.c_str(), is_selected, flags, select_size)) {

View File

@ -108,6 +108,7 @@ private:
void draw_style_save_button(bool is_modified); void draw_style_save_button(bool is_modified);
void draw_style_save_as_popup(); void draw_style_save_as_popup();
void draw_style_add_button(); void draw_style_add_button();
void init_font_name_texture();
void draw_font_list(); void draw_font_list();
void draw_style_edit(); void draw_style_edit();
bool draw_italic_button(); bool draw_italic_button();

View File

@ -62,7 +62,7 @@ void CreateFontImageJob::process(Ctl &ctl)
if (m_tex_size.x() > m_input.size.x()) m_tex_size.x() = m_input.size.x(); if (m_tex_size.x() > m_input.size.x()) m_tex_size.x() = m_input.size.x();
// Set up result // Set up result
m_result = std::vector<unsigned char>(m_tex_size.x() * m_tex_size.y(), {0}); m_result = std::vector<unsigned char>(m_tex_size.x() * m_tex_size.y() * 4, {255});
sla::Resolution resolution(m_tex_size.x(), m_tex_size.y()); sla::Resolution resolution(m_tex_size.x(), m_tex_size.y());
double pixel_dim = SCALING_FACTOR / scale; double pixel_dim = SCALING_FACTOR / scale;
@ -85,7 +85,7 @@ void CreateFontImageJob::process(Ctl &ctl)
size_t index = y*w + x; size_t index = y*w + x;
assert(index < size); assert(index < size);
if (index >= size) continue; if (index >= size) continue;
pix[index] = ptr2[y * width + x] / gray_level; pix[3+4*index] = ptr2[y * width + x] / gray_level;
} }
return sla::EncodedRaster(); return sla::EncodedRaster();
}; };

View File

@ -89,7 +89,7 @@ void CreateFontStyleImagesJob::process(Ctl &ctl)
} }
// Set up result // Set up result
pixels = std::vector<unsigned char>(width * height, {0}); pixels = std::vector<unsigned char>(4*width * height, {255});
// upload sub textures // upload sub textures
for (EmbossStyleManager::StyleImage &image : images) { for (EmbossStyleManager::StyleImage &image : images) {
@ -116,7 +116,7 @@ void CreateFontStyleImagesJob::process(Ctl &ctl)
size_t index = (offset.y() + y)*w + offset.x() + x; size_t index = (offset.y() + y)*w + offset.x() + x;
assert(index < size); assert(index < size);
if (index >= size) continue; if (index >= size) continue;
pix[index] = ptr2[y * width + x] / gray_level; pix[4*index+3] = ptr2[y * width + x] / gray_level;
} }
return sla::EncodedRaster(); return sla::EncodedRaster();
}; };
@ -128,14 +128,14 @@ void CreateFontStyleImagesJob::finalize(bool canceled, std::exception_ptr &)
{ {
// upload texture on GPU // upload texture on GPU
GLuint tex_id; GLuint tex_id;
GLenum target = GL_TEXTURE_2D, format = GL_ALPHA, type = GL_UNSIGNED_BYTE; GLenum target = GL_TEXTURE_2D, format = GL_RGBA, type = GL_UNSIGNED_BYTE;
GLint level = 0, border = 0; GLint level = 0, border = 0;
glsafe(::glGenTextures(1, &tex_id)); glsafe(::glGenTextures(1, &tex_id));
glsafe(::glBindTexture(target, tex_id)); glsafe(::glBindTexture(target, tex_id));
glsafe(::glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); glsafe(::glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
glsafe(::glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); glsafe(::glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GLint w = width, h=height; GLint w = width, h=height;
glsafe(::glTexImage2D(target, level, GL_ALPHA, w, h, border, format, type, glsafe(::glTexImage2D(target, level, GL_RGBA, w, h, border, format, type,
(const void *) pixels.data())); (const void *) pixels.data()));
// set up texture id // set up texture id

View File

@ -346,10 +346,7 @@ void EmbossStyleManager::init_style_images(const Vec2i &max_size,
void EmbossStyleManager::free_style_images() { void EmbossStyleManager::free_style_images() {
if (!m_exist_style_images) return; if (!m_exist_style_images) return;
if (!is_activ_font()) return;
GLuint tex_id = 0; GLuint tex_id = 0;
for (Item &it : m_style_items) { for (Item &it : m_style_items) {
if (tex_id == 0 && it.image.has_value()) if (tex_id == 0 && it.image.has_value())
tex_id = (GLuint)(intptr_t) it.image->texture_id; tex_id = (GLuint)(intptr_t) it.image->texture_id;
@ -475,6 +472,5 @@ bool EmbossStyleManager::set_wx_font(const wxFont &wx_font, std::unique_ptr<Embo
// update string path // update string path
style.path = WxFontUtils::store_wxFont(wx_font); style.path = WxFontUtils::store_wxFont(wx_font);
clear_imgui_font(); clear_imgui_font();
free_style_images();
return true; return true;
} }