Tech ENABLE_LEGACY_OPENGL_REMOVAL - A few refactorings and fixes

This commit is contained in:
enricoturri1966 2022-03-22 08:18:39 +01:00
parent a2a9281ec8
commit 9be3d926c5
7 changed files with 30 additions and 26 deletions

View file

@ -6,5 +6,5 @@ in vec2 tex_coord;
void main() void main()
{ {
gl_FragColor = texture2D(uniform_texture, tex_coord); gl_FragColor = texture(uniform_texture, tex_coord);
} }

View file

@ -74,7 +74,7 @@ void main()
#ifdef ENABLE_ENVIRONMENT_MAP #ifdef ENABLE_ENVIRONMENT_MAP
if (use_environment_tex) 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 else
#endif #endif
gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha); gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha);

View file

@ -1,4 +1,4 @@
#version 110 #version 140
uniform sampler2D Texture; uniform sampler2D Texture;
@ -7,5 +7,5 @@ in vec4 Frag_Color;
void main() void main()
{ {
gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st); gl_FragColor = Frag_Color * texture(Texture, Frag_UV.st);
} }

View file

@ -12,7 +12,7 @@ in vec2 tex_coord;
vec4 svg_color() vec4 svg_color()
{ {
// takes foreground from texture // takes foreground from texture
vec4 fore_color = texture2D(texture, tex_coord); vec4 fore_color = texture(texture, tex_coord);
// calculates radial gradient // 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))))); 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() vec4 non_svg_color()
{ {
// takes foreground from texture // 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); return vec4(color.rgb, transparent_background ? color.a * 0.25 : color.a);
} }

View file

@ -34,8 +34,8 @@ void main()
// Sample the Z texture. Texture coordinates are normalized to <0, 1>. // Sample the Z texture. Texture coordinates are normalized to <0, 1>.
vec4 color = vec4(0.25, 0.25, 0.25, 1.0); vec4 color = vec4(0.25, 0.25, 0.25, 1.0);
if (z_texture_row >= 0.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.), color = mix(texture(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); texture(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row * 2. + 1.)), 10000.), lod);
// Mix the final color. // 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); gl_FragColor = vec4(vec3(intensity.y), 1.0) + intensity.x * mix(color, vec4(1.0, 1.0, 0.0, 1.0), z_blend);
} }

View file

@ -861,7 +861,7 @@ void GLModel::render(const std::pair<size_t, size_t>& range)
#if ENABLE_GL_SHADERS_ATTRIBUTES #if ENABLE_GL_SHADERS_ATTRIBUTES
position_id = shader->get_attrib_location("v_position"); position_id = shader->get_attrib_location("v_position");
if (position_id != -1) { 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)); glsafe(::glEnableVertexAttribArray(position_id));
} }
#else #else
@ -873,7 +873,7 @@ void GLModel::render(const std::pair<size_t, size_t>& range)
#if ENABLE_GL_SHADERS_ATTRIBUTES #if ENABLE_GL_SHADERS_ATTRIBUTES
normal_id = shader->get_attrib_location("v_normal"); normal_id = shader->get_attrib_location("v_normal");
if (normal_id != -1) { 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)); glsafe(::glEnableVertexAttribArray(normal_id));
} }
#else #else
@ -885,7 +885,7 @@ void GLModel::render(const std::pair<size_t, size_t>& range)
#if ENABLE_GL_SHADERS_ATTRIBUTES #if ENABLE_GL_SHADERS_ATTRIBUTES
tex_coord_id = shader->get_attrib_location("v_tex_coord"); tex_coord_id = shader->get_attrib_location("v_tex_coord");
if (tex_coord_id != -1) { 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)); glsafe(::glEnableVertexAttribArray(tex_coord_id));
} }
#else #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 void GLModel::render_instanced(unsigned int instances_vbo, unsigned int instances_count) const
#endif // ENABLE_LEGACY_OPENGL_REMOVAL #endif // ENABLE_LEGACY_OPENGL_REMOVAL
{ {
if (instances_vbo == 0) if (instances_vbo == 0 || instances_count == 0)
return; return;
GLShaderProgram* shader = wxGetApp().get_current_shader(); 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)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, instances_vbo));
#if ENABLE_LEGACY_OPENGL_REMOVAL #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(::glEnableVertexAttribArray(offset_id));
glsafe(::glVertexAttribDivisor(offset_id, 1)); 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(::glEnableVertexAttribArray(scales_id));
glsafe(::glVertexAttribDivisor(scales_id, 1)); glsafe(::glVertexAttribDivisor(scales_id, 1));
#else #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 mode = get_primitive_mode(data.format);
const GLenum index_type = get_index_type(data); 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 size_t vertex_stride_bytes = Geometry::vertex_stride_bytes(data.format);
const bool position = Geometry::has_position(data.format); const bool position = Geometry::has_position(data.format);
const bool normal = Geometry::has_normal(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)); glsafe(::glBindBuffer(GL_ARRAY_BUFFER, m_render_data.vbo_id));
if (position) { 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)); glsafe(::glEnableVertexAttribArray(position_id));
} }
if (normal) { 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)); glsafe(::glEnableVertexAttribArray(normal_id));
} }
shader->set_uniform("uniform_color", data.color);
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_render_data.ibo_id)); glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_render_data.ibo_id));
glsafe(::glDrawElementsInstanced(mode, indices_count(), index_type, (const void*)0, instances_count)); glsafe(::glDrawElementsInstanced(mode, indices_count(), index_type, (const void*)0, instances_count));
glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); glsafe(::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));

View file

@ -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! // 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. // 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_texture; glsafe(::glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
GLint last_polygon_mode[2]; glsafe(::glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode)); 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_viewport[4]; glsafe(::glGetIntegerv(GL_VIEWPORT, last_viewport));
GLint last_scissor_box[4]; glsafe(::glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box)); 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)); 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(::glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT));
glsafe(::glEnable(GL_BLEND)); 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"); const int position_id = shader->get_attrib_location("Position");
if (position_id != -1) { 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)); glsafe(::glEnableVertexAttribArray(position_id));
} }
const int uv_id = shader->get_attrib_location("UV"); const int uv_id = shader->get_attrib_location("UV");
if (uv_id != -1) { 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)); glsafe(::glEnableVertexAttribArray(uv_id));
} }
const int color_id = shader->get_attrib_location("Color"); const int color_id = shader->get_attrib_location("Color");
if (color_id != -1) { 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)); glsafe(::glEnableVertexAttribArray(color_id));
} }
#else #else
@ -1637,11 +1637,14 @@ void ImGuiWrapper::render_draw_data(ImDrawData *draw_data)
glsafe(::glPopMatrix()); glsafe(::glPopMatrix());
#endif // !ENABLE_GL_IMGUI_SHADERS #endif // !ENABLE_GL_IMGUI_SHADERS
glsafe(::glPopAttrib()); 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(::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])); 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 #if ENABLE_GL_IMGUI_SHADERS
shader->stop_using();
if (curr_shader != nullptr) if (curr_shader != nullptr)
curr_shader->start_using(); curr_shader->start_using();
#endif // ENABLE_GL_IMGUI_SHADERS #endif // ENABLE_GL_IMGUI_SHADERS