RetroArch/gfx/gl_common.c

136 lines
4.1 KiB
C
Raw Normal View History

2014-10-02 11:27:35 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 17:06:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2014-10-02 11:27:35 +00:00
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
2014-10-02 11:32:42 +00:00
#include "gl_common.h"
2014-10-02 11:27:35 +00:00
void gl_load_texture_data(GLuint id,
enum gfx_wrap_type wrap_type,
2015-02-11 14:46:55 +00:00
enum texture_filter_type filter_type,
2015-02-11 14:53:37 +00:00
unsigned alignment,
unsigned width, unsigned height,
2015-02-11 15:05:11 +00:00
const void *frame, unsigned base_size)
2014-10-02 11:27:35 +00:00
{
GLint mag_filter, min_filter;
GLenum wrap;
bool want_mipmap = false;
2015-02-11 15:05:11 +00:00
bool rgb32 = (base_size == (sizeof(uint32_t)));
if (!driver.video)
return;
glBindTexture(GL_TEXTURE_2D, id);
wrap = driver.video->wrap_type_to_enum(wrap_type);
2014-10-02 11:27:35 +00:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
switch (filter_type)
{
case TEXTURE_FILTER_MIPMAP_LINEAR:
min_filter = GL_LINEAR_MIPMAP_LINEAR;
mag_filter = GL_LINEAR;
#ifndef HAVE_PSGL
want_mipmap = true;
#endif
break;
case TEXTURE_FILTER_MIPMAP_NEAREST:
min_filter = GL_NEAREST_MIPMAP_NEAREST;
mag_filter = GL_NEAREST;
#ifndef HAVE_PSGL
want_mipmap = true;
#endif
break;
case TEXTURE_FILTER_NEAREST:
min_filter = GL_NEAREST;
mag_filter = GL_NEAREST;
break;
case TEXTURE_FILTER_LINEAR:
default:
min_filter = GL_LINEAR;
mag_filter = GL_LINEAR;
break;
}
2014-10-02 11:27:35 +00:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
#ifndef HAVE_PSGL
2015-02-11 14:46:55 +00:00
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
2014-10-02 11:27:35 +00:00
#endif
glTexImage2D(GL_TEXTURE_2D,
0,
2015-02-11 15:05:11 +00:00
(driver.gfx_use_rgba || !rgb32) ? GL_RGBA : RARCH_GL_INTERNAL_FORMAT32,
2015-02-11 14:53:37 +00:00
width, height, 0,
2015-02-11 15:05:11 +00:00
(driver.gfx_use_rgba || !rgb32) ? GL_RGBA : RARCH_GL_TEXTURE_TYPE32,
(rgb32) ? RARCH_GL_FORMAT32 : GL_UNSIGNED_SHORT_4_4_4_4, frame);
if (want_mipmap)
2014-10-02 11:27:35 +00:00
glGenerateMipmap(GL_TEXTURE_2D);
}
2015-01-19 20:24:08 +00:00
bool gl_load_luts(const struct video_shader *generic_shader,
GLuint *textures_lut)
2014-10-02 11:27:35 +00:00
{
unsigned i;
unsigned num_luts = min(generic_shader->luts, GFX_MAX_TEXTURES);
if (!generic_shader->luts)
return true;
/* Original shader_glsl.c code only generated one
* texture handle. I assume it was a bug, but if not,
* replace num_luts with 1 when GLSL is used. */
glGenTextures(num_luts, textures_lut);
2014-10-02 11:27:35 +00:00
for (i = 0; i < num_luts; i++)
{
struct texture_image img = {0};
enum texture_filter_type filter_type = TEXTURE_FILTER_LINEAR;
2014-10-02 11:27:35 +00:00
RARCH_LOG("Loading texture image from: \"%s\" ...\n",
generic_shader->lut[i].path);
if (!texture_image_load(&img, generic_shader->lut[i].path))
{
RARCH_ERR("Failed to load texture image from: \"%s\"\n",
generic_shader->lut[i].path);
return false;
}
if (generic_shader->lut[i].filter == RARCH_FILTER_NEAREST)
filter_type = TEXTURE_FILTER_NEAREST;
if (generic_shader->lut[i].mipmap)
{
if (filter_type == TEXTURE_FILTER_NEAREST)
filter_type = TEXTURE_FILTER_MIPMAP_NEAREST;
else
filter_type = TEXTURE_FILTER_MIPMAP_LINEAR;
}
2015-02-11 14:53:37 +00:00
gl_load_texture_data(textures_lut[i],
generic_shader->lut[i].wrap,
2015-02-11 14:53:37 +00:00
filter_type, 4,
img.width, img.height,
2015-02-11 15:05:11 +00:00
img.pixels, sizeof(uint32_t));
2014-10-02 11:27:35 +00:00
texture_image_free(&img);
}
glBindTexture(GL_TEXTURE_2D, 0);
return true;
}