2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2013-01-01 00:37:37 +00:00
|
|
|
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
|
2011-01-05 18:07:12 +00:00
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2011-01-05 18:07:12 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2011-01-05 18:07:12 +00:00
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
2012-04-21 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-01-05 18:07:12 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
#include "../boolean.h"
|
2011-01-05 18:07:12 +00:00
|
|
|
#include <string.h>
|
2011-11-30 17:31:09 +00:00
|
|
|
#include "../general.h"
|
2011-03-16 20:48:44 +00:00
|
|
|
#include "shader_glsl.h"
|
2012-03-16 22:26:57 +00:00
|
|
|
#include "../compat/strl.h"
|
|
|
|
#include "../compat/posix_string.h"
|
2012-04-07 10:17:40 +00:00
|
|
|
#include "state_tracker.h"
|
2011-11-30 17:31:09 +00:00
|
|
|
#include "../dynamic.h"
|
2012-11-18 16:08:28 +00:00
|
|
|
#include "../file.h"
|
2011-05-18 18:22:27 +00:00
|
|
|
|
2012-09-13 18:15:37 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-02-06 14:54:15 +00:00
|
|
|
#if defined(IOS)
|
|
|
|
#include <OpenGLES/ES2/gl.h>
|
|
|
|
#include <OpenGLES/ES2/glext.h>
|
|
|
|
#elif defined(__APPLE__) // Because they like to be "oh, so, special".
|
2011-02-04 12:46:51 +00:00
|
|
|
#include <OpenGL/gl.h>
|
2012-09-12 14:40:28 +00:00
|
|
|
#include <OpenGL/glext.h>
|
|
|
|
#elif defined(HAVE_PSGL)
|
|
|
|
#include <PSGL/psgl.h>
|
|
|
|
#include <PSGL/psglu.h>
|
|
|
|
#include <GLES/glext.h>
|
|
|
|
#elif defined(HAVE_OPENGL_MODERN)
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <GL3/gl3.h>
|
|
|
|
#include <GL3/gl3ext.h>
|
|
|
|
#elif defined(HAVE_OPENGLES2)
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#elif defined(HAVE_OPENGLES1)
|
|
|
|
#include <GLES/gl.h>
|
|
|
|
#include <GLES/glext.h>
|
2011-02-04 12:46:51 +00:00
|
|
|
#else
|
|
|
|
#define GL_GLEXT_PROTOTYPES
|
2011-01-05 18:07:12 +00:00
|
|
|
#include <GL/gl.h>
|
2011-02-04 12:46:51 +00:00
|
|
|
#include <GL/glext.h>
|
|
|
|
#endif
|
|
|
|
|
2012-05-26 02:47:24 +00:00
|
|
|
#include "gfx_context.h"
|
2011-01-05 18:07:12 +00:00
|
|
|
#include <stdlib.h>
|
2011-12-26 00:32:19 +00:00
|
|
|
|
2011-01-11 21:13:55 +00:00
|
|
|
#include "gl_common.h"
|
2011-05-18 18:22:27 +00:00
|
|
|
#include "image.h"
|
|
|
|
|
2012-09-15 13:17:34 +00:00
|
|
|
#ifdef HAVE_OPENGLES2
|
|
|
|
#define BORDER_FUNC GL_CLAMP_TO_EDGE
|
|
|
|
#else
|
|
|
|
#define BORDER_FUNC GL_CLAMP_TO_BORDER
|
|
|
|
#endif
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2013-05-18 15:51:10 +00:00
|
|
|
#define PREV_TEXTURES (TEXTURES - 1)
|
2011-03-12 14:30:57 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
static struct gfx_shader *glsl_shader;
|
2013-06-30 20:58:22 +00:00
|
|
|
static bool glsl_core;
|
|
|
|
static unsigned glsl_major;
|
|
|
|
static unsigned glsl_minor;
|
2013-04-02 21:14:14 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
static bool glsl_enable;
|
2013-04-07 10:41:05 +00:00
|
|
|
static GLuint gl_program[GFX_MAX_SHADERS];
|
2012-10-21 00:58:51 +00:00
|
|
|
static unsigned active_index;
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
static GLuint gl_teximage[GFX_MAX_TEXTURES];
|
2011-05-18 18:22:27 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
static state_tracker_t *gl_state_tracker;
|
2011-05-25 13:58:12 +00:00
|
|
|
|
2013-04-20 13:46:50 +00:00
|
|
|
static GLint gl_attribs[PREV_TEXTURES + 1 + 4 + GFX_MAX_SHADERS];
|
2012-10-21 00:58:51 +00:00
|
|
|
static unsigned gl_attrib_index;
|
2011-11-12 17:20:54 +00:00
|
|
|
|
2013-05-21 08:39:39 +00:00
|
|
|
// Cache the VBO.
|
|
|
|
struct cache_vbo
|
|
|
|
{
|
|
|
|
GLuint vbo_primary;
|
|
|
|
GLfloat buffer_primary[128];
|
|
|
|
size_t size_primary;
|
|
|
|
|
|
|
|
GLuint vbo_secondary;
|
|
|
|
GLfloat buffer_secondary[128];
|
|
|
|
size_t size_secondary;
|
|
|
|
};
|
|
|
|
static struct cache_vbo glsl_vbo[GFX_MAX_SHADERS];
|
|
|
|
|
|
|
|
struct glsl_attrib
|
|
|
|
{
|
|
|
|
GLint loc;
|
|
|
|
GLsizei size;
|
|
|
|
GLsizei offset;
|
|
|
|
};
|
|
|
|
|
2012-09-24 23:26:22 +00:00
|
|
|
static gfx_ctx_proc_t (*glsl_get_proc_address)(const char*);
|
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
struct shader_uniforms_frame
|
|
|
|
{
|
|
|
|
int texture;
|
|
|
|
int input_size;
|
|
|
|
int texture_size;
|
|
|
|
int tex_coord;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct shader_uniforms
|
|
|
|
{
|
|
|
|
int mvp;
|
|
|
|
int tex_coord;
|
|
|
|
int vertex_coord;
|
|
|
|
int color;
|
|
|
|
int lut_tex_coord;
|
|
|
|
|
|
|
|
int input_size;
|
|
|
|
int output_size;
|
|
|
|
int texture_size;
|
|
|
|
|
|
|
|
int frame_count;
|
2013-03-25 13:47:33 +00:00
|
|
|
unsigned frame_count_mod;
|
2012-10-21 00:58:51 +00:00
|
|
|
int frame_direction;
|
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
int lut_texture[GFX_MAX_TEXTURES];
|
2012-10-21 00:58:51 +00:00
|
|
|
|
|
|
|
struct shader_uniforms_frame orig;
|
2013-04-20 13:46:50 +00:00
|
|
|
struct shader_uniforms_frame pass[GFX_MAX_SHADERS];
|
2012-10-21 00:58:51 +00:00
|
|
|
struct shader_uniforms_frame prev[PREV_TEXTURES];
|
|
|
|
};
|
|
|
|
|
2013-04-20 13:46:50 +00:00
|
|
|
static struct shader_uniforms gl_uniforms[GFX_MAX_SHADERS];
|
2012-10-21 00:58:51 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
static const char *glsl_prefixes[] = {
|
|
|
|
"",
|
|
|
|
"ruby",
|
|
|
|
};
|
|
|
|
|
2013-04-20 13:46:50 +00:00
|
|
|
// Need to duplicate these to work around broken stuff on Android.
|
|
|
|
// Must enforce alpha = 1.0 or 32-bit games can potentially go black.
|
|
|
|
static const char *stock_vertex_modern =
|
|
|
|
"attribute vec2 TexCoord;\n"
|
|
|
|
"attribute vec2 VertexCoord;\n"
|
|
|
|
"attribute vec4 Color;\n"
|
|
|
|
"uniform mat4 MVPMatrix;\n"
|
|
|
|
"varying vec2 tex_coord;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = MVPMatrix * vec4(VertexCoord, 0.0, 1.0);\n"
|
|
|
|
" tex_coord = TexCoord;\n"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
static const char *stock_fragment_modern =
|
|
|
|
"#ifdef GL_ES\n"
|
|
|
|
"precision mediump float;\n"
|
|
|
|
"#endif\n"
|
|
|
|
"uniform sampler2D Texture;\n"
|
|
|
|
"varying vec2 tex_coord;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_FragColor = vec4(texture2D(Texture, tex_coord).rgb, 1.0);\n"
|
|
|
|
"}";
|
|
|
|
|
2013-06-30 20:58:22 +00:00
|
|
|
static const char *stock_vertex_core =
|
|
|
|
"in vec2 TexCoord;\n"
|
|
|
|
"in vec2 VertexCoord;\n"
|
|
|
|
"in vec4 Color;\n"
|
|
|
|
"uniform mat4 MVPMatrix;\n"
|
|
|
|
"out vec2 tex_coord;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = MVPMatrix * vec4(VertexCoord, 0.0, 1.0);\n"
|
|
|
|
" tex_coord = TexCoord;\n"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
static const char *stock_fragment_core =
|
|
|
|
"uniform sampler2D Texture;\n"
|
|
|
|
"in vec2 tex_coord;\n"
|
|
|
|
"out vec4 FragColor;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" FragColor = vec4(texture(Texture, tex_coord).rgb, 1.0);\n"
|
|
|
|
"}";
|
|
|
|
|
2012-09-11 21:32:00 +00:00
|
|
|
static const char *stock_vertex_legacy =
|
2011-11-12 14:26:24 +00:00
|
|
|
"varying vec4 color;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
|
|
|
|
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
|
|
|
|
" color = gl_Color;\n"
|
|
|
|
"}";
|
|
|
|
|
2012-09-11 21:32:00 +00:00
|
|
|
static const char *stock_fragment_legacy =
|
2013-04-02 21:14:14 +00:00
|
|
|
"uniform sampler2D Texture;\n"
|
2011-11-12 14:26:24 +00:00
|
|
|
"varying vec4 color;\n"
|
|
|
|
"void main() {\n"
|
2013-04-02 21:14:14 +00:00
|
|
|
" gl_FragColor = color * texture2D(Texture, gl_TexCoord[0].xy);\n"
|
2011-11-12 14:26:24 +00:00
|
|
|
"}";
|
|
|
|
|
2013-04-20 13:46:50 +00:00
|
|
|
static const char *stock_vertex_modern_blend =
|
2013-04-02 21:14:14 +00:00
|
|
|
"attribute vec2 TexCoord;\n"
|
|
|
|
"attribute vec2 VertexCoord;\n"
|
|
|
|
"attribute vec4 Color;\n"
|
|
|
|
"uniform mat4 MVPMatrix;\n"
|
2012-09-11 21:32:00 +00:00
|
|
|
"varying vec2 tex_coord;\n"
|
|
|
|
"varying vec4 color;\n"
|
|
|
|
"void main() {\n"
|
2013-04-02 21:14:14 +00:00
|
|
|
" gl_Position = MVPMatrix * vec4(VertexCoord, 0.0, 1.0);\n"
|
|
|
|
" tex_coord = TexCoord;\n"
|
|
|
|
" color = Color;\n"
|
2012-09-11 21:32:00 +00:00
|
|
|
"}";
|
|
|
|
|
2013-04-20 13:46:50 +00:00
|
|
|
static const char *stock_fragment_modern_blend =
|
2012-09-13 17:55:42 +00:00
|
|
|
"#ifdef GL_ES\n"
|
|
|
|
"precision mediump float;\n"
|
|
|
|
"#endif\n"
|
2013-04-02 21:14:14 +00:00
|
|
|
"uniform sampler2D Texture;\n"
|
2012-09-11 21:32:00 +00:00
|
|
|
"varying vec2 tex_coord;\n"
|
|
|
|
"varying vec4 color;\n"
|
|
|
|
"void main() {\n"
|
2013-04-02 21:14:14 +00:00
|
|
|
" gl_FragColor = color * texture2D(Texture, tex_coord);\n"
|
2012-09-11 21:32:00 +00:00
|
|
|
"}";
|
|
|
|
|
2013-07-05 17:29:48 +00:00
|
|
|
#ifndef HAVE_OPENGLES2
|
2013-06-30 20:58:22 +00:00
|
|
|
static const char *stock_vertex_core_blend =
|
|
|
|
"in vec2 TexCoord;\n"
|
|
|
|
"in vec2 VertexCoord;\n"
|
|
|
|
"in vec4 Color;\n"
|
|
|
|
"uniform mat4 MVPMatrix;\n"
|
|
|
|
"out vec2 tex_coord;\n"
|
|
|
|
"out vec4 color;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = MVPMatrix * vec4(VertexCoord, 0.0, 1.0);\n"
|
|
|
|
" tex_coord = TexCoord;\n"
|
|
|
|
" color = Color;\n"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
static const char *stock_fragment_core_blend =
|
|
|
|
"uniform sampler2D Texture;\n"
|
|
|
|
"in vec2 tex_coord;\n"
|
|
|
|
"in vec4 color;\n"
|
|
|
|
"out vec4 FragColor;\n"
|
|
|
|
"void main() {\n"
|
2013-06-30 21:57:25 +00:00
|
|
|
" FragColor = color * texture(Texture, tex_coord);\n"
|
2013-06-30 20:58:22 +00:00
|
|
|
"}";
|
2013-07-05 17:29:48 +00:00
|
|
|
#endif
|
2012-11-18 18:39:34 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
static GLint get_uniform(GLuint prog, const char *base)
|
2011-03-14 21:51:03 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
char buf[64];
|
2011-03-14 21:51:03 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s%s", glsl_shader->prefix, base);
|
2013-07-05 16:15:42 +00:00
|
|
|
GLint loc = glGetUniformLocation(prog, buf);
|
2013-04-07 10:41:05 +00:00
|
|
|
if (loc >= 0)
|
|
|
|
return loc;
|
2013-04-02 21:14:14 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(glsl_prefixes); i++)
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "%s%s", glsl_prefixes[i], base);
|
2013-07-05 16:15:42 +00:00
|
|
|
GLint loc = glGetUniformLocation(prog, buf);
|
2013-04-02 21:14:14 +00:00
|
|
|
if (loc >= 0)
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLint get_attrib(GLuint prog, const char *base)
|
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
char buf[64];
|
|
|
|
snprintf(buf, sizeof(buf), "%s%s", glsl_shader->prefix, base);
|
2013-07-05 16:15:42 +00:00
|
|
|
GLint loc = glGetUniformLocation(prog, buf);
|
2013-04-07 10:41:05 +00:00
|
|
|
if (loc >= 0)
|
|
|
|
return loc;
|
|
|
|
|
2013-04-02 21:14:14 +00:00
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(glsl_prefixes); i++)
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "%s%s", glsl_prefixes[i], base);
|
2013-07-05 16:15:42 +00:00
|
|
|
GLint loc = glGetAttribLocation(prog, buf);
|
2013-04-02 21:14:14 +00:00
|
|
|
if (loc >= 0)
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-15 21:45:11 +00:00
|
|
|
static bool load_luts(void)
|
2011-05-18 18:22:27 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
if (!glsl_shader->luts)
|
2011-05-18 18:27:27 +00:00
|
|
|
return true;
|
2011-05-18 18:22:27 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
glGenTextures(1, gl_teximage);
|
2011-06-07 13:33:29 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
for (unsigned i = 0; i < glsl_shader->luts; i++)
|
2011-05-25 13:58:12 +00:00
|
|
|
{
|
2013-04-15 21:45:11 +00:00
|
|
|
RARCH_LOG("Loading texture image from: \"%s\" ...\n",
|
|
|
|
glsl_shader->lut[i].path);
|
2011-05-25 13:58:12 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
struct texture_image img = {0};
|
2013-04-15 21:45:11 +00:00
|
|
|
if (!texture_image_load(glsl_shader->lut[i].path, &img))
|
2012-04-07 10:17:40 +00:00
|
|
|
{
|
2013-04-15 21:45:11 +00:00
|
|
|
RARCH_ERR("Failed to load texture image from: \"%s\"\n", glsl_shader->lut[i].path);
|
2012-11-18 18:39:34 +00:00
|
|
|
return false;
|
2011-06-06 16:50:36 +00:00
|
|
|
}
|
2011-03-12 14:30:57 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl_teximage[i]);
|
2012-11-18 16:39:39 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, BORDER_FUNC);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, BORDER_FUNC);
|
2012-11-18 16:39:39 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
GLenum filter = glsl_shader->lut[i].filter == RARCH_FILTER_NEAREST ?
|
|
|
|
GL_NEAREST : GL_LINEAR;
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
|
2012-09-12 18:46:38 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D,
|
|
|
|
0, driver.gfx_use_rgba ? GL_RGBA : RARCH_GL_INTERNAL_FORMAT32,
|
|
|
|
img.width, img.height, 0,
|
|
|
|
driver.gfx_use_rgba ? GL_RGBA : RARCH_GL_TEXTURE_TYPE32,
|
|
|
|
RARCH_GL_FORMAT32, img.pixels);
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
free(img.pixels);
|
2011-01-05 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
return true;
|
2011-01-05 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 20:41:11 +00:00
|
|
|
static void print_shader_log(GLuint obj)
|
|
|
|
{
|
2011-06-29 01:26:21 +00:00
|
|
|
GLint info_len = 0;
|
|
|
|
GLint max_len;
|
2011-01-07 20:41:11 +00:00
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &max_len);
|
2011-01-07 20:41:11 +00:00
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
if (max_len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *info_log = (char*)malloc(max_len);
|
|
|
|
if (!info_log)
|
|
|
|
return;
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glGetShaderInfoLog(obj, max_len, &info_len, info_log);
|
2011-01-07 20:41:11 +00:00
|
|
|
|
|
|
|
if (info_len > 0)
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("Shader log: %s\n", info_log);
|
2011-12-24 12:46:12 +00:00
|
|
|
|
|
|
|
free(info_log);
|
2011-01-07 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2011-01-11 21:13:55 +00:00
|
|
|
static void print_linker_log(GLuint obj)
|
|
|
|
{
|
2011-06-29 01:26:21 +00:00
|
|
|
GLint info_len = 0;
|
|
|
|
GLint max_len;
|
2011-01-11 21:13:55 +00:00
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &max_len);
|
2011-01-11 21:13:55 +00:00
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
if (max_len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *info_log = (char*)malloc(max_len);
|
|
|
|
if (!info_log)
|
|
|
|
return;
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glGetProgramInfoLog(obj, max_len, &info_len, info_log);
|
2011-01-11 21:13:55 +00:00
|
|
|
|
|
|
|
if (info_len > 0)
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("Linker log: %s\n", info_log);
|
2011-12-24 12:46:12 +00:00
|
|
|
|
|
|
|
free(info_log);
|
2011-01-11 21:13:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 20:38:45 +00:00
|
|
|
static bool compile_shader(GLuint shader, const char *define, const char *program)
|
2011-06-02 11:49:22 +00:00
|
|
|
{
|
2013-06-30 20:58:22 +00:00
|
|
|
char version[32] = {0};
|
2013-07-05 17:58:15 +00:00
|
|
|
if (glsl_core && !strstr(program, "#version"))
|
2013-06-30 20:58:22 +00:00
|
|
|
{
|
|
|
|
unsigned version_no = 0;
|
|
|
|
unsigned gl_ver = glsl_major * 100 + glsl_minor * 10;
|
|
|
|
switch (gl_ver)
|
|
|
|
{
|
|
|
|
case 300: version_no = 130; break;
|
|
|
|
case 310: version_no = 140; break;
|
|
|
|
case 320: version_no = 150; break;
|
|
|
|
default: version_no = gl_ver; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(version, sizeof(version), "#version %u\n", version_no);
|
|
|
|
RARCH_LOG("[GL]: Using GLSL version %u.\n", version_no);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *source[] = { version, define, program };
|
2013-07-05 16:15:42 +00:00
|
|
|
glShaderSource(shader, ARRAY_SIZE(source), source, NULL);
|
|
|
|
glCompileShader(shader);
|
2011-06-02 11:49:22 +00:00
|
|
|
|
|
|
|
GLint status;
|
2013-07-05 16:15:42 +00:00
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
2011-06-02 11:49:22 +00:00
|
|
|
print_shader_log(shader);
|
|
|
|
|
|
|
|
return status == GL_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-06-07 13:58:30 +00:00
|
|
|
static bool link_program(GLuint prog)
|
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glLinkProgram(prog);
|
2011-06-07 13:58:30 +00:00
|
|
|
|
|
|
|
GLint status;
|
2013-07-05 16:15:42 +00:00
|
|
|
glGetProgramiv(prog, GL_LINK_STATUS, &status);
|
2011-06-07 13:58:30 +00:00
|
|
|
print_linker_log(prog);
|
|
|
|
|
|
|
|
if (status == GL_TRUE)
|
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glUseProgram(prog);
|
2011-06-07 13:58:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
static GLuint compile_program(const char *vertex, const char *fragment, unsigned i)
|
2011-01-05 18:07:12 +00:00
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
GLuint prog = glCreateProgram();
|
2013-04-07 10:41:05 +00:00
|
|
|
if (!prog)
|
|
|
|
return 0;
|
2013-01-08 02:46:18 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
if (vertex)
|
2011-03-12 14:30:57 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
RARCH_LOG("Found GLSL vertex shader.\n");
|
2013-07-05 16:15:42 +00:00
|
|
|
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
|
2013-04-10 20:38:45 +00:00
|
|
|
if (!compile_shader(shader, "#define VERTEX\n", vertex))
|
2011-03-30 09:05:56 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
RARCH_ERR("Failed to compile vertex shader #%u\n", i);
|
|
|
|
return false;
|
2011-03-30 09:05:56 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glAttachShader(prog, shader);
|
2013-04-07 10:41:05 +00:00
|
|
|
}
|
2011-03-12 14:30:57 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
if (fragment)
|
|
|
|
{
|
|
|
|
RARCH_LOG("Found GLSL fragment shader.\n");
|
2013-07-05 16:15:42 +00:00
|
|
|
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
|
2013-04-10 20:38:45 +00:00
|
|
|
if (!compile_shader(shader, "#define FRAGMENT\n", fragment))
|
2011-03-12 14:30:57 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
RARCH_ERR("Failed to compile fragment shader #%u\n", i);
|
|
|
|
return false;
|
2011-03-12 14:30:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glAttachShader(prog, shader);
|
2013-04-07 10:41:05 +00:00
|
|
|
}
|
2011-03-12 14:30:57 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
if (vertex || fragment)
|
|
|
|
{
|
|
|
|
RARCH_LOG("Linking GLSL program.\n");
|
|
|
|
if (!link_program(prog))
|
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to link program #%u.\n", i);
|
|
|
|
return 0;
|
2011-03-12 14:30:57 +00:00
|
|
|
}
|
2013-04-07 10:41:05 +00:00
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glUseProgram(prog);
|
2013-04-07 10:41:05 +00:00
|
|
|
GLint location = get_uniform(prog, "Texture");
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform1i(location, 0);
|
|
|
|
glUseProgram(0);
|
2011-03-12 14:30:57 +00:00
|
|
|
}
|
2011-03-29 22:10:16 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
return prog;
|
|
|
|
}
|
|
|
|
|
2013-04-15 21:45:11 +00:00
|
|
|
static bool load_source_path(struct gfx_shader_pass *pass, const char *path)
|
2013-04-10 20:38:45 +00:00
|
|
|
{
|
2013-04-15 21:45:11 +00:00
|
|
|
if (read_file(path, (void**)&pass->source.xml.vertex) <= 0)
|
2013-04-10 20:38:45 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
pass->source.xml.fragment = strdup(pass->source.xml.vertex);
|
|
|
|
return pass->source.xml.fragment && pass->source.xml.vertex;
|
|
|
|
}
|
|
|
|
|
2013-04-15 21:45:11 +00:00
|
|
|
static bool compile_programs(GLuint *gl_prog)
|
2013-04-07 10:41:05 +00:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < glsl_shader->passes; i++)
|
2013-01-08 02:46:18 +00:00
|
|
|
{
|
2013-04-10 20:38:45 +00:00
|
|
|
struct gfx_shader_pass *pass = &glsl_shader->pass[i];
|
|
|
|
|
|
|
|
// If we load from GLSLP (CGP),
|
|
|
|
// load the file here, and pretend
|
|
|
|
// we were really using XML all along.
|
2013-04-15 21:45:11 +00:00
|
|
|
if (*pass->source.cg && !load_source_path(pass, pass->source.cg))
|
2013-04-10 20:38:45 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to load GLSL shader: %s.\n", pass->source.cg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*pass->source.cg = '\0';
|
|
|
|
|
|
|
|
const char *vertex = pass->source.xml.vertex;
|
|
|
|
const char *fragment = pass->source.xml.fragment;
|
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
gl_prog[i] = compile_program(vertex, fragment, i);
|
|
|
|
|
|
|
|
if (!gl_prog[i])
|
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to create GL program #%u.\n", i);
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-08 02:46:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
return true;
|
2011-03-12 14:30:57 +00:00
|
|
|
}
|
2011-02-22 22:43:00 +00:00
|
|
|
|
2011-11-12 17:20:54 +00:00
|
|
|
static void gl_glsl_reset_attrib(void)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < gl_attrib_index; i++)
|
2013-07-05 16:15:42 +00:00
|
|
|
glDisableVertexAttribArray(gl_attribs[i]);
|
2011-11-12 17:20:54 +00:00
|
|
|
gl_attrib_index = 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 08:39:39 +00:00
|
|
|
static void gl_glsl_set_vbo(GLfloat *buffer, size_t *buffer_elems, const GLfloat *data, size_t elems)
|
|
|
|
{
|
|
|
|
if (elems != *buffer_elems || memcmp(data, buffer, elems * sizeof(GLfloat)))
|
|
|
|
{
|
|
|
|
//RARCH_LOG("[GL]: VBO updated with %u elems.\n", (unsigned)elems);
|
|
|
|
memcpy(buffer, data, elems * sizeof(GLfloat));
|
2013-07-05 16:15:42 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), data, GL_STATIC_DRAW);
|
2013-05-21 08:39:39 +00:00
|
|
|
*buffer_elems = elems;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gl_glsl_set_attribs(GLuint vbo, GLfloat *buffer, size_t *buffer_elems,
|
|
|
|
const GLfloat *data, size_t elems, const struct glsl_attrib *attrs, size_t num_attrs)
|
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
2013-05-21 08:39:39 +00:00
|
|
|
|
|
|
|
gl_glsl_set_vbo(buffer, buffer_elems, data, elems);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_attrs; i++)
|
|
|
|
{
|
|
|
|
GLint loc = attrs[i].loc;
|
2013-07-05 16:15:42 +00:00
|
|
|
glEnableVertexAttribArray(loc);
|
2013-05-21 08:39:39 +00:00
|
|
|
gl_attribs[gl_attrib_index++] = loc;
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glVertexAttribPointer(loc, attrs[i].size, GL_FLOAT, GL_FALSE, 0,
|
2013-05-21 08:39:39 +00:00
|
|
|
(const GLvoid*)(uintptr_t)attrs[i].offset);
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2013-05-21 08:39:39 +00:00
|
|
|
}
|
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
static void find_uniforms_frame(GLuint prog, struct shader_uniforms_frame *frame, const char *base)
|
|
|
|
{
|
|
|
|
char texture[64];
|
|
|
|
char texture_size[64];
|
|
|
|
char input_size[64];
|
|
|
|
char tex_coord[64];
|
|
|
|
|
|
|
|
snprintf(texture, sizeof(texture), "%s%s", base, "Texture");
|
|
|
|
snprintf(texture_size, sizeof(texture_size), "%s%s", base, "TextureSize");
|
|
|
|
snprintf(input_size, sizeof(input_size), "%s%s", base, "InputSize");
|
|
|
|
snprintf(tex_coord, sizeof(tex_coord), "%s%s", base, "TexCoord");
|
|
|
|
|
2013-04-02 21:14:14 +00:00
|
|
|
frame->texture = get_uniform(prog, texture);
|
|
|
|
frame->texture_size = get_uniform(prog, texture_size);
|
|
|
|
frame->input_size = get_uniform(prog, input_size);
|
|
|
|
frame->tex_coord = get_attrib(prog, tex_coord);
|
2012-10-21 00:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void find_uniforms(GLuint prog, struct shader_uniforms *uni)
|
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glUseProgram(prog);
|
2012-10-21 00:58:51 +00:00
|
|
|
|
2013-04-02 21:14:14 +00:00
|
|
|
uni->mvp = get_uniform(prog, "MVPMatrix");
|
|
|
|
uni->tex_coord = get_attrib(prog, "TexCoord");
|
|
|
|
uni->vertex_coord = get_attrib(prog, "VertexCoord");
|
|
|
|
uni->color = get_attrib(prog, "Color");
|
|
|
|
uni->lut_tex_coord = get_attrib(prog, "LUTTexCoord");
|
2012-10-21 00:58:51 +00:00
|
|
|
|
2013-04-02 21:14:14 +00:00
|
|
|
uni->input_size = get_uniform(prog, "InputSize");
|
|
|
|
uni->output_size = get_uniform(prog, "OutputSize");
|
|
|
|
uni->texture_size = get_uniform(prog, "TextureSize");
|
2012-10-21 00:58:51 +00:00
|
|
|
|
2013-04-02 21:14:14 +00:00
|
|
|
uni->frame_count = get_uniform(prog, "FrameCount");
|
|
|
|
uni->frame_direction = get_uniform(prog, "FrameDirection");
|
2012-10-21 00:58:51 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
for (unsigned i = 0; i < glsl_shader->luts; i++)
|
2013-07-05 16:15:42 +00:00
|
|
|
uni->lut_texture[i] = glGetUniformLocation(prog, glsl_shader->lut[i].id);
|
2012-10-21 00:58:51 +00:00
|
|
|
|
2013-04-02 21:14:14 +00:00
|
|
|
find_uniforms_frame(prog, &uni->orig, "Orig");
|
2012-10-21 00:58:51 +00:00
|
|
|
|
|
|
|
char frame_base[64];
|
2013-04-07 10:41:05 +00:00
|
|
|
for (unsigned i = 0; i < GFX_MAX_SHADERS; i++)
|
2012-10-21 00:58:51 +00:00
|
|
|
{
|
2013-04-02 21:14:14 +00:00
|
|
|
snprintf(frame_base, sizeof(frame_base), "Pass%u", i + 1);
|
2012-10-21 00:58:51 +00:00
|
|
|
find_uniforms_frame(prog, &uni->pass[i], frame_base);
|
|
|
|
}
|
|
|
|
|
2013-04-02 21:14:14 +00:00
|
|
|
find_uniforms_frame(prog, &uni->prev[0], "Prev");
|
2012-10-21 00:58:51 +00:00
|
|
|
for (unsigned i = 1; i < PREV_TEXTURES; i++)
|
|
|
|
{
|
2013-04-02 21:14:14 +00:00
|
|
|
snprintf(frame_base, sizeof(frame_base), "Prev%u", i);
|
2012-10-21 00:58:51 +00:00
|
|
|
find_uniforms_frame(prog, &uni->prev[i], frame_base);
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glUseProgram(0);
|
2012-10-21 00:58:51 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 02:46:18 +00:00
|
|
|
static void gl_glsl_delete_shader(GLuint prog)
|
|
|
|
{
|
|
|
|
GLsizei count;
|
|
|
|
GLuint shaders[2] = {0};
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glGetAttachedShaders(prog, 2, &count, shaders);
|
2013-01-08 02:46:18 +00:00
|
|
|
for (GLsizei i = 0; i < count; i++)
|
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glDetachShader(prog, shaders[i]);
|
|
|
|
glDeleteShader(shaders[i]);
|
2013-01-08 02:46:18 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glDeleteProgram(prog);
|
2012-09-12 07:30:36 +00:00
|
|
|
}
|
2011-03-30 09:05:56 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
static void gl_glsl_free_shader(void)
|
|
|
|
{
|
|
|
|
if (!glsl_shader)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < glsl_shader->passes; i++)
|
|
|
|
{
|
|
|
|
free(glsl_shader->pass[i].source.xml.vertex);
|
|
|
|
free(glsl_shader->pass[i].source.xml.fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(glsl_shader->script);
|
|
|
|
free(glsl_shader);
|
|
|
|
glsl_shader = NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-05 08:42:39 +00:00
|
|
|
static void gl_glsl_deinit(void)
|
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glUseProgram(0);
|
2013-06-05 08:42:39 +00:00
|
|
|
for (unsigned i = 0; i < GFX_MAX_SHADERS; i++)
|
|
|
|
{
|
|
|
|
if (gl_program[i] == 0 || (i && gl_program[i] == gl_program[0]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
gl_glsl_delete_shader(gl_program[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glsl_shader && glsl_shader->luts)
|
|
|
|
glDeleteTextures(glsl_shader->luts, gl_teximage);
|
|
|
|
|
|
|
|
memset(gl_program, 0, sizeof(gl_program));
|
|
|
|
glsl_enable = false;
|
|
|
|
active_index = 0;
|
|
|
|
|
|
|
|
gl_glsl_free_shader();
|
|
|
|
|
|
|
|
if (gl_state_tracker)
|
|
|
|
state_tracker_free(gl_state_tracker);
|
|
|
|
gl_state_tracker = NULL;
|
|
|
|
|
|
|
|
gl_glsl_reset_attrib();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < GFX_MAX_SHADERS; i++)
|
|
|
|
{
|
|
|
|
if (glsl_vbo[i].vbo_primary)
|
2013-07-05 16:15:42 +00:00
|
|
|
glDeleteBuffers(1, &glsl_vbo[i].vbo_primary);
|
2013-06-05 08:42:39 +00:00
|
|
|
if (glsl_vbo[i].vbo_secondary)
|
2013-07-05 16:15:42 +00:00
|
|
|
glDeleteBuffers(1, &glsl_vbo[i].vbo_secondary);
|
2013-06-05 08:42:39 +00:00
|
|
|
}
|
|
|
|
memset(&glsl_vbo, 0, sizeof(glsl_vbo));
|
|
|
|
}
|
|
|
|
|
2013-04-12 20:07:17 +00:00
|
|
|
static bool gl_glsl_init(const char *path)
|
2011-03-12 14:30:57 +00:00
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
#ifndef HAVE_OPENGLES2
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("Checking GLSL shader support ...\n");
|
2013-07-05 16:15:42 +00:00
|
|
|
bool shader_support = glCreateProgram && glUseProgram && glCreateShader
|
|
|
|
&& glDeleteShader && glShaderSource && glCompileShader && glAttachShader
|
|
|
|
&& glDetachShader && glLinkProgram && glGetUniformLocation
|
|
|
|
&& glUniform1i && glUniform1f && glUniform2fv && glUniform4fv && glUniformMatrix4fv
|
|
|
|
&& glGetShaderiv && glGetShaderInfoLog && glGetProgramiv && glGetProgramInfoLog
|
|
|
|
&& glDeleteProgram && glGetAttachedShaders
|
|
|
|
&& glGetAttribLocation && glEnableVertexAttribArray && glDisableVertexAttribArray
|
|
|
|
&& glVertexAttribPointer
|
|
|
|
&& glGenBuffers && glBufferData && glDeleteBuffers && glBindBuffer;
|
2011-01-05 18:07:12 +00:00
|
|
|
|
|
|
|
if (!shader_support)
|
|
|
|
{
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_ERR("GLSL shaders aren't supported by your OpenGL driver.\n");
|
2011-01-05 18:07:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-15 13:17:34 +00:00
|
|
|
#endif
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
glsl_shader = (struct gfx_shader*)calloc(1, sizeof(*glsl_shader));
|
|
|
|
if (!glsl_shader)
|
|
|
|
return false;
|
2012-09-15 13:37:08 +00:00
|
|
|
|
2013-04-07 10:51:26 +00:00
|
|
|
if (path)
|
2013-04-07 10:41:05 +00:00
|
|
|
{
|
2013-04-10 20:38:45 +00:00
|
|
|
bool ret;
|
|
|
|
if (strcmp(path_get_extension(path), "glsl") == 0)
|
|
|
|
{
|
|
|
|
strlcpy(glsl_shader->pass[0].source.cg, path, sizeof(glsl_shader->pass[0].source.cg));
|
|
|
|
glsl_shader->passes = 1;
|
|
|
|
glsl_shader->modern = true;
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else if (strcmp(path_get_extension(path), "glslp") == 0)
|
|
|
|
{
|
|
|
|
config_file_t *conf = config_file_new(path);
|
|
|
|
if (conf)
|
|
|
|
{
|
|
|
|
ret = gfx_shader_read_conf_cgp(conf, glsl_shader);
|
|
|
|
glsl_shader->modern = true;
|
|
|
|
config_file_free(conf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = gfx_shader_read_xml(path, glsl_shader);
|
|
|
|
|
|
|
|
if (!ret)
|
2013-04-07 10:51:26 +00:00
|
|
|
{
|
2013-04-10 20:38:45 +00:00
|
|
|
RARCH_ERR("[GL]: Failed to parse GLSL shader.\n");
|
2013-04-07 10:51:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-12 14:30:57 +00:00
|
|
|
}
|
2012-09-15 13:37:08 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
RARCH_WARN("[GL]: Stock GLSL shaders will be used.\n");
|
2013-04-07 10:41:05 +00:00
|
|
|
glsl_shader->passes = 1;
|
2013-06-30 20:58:22 +00:00
|
|
|
glsl_shader->pass[0].source.xml.vertex = strdup(glsl_core ? stock_vertex_core : stock_vertex_modern);
|
|
|
|
glsl_shader->pass[0].source.xml.fragment = strdup(glsl_core ? stock_fragment_core : stock_fragment_modern);
|
2013-04-07 10:41:05 +00:00
|
|
|
glsl_shader->modern = true;
|
2012-09-15 13:37:08 +00:00
|
|
|
}
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2013-04-15 21:45:11 +00:00
|
|
|
gfx_shader_resolve_relative(glsl_shader, path);
|
|
|
|
|
2013-06-05 08:42:39 +00:00
|
|
|
const char *stock_vertex = glsl_shader->modern ?
|
|
|
|
stock_vertex_modern : stock_vertex_legacy;
|
|
|
|
const char *stock_fragment = glsl_shader->modern ?
|
|
|
|
stock_fragment_modern : stock_fragment_legacy;
|
|
|
|
|
2012-09-15 13:46:02 +00:00
|
|
|
#ifdef HAVE_OPENGLES2
|
2013-04-07 10:41:05 +00:00
|
|
|
if (!glsl_shader->modern)
|
2012-09-15 13:46:02 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("[GL]: GLES context is used, but shader is not modern. Cannot use it.\n");
|
2013-06-05 08:42:39 +00:00
|
|
|
goto error;
|
2012-09-15 13:46:02 +00:00
|
|
|
}
|
2013-06-30 20:58:22 +00:00
|
|
|
#else
|
|
|
|
if (glsl_core && !glsl_shader->modern)
|
|
|
|
{
|
|
|
|
RARCH_ERR("[GL]: GL core context is used, but shader is not core compatible. Cannot use it.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2012-09-15 13:46:02 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
if (!(gl_program[0] = compile_program(stock_vertex, stock_fragment, 0)))
|
2012-09-11 21:32:00 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("GLSL stock programs failed to compile.\n");
|
2013-06-05 08:42:39 +00:00
|
|
|
goto error;
|
2012-09-11 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
2013-04-15 21:45:11 +00:00
|
|
|
if (!compile_programs(&gl_program[1]))
|
2013-06-05 08:42:39 +00:00
|
|
|
goto error;
|
2011-03-14 20:28:30 +00:00
|
|
|
|
2013-04-15 21:45:11 +00:00
|
|
|
if (!load_luts())
|
2013-04-07 10:41:05 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("[GL]: Failed to load LUTs.\n");
|
2013-06-05 08:42:39 +00:00
|
|
|
goto error;
|
2013-04-07 10:41:05 +00:00
|
|
|
}
|
2011-03-06 18:56:35 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
for (unsigned i = 0; i <= glsl_shader->passes; i++)
|
2012-10-21 00:58:51 +00:00
|
|
|
find_uniforms(gl_program[i], &gl_uniforms[i]);
|
|
|
|
|
2012-10-16 13:38:25 +00:00
|
|
|
#ifdef GLSL_DEBUG
|
|
|
|
if (!gl_check_error())
|
2012-10-21 00:58:51 +00:00
|
|
|
RARCH_WARN("Detected GL error in GLSL.\n");
|
2012-10-16 13:38:25 +00:00
|
|
|
#endif
|
2011-05-25 13:58:12 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
if (glsl_shader->variables)
|
2011-05-25 13:58:12 +00:00
|
|
|
{
|
2012-04-07 10:17:40 +00:00
|
|
|
struct state_tracker_info info = {0};
|
2012-09-11 21:32:00 +00:00
|
|
|
info.wram = (uint8_t*)pretro_get_memory_data(RETRO_MEMORY_SYSTEM_RAM);
|
2013-04-07 10:41:05 +00:00
|
|
|
info.info = glsl_shader->variable;
|
|
|
|
info.info_elem = glsl_shader->variables;
|
2011-06-07 13:33:29 +00:00
|
|
|
|
2011-06-06 16:50:36 +00:00
|
|
|
#ifdef HAVE_PYTHON
|
2013-04-07 10:41:05 +00:00
|
|
|
info.script = glsl_shader->script;
|
|
|
|
info.script_class = *glsl_shader->script_class ?
|
|
|
|
glsl_shader->script_class : NULL;
|
2011-06-06 16:50:36 +00:00
|
|
|
#endif
|
2011-06-07 13:33:29 +00:00
|
|
|
|
2012-04-07 10:17:40 +00:00
|
|
|
gl_state_tracker = state_tracker_init(&info);
|
|
|
|
if (!gl_state_tracker)
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_WARN("Failed to init state tracker.\n");
|
2011-05-25 13:58:12 +00:00
|
|
|
}
|
2011-01-11 21:13:55 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
glsl_enable = true;
|
|
|
|
gl_program[glsl_shader->passes + 1] = gl_program[0];
|
|
|
|
gl_uniforms[glsl_shader->passes + 1] = gl_uniforms[0];
|
2011-11-12 17:20:54 +00:00
|
|
|
|
2013-04-20 13:46:50 +00:00
|
|
|
if (glsl_shader->modern)
|
|
|
|
{
|
2013-07-05 17:29:48 +00:00
|
|
|
#ifdef HAVE_OPENGLES2 // Avoid compiler warnings.
|
|
|
|
gl_program[GL_SHADER_STOCK_BLEND] = compile_program(stock_vertex_modern_blend,
|
|
|
|
stock_fragment_modern_blend, GL_SHADER_STOCK_BLEND);
|
|
|
|
#else
|
2013-06-30 20:58:22 +00:00
|
|
|
gl_program[GL_SHADER_STOCK_BLEND] = compile_program(glsl_core ? stock_vertex_core_blend : stock_vertex_modern_blend,
|
|
|
|
glsl_core ? stock_fragment_modern_blend : stock_fragment_modern_blend, GL_SHADER_STOCK_BLEND);
|
2013-07-05 17:29:48 +00:00
|
|
|
#endif
|
2013-04-20 13:46:50 +00:00
|
|
|
|
|
|
|
find_uniforms(gl_program[GL_SHADER_STOCK_BLEND], &gl_uniforms[GL_SHADER_STOCK_BLEND]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gl_program[GL_SHADER_STOCK_BLEND] = gl_program[0];
|
|
|
|
gl_uniforms[GL_SHADER_STOCK_BLEND] = gl_uniforms[0];
|
|
|
|
}
|
|
|
|
|
2011-11-12 17:20:54 +00:00
|
|
|
gl_glsl_reset_attrib();
|
|
|
|
|
2013-05-21 08:39:39 +00:00
|
|
|
for (unsigned i = 0; i < GFX_MAX_SHADERS; i++)
|
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glGenBuffers(1, &glsl_vbo[i].vbo_primary);
|
|
|
|
glGenBuffers(1, &glsl_vbo[i].vbo_secondary);
|
2013-05-21 08:39:39 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 18:07:12 +00:00
|
|
|
return true;
|
|
|
|
|
2013-06-05 08:42:39 +00:00
|
|
|
error:
|
|
|
|
gl_glsl_deinit();
|
|
|
|
return false;
|
2011-03-29 16:59:06 +00:00
|
|
|
}
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2013-04-12 20:07:17 +00:00
|
|
|
static void gl_glsl_set_params(unsigned width, unsigned height,
|
2011-01-05 18:07:12 +00:00
|
|
|
unsigned tex_width, unsigned tex_height,
|
2011-05-11 11:24:59 +00:00
|
|
|
unsigned out_width, unsigned out_height,
|
2011-05-23 15:41:52 +00:00
|
|
|
unsigned frame_count,
|
2011-05-23 17:57:52 +00:00
|
|
|
const struct gl_tex_info *info,
|
2011-07-03 13:39:35 +00:00
|
|
|
const struct gl_tex_info *prev_info,
|
2011-05-23 17:57:52 +00:00
|
|
|
const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt)
|
2011-01-05 18:07:12 +00:00
|
|
|
{
|
2011-07-03 13:39:35 +00:00
|
|
|
// We enforce a certain layout for our various texture types in the texunits.
|
2013-04-07 10:41:05 +00:00
|
|
|
// - Regular frame (Texture) (always bound).
|
2011-11-12 13:18:10 +00:00
|
|
|
// - LUT textures (always bound).
|
|
|
|
// - Original texture (always bound if meaningful).
|
|
|
|
// - FBO textures (always bound if available).
|
|
|
|
// - Previous textures.
|
2011-07-03 13:39:35 +00:00
|
|
|
|
2011-11-12 13:18:10 +00:00
|
|
|
if (!glsl_enable || (gl_program[active_index] == 0))
|
|
|
|
return;
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2013-05-21 08:39:39 +00:00
|
|
|
GLfloat buffer[128];
|
|
|
|
size_t size = 0;
|
|
|
|
struct glsl_attrib attribs[32];
|
|
|
|
size_t attribs_size = 0;
|
|
|
|
struct glsl_attrib *attr = attribs;
|
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
const struct shader_uniforms *uni = &gl_uniforms[active_index];
|
|
|
|
|
|
|
|
float input_size[2] = {(float)width, (float)height};
|
|
|
|
float output_size[2] = {(float)out_width, (float)out_height};
|
|
|
|
float texture_size[2] = {(float)tex_width, (float)tex_height};
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->input_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->input_size, 1, input_size);
|
2011-01-05 18:07:12 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->output_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->output_size, 1, output_size);
|
2011-05-11 11:24:59 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->texture_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->texture_size, 1, texture_size);
|
2011-05-18 18:22:27 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
if (uni->frame_count >= 0 && active_index)
|
2013-03-25 13:47:33 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
unsigned modulo = glsl_shader->pass[active_index - 1].frame_count_mod;
|
|
|
|
if (modulo)
|
|
|
|
frame_count %= modulo;
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform1i(uni->frame_count, frame_count);
|
2013-03-25 13:47:33 +00:00
|
|
|
}
|
2011-11-09 22:45:34 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->frame_direction >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform1i(uni->frame_direction, g_extern.frame_is_reverse ? -1 : 1);
|
2011-11-12 13:18:10 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
for (unsigned i = 0; i < glsl_shader->luts; i++)
|
2011-11-12 13:18:10 +00:00
|
|
|
{
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->lut_texture[i] >= 0)
|
2013-04-07 10:41:05 +00:00
|
|
|
{
|
|
|
|
// Have to rebind as HW render could override this.
|
2013-07-05 16:15:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + i + 1);
|
2013-04-07 10:41:05 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl_teximage[i]);
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform1i(uni->lut_texture[i], i + 1);
|
2013-04-07 10:41:05 +00:00
|
|
|
}
|
2011-11-12 13:18:10 +00:00
|
|
|
}
|
2011-05-23 15:41:52 +00:00
|
|
|
|
2013-04-07 10:41:05 +00:00
|
|
|
unsigned texunit = glsl_shader->luts + 1;
|
2011-07-03 13:39:35 +00:00
|
|
|
|
2011-11-12 13:18:10 +00:00
|
|
|
// Set original texture unless we're in first pass (pointless).
|
|
|
|
if (active_index > 1)
|
|
|
|
{
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->orig.texture >= 0)
|
|
|
|
{
|
|
|
|
// Bind original texture.
|
2013-07-05 16:15:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + texunit);
|
|
|
|
glUniform1i(uni->orig.texture, texunit);
|
2012-10-21 00:58:51 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, info->tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
texunit++;
|
2011-11-12 13:18:10 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->orig.texture_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->orig.texture_size, 1, info->tex_size);
|
2011-11-12 13:18:10 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->orig.input_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->orig.input_size, 1, info->input_size);
|
2011-07-03 13:39:35 +00:00
|
|
|
|
|
|
|
// Pass texture coordinates.
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->orig.tex_coord >= 0)
|
2011-07-03 13:39:35 +00:00
|
|
|
{
|
2013-05-21 08:39:39 +00:00
|
|
|
attr->loc = uni->orig.tex_coord;
|
|
|
|
attr->size = 2;
|
|
|
|
attr->offset = size * sizeof(GLfloat);
|
|
|
|
attribs_size++;
|
|
|
|
attr++;
|
|
|
|
|
|
|
|
memcpy(buffer + size, info->coord, 8 * sizeof(GLfloat));
|
|
|
|
size += 8;
|
2011-07-03 13:39:35 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 13:18:10 +00:00
|
|
|
// Bind new texture in the chain.
|
|
|
|
if (fbo_info_cnt > 0)
|
2011-05-23 15:41:52 +00:00
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + texunit + fbo_info_cnt - 1);
|
2011-11-12 13:18:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, fbo_info[fbo_info_cnt - 1].tex);
|
|
|
|
}
|
2011-05-23 15:41:52 +00:00
|
|
|
|
2011-11-12 13:18:10 +00:00
|
|
|
// Bind FBO textures.
|
|
|
|
for (unsigned i = 0; i < fbo_info_cnt; i++)
|
|
|
|
{
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->pass[i].texture)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform1i(uni->pass[i].texture, texunit);
|
2011-05-23 15:41:52 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
texunit++;
|
2011-05-23 15:41:52 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->pass[i].texture_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->pass[i].texture_size, 1, fbo_info[i].tex_size);
|
2011-05-23 17:57:52 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->pass[i].input_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->pass[i].input_size, 1, fbo_info[i].input_size);
|
2011-05-23 17:57:52 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->pass[i].tex_coord >= 0)
|
2011-05-23 17:57:52 +00:00
|
|
|
{
|
2013-05-21 08:39:39 +00:00
|
|
|
attr->loc = uni->pass[i].tex_coord;
|
|
|
|
attr->size = 2;
|
|
|
|
attr->offset = size * sizeof(GLfloat);
|
|
|
|
attribs_size++;
|
|
|
|
attr++;
|
|
|
|
|
|
|
|
memcpy(buffer + size, fbo_info[i].coord, 8 * sizeof(GLfloat));
|
|
|
|
size += 8;
|
2011-05-23 17:57:52 +00:00
|
|
|
}
|
2011-05-23 15:41:52 +00:00
|
|
|
}
|
2011-11-12 13:18:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// First pass, so unbind everything to avoid collitions.
|
|
|
|
// Unbind ORIG.
|
2013-07-05 16:15:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + texunit);
|
2011-11-12 13:18:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
GLuint base_tex = texunit + 1;
|
|
|
|
// Unbind any lurking FBO passes.
|
|
|
|
// Rendering to a texture that is bound to a texture unit
|
|
|
|
// sounds very shaky ... ;)
|
2013-04-07 10:41:05 +00:00
|
|
|
for (unsigned i = 0; i < glsl_shader->passes; i++)
|
2011-05-23 15:41:52 +00:00
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + base_tex + i);
|
2011-05-23 15:41:52 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2011-11-12 13:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-23 17:57:52 +00:00
|
|
|
|
2011-11-12 14:51:37 +00:00
|
|
|
// Set previous textures. Only bind if they're actually used.
|
2011-11-12 13:18:10 +00:00
|
|
|
for (unsigned i = 0; i < PREV_TEXTURES; i++)
|
|
|
|
{
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->prev[i].texture >= 0)
|
2011-11-12 13:18:10 +00:00
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + texunit);
|
2011-11-12 13:18:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, prev_info[i].tex);
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform1i(uni->prev[i].texture, texunit++);
|
2011-05-23 15:41:52 +00:00
|
|
|
}
|
2011-05-23 17:57:52 +00:00
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
texunit++;
|
|
|
|
|
|
|
|
if (uni->prev[i].texture_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->prev[i].texture_size, 1, prev_info[i].tex_size);
|
2012-10-21 00:58:51 +00:00
|
|
|
|
|
|
|
if (uni->prev[i].input_size >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniform2fv(uni->prev[i].input_size, 1, prev_info[i].input_size);
|
2011-05-25 13:58:12 +00:00
|
|
|
|
2011-11-12 13:18:10 +00:00
|
|
|
// Pass texture coordinates.
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->prev[i].tex_coord >= 0)
|
2011-05-25 13:58:12 +00:00
|
|
|
{
|
2013-05-21 08:39:39 +00:00
|
|
|
attr->loc = uni->prev[i].tex_coord;
|
|
|
|
attr->size = 2;
|
|
|
|
attr->offset = size * sizeof(GLfloat);
|
|
|
|
attribs_size++;
|
|
|
|
attr++;
|
|
|
|
|
|
|
|
memcpy(buffer + size, prev_info[i].coord, 8 * sizeof(GLfloat));
|
|
|
|
size += 8;
|
2011-11-12 13:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-08 15:59:29 +00:00
|
|
|
|
2013-05-21 08:39:39 +00:00
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
gl_glsl_set_attribs(glsl_vbo[active_index].vbo_secondary,
|
|
|
|
glsl_vbo[active_index].buffer_secondary,
|
|
|
|
&glsl_vbo[active_index].size_secondary,
|
|
|
|
buffer, size, attribs, attribs_size);
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:15:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2011-11-12 13:18:10 +00:00
|
|
|
|
2012-04-07 10:17:40 +00:00
|
|
|
if (gl_state_tracker)
|
2011-11-12 13:18:10 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
static struct state_tracker_uniform info[GFX_MAX_VARIABLES];
|
2011-11-12 13:18:10 +00:00
|
|
|
static unsigned cnt = 0;
|
|
|
|
|
|
|
|
if (active_index == 1)
|
2013-04-07 10:41:05 +00:00
|
|
|
cnt = state_get_uniform(gl_state_tracker, info, GFX_MAX_VARIABLES, frame_count);
|
2011-11-12 13:18:10 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < cnt; i++)
|
|
|
|
{
|
2013-07-05 16:15:42 +00:00
|
|
|
int location = glGetUniformLocation(gl_program[active_index], info[i].id);
|
|
|
|
glUniform1f(location, info[i].value);
|
2011-05-25 13:58:12 +00:00
|
|
|
}
|
2011-01-05 18:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 20:07:17 +00:00
|
|
|
static bool gl_glsl_set_mvp(const math_matrix *mat)
|
2012-09-11 21:32:00 +00:00
|
|
|
{
|
2013-04-07 10:51:26 +00:00
|
|
|
if (!glsl_enable || !glsl_shader->modern)
|
2012-09-11 21:32:00 +00:00
|
|
|
return false;
|
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
int loc = gl_uniforms[active_index].mvp;
|
2012-09-11 21:32:00 +00:00
|
|
|
if (loc >= 0)
|
2013-07-05 16:15:42 +00:00
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, mat->data);
|
2012-10-21 00:58:51 +00:00
|
|
|
|
2012-09-11 21:32:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-12 20:07:17 +00:00
|
|
|
static bool gl_glsl_set_coords(const struct gl_coords *coords)
|
2012-08-09 20:33:32 +00:00
|
|
|
{
|
2013-04-07 10:51:26 +00:00
|
|
|
if (!glsl_enable || !glsl_shader->modern)
|
2012-09-11 21:32:00 +00:00
|
|
|
return false;
|
|
|
|
|
2013-05-21 08:39:39 +00:00
|
|
|
GLfloat buffer[128];
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
struct glsl_attrib attribs[4];
|
|
|
|
size_t attribs_size = 0;
|
|
|
|
struct glsl_attrib *attr = attribs;
|
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
const struct shader_uniforms *uni = &gl_uniforms[active_index];
|
|
|
|
if (uni->tex_coord >= 0)
|
2012-09-11 21:32:00 +00:00
|
|
|
{
|
2013-05-21 08:39:39 +00:00
|
|
|
attr->loc = uni->tex_coord;
|
|
|
|
attr->size = 2;
|
|
|
|
attr->offset = size * sizeof(GLfloat);
|
|
|
|
attribs_size++;
|
|
|
|
attr++;
|
|
|
|
|
|
|
|
memcpy(buffer + size, coords->tex_coord, 8 * sizeof(GLfloat));
|
|
|
|
size += 8;
|
2012-09-11 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->vertex_coord >= 0)
|
2012-09-11 21:32:00 +00:00
|
|
|
{
|
2013-05-21 08:39:39 +00:00
|
|
|
attr->loc = uni->vertex_coord;
|
|
|
|
attr->size = 2;
|
|
|
|
attr->offset = size * sizeof(GLfloat);
|
|
|
|
attribs_size++;
|
|
|
|
attr++;
|
|
|
|
|
|
|
|
memcpy(buffer + size, coords->vertex, 8 * sizeof(GLfloat));
|
|
|
|
size += 8;
|
2012-09-11 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->color >= 0)
|
2012-09-11 21:32:00 +00:00
|
|
|
{
|
2013-05-21 08:39:39 +00:00
|
|
|
attr->loc = uni->color;
|
|
|
|
attr->size = 4;
|
|
|
|
attr->offset = size * sizeof(GLfloat);
|
|
|
|
attribs_size++;
|
|
|
|
attr++;
|
|
|
|
|
|
|
|
memcpy(buffer + size, coords->color, 16 * sizeof(GLfloat));
|
|
|
|
size += 16;
|
2012-09-11 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-21 00:58:51 +00:00
|
|
|
if (uni->lut_tex_coord >= 0)
|
2012-09-11 21:32:00 +00:00
|
|
|
{
|
2013-05-21 08:39:39 +00:00
|
|
|
attr->loc = uni->lut_tex_coord;
|
|
|
|
attr->size = 2;
|
|
|
|
attr->offset = size * sizeof(GLfloat);
|
|
|
|
attribs_size++;
|
|
|
|
attr++;
|
|
|
|
|
|
|
|
memcpy(buffer + size, coords->lut_tex_coord, 8 * sizeof(GLfloat));
|
|
|
|
size += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
gl_glsl_set_attribs(glsl_vbo[active_index].vbo_primary,
|
|
|
|
glsl_vbo[active_index].buffer_primary,
|
|
|
|
&glsl_vbo[active_index].size_primary,
|
|
|
|
buffer, size,
|
|
|
|
attribs, attribs_size);
|
2012-09-11 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-08-09 20:33:32 +00:00
|
|
|
}
|
2011-01-22 23:48:47 +00:00
|
|
|
|
2013-04-12 20:07:17 +00:00
|
|
|
static void gl_glsl_use(unsigned index)
|
2011-01-22 23:48:47 +00:00
|
|
|
{
|
|
|
|
if (glsl_enable)
|
2011-03-06 18:56:35 +00:00
|
|
|
{
|
2011-11-12 17:20:54 +00:00
|
|
|
gl_glsl_reset_attrib();
|
|
|
|
|
2011-03-06 18:56:35 +00:00
|
|
|
active_index = index;
|
2013-07-05 16:15:42 +00:00
|
|
|
glUseProgram(gl_program[index]);
|
2011-03-06 18:56:35 +00:00
|
|
|
}
|
2011-01-22 23:48:47 +00:00
|
|
|
}
|
2011-03-12 14:30:57 +00:00
|
|
|
|
2013-04-12 20:07:17 +00:00
|
|
|
static unsigned gl_glsl_num(void)
|
2011-03-12 14:30:57 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
return glsl_shader->passes;
|
2011-03-12 14:30:57 +00:00
|
|
|
}
|
2011-03-14 20:28:30 +00:00
|
|
|
|
2013-04-12 20:07:17 +00:00
|
|
|
static bool gl_glsl_filter_type(unsigned index, bool *smooth)
|
2011-03-14 20:28:30 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
if (glsl_enable && index)
|
2011-03-14 20:28:30 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
if (glsl_shader->pass[index - 1].filter == RARCH_FILTER_UNSPEC)
|
2011-03-14 20:28:30 +00:00
|
|
|
return false;
|
2013-04-07 10:41:05 +00:00
|
|
|
*smooth = glsl_shader->pass[index - 1].filter == RARCH_FILTER_LINEAR;
|
|
|
|
return true;
|
2011-03-14 20:28:30 +00:00
|
|
|
}
|
2013-04-07 10:41:05 +00:00
|
|
|
else
|
|
|
|
return false;
|
2011-03-14 20:28:30 +00:00
|
|
|
}
|
2011-03-14 21:09:35 +00:00
|
|
|
|
2013-04-12 20:07:17 +00:00
|
|
|
static void gl_glsl_shader_scale(unsigned index, struct gfx_fbo_scale *scale)
|
2011-03-14 21:09:35 +00:00
|
|
|
{
|
2013-04-07 10:41:05 +00:00
|
|
|
if (glsl_enable && index)
|
|
|
|
*scale = glsl_shader->pass[index - 1].fbo;
|
2011-03-14 21:51:03 +00:00
|
|
|
else
|
|
|
|
scale->valid = false;
|
2011-03-14 21:09:35 +00:00
|
|
|
}
|
2012-09-24 23:26:22 +00:00
|
|
|
|
|
|
|
void gl_glsl_set_get_proc_address(gfx_ctx_proc_t (*proc)(const char*))
|
|
|
|
{
|
|
|
|
glsl_get_proc_address = proc;
|
|
|
|
}
|
|
|
|
|
2013-06-30 20:58:22 +00:00
|
|
|
void gl_glsl_set_context_type(bool core_profile, unsigned major, unsigned minor)
|
|
|
|
{
|
|
|
|
glsl_core = core_profile;
|
|
|
|
glsl_major = major;
|
|
|
|
glsl_minor = minor;
|
|
|
|
}
|
|
|
|
|
2012-11-14 21:51:08 +00:00
|
|
|
const gl_shader_backend_t gl_glsl_backend = {
|
|
|
|
gl_glsl_init,
|
|
|
|
gl_glsl_deinit,
|
|
|
|
gl_glsl_set_params,
|
|
|
|
gl_glsl_use,
|
|
|
|
gl_glsl_num,
|
|
|
|
gl_glsl_filter_type,
|
|
|
|
gl_glsl_shader_scale,
|
|
|
|
gl_glsl_set_coords,
|
|
|
|
gl_glsl_set_mvp,
|
2013-01-08 02:46:18 +00:00
|
|
|
|
|
|
|
RARCH_SHADER_GLSL,
|
2012-11-14 21:51:08 +00:00
|
|
|
};
|
|
|
|
|