From 9be3d926c5ff8debf25f22e2d53dd1410bfcdd79 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Tue, 22 Mar 2022 08:18:39 +0100 Subject: [PATCH] Tech ENABLE_LEGACY_OPENGL_REMOVAL - A few refactorings and fixes --- resources/shaders/140/flat_texture.fs | 2 +- resources/shaders/140/gouraud.fs | 2 +- resources/shaders/140/imgui.fs | 4 ++-- resources/shaders/140/printbed.fs | 4 ++-- .../shaders/140/variable_layer_height.fs | 4 ++-- src/slic3r/GUI/GLModel.cpp | 21 ++++++++++--------- src/slic3r/GUI/ImGuiWrapper.cpp | 19 ++++++++++------- 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/resources/shaders/140/flat_texture.fs b/resources/shaders/140/flat_texture.fs index 7db62b682..dec946721 100644 --- a/resources/shaders/140/flat_texture.fs +++ b/resources/shaders/140/flat_texture.fs @@ -6,5 +6,5 @@ in vec2 tex_coord; void main() { - gl_FragColor = texture2D(uniform_texture, tex_coord); + gl_FragColor = texture(uniform_texture, tex_coord); } diff --git a/resources/shaders/140/gouraud.fs b/resources/shaders/140/gouraud.fs index 7651380d7..beb3a947b 100644 --- a/resources/shaders/140/gouraud.fs +++ b/resources/shaders/140/gouraud.fs @@ -74,7 +74,7 @@ void main() #ifdef 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, alpha); + gl_FragColor = vec4(0.45 * texture(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity.x, alpha); else #endif gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha); diff --git a/resources/shaders/140/imgui.fs b/resources/shaders/140/imgui.fs index 8e7999fdf..4b2571749 100644 --- a/resources/shaders/140/imgui.fs +++ b/resources/shaders/140/imgui.fs @@ -1,4 +1,4 @@ -#version 110 +#version 140 uniform sampler2D Texture; @@ -7,5 +7,5 @@ in vec4 Frag_Color; void main() { - gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st); + gl_FragColor = Frag_Color * texture(Texture, Frag_UV.st); } \ No newline at end of file diff --git a/resources/shaders/140/printbed.fs b/resources/shaders/140/printbed.fs index 73ff8f690..86edd0daf 100644 --- a/resources/shaders/140/printbed.fs +++ b/resources/shaders/140/printbed.fs @@ -12,7 +12,7 @@ in vec2 tex_coord; vec4 svg_color() { // takes foreground from texture - vec4 fore_color = texture2D(texture, tex_coord); + vec4 fore_color = texture(texture, tex_coord); // calculates radial gradient vec3 back_color = vec3(mix(back_color_light, back_color_dark, smoothstep(0.0, 0.5, length(abs(tex_coord.xy) - vec2(0.5))))); @@ -24,7 +24,7 @@ vec4 svg_color() vec4 non_svg_color() { // takes foreground from texture - vec4 color = texture2D(texture, tex_coord); + vec4 color = texture(texture, tex_coord); return vec4(color.rgb, transparent_background ? color.a * 0.25 : color.a); } diff --git a/resources/shaders/140/variable_layer_height.fs b/resources/shaders/140/variable_layer_height.fs index 5fa7e6835..cf1fc309c 100644 --- a/resources/shaders/140/variable_layer_height.fs +++ b/resources/shaders/140/variable_layer_height.fs @@ -34,8 +34,8 @@ void main() // Sample the Z texture. Texture coordinates are normalized to <0, 1>. vec4 color = vec4(0.25, 0.25, 0.25, 1.0); if (z_texture_row >= 0.0) - color = mix(texture2D(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row + 0.5 )), -10000.), - texture2D(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row * 2. + 1.)), 10000.), lod); + color = mix(texture(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row + 0.5 )), -10000.), + texture(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row * 2. + 1.)), 10000.), lod); // Mix the final color. gl_FragColor = vec4(vec3(intensity.y), 1.0) + intensity.x * mix(color, vec4(1.0, 1.0, 0.0, 1.0), z_blend); } diff --git a/src/slic3r/GUI/GLModel.cpp b/src/slic3r/GUI/GLModel.cpp index 4297a9091..9c9d69526 100644 --- a/src/slic3r/GUI/GLModel.cpp +++ b/src/slic3r/GUI/GLModel.cpp @@ -861,7 +861,7 @@ void GLModel::render(const std::pair& range) #if ENABLE_GL_SHADERS_ATTRIBUTES position_id = shader->get_attrib_location("v_position"); if (position_id != -1) { - glsafe(::glVertexAttribPointer(position_id, Geometry::position_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (GLvoid*)Geometry::position_offset_bytes(data.format))); + glsafe(::glVertexAttribPointer(position_id, Geometry::position_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (const void*)Geometry::position_offset_bytes(data.format))); glsafe(::glEnableVertexAttribArray(position_id)); } #else @@ -873,7 +873,7 @@ void GLModel::render(const std::pair& range) #if ENABLE_GL_SHADERS_ATTRIBUTES normal_id = shader->get_attrib_location("v_normal"); if (normal_id != -1) { - glsafe(::glVertexAttribPointer(normal_id, Geometry::normal_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (GLvoid*)Geometry::normal_offset_bytes(data.format))); + glsafe(::glVertexAttribPointer(normal_id, Geometry::normal_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (const void*)Geometry::normal_offset_bytes(data.format))); glsafe(::glEnableVertexAttribArray(normal_id)); } #else @@ -885,7 +885,7 @@ void GLModel::render(const std::pair& range) #if ENABLE_GL_SHADERS_ATTRIBUTES tex_coord_id = shader->get_attrib_location("v_tex_coord"); if (tex_coord_id != -1) { - glsafe(::glVertexAttribPointer(tex_coord_id, Geometry::tex_coord_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (GLvoid*)Geometry::tex_coord_offset_bytes(data.format))); + glsafe(::glVertexAttribPointer(tex_coord_id, Geometry::tex_coord_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (const void*)Geometry::tex_coord_offset_bytes(data.format))); glsafe(::glEnableVertexAttribArray(tex_coord_id)); } #else @@ -930,7 +930,7 @@ void GLModel::render_instanced(unsigned int instances_vbo, unsigned int instance void GLModel::render_instanced(unsigned int instances_vbo, unsigned int instances_count) const #endif // ENABLE_LEGACY_OPENGL_REMOVAL { - if (instances_vbo == 0) + if (instances_vbo == 0 || instances_count == 0) return; GLShaderProgram* shader = wxGetApp().get_current_shader(); @@ -970,11 +970,12 @@ void GLModel::render_instanced(unsigned int instances_vbo, unsigned int instance glsafe(::glBindBuffer(GL_ARRAY_BUFFER, instances_vbo)); #if ENABLE_LEGACY_OPENGL_REMOVAL - glsafe(::glVertexAttribPointer(offset_id, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (GLvoid*)0)); + const size_t instance_stride = 5 * sizeof(float); + glsafe(::glVertexAttribPointer(offset_id, 3, GL_FLOAT, GL_FALSE, instance_stride, (const void*)0)); glsafe(::glEnableVertexAttribArray(offset_id)); glsafe(::glVertexAttribDivisor(offset_id, 1)); - glsafe(::glVertexAttribPointer(scales_id, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (GLvoid*)(3 * sizeof(float)))); + glsafe(::glVertexAttribPointer(scales_id, 2, GL_FLOAT, GL_FALSE, instance_stride, (const void*)(3 * sizeof(float)))); glsafe(::glEnableVertexAttribArray(scales_id)); glsafe(::glVertexAttribDivisor(scales_id, 1)); #else @@ -996,8 +997,6 @@ void GLModel::render_instanced(unsigned int instances_vbo, unsigned int instance const GLenum mode = get_primitive_mode(data.format); const GLenum index_type = get_index_type(data); - shader->set_uniform("uniform_color", data.color); - const size_t vertex_stride_bytes = Geometry::vertex_stride_bytes(data.format); const bool position = Geometry::has_position(data.format); const bool normal = Geometry::has_normal(data.format); @@ -1005,15 +1004,17 @@ void GLModel::render_instanced(unsigned int instances_vbo, unsigned int instance glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_render_data.vbo_id)); if (position) { - glsafe(::glVertexAttribPointer(position_id, Geometry::position_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (GLvoid*)Geometry::position_offset_bytes(data.format))); + glsafe(::glVertexAttribPointer(position_id, Geometry::position_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (const void*)Geometry::position_offset_bytes(data.format))); glsafe(::glEnableVertexAttribArray(position_id)); } if (normal) { - glsafe(::glVertexAttribPointer(normal_id, Geometry::normal_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (GLvoid*)Geometry::normal_offset_bytes(data.format))); + glsafe(::glVertexAttribPointer(normal_id, Geometry::normal_stride_floats(data.format), GL_FLOAT, GL_FALSE, vertex_stride_bytes, (const void*)Geometry::normal_offset_bytes(data.format))); glsafe(::glEnableVertexAttribArray(normal_id)); } + shader->set_uniform("uniform_color", data.color); + glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_render_data.ibo_id)); glsafe(::glDrawElementsInstanced(mode, indices_count(), index_type, (const void*)0, instances_count)); glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index e659a386b..d5dfa276c 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -1475,10 +1475,10 @@ void ImGuiWrapper::render_draw_data(ImDrawData *draw_data) // We are using the OpenGL fixed pipeline to make the example code simpler to read! // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers, polygon fill. - GLint last_texture; glsafe(::glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); - GLint last_polygon_mode[2]; glsafe(::glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode)); - GLint last_viewport[4]; glsafe(::glGetIntegerv(GL_VIEWPORT, last_viewport)); - GLint last_scissor_box[4]; glsafe(::glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box)); + GLint last_texture; glsafe(::glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); + GLint last_polygon_mode[2]; glsafe(::glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode)); + GLint last_viewport[4]; glsafe(::glGetIntegerv(GL_VIEWPORT, last_viewport)); + GLint last_scissor_box[4]; glsafe(::glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box)); GLint last_texture_env_mode; glsafe(::glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &last_texture_env_mode)); glsafe(::glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT)); glsafe(::glEnable(GL_BLEND)); @@ -1553,17 +1553,17 @@ void ImGuiWrapper::render_draw_data(ImDrawData *draw_data) const int position_id = shader->get_attrib_location("Position"); if (position_id != -1) { - glsafe(::glVertexAttribPointer(position_id, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos))); + glsafe(::glVertexAttribPointer(position_id, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (const void*)IM_OFFSETOF(ImDrawVert, pos))); glsafe(::glEnableVertexAttribArray(position_id)); } const int uv_id = shader->get_attrib_location("UV"); if (uv_id != -1) { - glsafe(::glVertexAttribPointer(uv_id, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv))); + glsafe(::glVertexAttribPointer(uv_id, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (const void*)IM_OFFSETOF(ImDrawVert, uv))); glsafe(::glEnableVertexAttribArray(uv_id)); } const int color_id = shader->get_attrib_location("Color"); if (color_id != -1) { - glsafe(::glVertexAttribPointer(color_id, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col))); + glsafe(::glVertexAttribPointer(color_id, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (const void*)IM_OFFSETOF(ImDrawVert, col))); glsafe(::glEnableVertexAttribArray(color_id)); } #else @@ -1637,11 +1637,14 @@ void ImGuiWrapper::render_draw_data(ImDrawData *draw_data) glsafe(::glPopMatrix()); #endif // !ENABLE_GL_IMGUI_SHADERS glsafe(::glPopAttrib()); - glsafe(::glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1])); + glsafe(::glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); + glsafe(::glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]))); glsafe(::glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3])); glsafe(::glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3])); #if ENABLE_GL_IMGUI_SHADERS + shader->stop_using(); + if (curr_shader != nullptr) curr_shader->start_using(); #endif // ENABLE_GL_IMGUI_SHADERS