RetroArch/gfx/drivers/gl_common.c

158 lines
4.8 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_ff_vertex(const void *data)
{
#ifndef NO_GL_FF_VERTEX
2015-07-12 01:57:06 +00:00
const struct gfx_coords *coords = (const struct gfx_coords*)data;
/* Fall back to fixed function-style if needed and possible. */
glClientActiveTexture(GL_TEXTURE1);
glTexCoordPointer(2, GL_FLOAT, 0, coords->lut_tex_coord);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glVertexPointer(2, GL_FLOAT, 0, coords->vertex);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_FLOAT, 0, coords->color);
glEnableClientState(GL_COLOR_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, coords->tex_coord);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
#endif
}
void gl_ff_matrix(const void *data)
{
#ifndef NO_GL_FF_MATRIX
const math_matrix_4x4 *mat = (const math_matrix_4x4*)data;
/* Fall back to fixed function-style if needed and possible. */
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(mat->data);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
#endif
}
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;
bool want_mipmap = false;
2015-02-11 15:05:11 +00:00
bool rgb32 = (base_size == (sizeof(uint32_t)));
driver_t *driver = driver_get_ptr();
2015-03-20 01:51:16 +00:00
GLenum wrap = gl_wrap_type_to_enum(wrap_type);
glBindTexture(GL_TEXTURE_2D, id);
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_NEAREST;
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);
2015-02-11 14:46:55 +00:00
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
2014-10-02 11:27:35 +00:00
glTexImage2D(GL_TEXTURE_2D,
0,
(driver->gfx_use_rgba || !rgb32) ? GL_RGBA : RARCH_GL_INTERNAL_FORMAT32,
2015-02-11 14:53:37 +00:00
width, height, 0,
(driver->gfx_use_rgba || !rgb32) ? GL_RGBA : RARCH_GL_TEXTURE_TYPE32,
2015-02-11 15:05:11 +00:00
(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-02-13 00:50:44 +00:00
bool gl_load_luts(const struct video_shader *shader,
GLuint *textures_lut)
2014-10-02 11:27:35 +00:00
{
unsigned i;
2015-02-13 00:50:44 +00:00
unsigned num_luts = min(shader->luts, GFX_MAX_TEXTURES);
2014-10-02 11:27:35 +00:00
2015-02-13 00:50:44 +00:00
if (!shader->luts)
2014-10-02 11:27:35 +00:00
return true;
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",
2015-02-13 00:50:44 +00:00
shader->lut[i].path);
2014-10-02 11:27:35 +00:00
2015-02-13 00:50:44 +00:00
if (!texture_image_load(&img, shader->lut[i].path))
2014-10-02 11:27:35 +00:00
{
RARCH_ERR("Failed to load texture image from: \"%s\"\n",
2015-02-13 00:50:44 +00:00
shader->lut[i].path);
2014-10-02 11:27:35 +00:00
return false;
}
2015-02-13 00:50:44 +00:00
if (shader->lut[i].filter == RARCH_FILTER_NEAREST)
filter_type = TEXTURE_FILTER_NEAREST;
2015-02-13 00:50:44 +00:00
if (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],
2015-02-13 00:50:44 +00:00
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;
}