snes9x/shaders/glsl.cpp

1283 lines
40 KiB
C++
Raw Normal View History

/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
This file is licensed under the Snes9x License.
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/
2019-01-09 02:43:51 +01:00
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
2018-05-11 01:47:55 +02:00
#include "glsl.h"
#include "../../conffile.h"
#include "shader_helpers.h"
2018-05-14 01:22:54 +02:00
#include "shader_platform.h"
2018-05-11 01:47:55 +02:00
static const GLfloat tex_coords[16] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f };
2018-05-11 01:47:55 +02:00
static const GLfloat mvp_ortho[16] = { 2.0f, 0.0f, 0.0f, 0.0f,
2019-01-09 00:18:17 +01:00
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f };
2018-05-11 01:47:55 +02:00
static int scale_string_to_enum(const char *string)
2018-05-11 01:47:55 +02:00
{
if (!strcasecmp(string, "source"))
{
return GLSL_SOURCE;
}
else if (!strcasecmp(string, "viewport"))
{
return GLSL_VIEWPORT;
}
else if (!strcasecmp(string, "absolute"))
{
return GLSL_ABSOLUTE;
}
else
return GLSL_NONE;
2018-05-11 01:47:55 +02:00
}
2019-01-09 00:18:17 +01:00
static const char *scale_enum_to_string(int val)
2018-05-12 22:07:07 +02:00
{
switch (val)
{
case GLSL_SOURCE:
return "source";
case GLSL_VIEWPORT:
return "viewport";
case GLSL_ABSOLUTE:
return "absolute";
default:
return "undefined";
2018-05-12 22:07:07 +02:00
}
}
static int wrap_mode_string_to_enum(const char *string)
{
if (!strcasecmp(string, "repeat"))
{
return GL_REPEAT;
}
else if (!strcasecmp(string, "clamp_to_edge"))
{
return GL_CLAMP_TO_EDGE;
}
else if (!strcasecmp(string, "clamp"))
{
return GL_CLAMP;
}
else
return GL_CLAMP_TO_BORDER;
}
2018-05-12 22:07:07 +02:00
static const char *wrap_mode_enum_to_string(int val)
{
switch (val)
{
case GL_REPEAT:
return "repeat";
case GL_CLAMP_TO_EDGE:
return "clamp_to_edge";
case GL_CLAMP:
return "clamp";
default:
return "clamp_to_border";
}
}
2019-01-09 02:43:51 +01:00
bool GLSLShader::load_shader_preset_file(char *filename)
2018-05-11 01:47:55 +02:00
{
char key[256];
2019-01-25 20:28:24 +01:00
int length = strlen(filename);
bool singlepass = false;
2018-05-11 01:47:55 +02:00
2019-01-25 20:28:24 +01:00
if (length > 6 && (!strcasecmp(&filename[length - 5], ".glsl") ||
!strcasecmp(&filename[length - 6], ".slang")))
singlepass = true;
this->using_slang = false;
2019-01-25 20:28:24 +01:00
if (length > 7 && (!strcasecmp(&filename[length - 6], ".slang") ||
!strcasecmp(&filename[length - 7], ".slangp")))
{
#ifdef USE_SLANG
this->using_slang = true;
#else
return false;
#endif
}
if (singlepass)
2018-05-11 01:47:55 +02:00
{
GLSLPass pass;
2019-01-25 20:28:24 +01:00
this->pass.push_back(GLSLPass());
2018-05-11 01:57:52 +02:00
pass.scale_type_x = pass.scale_type_y = GLSL_VIEWPORT;
2018-05-11 01:47:55 +02:00
pass.filter = GLSL_UNDEFINED;
2018-05-20 19:49:58 +02:00
pass.wrap_mode = GL_CLAMP_TO_BORDER;
2018-05-11 01:47:55 +02:00
strcpy(pass.filename, filename);
2018-05-11 01:57:52 +02:00
pass.frame_count_mod = 0;
pass.frame_count = 0;
pass.fp = 0;
pass.scale_x = 1.0;
pass.scale_y = 1.0;
2019-01-25 20:28:24 +01:00
this->pass.push_back(pass);
2018-05-11 01:57:52 +02:00
return true;
2018-05-11 01:47:55 +02:00
}
else
{
conf.LoadFile(filename);
}
int shader_count = conf.GetInt("::shaders", 0);
if (shader_count < 1)
return false;
2019-01-09 00:18:17 +01:00
this->pass.push_back(GLSLPass());
2018-05-11 01:47:55 +02:00
for (int i = 0; i < shader_count; i++)
{
GLSLPass pass;
snprintf(key, 256, "::filter_linear%u", i);
2018-05-13 00:36:45 +02:00
pass.filter = conf.Exists(key) ? (conf.GetBool(key) ? GL_LINEAR : GL_NEAREST) : GLSL_UNDEFINED;
2018-05-11 01:47:55 +02:00
sprintf(key, "::scale_type%u", i);
const char* scaleType = conf.GetString(key, "");
2018-05-11 01:47:55 +02:00
if (!strcasecmp(scaleType, ""))
2018-05-11 01:47:55 +02:00
{
sprintf(key, "::scale_type_x%u", i);
const char* scaleTypeX = conf.GetString(key, "");
pass.scale_type_x = scale_string_to_enum(scaleTypeX);
2018-05-11 01:47:55 +02:00
sprintf(key, "::scale_type_y%u", i);
const char* scaleTypeY = conf.GetString(key, "");
pass.scale_type_y = scale_string_to_enum(scaleTypeY);
2018-05-11 01:47:55 +02:00
}
else
{
int scale_type = scale_string_to_enum(scaleType);
2018-05-11 01:47:55 +02:00
pass.scale_type_x = scale_type;
pass.scale_type_y = scale_type;
}
sprintf(key, "::scale%u", i);
const char* scaleFloat = conf.GetString(key, "");
if (!strcasecmp(scaleFloat, ""))
2018-05-11 01:47:55 +02:00
{
sprintf(key, "::scale_x%u", i);
const char* scaleFloatX = conf.GetString(key, "1.0");
pass.scale_x = atof(scaleFloatX);
2018-05-11 01:47:55 +02:00
sprintf(key, "::scale_y%u", i);
const char* scaleFloatY = conf.GetString(key, "1.0");
pass.scale_y = atof(scaleFloatY);
2018-05-11 01:47:55 +02:00
}
else
{
pass.scale_x = pass.scale_y = atof(scaleFloat);
2018-05-11 01:47:55 +02:00
}
sprintf(key, "::shader%u", i);
strcpy(pass.filename, conf.GetString(key, ""));
2018-05-11 01:47:55 +02:00
2018-05-14 21:27:07 +02:00
sprintf(key, "::wrap_mode%u", i);
pass.wrap_mode = wrap_mode_string_to_enum (conf.GetString (key ,""));
2018-05-14 21:27:07 +02:00
2019-01-28 03:14:28 +01:00
sprintf(key, "::mipmap_input%u", i);
pass.mipmap_input = conf.GetBool (key);
2018-05-11 01:47:55 +02:00
sprintf(key, "::frame_count_mod%u", i);
pass.frame_count_mod = conf.GetInt(key, 0);
2019-01-09 00:18:17 +01:00
if (gl_float_texture_available())
{
sprintf(key, "::float_framebuffer%u", i);
pass.fp = conf.GetBool(key);
}
else
pass.fp = false;
2019-01-09 00:18:17 +01:00
if (gl_srgb_available())
{
sprintf(key, "::srgb_framebuffer%u", i);
pass.srgb = conf.GetBool(key);
}
else
pass.srgb = false;
sprintf(key, "::alias%u", i);
strcpy(pass.alias, conf.GetString(key, ""));
2018-05-11 01:47:55 +02:00
this->pass.push_back(pass);
}
char* ids = conf.GetStringDup("::textures", "");
char* id = strtok(ids, ";");
while (id != NULL)
{
GLSLLut lut;
sprintf(key, "::%s", id);
strcpy(lut.id, id);
strcpy(lut.filename, conf.GetString(key, ""));
sprintf(key, "::%s_wrap_mode", id);
lut.wrap_mode = wrap_mode_string_to_enum (conf.GetString (key, ""));
sprintf(key, "::%s_mipmap", id);
lut.mipmap = conf.GetBool (key);
2018-05-11 01:47:55 +02:00
sprintf(key, "::%s_linear", id);
lut.filter = (conf.GetBool(key, false)) ? GL_LINEAR : GL_NEAREST;
if (lut.mipmap)
{
lut.filter = (lut.filter == GL_LINEAR) ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST;
}
2018-05-11 01:47:55 +02:00
this->lut.push_back(lut);
id = strtok(NULL, ";");
}
2018-05-12 22:07:07 +02:00
2018-05-11 01:47:55 +02:00
free(ids);
return true;
}
static std::string folder_from_path(std::string filename)
2018-05-11 01:47:55 +02:00
{
for (int i = filename.length() - 1; i >= 0; i--)
if (filename[i] == '\\' || filename[i] == '/')
return filename.substr(0, i);
return std::string(".");
}
static std::string canonicalize(const std::string &noncanonical)
{
char *temp = realpath(noncanonical.c_str(), NULL);
std::string filename_string(temp);
free(temp);
return filename_string;
}
static GLuint string_to_format(char *format)
{
#define MATCH(s, f) \
if (!strcmp(format, s)) \
return f;
MATCH("R8_UNORM", GL_R8);
MATCH("R8_UINT", GL_R8UI);
MATCH("R8_SINT", GL_R8I);
MATCH("R8G8_UNORM", GL_RG8);
MATCH("R8G8_UINT", GL_RG8UI);
MATCH("R8G8_SINT", GL_RG8I);
MATCH("R8G8B8A8_UNORM", GL_RGBA8);
MATCH("R8G8B8A8_UINT", GL_RGBA8UI);
MATCH("R8G8B8A8_SINT", GL_RGBA8I);
MATCH("R8G8B8A8_SRGB", GL_SRGB8_ALPHA8);
MATCH("R16_UINT", GL_R16UI);
MATCH("R16_SINT", GL_R16I);
MATCH("R16_SFLOAT", GL_R16F);
MATCH("R16G16_UINT", GL_RG16UI);
MATCH("R16G16_SINT", GL_RG16I);
MATCH("R16G16_SFLOAT", GL_RG16F);
MATCH("R16G16B16A16_UINT", GL_RGBA16UI);
MATCH("R16G16B16A16_SINT", GL_RGBA16I);
MATCH("R16G16B16A16_SFLOAT", GL_RGBA16F);
MATCH("R32_UINT", GL_R32UI);
MATCH("R32_SINT", GL_R32I);
MATCH("R32_SFLOAT", GL_R32F);
MATCH("R32G32_UINT", GL_RG32UI);
MATCH("R32G32_SINT", GL_RG32I);
MATCH("R32G32_SFLOAT", GL_RG32F);
MATCH("R32G32B32A32_UINT", GL_RGBA32UI);
MATCH("R32G32B32A32_SINT", GL_RGBA32I);
MATCH("R32G32B32A32_SFLOAT", GL_RGBA32F);
return GL_RGBA;
}
// filename must be canonical
void GLSLShader::read_shader_file_with_includes(std::string filename,
2019-01-30 17:39:18 +01:00
std::vector<std::string> &lines,
int p)
{
2019-01-30 19:53:47 +01:00
std::ifstream ss(filename.c_str());
if (ss.fail())
{
printf ("Couldn't open file \"%s\"\n", filename.c_str());
return;
}
std::string line;
while (std::getline(ss, line, '\n'))
{
if (line.empty())
continue;
if (line.compare(0, 8, "#include") == 0)
{
char tmp[PATH_MAX];
sscanf(line.c_str(), "#include \"%[^\"]\"", tmp);
std::string fullpath = canonicalize(folder_from_path(filename) + "/" + tmp);
read_shader_file_with_includes(fullpath.c_str(), lines, p);
continue;
}
else if (line.compare(0, 17, "#pragma parameter") == 0)
2019-01-09 02:43:51 +01:00
{
GLSLParam par;
sscanf(line.c_str(), "#pragma parameter %s \"%[^\"]\" %f %f %f %f",
2019-01-09 02:43:51 +01:00
par.id, par.name, &par.val, &par.min, &par.max, &par.step);
if (par.step == 0.0f)
par.step = 1.0f;
unsigned int i = 0;
for (; i < param.size(); i++)
{
if (!strcmp(param[i].id, par.id))
break;
}
if (i >= param.size())
param.push_back(par);
continue;
2019-01-09 02:43:51 +01:00
}
#ifdef USE_SLANG
else if (line.compare(0, 12, "#pragma name") == 0)
{
sscanf(line.c_str(), "#pragma name %255s", pass[p].alias);
continue;
}
else if (line.compare(0, 14, "#pragma format") == 0)
{
char format[256];
sscanf(line.c_str(), "#pragma format %255s", format);
pass[p].format = string_to_format(format);
if (pass[p].format == GL_RGBA16F || pass[p].format == GL_RGBA32F)
pass[p].fp = TRUE;
else if (pass[p].format == GL_SRGB8_ALPHA8)
pass[p].srgb = TRUE;
continue;
}
#endif
lines.push_back(line);
}
return;
2018-05-11 01:47:55 +02:00
}
2019-01-09 02:43:51 +01:00
GLuint GLSLShader::compile_shader(std::vector<std::string> &lines,
2018-05-12 00:08:13 +02:00
const char *aliases,
const char *defines,
GLuint type,
GLuint *out)
2018-05-11 01:47:55 +02:00
{
char info_log[1024];
2019-01-09 02:43:51 +01:00
std::string source;
unsigned int first_line = 0;
if (lines.empty())
return 0;
2018-05-11 01:47:55 +02:00
2019-01-09 02:43:51 +01:00
if (lines[0].find("#version") == std::string::npos)
2018-05-11 01:47:55 +02:00
{
2019-01-09 02:43:51 +01:00
int version = gl_version();
if (version >= 33)
version *= 10;
else
version = 150;
source += "#version " + std::to_string(version) + "\n";
2018-05-11 01:47:55 +02:00
}
else
{
2019-01-09 02:43:51 +01:00
source += lines[0] + "\n";
first_line++;
2018-05-11 01:47:55 +02:00
}
2019-01-09 02:43:51 +01:00
source += aliases;
source += defines;
for (unsigned int i = first_line; i < lines.size(); i++)
source += lines[i] + "\n";
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
GLuint shader = glCreateShader(type);
2018-05-11 01:47:55 +02:00
GLint status;
2019-01-09 02:43:51 +01:00
GLint length = source.length();
GLchar *prog = (GLchar *)source.c_str();
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
glShaderSource(shader, 1, &prog, &length);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
2018-05-11 01:47:55 +02:00
2018-05-14 03:17:02 +02:00
info_log[0] = '\0';
2019-01-09 00:18:17 +01:00
glGetShaderInfoLog(shader, 1024, NULL, info_log);
2018-05-14 03:17:02 +02:00
if (*info_log)
2019-01-09 00:18:17 +01:00
printf("%s\n", info_log);
2018-05-11 01:47:55 +02:00
*out = shader;
return status;
}
2019-01-09 00:18:17 +01:00
bool GLSLShader::load_shader(char *filename)
2018-05-11 01:47:55 +02:00
{
char shader_path[PATH_MAX];
char temp[PATH_MAX];
std::string aliases = "";
2018-05-11 01:47:55 +02:00
GLint status;
char log[1024];
if (this->pass.size())
return false;
if (!filename || *filename == '\0')
return false;
strcpy(shader_path, filename);
reduce_to_path(shader_path);
chdir(shader_path);
2019-01-09 02:43:51 +01:00
if (!load_shader_preset_file(filename))
2018-05-11 01:47:55 +02:00
return false;
for (unsigned int i = 1; i < pass.size(); i++)
{
GLSLPass *p = &pass[i];
GLuint vertex_shader = 0, fragment_shader = 0;
realpath(p->filename, temp);
2019-01-09 00:18:17 +01:00
strcpy(p->filename, temp);
2018-05-11 01:47:55 +02:00
2019-01-13 18:39:54 +01:00
std::vector<std::string> lines;
read_shader_file_with_includes(p->filename, lines, i);
2019-01-13 18:39:54 +01:00
2019-01-09 02:43:51 +01:00
if (lines.empty())
2018-05-11 01:47:55 +02:00
{
printf("Couldn't read shader file \"%s\"\n", temp);
2018-05-11 01:47:55 +02:00
return false;
}
2019-01-25 20:28:24 +01:00
#ifdef USE_SLANG
if (using_slang)
2018-05-11 01:47:55 +02:00
{
2019-01-25 20:28:24 +01:00
GLint retval;
retval = slang_compile(lines, "vertex");
if (retval < 0)
{
printf("Vertex shader in \"%s\" failed to compile.\n", p->filename);
2019-01-25 20:28:24 +01:00
return false;
}
vertex_shader = retval;
2018-05-11 01:47:55 +02:00
2019-01-25 20:28:24 +01:00
retval = slang_compile(lines, "fragment");
if (retval < 0)
{
printf ("Fragment shader in \"%s\" failed to compile.\n", p->filename);
2019-01-25 20:28:24 +01:00
return false;
}
fragment_shader = retval;
}
else
#endif
2018-05-11 01:47:55 +02:00
{
2019-01-25 20:28:24 +01:00
if (!compile_shader(lines,
"#define VERTEX\n#define PARAMETER_UNIFORM\n",
aliases.c_str(),
GL_VERTEX_SHADER,
&vertex_shader) || !vertex_shader)
{
printf("Couldn't compile vertex shader in \"%s\".\n", p->filename);
2019-01-25 20:28:24 +01:00
return false;
}
if (!compile_shader(lines,
"#define FRAGMENT\n#define PARAMETER_UNIFORM\n",
aliases.c_str(),
GL_FRAGMENT_SHADER,
&fragment_shader) || !fragment_shader)
{
printf("Couldn't compile fragment shader in \"%s\".\n", p->filename);
2019-01-25 20:28:24 +01:00
return false;
}
2018-05-11 01:47:55 +02:00
}
2019-01-09 00:18:17 +01:00
p->program = glCreateProgram();
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
glAttachShader(p->program, vertex_shader);
glAttachShader(p->program, fragment_shader);
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
glLinkProgram(p->program);
glGetProgramiv(p->program, GL_LINK_STATUS, &status);
2018-05-14 03:17:02 +02:00
log[0] = '\0';
2018-05-11 01:47:55 +02:00
glGetProgramInfoLog(p->program, 1024, NULL, log);
2018-05-14 03:17:02 +02:00
if (*log)
2019-01-09 00:18:17 +01:00
printf("%s\n", log);
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
2018-05-11 01:47:55 +02:00
if (status != GL_TRUE)
{
2019-01-09 00:18:17 +01:00
printf("Failed to link program\n");
glDeleteProgram(p->program);
2018-05-11 01:47:55 +02:00
return false;
}
glGenFramebuffers(1, &p->fbo);
glGenTextures(1, &p->texture);
glBindTexture(GL_TEXTURE_2D, p->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
p->frame_count = 0;
}
for (unsigned int i = 0; i < lut.size(); i++)
{
GLSLLut *l = &lut[i];
2018-05-14 01:39:56 +02:00
// generate texture for the lut and apply specified filter setting
2018-05-11 01:47:55 +02:00
glGenTextures(1, &l->texture);
glBindTexture(GL_TEXTURE_2D, l->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, l->wrap_mode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, l->wrap_mode);
2018-05-11 01:47:55 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, l->filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, l->filter);
realpath(l->filename, temp);
2019-01-09 00:18:17 +01:00
strcpy(l->filename, temp);
2018-05-11 01:47:55 +02:00
// simple file extension png/tga decision
int length = strlen(temp);
if (length > 4)
{
if (!strcasecmp(&temp[length - 4], ".png"))
{
int width, height;
bool hasAlpha;
bool grayscale;
2018-05-11 01:47:55 +02:00
GLubyte* texData;
if (loadPngImage(temp, width, height, grayscale, hasAlpha,
&texData))
2018-05-11 01:47:55 +02:00
{
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
if (grayscale)
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
width,
height,
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
texData);
else
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
width,
height,
0,
hasAlpha ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE,
texData);
2019-01-25 20:28:24 +01:00
l->width = width;
l->height = height;
2018-05-11 01:47:55 +02:00
free(texData);
}
else
{
printf ("Failed to load PNG LUT: %s\n", temp);
2018-05-11 01:47:55 +02:00
}
}
else if (!strcasecmp(&temp[length - 4], ".tga"))
{
STGA stga;
if (loadTGA(temp, stga))
{
glPixelStorei(GL_UNPACK_ROW_LENGTH, stga.width);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
stga.width,
stga.height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
stga.data);
2019-01-25 20:28:24 +01:00
l->width = stga.width;
l->height = stga.height;
2018-05-11 01:47:55 +02:00
}
else
{
2019-01-09 00:18:17 +01:00
printf("Failed to load TGA LUT: %s\n", temp);
2018-05-11 01:47:55 +02:00
}
}
}
if (l->mipmap)
2019-01-09 00:18:17 +01:00
glGenerateMipmap(GL_TEXTURE_2D);
2018-05-11 01:47:55 +02:00
}
2018-05-14 01:39:56 +02:00
// Check for parameters specified in file
2018-05-12 00:08:13 +02:00
for (unsigned int i = 0; i < param.size(); i++)
{
char key[266];
const char *value;
snprintf (key, 266, "::%s", param[i].id);
value = conf.GetString (key, NULL);
if (value)
{
2019-01-09 00:18:17 +01:00
param[i].val = atof(value);
if (param[i].val < param[i].min)
param[i].val = param[i].min;
if (param[i].val > param[i].max)
param[i].val = param[i].max;
2018-05-12 00:08:13 +02:00
}
}
2018-05-11 01:47:55 +02:00
glActiveTexture(GL_TEXTURE0);
2019-01-25 20:28:24 +01:00
#ifdef USE_SLANG
if (using_slang)
slang_introspect();
else
#endif
register_uniforms();
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
prev_frame.resize(max_prev_frame);
2018-05-11 01:47:55 +02:00
for (unsigned int i = 0; i < prev_frame.size(); i++)
{
glGenTextures(1, &(prev_frame[i].texture));
prev_frame[i].width = prev_frame[i].height = 0;
}
glGenBuffers(1, &vbo);
frame_count = 0;
return true;
}
void GLSLShader::render(GLuint &orig,
int width, int height,
int viewport_x, int viewport_y,
int viewport_width, int viewport_height,
GLSLViewportCallback vpcallback)
2018-05-11 01:47:55 +02:00
{
2018-10-22 20:56:18 +02:00
GLint saved_framebuffer;
2019-01-09 00:18:17 +01:00
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &saved_framebuffer);
2018-10-22 20:56:18 +02:00
2018-05-11 01:47:55 +02:00
frame_count++;
2018-05-14 01:39:56 +02:00
// set up our dummy pass for easier loop code
2018-05-11 01:47:55 +02:00
pass[0].texture = orig;
pass[0].width = width;
pass[0].height = height;
#ifdef USE_SLANG
if (using_slang && using_feedback)
for (int i = 1; i < (int)pass.size(); i++)
{
if (!pass[i].uses_feedback)
continue;
GLuint tmp = pass[i].texture;
pass[i].texture = pass[i].feedback_texture;
pass[i].feedback_texture = tmp;
}
#endif
2018-05-14 01:39:56 +02:00
// loop through all real passes
2018-05-11 01:47:55 +02:00
for (unsigned int i = 1; i < pass.size(); i++)
{
2018-05-13 00:36:45 +02:00
bool lastpass = (i == pass.size() - 1);
2018-05-11 01:47:55 +02:00
switch (pass[i].scale_type_x)
{
case GLSL_ABSOLUTE:
2019-02-23 15:00:57 +01:00
pass[i].width = (GLuint)pass[i].scale_x;
2018-05-11 01:47:55 +02:00
break;
case GLSL_SOURCE:
2019-02-23 15:00:57 +01:00
pass[i].width = (GLuint)(pass[i-1].width * pass[i].scale_x);
2018-05-11 01:47:55 +02:00
break;
case GLSL_VIEWPORT:
2019-02-23 15:00:57 +01:00
pass[i].width = (GLuint)(viewport_width * pass[i].scale_x);
2018-05-11 01:47:55 +02:00
break;
default:
if (lastpass)
pass[i].width = viewport_width;
else
2019-02-23 15:00:57 +01:00
pass[i].width = (GLuint)(pass[i - 1].width * pass[i].scale_x);
2018-05-11 01:47:55 +02:00
}
switch (pass[i].scale_type_y)
{
case GLSL_ABSOLUTE:
2019-02-23 15:00:57 +01:00
pass[i].height = (GLuint)pass[i].scale_y;
2018-05-11 01:47:55 +02:00
break;
case GLSL_SOURCE:
2019-02-23 15:00:57 +01:00
pass[i].height = (GLuint)(pass[i - 1].height * pass[i].scale_y);
2018-05-11 01:47:55 +02:00
break;
case GLSL_VIEWPORT:
2019-02-23 15:00:57 +01:00
pass[i].height = (GLuint)(viewport_height * pass[i].scale_y);
2018-05-11 01:47:55 +02:00
break;
default:
if (lastpass)
pass[i].height = viewport_height;
else
2019-02-23 15:00:57 +01:00
pass[i].height = (GLuint)(pass[i - 1].height * pass[i].scale_y);
2018-05-11 01:47:55 +02:00
}
bool direct_lastpass = true;
#ifdef USE_SLANG
if (lastpass && using_feedback && pass[i].scale_type_x != GLSL_NONE &&
pass[i].scale_type_y != GLSL_NONE && pass[i].uses_feedback)
{
direct_lastpass = false;
}
#endif
if (!lastpass || !direct_lastpass)
{
2018-05-14 01:39:56 +02:00
// Output to a framebuffer texture
glBindTexture(GL_TEXTURE_2D, pass[i].texture);
if (pass[i].srgb)
{
glEnable(GL_FRAMEBUFFER_SRGB);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_SRGB8_ALPHA8,
2019-01-09 00:18:17 +01:00
(unsigned int)pass[i].width,
(unsigned int)pass[i].height,
0,
GL_RGBA,
GL_UNSIGNED_INT_8_8_8_8,
NULL);
}
else
{
glTexImage2D(GL_TEXTURE_2D,
0,
(pass[i].fp ? GL_RGBA32F : GL_RGBA),
2019-01-09 00:18:17 +01:00
(unsigned int)pass[i].width,
(unsigned int)pass[i].height,
0,
GL_RGBA,
(pass[i].fp ? GL_FLOAT : GL_UNSIGNED_INT_8_8_8_8),
NULL);
}
2018-05-14 01:39:56 +02:00
// viewport determines the area we render into the output texture
glViewport(0, 0, pass[i].width, pass[i].height);
2018-05-14 01:39:56 +02:00
// set up framebuffer and attach output texture
glBindFramebuffer(GL_FRAMEBUFFER, pass[i].fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
pass[i].texture,
0);
}
else
{
int out_x = 0;
int out_y = 0;
int out_width = 0;
int out_height = 0;
2018-05-14 01:39:56 +02:00
// output to the screen
2018-10-22 20:56:18 +02:00
glBindFramebuffer(GL_FRAMEBUFFER, saved_framebuffer);
vpcallback (pass[i].width, pass[i].height,
viewport_x, viewport_y,
viewport_width, viewport_height,
&out_x, &out_y,
&out_width, &out_height);
glViewport(out_x, out_y, out_width, out_height);
}
2018-05-11 01:47:55 +02:00
// set up input texture (output of previous pass) and apply filter settings
2019-01-09 00:18:17 +01:00
GLuint filter = pass[i].filter;
if (filter == GLSL_UNDEFINED)
{
if (lastpass && Settings.BilinearFilter)
filter = GL_LINEAR;
else
filter = GL_NEAREST;
}
2018-05-13 00:36:45 +02:00
2018-05-11 01:47:55 +02:00
glBindTexture(GL_TEXTURE_2D, pass[i - 1].texture);
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint) pass[i - 1].width);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, pass[i].wrap_mode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, pass[i].wrap_mode);
2018-05-11 01:47:55 +02:00
2019-01-28 03:14:28 +01:00
if (pass[i].mipmap_input)
glGenerateMipmap(GL_TEXTURE_2D);
2019-01-09 00:18:17 +01:00
glUseProgram(pass[i].program);
2019-01-25 20:28:24 +01:00
#ifdef USE_SLANG
if (using_slang)
slang_set_shader_vars(i, lastpass && direct_lastpass);
2019-01-25 20:28:24 +01:00
else
#endif
set_shader_vars(i, lastpass && direct_lastpass);
2018-05-11 01:47:55 +02:00
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2018-05-11 01:47:55 +02:00
2019-01-25 20:28:24 +01:00
#ifdef USE_SLANG
if (lastpass && !direct_lastpass)
{
glBindTexture(GL_TEXTURE_2D, pass[i].texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, pass[i].wrap_mode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, pass[i].wrap_mode);
int out_x = 0, out_y = 0, out_width = 0, out_height = 0;
vpcallback (pass[i].width, pass[i].height,
viewport_x, viewport_y,
viewport_width, viewport_height,
&out_x, &out_y,
&out_width, &out_height);
glViewport(out_x, out_y, out_width, out_height);
glBindFramebuffer(GL_FRAMEBUFFER, saved_framebuffer);
glClear(GL_COLOR_BUFFER_BIT);
glBindFramebuffer(GL_READ_FRAMEBUFFER, pass[i].fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, saved_framebuffer);
glBlitFramebuffer(0, 0, pass[i].width, pass[i].height, out_x,
out_height + out_y, out_x + out_width, out_y,
GL_COLOR_BUFFER_BIT,
Settings.BilinearFilter ? GL_LINEAR : GL_NEAREST);
}
// reset vertex attribs set in set_shader_vars
2019-01-25 20:28:24 +01:00
if (using_slang)
slang_clear_shader_vars();
else
#endif
clear_shader_vars();
if (pass[i].srgb)
{
2019-01-09 00:18:17 +01:00
glDisable(GL_FRAMEBUFFER_SRGB);
}
2018-05-11 01:47:55 +02:00
}
2018-05-14 01:39:56 +02:00
// Disable framebuffer
2018-10-22 20:56:18 +02:00
glBindFramebuffer(GL_FRAMEBUFFER, saved_framebuffer);
2018-05-11 01:47:55 +02:00
glActiveTexture(GL_TEXTURE0);
2019-01-09 00:18:17 +01:00
glUseProgram(0);
2018-05-11 01:47:55 +02:00
2018-05-14 01:39:56 +02:00
// Pop back of previous frame stack and use as upload buffer
2018-05-11 01:47:55 +02:00
if (prev_frame.size() > 0)
{
GLint internal_format;
orig = prev_frame.back().texture;
prev_frame.pop_back();
2019-01-25 20:28:24 +01:00
GLSLPass *newprevframe = new GLSLPass;
newprevframe->width = width;
newprevframe->height = height;
newprevframe->texture = pass[0].texture;
prev_frame.push_front(*newprevframe);
glBindTexture(GL_TEXTURE_2D, newprevframe->texture);
delete newprevframe;
2018-05-11 01:47:55 +02:00
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format);
glBindTexture(GL_TEXTURE_2D, orig);
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
glTexImage2D(GL_TEXTURE_2D,
0,
internal_format,
width,
height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
NULL);
}
glBindTexture(GL_TEXTURE_2D, orig);
2018-05-11 01:47:55 +02:00
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)pass.back().width);
}
2019-01-09 00:18:17 +01:00
void GLSLShader::register_uniforms()
2018-05-11 01:47:55 +02:00
{
max_prev_frame = 0;
char varname[100];
for (unsigned int i = 1; i < pass.size(); i++)
{
GLSLUniforms *u = &pass[i].unif;
GLuint program = pass[i].program;
glUseProgram (program);
2019-01-09 00:18:17 +01:00
GLint mvp = glGetUniformLocation(program, "MVPMatrix");
2018-05-11 01:47:55 +02:00
if (mvp > -1)
glUniformMatrix4fv(mvp, 1, GL_FALSE, mvp_ortho);
2019-01-09 00:18:17 +01:00
u->Texture = glGetUniformLocation(program, "Texture");
u->InputSize = glGetUniformLocation(program, "InputSize");
u->OutputSize = glGetUniformLocation(program, "OutputSize");
u->TextureSize = glGetUniformLocation(program, "TextureSize");
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
u->TexCoord = glGetAttribLocation(program, "TexCoord");
u->LUTTexCoord = glGetAttribLocation(program, "LUTTexCoord");
u->VertexCoord = glGetAttribLocation(program, "VertexCoord");
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
u->FrameCount = glGetUniformLocation(program, "FrameCount");
u->FrameDirection = glGetUniformLocation(program, "FrameDirection");
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
u->OrigTexture = glGetUniformLocation(program, "OrigTexture");
u->OrigInputSize = glGetUniformLocation(program, "OrigInputSize");
u->OrigTextureSize = glGetUniformLocation(program, "OrigTextureSize");
u->OrigTexCoord = glGetAttribLocation(program, "OrigTexCoord");
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
u->Prev[0].Texture = glGetUniformLocation(program, "PrevTexture");
u->Prev[0].InputSize = glGetUniformLocation(program, "PrevInputSize");
u->Prev[0].TextureSize = glGetUniformLocation(program, "PrevTextureSize");
u->Prev[0].TexCoord = glGetAttribLocation(program, "PrevTexCoord");
2018-05-11 01:47:55 +02:00
if (u->Prev[0].Texture > -1)
max_prev_frame = 1;
for (unsigned int j = 1; j < 7; j++)
{
sprintf(varname, "Prev%dTexture", j);
2019-01-09 00:18:17 +01:00
u->Prev[j].Texture = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "Prev%dInputSize", j);
2019-01-09 00:18:17 +01:00
u->Prev[j].InputSize = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "Prev%dTextureSize", j);
2019-01-09 00:18:17 +01:00
u->Prev[j].TextureSize = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "Prev%dTexCoord", j);
2019-01-09 00:18:17 +01:00
u->Prev[j].TexCoord = glGetAttribLocation(program, varname);
2018-05-11 01:47:55 +02:00
if (u->Prev[j].Texture > -1)
max_prev_frame = j + 1;
}
for (unsigned int j = 0; j < pass.size(); j++)
{
sprintf(varname, "Pass%dTexture", j);
2019-01-09 00:18:17 +01:00
u->Pass[j].Texture = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "Pass%dInputSize", j);
2019-01-09 00:18:17 +01:00
u->Pass[j].InputSize = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "Pass%dTextureSize", j);
2019-01-09 00:18:17 +01:00
u->Pass[j].TextureSize = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "Pass%dTexCoord", j);
2019-01-09 00:18:17 +01:00
u->Pass[j].TexCoord = glGetAttribLocation(program, varname);
2018-05-11 01:47:55 +02:00
if (u->Pass[j].Texture)
u->max_pass = j;
sprintf(varname, "PassPrev%dTexture", j);
2019-01-09 00:18:17 +01:00
u->PassPrev[j].Texture = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "PassPrev%dInputSize", j);
2019-01-09 00:18:17 +01:00
u->PassPrev[j].InputSize = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "PassPrev%dTextureSize", j);
2019-01-09 00:18:17 +01:00
u->PassPrev[j].TextureSize = glGetUniformLocation(program, varname);
2018-05-11 01:47:55 +02:00
sprintf(varname, "PassPrev%dTexCoord", j);
2019-01-09 00:18:17 +01:00
u->PassPrev[j].TexCoord = glGetAttribLocation(program, varname);
2018-05-11 01:47:55 +02:00
if (u->PassPrev[j].Texture > -1)
u->max_prevpass = j;
}
for (unsigned int j = 0; j < lut.size(); j++)
{
2019-01-09 00:18:17 +01:00
u->Lut[j] = glGetUniformLocation(program, lut[j].id);
2018-05-11 01:47:55 +02:00
}
2018-05-12 00:08:13 +02:00
for (unsigned int j = 0; j < param.size(); j++)
{
2019-01-09 00:18:17 +01:00
param[j].unif[i] = glGetUniformLocation(program, param[j].id);
2018-05-12 00:08:13 +02:00
}
2018-05-11 01:47:55 +02:00
}
2019-01-09 00:18:17 +01:00
glUseProgram(0);
2018-05-11 01:47:55 +02:00
}
void GLSLShader::set_shader_vars(unsigned int p, bool inverted)
2018-05-11 01:47:55 +02:00
{
unsigned int texunit = 0;
unsigned int offset = 0;
if (inverted)
offset = 8;
2018-05-11 01:47:55 +02:00
GLSLUniforms *u = &pass[p].unif;
2019-01-09 00:18:17 +01:00
GLint mvp = glGetUniformLocation(pass[p].program, "MVPMatrix");
2018-05-11 01:47:55 +02:00
if (mvp > -1)
glUniformMatrix4fv(mvp, 1, GL_FALSE, mvp_ortho);
2019-01-09 00:18:17 +01:00
#define setUniform2fv(uni, val) if (uni > -1) glUniform2fv(uni, 1, val);
#define setUniform1f(uni, val) if (uni > -1) glUniform1f(uni, val);
#define setUniform1i(uni, val) if (uni > -1) glUniform1i(uni, val);
2018-05-11 01:47:55 +02:00
#define setTexture1i(uni, val) \
if (uni > -1) \
{ \
2019-01-09 00:18:17 +01:00
glActiveTexture(GL_TEXTURE0 + texunit); \
glBindTexture(GL_TEXTURE_2D, val); \
glUniform1i(uni, texunit); \
2018-05-11 01:47:55 +02:00
texunit++; \
}
2018-05-14 01:39:56 +02:00
// We use non-power-of-two textures,
// so no need to mess with input size/texture size
2018-05-11 01:47:55 +02:00
#define setTexCoords(attr) \
if (attr > -1) \
{ \
glEnableVertexAttribArray(attr); \
2019-01-09 00:18:17 +01:00
glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 0, (void *)(sizeof(float) * offset)); \
vaos.push_back(attr); \
}
#define setTexCoordsNoOffset(attr) \
2018-05-11 01:47:55 +02:00
if (attr > -1) \
{ \
glEnableVertexAttribArray(attr); \
glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 0, NULL); \
2019-01-09 00:18:17 +01:00
vaos.push_back(attr); \
2018-05-11 01:47:55 +02:00
}
2019-01-09 00:18:17 +01:00
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 16, tex_coords, GL_STATIC_DRAW);
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
float inputSize[2] = { (float)pass[p - 1].width, (float)pass[p - 1].height };
float outputSize[2] = { (float)pass[p].width, (float)pass[p].height };
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
setTexture1i(u->Texture, pass[p - 1].texture);
2018-05-11 01:47:55 +02:00
setUniform2fv(u->InputSize, inputSize);
setUniform2fv(u->OutputSize, outputSize);
setUniform2fv(u->TextureSize, inputSize);
unsigned int shaderFrameCnt = frame_count;
if (pass[p].frame_count_mod)
shaderFrameCnt %= pass[p].frame_count_mod;
2019-02-23 15:00:57 +01:00
setUniform1i(u->FrameCount, shaderFrameCnt);
setUniform1i(u->FrameDirection, Settings.Rewinding ? -1 : 1);
2018-05-11 01:47:55 +02:00
2019-01-09 00:18:17 +01:00
setTexCoords(u->TexCoord);
setTexCoords(u->LUTTexCoord);
setTexCoordsNoOffset(u->VertexCoord);
2018-05-14 01:39:56 +02:00
// Orig parameter
2019-01-09 00:18:17 +01:00
float orig_videoSize[2] = { (float)pass[0].width, (float)pass[0].height };
2018-05-11 01:47:55 +02:00
setUniform2fv(u->OrigInputSize, orig_videoSize);
setUniform2fv(u->OrigTextureSize, orig_videoSize);
2019-01-09 00:18:17 +01:00
setTexture1i(u->OrigTexture, pass[0].texture);
setTexCoords(u->OrigTexCoord);
2018-05-11 01:47:55 +02:00
2018-05-14 01:39:56 +02:00
// Prev parameter
2018-05-11 01:47:55 +02:00
if (max_prev_frame >= 1 && prev_frame[0].width > 0) {
2019-01-09 00:18:17 +01:00
float prevSize[2] = { (float)prev_frame[0].width, (float)prev_frame[0].height };
2018-05-11 01:47:55 +02:00
setUniform2fv(u->Prev[0].InputSize, prevSize);
setUniform2fv(u->Prev[0].TextureSize, prevSize);
2019-01-09 00:18:17 +01:00
setTexture1i(u->Prev[0].Texture, prev_frame[0].texture);
setTexCoords(u->Prev[0].TexCoord);
2018-05-11 01:47:55 +02:00
}
2018-05-14 01:39:56 +02:00
// Prev[1-6] parameters
2018-05-11 01:47:55 +02:00
for (unsigned int i = 1; i < prev_frame.size(); i++)
{
if (prev_frame[i].width <= 0)
break;
2019-01-09 00:18:17 +01:00
float prevSize[2] = { (float)prev_frame[i].width, (float)prev_frame[i].height };
2018-05-11 01:47:55 +02:00
setUniform2fv(u->Prev[i].InputSize, prevSize);
setUniform2fv(u->Prev[i].TextureSize, prevSize);
2019-01-09 00:18:17 +01:00
setTexture1i(u->Prev[i].Texture, prev_frame[i].texture);
setTexCoords(u->Prev[i].TexCoord);
2018-05-11 01:47:55 +02:00
}
2018-05-14 01:39:56 +02:00
// LUT parameters
2018-05-11 01:47:55 +02:00
for (unsigned int i = 0; i < lut.size(); i++)
{
2019-01-09 00:18:17 +01:00
setTexture1i(u->Lut[i], lut[i].texture);
2018-05-11 01:47:55 +02:00
}
2018-05-14 01:39:56 +02:00
// PassX parameters, only for third pass and up
2018-05-11 01:47:55 +02:00
if (p > 2) {
for (unsigned int i = 1; i < p - 1; i++) {
2019-01-09 00:18:17 +01:00
float passSize[2] = { (float)pass[i].width, (float)pass[i].height };
2018-05-11 01:47:55 +02:00
setUniform2fv(u->Pass[i].InputSize, passSize);
setUniform2fv(u->Pass[i].TextureSize, passSize);
2019-01-09 00:18:17 +01:00
setTexture1i(u->Pass[i].Texture, pass[i].texture);
setTexCoords(u->Pass[i].TexCoord);
2018-05-11 01:47:55 +02:00
}
}
2018-05-14 01:39:56 +02:00
// PassPrev parameter
for (unsigned int i = 0; i < p; i++)
2018-05-11 01:47:55 +02:00
{
2019-01-09 00:18:17 +01:00
float passSize[2] = { (float)pass[i].width, (float)pass[i].height };
2018-05-11 01:47:55 +02:00
setUniform2fv(u->PassPrev[p - i].InputSize, passSize);
setUniform2fv(u->PassPrev[p - i].TextureSize, passSize);
2019-01-09 00:18:17 +01:00
setTexture1i(u->PassPrev[p - i].Texture, pass[i].texture);
setTexCoords(u->PassPrev[p - i].TexCoord);
2018-05-11 01:47:55 +02:00
}
2018-05-14 01:39:56 +02:00
// User and Preset Parameters
2018-05-12 00:08:13 +02:00
for (unsigned int i = 0; i < param.size(); i++)
{
2019-01-09 00:18:17 +01:00
setUniform1f(param[i].unif[p], param[i].val);
2018-05-12 00:08:13 +02:00
}
2019-01-09 00:18:17 +01:00
glActiveTexture(GL_TEXTURE0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
2018-05-11 01:47:55 +02:00
}
2019-01-25 20:28:24 +01:00
void GLSLShader::clear_shader_vars()
2018-05-12 22:07:07 +02:00
{
for (unsigned int i = 0; i < vaos.size(); i++)
2019-01-09 00:18:17 +01:00
glDisableVertexAttribArray(vaos[i]);
2018-05-12 22:07:07 +02:00
vaos.clear();
}
#define outs(s, v) fprintf(file, "%s%d = \"%s\"\n", s, i, v)
#define outf(s, v) fprintf(file, "%s%d = \"%f\"\n", s, i, v)
#define outd(s, v) fprintf(file, "%s%d = \"%d\"\n", s, i, v)
2019-01-09 00:18:17 +01:00
void GLSLShader::save(const char *filename)
2018-05-12 22:07:07 +02:00
{
2019-01-09 00:18:17 +01:00
FILE *file = fopen(filename, "wb");
2018-05-12 22:07:07 +02:00
if (!file)
{
2019-01-09 00:18:17 +01:00
printf("Couldn't save shader config to %s\n", filename);
2018-05-12 22:07:07 +02:00
}
2019-01-09 00:18:17 +01:00
fprintf(file, "shaders = \"%d\"\n", (unsigned int)pass.size() - 1);
2018-05-12 22:07:07 +02:00
for (unsigned int pn = 1; pn < pass.size(); pn++)
{
GLSLPass *p = &pass[pn];
unsigned int i = pn - 1;
outs("shader", p->filename);
if (p->filter != GLSL_UNDEFINED)
{
outs("filter_linear", p->filter == GL_LINEAR ? "true" : "false");
}
2019-01-09 00:18:17 +01:00
outs("wrap_mode", wrap_mode_enum_to_string(p->wrap_mode));
outs("alias", p->alias);
outs("float_framebuffer", p->fp ? "true" : "false");
outs("srgb_framebuffer", p->srgb ? "true" : "false");
outs("scale_type_x", scale_enum_to_string(p->scale_type_x));
2018-05-12 22:07:07 +02:00
if (p->scale_type_x == GLSL_ABSOLUTE)
2019-01-09 00:18:17 +01:00
outd("scale_x", (int)p->scale_x);
2018-05-12 22:07:07 +02:00
else
2019-01-09 00:18:17 +01:00
outf("scale_x", p->scale_x);
2018-05-12 22:07:07 +02:00
2019-01-09 00:18:17 +01:00
outs("scale_type_y", scale_enum_to_string(p->scale_type_y));
2018-05-12 22:07:07 +02:00
if (p->scale_type_y == GLSL_ABSOLUTE)
2019-01-09 00:18:17 +01:00
outd("scale_y", (int)p->scale_y);
2018-05-12 22:07:07 +02:00
else
2019-01-09 00:18:17 +01:00
outf("scale_y", p->scale_y);
2018-05-12 22:07:07 +02:00
if (p->frame_count_mod)
2019-01-09 00:18:17 +01:00
outd("frame_count_mod", p->frame_count_mod);
2018-05-12 22:07:07 +02:00
}
if (param.size() > 0)
{
2019-01-09 00:18:17 +01:00
fprintf(file, "parameters = \"");
2018-05-12 22:07:07 +02:00
for (unsigned int i = 0; i < param.size(); i++)
{
2019-01-09 00:18:17 +01:00
fprintf(file, "%s%c", param[i].id, (i == param.size() - 1) ? '\"' : ';');
2018-05-12 22:07:07 +02:00
}
2019-01-09 00:18:17 +01:00
fprintf(file, "\n");
2018-05-12 22:07:07 +02:00
}
for (unsigned int i = 0; i < param.size(); i++)
{
2019-01-09 00:18:17 +01:00
fprintf(file, "%s = \"%f\"\n", param[i].id, param[i].val);
2018-05-12 22:07:07 +02:00
}
if (lut.size() > 0)
{
2019-01-09 00:18:17 +01:00
fprintf(file, "textures = \"");
2018-05-12 22:07:07 +02:00
for (unsigned int i = 0; i < lut.size(); i++)
{
2019-01-09 00:18:17 +01:00
fprintf(file, "%s%c", lut[i].id, (i == lut.size() - 1) ? '\"' : ';');
2018-05-12 22:07:07 +02:00
}
2019-01-09 00:18:17 +01:00
fprintf(file, "\n");
2018-05-12 22:07:07 +02:00
}
for (unsigned int i = 0; i < lut.size(); i++)
{
2019-01-09 00:18:17 +01:00
fprintf(file, "%s = \"%s\"\n", lut[i].id, lut[i].filename);
fprintf(file, "%s_linear = \"%s\"\n", lut[i].id, lut[i].filter == GL_LINEAR || lut[i].filter == GL_LINEAR_MIPMAP_LINEAR ? "true" : "false");
fprintf(file, "%s_wrap_mode = \"%s\"\n", lut[i].id, wrap_mode_enum_to_string(lut[i].wrap_mode));
fprintf(file, "%s_mipmap = \"%s\"\n", lut[i].id, lut[i].mipmap ? "true" : "false");
2018-05-12 22:07:07 +02:00
}
2019-01-09 00:18:17 +01:00
fclose(file);
2018-05-12 22:07:07 +02:00
}
#undef outf
#undef outs
#undef outd
2019-01-25 20:28:24 +01:00
void GLSLShader::destroy()
2018-05-11 01:47:55 +02:00
{
2018-05-12 22:07:07 +02:00
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
glActiveTexture(GL_TEXTURE0);
2018-05-11 01:47:55 +02:00
for (unsigned int i = 1; i < pass.size(); i++)
{
2018-05-12 22:07:07 +02:00
glDeleteProgram(pass[i].program);
glDeleteTextures(1, &pass[i].texture);
2018-05-11 01:47:55 +02:00
glDeleteFramebuffers(1, &pass[i].fbo);
#ifdef USE_SLANG
if (using_slang)
{
if (pass[i].uses_feedback)
glDeleteTextures(1, &pass[i].feedback_texture);
if (pass[i].ubo_buffer.size() > 0)
glDeleteBuffers(1, &pass[i].ubo);
}
#endif
2018-05-11 01:47:55 +02:00
}
for (unsigned int i = 0; i < lut.size(); i++)
{
2019-01-09 00:18:17 +01:00
glDeleteTextures(1, &lut[i].texture);
2018-05-11 01:47:55 +02:00
}
2019-01-09 00:18:17 +01:00
for (unsigned int i = 0; i < prev_frame.size(); i++)
2018-05-11 01:47:55 +02:00
{
2019-01-09 00:18:17 +01:00
glDeleteTextures(1, &prev_frame[i].texture);
2018-05-11 01:47:55 +02:00
}
glDeleteBuffers(1, &vbo);
2018-05-12 00:08:13 +02:00
param.clear();
2018-05-11 01:47:55 +02:00
pass.clear();
lut.clear();
prev_frame.clear();
2018-05-20 19:50:28 +02:00
conf.Clear();
2018-05-11 01:47:55 +02:00
}