Skip to content

Commit

Permalink
[OpenGL backend] Add fallback for if glGenerateMipmap is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Mar 11, 2019
1 parent 3279398 commit a850fb5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
3 changes: 1 addition & 2 deletions lib/ivis_opengl/gfx_api.h
Expand Up @@ -43,8 +43,7 @@ namespace gfx_api
{
virtual ~texture() {};
virtual void bind() = 0;
virtual void upload(const size_t& mip_level, const size_t& offset_x, const size_t& offset_y, const size_t& width, const size_t& height, const pixel_format& buffer_format, const void* data) = 0;
virtual void generate_mip_levels() = 0;
virtual void upload(const size_t& mip_level, const size_t& offset_x, const size_t& offset_y, const size_t& width, const size_t& height, const pixel_format& buffer_format, const void* data, bool generate_mip_levels = false) = 0;
virtual unsigned id() = 0;
};

Expand Down
20 changes: 14 additions & 6 deletions lib/ivis_opengl/gfx_api_gl.cpp
Expand Up @@ -85,22 +85,30 @@ void gl_texture::bind()
glBindTexture(GL_TEXTURE_2D, _id);
}

void gl_texture::upload(const size_t& mip_level, const size_t& offset_x, const size_t& offset_y, const size_t & width, const size_t & height, const gfx_api::pixel_format & buffer_format, const void * data)
void gl_texture::upload(const size_t& mip_level, const size_t& offset_x, const size_t& offset_y, const size_t & width, const size_t & height, const gfx_api::pixel_format & buffer_format, const void * data, bool generate_mip_levels /*= false*/)
{
bind();
if(generate_mip_levels && !glGenerateMipmap)
{
// fallback for if glGenerateMipmap is unavailable
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
}
glTexSubImage2D(GL_TEXTURE_2D, mip_level, offset_x, offset_y, width, height, to_gl(buffer_format), GL_UNSIGNED_BYTE, data);
if(generate_mip_levels && glGenerateMipmap)
{
glGenerateMipmap(GL_TEXTURE_2D);
}
}

unsigned gl_texture::id()
{
return _id;
}

void gl_texture::generate_mip_levels()
{
glGenerateMipmap(GL_TEXTURE_2D);
}

// MARK: gl_buffer

gl_buffer::gl_buffer(const gfx_api::buffer::usage& usage, const gfx_api::context::buffer_storage_hint& hint)
Expand Down
3 changes: 1 addition & 2 deletions lib/ivis_opengl/gfx_api_gl.h
Expand Up @@ -35,9 +35,8 @@ struct gl_texture final : public gfx_api::texture
virtual ~gl_texture();
public:
virtual void bind() override;
virtual void upload(const size_t& mip_level, const size_t& offset_x, const size_t& offset_y, const size_t & width, const size_t & height, const gfx_api::pixel_format & buffer_format, const void * data) override;
virtual void upload(const size_t& mip_level, const size_t& offset_x, const size_t& offset_y, const size_t & width, const size_t & height, const gfx_api::pixel_format & buffer_format, const void * data, bool generate_mip_levels = false) override;
virtual unsigned id() override;
virtual void generate_mip_levels() override;
};

struct gl_buffer final : public gfx_api::buffer
Expand Down
1 change: 1 addition & 0 deletions lib/ivis_opengl/screen.cpp
Expand Up @@ -275,6 +275,7 @@ bool screenInitialise()
debug(LOG_3D, " * Total number of Texture Units (TUs) supported is %d.", (int) glMaxTUs);
debug(LOG_3D, " * GL_ARB_timer_query %s supported!", GLEW_ARB_timer_query ? "is" : "is NOT");
debug(LOG_3D, " * KHR_DEBUG support %s detected", khr_debug ? "was" : "was NOT");
debug(LOG_3D, " * glGenerateMipmap support %s detected", glGenerateMipmap ? "was" : "was NOT");

if (!GLEW_VERSION_2_0)
{
Expand Down
3 changes: 1 addition & 2 deletions lib/ivis_opengl/tex.cpp
Expand Up @@ -116,8 +116,7 @@ int pie_AddTexPage(iV_Image *s, const char *filename, bool gameTexture, int page
if (_TEX_PAGE[page].id)
delete _TEX_PAGE[page].id;
_TEX_PAGE[page].id = gfx_api::context::get().create_texture(s->width, s->height, format, filename);
pie_Texture(page).upload(0u, 0u, 0u, s->width, s->height, iV_getPixelFormat(s), s->bmp);
pie_Texture(page).generate_mip_levels();
pie_Texture(page).upload(0u, 0u, 0u, s->width, s->height, iV_getPixelFormat(s), s->bmp, true);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
else // this is an interface texture, do not use compression
Expand Down

0 comments on commit a850fb5

Please sign in to comment.