Clean up shader files a bit.

This commit is contained in:
Brandon Wright 2019-01-08 17:18:17 -06:00
parent 2c1753474c
commit 405aee6d1e
4 changed files with 273 additions and 328 deletions

View File

@ -12,9 +12,9 @@
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 };
static const GLfloat mvp_ortho[16] = { 2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f };
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f };
static void reduce_to_path(char* filename)
{
@ -28,11 +28,11 @@ static void reduce_to_path(char* filename)
}
}
static char* read_file(const char *filename)
static char *read_file(const char *filename)
{
FILE* file = NULL;
FILE *file = NULL;
int size;
char* contents;
char *contents;
file = fopen(filename, "rb");
if (!file)
@ -70,7 +70,7 @@ static int scale_string_to_enum(const char *string, bool last)
return GLSL_SOURCE;
}
static const char *scale_enum_to_string (int val)
static const char *scale_enum_to_string(int val)
{
switch (val)
{
@ -119,7 +119,7 @@ static const char *wrap_mode_enum_to_string(int val)
}
bool GLSLShader::load_shader_file (char *filename)
bool GLSLShader::load_shader_file(char *filename)
{
char key[256];
@ -151,7 +151,7 @@ bool GLSLShader::load_shader_file (char *filename)
if (shader_count < 1)
return false;
this->pass.push_back (GLSLPass());
this->pass.push_back(GLSLPass());
for (int i = 0; i < shader_count; i++)
{
@ -205,7 +205,7 @@ bool GLSLShader::load_shader_file (char *filename)
sprintf(key, "::frame_count_mod%u", i);
pass.frame_count_mod = conf.GetInt(key, 0);
if (float_texture_available ())
if (gl_float_texture_available())
{
sprintf(key, "::float_framebuffer%u", i);
pass.fp = conf.GetBool(key);
@ -213,7 +213,7 @@ bool GLSLShader::load_shader_file (char *filename)
else
pass.fp = false;
if (srgb_available ())
if (gl_srgb_available())
{
sprintf(key, "::srgb_framebuffer%u", i);
pass.srgb = conf.GetBool(key);
@ -274,8 +274,8 @@ void GLSLShader::strip_parameter_pragmas(char *buffer)
GLSLParam par;
unsigned int i;
sscanf (s, "#pragma parameter %s \"%[^\"]\" %f %f %f %f",
par.id, par.name, &par.val, &par.min, &par.max, &par.step);
sscanf(s, "#pragma parameter %s \"%[^\"]\" %f %f %f %f",
par.id, par.name, &par.val, &par.min, &par.max, &par.step);
if (par.step == 0.0f)
par.step = 1.0f;
@ -286,7 +286,7 @@ void GLSLShader::strip_parameter_pragmas(char *buffer)
break;
}
if (i >= param.size())
param.push_back (par);
param.push_back(par);
// blank out the line
while (*s != '\0' && *s != '\n')
@ -309,12 +309,12 @@ GLuint GLSLShader::compile_shader(char *program,
if (existing_version)
{
unsigned version_no = (unsigned) strtoul (existing_version + 8, &ptr, 10);
snprintf (version, 32, "#version %u\n", version_no);
unsigned version_no = (unsigned)strtoul(existing_version + 8, &ptr, 10);
snprintf(version, 32, "#version %u\n", version_no);
}
else
{
snprintf (version, 32, "#version 150\n");
snprintf(version, 32, "#version 150\n");
}
complete_program += version;
@ -322,25 +322,25 @@ GLuint GLSLShader::compile_shader(char *program,
complete_program += defines;
complete_program += ptr;
GLuint shader = glCreateShader (type);
GLuint shader = glCreateShader(type);
GLint status;
GLint length = complete_program.length();
GLchar *prog = (GLchar *) complete_program.c_str();
GLchar *prog = (GLchar *)complete_program.c_str();
glShaderSource (shader, 1, &prog, &length);
glCompileShader (shader);
glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
glShaderSource(shader, 1, &prog, &length);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
info_log[0] = '\0';
glGetShaderInfoLog (shader, 1024, NULL, info_log);
glGetShaderInfoLog(shader, 1024, NULL, info_log);
if (*info_log)
printf ("%s\n", info_log);
printf("%s\n", info_log);
*out = shader;
return status;
}
bool GLSLShader::load_shader (char *filename)
bool GLSLShader::load_shader(char *filename)
{
char shader_path[PATH_MAX];
char temp[PATH_MAX];
@ -381,9 +381,9 @@ bool GLSLShader::load_shader (char *filename)
GLuint vertex_shader = 0, fragment_shader = 0;
realpath(p->filename, temp);
strcpy (p->filename, temp);
strcpy(p->filename, temp);
char *contents = read_file (p->filename);
char *contents = read_file(p->filename);
if (!contents)
{
printf("Couldn't read shader file %s\n", temp);
@ -392,21 +392,21 @@ bool GLSLShader::load_shader (char *filename)
strip_parameter_pragmas(contents);
if (!compile_shader (contents,
"#define VERTEX\n#define PARAMETER_UNIFORM\n",
aliases.c_str(),
GL_VERTEX_SHADER,
&vertex_shader) || !vertex_shader)
if (!compile_shader(contents,
"#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);
return false;
}
if (!compile_shader (contents,
"#define FRAGMENT\n#define PARAMETER_UNIFORM\n",
aliases.c_str(),
GL_FRAGMENT_SHADER,
&fragment_shader) || !fragment_shader)
if (!compile_shader(contents,
"#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);
return false;
@ -414,25 +414,25 @@ bool GLSLShader::load_shader (char *filename)
delete[] contents;
p->program = glCreateProgram ();
p->program = glCreateProgram();
glAttachShader (p->program, vertex_shader);
glAttachShader (p->program, fragment_shader);
glAttachShader(p->program, vertex_shader);
glAttachShader(p->program, fragment_shader);
glLinkProgram (p->program);
glGetProgramiv (p->program, GL_LINK_STATUS, &status);
glLinkProgram(p->program);
glGetProgramiv(p->program, GL_LINK_STATUS, &status);
log[0] = '\0';
glGetProgramInfoLog(p->program, 1024, NULL, log);
if (*log)
printf ("%s\n", log);
printf("%s\n", log);
glDeleteShader (vertex_shader);
glDeleteShader (fragment_shader);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
if (status != GL_TRUE)
{
printf ("Failed to link program\n");
glDeleteProgram (p->program);
printf("Failed to link program\n");
glDeleteProgram(p->program);
return false;
}
@ -460,7 +460,7 @@ bool GLSLShader::load_shader (char *filename)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, l->filter);
realpath(l->filename, temp);
strcpy (l->filename, temp);
strcpy(l->filename, temp);
// simple file extension png/tga decision
int length = strlen(temp);
@ -510,13 +510,13 @@ bool GLSLShader::load_shader (char *filename)
}
else
{
printf ("Failed to load TGA LUT: %s\n", temp);
printf("Failed to load TGA LUT: %s\n", temp);
}
}
}
if (l->mipmap)
glGenerateMipmap (GL_TEXTURE_2D);
glGenerateMipmap(GL_TEXTURE_2D);
}
// Check for parameters specified in file
@ -528,7 +528,7 @@ bool GLSLShader::load_shader (char *filename)
value = conf.GetString (key, NULL);
if (value)
{
param[i].val = atof (value);
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)
@ -543,7 +543,7 @@ bool GLSLShader::load_shader (char *filename)
register_uniforms();
prev_frame.resize (max_prev_frame);
prev_frame.resize(max_prev_frame);
for (unsigned int i = 0; i < prev_frame.size(); i++)
{
@ -566,7 +566,7 @@ void GLSLShader::render(GLuint &orig,
{
GLint saved_framebuffer;
glGetIntegerv (GL_FRAMEBUFFER_BINDING, &saved_framebuffer);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &saved_framebuffer);
frame_count++;
@ -622,8 +622,8 @@ void GLSLShader::render(GLuint &orig,
glTexImage2D(GL_TEXTURE_2D,
0,
GL_SRGB8_ALPHA8,
(unsigned int) pass[i].width,
(unsigned int) pass[i].height,
(unsigned int)pass[i].width,
(unsigned int)pass[i].height,
0,
GL_RGBA,
GL_UNSIGNED_INT_8_8_8_8,
@ -634,8 +634,8 @@ void GLSLShader::render(GLuint &orig,
glTexImage2D(GL_TEXTURE_2D,
0,
(pass[i].fp ? GL_RGBA32F : GL_RGBA),
(unsigned int) pass[i].width,
(unsigned int) pass[i].height,
(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),
@ -671,10 +671,14 @@ void GLSLShader::render(GLuint &orig,
}
// set up input texture (output of previous pass) and apply filter settings
GLuint filter = (pass[i].filter == GLSL_UNDEFINED) ?
(lastpass ?
(Settings.BilinearFilter ? GL_LINEAR : GL_NEAREST) : GL_NEAREST
) : pass[i].filter;
GLuint filter = pass[i].filter;
if (filter == GLSL_UNDEFINED)
{
if (lastpass && Settings.BilinearFilter)
filter = GL_LINEAR;
else
filter = GL_NEAREST;
}
glBindTexture(GL_TEXTURE_2D, pass[i - 1].texture);
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint) pass[i - 1].width);
@ -691,7 +695,7 @@ void GLSLShader::render(GLuint &orig,
GL_TEXTURE_WRAP_T,
pass[i].wrap_mode);
glUseProgram (pass[i].program);
glUseProgram(pass[i].program);
set_shader_vars(i);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
@ -703,14 +707,14 @@ void GLSLShader::render(GLuint &orig,
if (pass[i].srgb)
{
glDisable (GL_FRAMEBUFFER_SRGB);
glDisable(GL_FRAMEBUFFER_SRGB);
}
}
// Disable framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, saved_framebuffer);
glActiveTexture(GL_TEXTURE0);
glUseProgram (0);
glUseProgram(0);
// Pop back of previous frame stack and use as upload buffer
if (prev_frame.size() > 0)
@ -744,7 +748,7 @@ void GLSLShader::render(GLuint &orig,
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)pass.back().width);
}
void GLSLShader::register_uniforms ()
void GLSLShader::register_uniforms()
{
max_prev_frame = 0;
char varname[100];
@ -756,31 +760,31 @@ void GLSLShader::register_uniforms ()
glUseProgram (program);
GLint mvp = glGetUniformLocation (program, "MVPMatrix");
GLint mvp = glGetUniformLocation(program, "MVPMatrix");
if (mvp > -1)
glUniformMatrix4fv(mvp, 1, GL_FALSE, mvp_ortho);
u->Texture = glGetUniformLocation (program, "Texture");
u->InputSize = glGetUniformLocation (program, "InputSize");
u->OutputSize = glGetUniformLocation (program, "OutputSize");
u->TextureSize = glGetUniformLocation (program, "TextureSize");
u->Texture = glGetUniformLocation(program, "Texture");
u->InputSize = glGetUniformLocation(program, "InputSize");
u->OutputSize = glGetUniformLocation(program, "OutputSize");
u->TextureSize = glGetUniformLocation(program, "TextureSize");
u->TexCoord = glGetAttribLocation (program, "TexCoord");
u->LUTTexCoord = glGetAttribLocation (program, "LUTTexCoord");
u->VertexCoord = glGetAttribLocation (program, "VertexCoord");
u->TexCoord = glGetAttribLocation(program, "TexCoord");
u->LUTTexCoord = glGetAttribLocation(program, "LUTTexCoord");
u->VertexCoord = glGetAttribLocation(program, "VertexCoord");
u->FrameCount = glGetUniformLocation (program, "FrameCount");
u->FrameDirection = glGetUniformLocation (program, "FrameDirection");
u->FrameCount = glGetUniformLocation(program, "FrameCount");
u->FrameDirection = glGetUniformLocation(program, "FrameDirection");
u->OrigTexture = glGetUniformLocation (program, "OrigTexture");
u->OrigInputSize = glGetUniformLocation (program, "OrigInputSize");
u->OrigTextureSize = glGetUniformLocation (program, "OrigTextureSize");
u->OrigTexCoord = glGetAttribLocation (program, "OrigTexCoord");
u->OrigTexture = glGetUniformLocation(program, "OrigTexture");
u->OrigInputSize = glGetUniformLocation(program, "OrigInputSize");
u->OrigTextureSize = glGetUniformLocation(program, "OrigTextureSize");
u->OrigTexCoord = glGetAttribLocation(program, "OrigTexCoord");
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");
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");
if (u->Prev[0].Texture > -1)
max_prev_frame = 1;
@ -788,13 +792,13 @@ void GLSLShader::register_uniforms ()
for (unsigned int j = 1; j < 7; j++)
{
sprintf(varname, "Prev%dTexture", j);
u->Prev[j].Texture = glGetUniformLocation (program, varname);
u->Prev[j].Texture = glGetUniformLocation(program, varname);
sprintf(varname, "Prev%dInputSize", j);
u->Prev[j].InputSize = glGetUniformLocation (program, varname);
u->Prev[j].InputSize = glGetUniformLocation(program, varname);
sprintf(varname, "Prev%dTextureSize", j);
u->Prev[j].TextureSize = glGetUniformLocation (program, varname);
u->Prev[j].TextureSize = glGetUniformLocation(program, varname);
sprintf(varname, "Prev%dTexCoord", j);
u->Prev[j].TexCoord = glGetAttribLocation (program, varname);
u->Prev[j].TexCoord = glGetAttribLocation(program, varname);
if (u->Prev[j].Texture > -1)
max_prev_frame = j + 1;
@ -802,43 +806,43 @@ void GLSLShader::register_uniforms ()
for (unsigned int j = 0; j < pass.size(); j++)
{
sprintf(varname, "Pass%dTexture", j);
u->Pass[j].Texture = glGetUniformLocation (program, varname);
u->Pass[j].Texture = glGetUniformLocation(program, varname);
sprintf(varname, "Pass%dInputSize", j);
u->Pass[j].InputSize = glGetUniformLocation (program, varname);
u->Pass[j].InputSize = glGetUniformLocation(program, varname);
sprintf(varname, "Pass%dTextureSize", j);
u->Pass[j].TextureSize = glGetUniformLocation (program, varname);
u->Pass[j].TextureSize = glGetUniformLocation(program, varname);
sprintf(varname, "Pass%dTexCoord", j);
u->Pass[j].TexCoord = glGetAttribLocation (program, varname);
u->Pass[j].TexCoord = glGetAttribLocation(program, varname);
if (u->Pass[j].Texture)
u->max_pass = j;
sprintf(varname, "PassPrev%dTexture", j);
u->PassPrev[j].Texture = glGetUniformLocation (program, varname);
u->PassPrev[j].Texture = glGetUniformLocation(program, varname);
sprintf(varname, "PassPrev%dInputSize", j);
u->PassPrev[j].InputSize = glGetUniformLocation (program, varname);
u->PassPrev[j].InputSize = glGetUniformLocation(program, varname);
sprintf(varname, "PassPrev%dTextureSize", j);
u->PassPrev[j].TextureSize = glGetUniformLocation (program, varname);
u->PassPrev[j].TextureSize = glGetUniformLocation(program, varname);
sprintf(varname, "PassPrev%dTexCoord", j);
u->PassPrev[j].TexCoord = glGetAttribLocation (program, varname);
u->PassPrev[j].TexCoord = glGetAttribLocation(program, varname);
if (u->PassPrev[j].Texture > -1)
u->max_prevpass = j;
}
for (unsigned int j = 0; j < lut.size(); j++)
{
u->Lut[j] = glGetUniformLocation (program, lut[j].id);
u->Lut[j] = glGetUniformLocation(program, lut[j].id);
}
for (unsigned int j = 0; j < param.size(); j++)
{
param[j].unif[i] = glGetUniformLocation (program, param[j].id);
param[j].unif[i] = glGetUniformLocation(program, param[j].id);
}
}
glUseProgram (0);
glUseProgram(0);
}
void GLSLShader::set_shader_vars (unsigned int p)
void GLSLShader::set_shader_vars(unsigned int p)
{
unsigned int texunit = 0;
unsigned int offset = 0;
@ -847,19 +851,19 @@ void GLSLShader::set_shader_vars (unsigned int p)
offset = 8;
GLSLUniforms *u = &pass[p].unif;
GLint mvp = glGetUniformLocation (pass[p].program, "MVPMatrix");
GLint mvp = glGetUniformLocation(pass[p].program, "MVPMatrix");
if (mvp > -1)
glUniformMatrix4fv(mvp, 1, GL_FALSE, mvp_ortho);
#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);
#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);
#define setTexture1i(uni, val) \
if (uni > -1) \
{ \
glActiveTexture (GL_TEXTURE0 + texunit); \
glBindTexture (GL_TEXTURE_2D, val); \
glUniform1i (uni, texunit); \
glActiveTexture(GL_TEXTURE0 + texunit); \
glBindTexture(GL_TEXTURE_2D, val); \
glUniform1i(uni, texunit); \
texunit++; \
}
// We use non-power-of-two textures,
@ -868,24 +872,24 @@ void GLSLShader::set_shader_vars (unsigned int p)
if (attr > -1) \
{ \
glEnableVertexAttribArray(attr); \
glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 0, (void *)(sizeof (float) * offset)); \
vaos.push_back (attr); \
glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 0, (void *)(sizeof(float) * offset)); \
vaos.push_back(attr); \
}
#define setTexCoordsNoOffset(attr) \
if (attr > -1) \
{ \
glEnableVertexAttribArray(attr); \
glVertexAttribPointer(attr, 2, GL_FLOAT, GL_FALSE, 0, NULL); \
vaos.push_back (attr); \
vaos.push_back(attr); \
}
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, sizeof (GLfloat) * 16, tex_coords, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 16, tex_coords, GL_STATIC_DRAW);
float inputSize[2] = { (float) pass[p - 1].width, (float) pass[p - 1].height };
float outputSize[2] = { (float) pass[p].width, (float) pass[p].height };
float inputSize[2] = { (float)pass[p - 1].width, (float)pass[p - 1].height };
float outputSize[2] = { (float)pass[p].width, (float)pass[p].height };
setTexture1i (u->Texture, pass[p - 1].texture);
setTexture1i(u->Texture, pass[p - 1].texture);
setUniform2fv(u->InputSize, inputSize);
setUniform2fv(u->OutputSize, outputSize);
setUniform2fv(u->TextureSize, inputSize);
@ -893,29 +897,29 @@ void GLSLShader::set_shader_vars (unsigned int p)
unsigned int shaderFrameCnt = frame_count;
if (pass[p].frame_count_mod)
shaderFrameCnt %= pass[p].frame_count_mod;
setUniform1i(u->FrameCount, (float) shaderFrameCnt);
setUniform1i(u->FrameCount, (float)shaderFrameCnt);
setUniform1i(u->FrameDirection, Settings.Rewinding ? -1.0f : 1.0f);
setTexCoords (u->TexCoord);
setTexCoords (u->LUTTexCoord);
setTexCoordsNoOffset (u->VertexCoord);
setTexCoords(u->TexCoord);
setTexCoords(u->LUTTexCoord);
setTexCoordsNoOffset(u->VertexCoord);
// Orig parameter
float orig_videoSize[2] = { (float) pass[0].width, (float) pass[0].height };
float orig_videoSize[2] = { (float)pass[0].width, (float)pass[0].height };
setUniform2fv(u->OrigInputSize, orig_videoSize);
setUniform2fv(u->OrigTextureSize, orig_videoSize);
setTexture1i (u->OrigTexture, pass[0].texture);
setTexCoords (u->OrigTexCoord);
setTexture1i(u->OrigTexture, pass[0].texture);
setTexCoords(u->OrigTexCoord);
// Prev parameter
if (max_prev_frame >= 1 && prev_frame[0].width > 0) {
float prevSize[2] = { (float) prev_frame[0].width, (float) prev_frame[0].height };
float prevSize[2] = { (float)prev_frame[0].width, (float)prev_frame[0].height };
setUniform2fv(u->Prev[0].InputSize, prevSize);
setUniform2fv(u->Prev[0].TextureSize, prevSize);
setTexture1i (u->Prev[0].Texture, prev_frame[0].texture);
setTexCoords (u->Prev[0].TexCoord);
setTexture1i(u->Prev[0].Texture, prev_frame[0].texture);
setTexCoords(u->Prev[0].TexCoord);
}
// Prev[1-6] parameters
@ -924,55 +928,55 @@ void GLSLShader::set_shader_vars (unsigned int p)
if (prev_frame[i].width <= 0)
break;
float prevSize[2] = { (float) prev_frame[i].width, (float) prev_frame[i].height };
float prevSize[2] = { (float)prev_frame[i].width, (float)prev_frame[i].height };
setUniform2fv(u->Prev[i].InputSize, prevSize);
setUniform2fv(u->Prev[i].TextureSize, prevSize);
setTexture1i (u->Prev[i].Texture, prev_frame[i].texture);
setTexCoords (u->Prev[i].TexCoord);
setTexture1i(u->Prev[i].Texture, prev_frame[i].texture);
setTexCoords(u->Prev[i].TexCoord);
}
// LUT parameters
for (unsigned int i = 0; i < lut.size(); i++)
{
setTexture1i (u->Lut[i], lut[i].texture);
setTexture1i(u->Lut[i], lut[i].texture);
}
// PassX parameters, only for third pass and up
if (p > 2) {
for (unsigned int i = 1; i < p - 1; i++) {
float passSize[2] = { (float) pass[i].width, (float) pass[i].height };
float passSize[2] = { (float)pass[i].width, (float)pass[i].height };
setUniform2fv(u->Pass[i].InputSize, passSize);
setUniform2fv(u->Pass[i].TextureSize, passSize);
setTexture1i (u->Pass[i].Texture, pass[i].texture);
setTexCoords (u->Pass[i].TexCoord);
setTexture1i(u->Pass[i].Texture, pass[i].texture);
setTexCoords(u->Pass[i].TexCoord);
}
}
// PassPrev parameter
for (unsigned int i = 0; i < p; i++)
{
float passSize[2] = { (float) pass[i].width, (float) pass[i].height };
float passSize[2] = { (float)pass[i].width, (float)pass[i].height };
setUniform2fv(u->PassPrev[p - i].InputSize, passSize);
setUniform2fv(u->PassPrev[p - i].TextureSize, passSize);
setTexture1i (u->PassPrev[p - i].Texture, pass[i].texture);
setTexCoords (u->PassPrev[p - i].TexCoord);
setTexture1i(u->PassPrev[p - i].Texture, pass[i].texture);
setTexCoords(u->PassPrev[p - i].TexCoord);
}
// User and Preset Parameters
for (unsigned int i = 0; i < param.size(); i++)
{
setUniform1f (param[i].unif[p], param[i].val);
setUniform1f(param[i].unif[p], param[i].val);
}
glActiveTexture (GL_TEXTURE0);
glBindBuffer (GL_ARRAY_BUFFER, 0);
glActiveTexture(GL_TEXTURE0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void GLSLShader::clear_shader_vars (void)
void GLSLShader::clear_shader_vars(void)
{
for (unsigned int i = 0; i < vaos.size(); i++)
glDisableVertexAttribArray (vaos[i]);
glDisableVertexAttribArray(vaos[i]);
vaos.clear();
}
@ -980,15 +984,15 @@ void GLSLShader::clear_shader_vars (void)
#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)
void GLSLShader::save (const char *filename)
void GLSLShader::save(const char *filename)
{
FILE *file = fopen (filename, "wb");
FILE *file = fopen(filename, "wb");
if (!file)
{
printf ("Couldn't save shader config to %s\n", filename);
printf("Couldn't save shader config to %s\n", filename);
}
fprintf (file, "shaders = \"%d\"\n", (unsigned int) pass.size() - 1);
fprintf(file, "shaders = \"%d\"\n", (unsigned int)pass.size() - 1);
for (unsigned int pn = 1; pn < pass.size(); pn++)
{
@ -1000,67 +1004,67 @@ void GLSLShader::save (const char *filename)
{
outs("filter_linear", p->filter == GL_LINEAR ? "true" : "false");
}
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));
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));
if (p->scale_type_x == GLSL_ABSOLUTE)
outd ("scale_x", (int) p->scale_x);
outd("scale_x", (int)p->scale_x);
else
outf ("scale_x", p->scale_x);
outf("scale_x", p->scale_x);
outs ("scale_type_y", scale_enum_to_string(p->scale_type_y));
outs("scale_type_y", scale_enum_to_string(p->scale_type_y));
if (p->scale_type_y == GLSL_ABSOLUTE)
outd ("scale_y", (int) p->scale_y);
outd("scale_y", (int)p->scale_y);
else
outf ("scale_y", p->scale_y);
outf("scale_y", p->scale_y);
if (p->frame_count_mod)
outd ("frame_count_mod", p->frame_count_mod);
outd("frame_count_mod", p->frame_count_mod);
}
if (param.size() > 0)
{
fprintf (file, "parameters = \"");
fprintf(file, "parameters = \"");
for (unsigned int i = 0; i < param.size(); i++)
{
fprintf (file, "%s%c", param[i].id, (i == param.size() - 1) ? '\"' : ';');
fprintf(file, "%s%c", param[i].id, (i == param.size() - 1) ? '\"' : ';');
}
fprintf (file, "\n");
fprintf(file, "\n");
}
for (unsigned int i = 0; i < param.size(); i++)
{
fprintf (file, "%s = \"%f\"\n", param[i].id, param[i].val);
fprintf(file, "%s = \"%f\"\n", param[i].id, param[i].val);
}
if (lut.size() > 0)
{
fprintf (file, "textures = \"");
fprintf(file, "textures = \"");
for (unsigned int i = 0; i < lut.size(); i++)
{
fprintf (file, "%s%c", lut[i].id, (i == lut.size() - 1) ? '\"' : ';');
fprintf(file, "%s%c", lut[i].id, (i == lut.size() - 1) ? '\"' : ';');
}
fprintf (file, "\n");
fprintf(file, "\n");
}
for (unsigned int i = 0; i < lut.size(); i++)
{
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");
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");
}
fclose (file);
fclose(file);
}
#undef outf
#undef outs
#undef outd
void GLSLShader::destroy (void)
void GLSLShader::destroy(void)
{
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
@ -1075,12 +1079,12 @@ void GLSLShader::destroy (void)
for (unsigned int i = 0; i < lut.size(); i++)
{
glDeleteTextures (1, &lut[i].texture);
glDeleteTextures(1, &lut[i].texture);
}
for (unsigned int i = 0; i < prev_frame.size (); i++)
for (unsigned int i = 0; i < prev_frame.size(); i++)
{
glDeleteTextures (1, &prev_frame[i].texture);
glDeleteTextures(1, &prev_frame[i].texture);
}
param.clear();

View File

@ -7,19 +7,19 @@
#ifndef __GLSL_H
#define __GLSL_H
#include <vector>
#include <deque>
#include <limits.h>
#include "../../conffile.h"
#include "shader_platform.h"
#include <deque>
#include <limits.h>
#include <vector>
static const unsigned int glsl_max_passes = 20;
typedef void (* GLSLViewportCallback) (int source_width, int source_height,
int viewport_x, int viewport_y,
int viewport_width, int viewport_height,
int *out_dst_x, int *out_dst_y,
int *out_dst_width, int *out_dst_height);
typedef void (*GLSLViewportCallback)(int source_width, int source_height,
int viewport_x, int viewport_y,
int viewport_width, int viewport_height,
int *out_dst_x, int *out_dst_y,
int *out_dst_width, int *out_dst_height);
enum GLSLScaleType
{
@ -40,7 +40,6 @@ typedef struct
GLint InputSize;
GLint TextureSize;
GLint TexCoord;
} GLSLUniformMetrics;
typedef struct
@ -68,7 +67,6 @@ typedef struct
GLSLUniformMetrics Pass[glsl_max_passes];
GLSLUniformMetrics PassPrev[glsl_max_passes];
GLint Lut[9];
} GLSLUniforms;
typedef struct
@ -120,21 +118,19 @@ typedef struct
typedef struct
{
bool load_shader (char *filename);
bool load_shader_file (char *filename);
void render (GLuint &orig, int width, int height, int viewport_x, int viewport_y, int viewport_width, int viewport_height, GLSLViewportCallback vpcallback);
void set_shader_vars (unsigned int pass);
void clear_shader_vars (void);
bool load_shader(char *filename);
bool load_shader_file(char *filename);
void render(GLuint &orig, int width, int height, int viewport_x,
int viewport_y, int viewport_width, int viewport_height,
GLSLViewportCallback vpcallback);
void set_shader_vars(unsigned int pass);
void clear_shader_vars(void);
void strip_parameter_pragmas(char *buffer);
GLuint compile_shader (char *program,
const char *aliases,
const char *defines,
GLuint type,
GLuint *out);
void save (const char *filename);
void destroy (void);
void register_uniforms (void);
GLuint compile_shader(char *program, const char *aliases,
const char *defines, GLuint type, GLuint *out);
void save(const char *filename);
void destroy(void);
void register_uniforms(void);
ConfigFile conf;
@ -149,7 +145,6 @@ typedef struct
GLuint vbo;
GLuint prev_fbo;
GLfloat *fa;
} GLSLShader;
#endif

View File

@ -11,178 +11,124 @@
#include "shader_helpers.h"
#include "shader_platform.h"
static void gl_error_callback( GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam )
static void gl_error_callback(GLenum source, GLenum type, GLuint id,
GLenum severity, GLsizei length,
const GLchar *message, const void *userParam)
{
if (type== GL_DEBUG_TYPE_ERROR)
if (type == GL_DEBUG_TYPE_ERROR)
{
fprintf( stderr, "GL: %s type = 0x%x, severity = 0x%x, \n %s\n",
(type == GL_DEBUG_TYPE_ERROR ? "*ERROR*" : ""),
type, severity, message );
fprintf(stderr, "GL: %s type = 0x%x, severity = 0x%x, \n %s\n",
(type == GL_DEBUG_TYPE_ERROR ? "*ERROR*" : ""), type, severity,
message);
}
else
{
fprintf( stderr, "GL type = 0x%x, severity = 0x%x, \n %s\n",
type, severity, message);
fprintf(stderr, "GL type = 0x%x, severity = 0x%x, \n %s\n", type,
severity, message);
}
return;
}
bool gl_version_at_least (int maj, int min)
int gl_version(void)
{
static int major_version = -1;
static int minor_version = -1;
static int version = -1;
if (major_version < 0 || minor_version < 0)
if (version < 0)
{
const char *version_string = (const char *) glGetString (GL_VERSION);
sscanf (version_string, "%d.%d", &major_version, &minor_version);
const char *version_string = (const char *)glGetString(GL_VERSION);
static int major_version = 1;
static int minor_version = 0;
sscanf(version_string, "%d.%d", &major_version, &minor_version);
version = major_version * 10 + minor_version;
}
if (major_version > maj)
return version;
}
bool gl_srgb_available(void)
{
if (gl_version() >= 30)
return true;
if (maj == major_version && minor_version >= min)
const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
if (strstr(extensions, "texture_sRGB") &&
strstr(extensions, "framebuffer_sRGB"))
return true;
return false;
}
bool srgb_available (void)
bool gl_float_texture_available(void)
{
if (gl_version_at_least (3, 0))
if (gl_version() >= 32)
return true;
const char *extensions = (const char *) glGetString (GL_EXTENSIONS);
const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
if (strstr (extensions, "texture_sRGB") &&
strstr (extensions, "framebuffer_sRGB"))
if (strstr(extensions, "texture_float"))
return true;
return false;
}
bool float_texture_available (void)
void gl_log_errors(void)
{
if (gl_version_at_least (3, 2))
return true;
const char *extensions = (const char *) glGetString (GL_EXTENSIONS);
if (strstr (extensions, "texture_float"))
return true;
return false;
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback((GLDEBUGPROC)gl_error_callback, 0);
}
void glLogErrors (void)
{
glEnable (GL_DEBUG_OUTPUT);
glDebugMessageCallback ((GLDEBUGPROC) gl_error_callback, 0);
}
bool loadPngImage(const char* name,
int& outWidth,
int& outHeight,
bool& outHasAlpha,
GLubyte** outData)
bool loadPngImage(const char *name, int &outWidth, int &outHeight,
bool &outHasAlpha, GLubyte **outData)
{
#ifdef HAVE_LIBPNG
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
FILE* fp;
FILE *fp;
if ((fp = fopen(name, "rb")) == NULL)
return false;
/* Create and initialize the png_struct
* with the desired error handler
* functions. If you want to use the
* default stderr and longjump method,
* you can supply NULL for the last
* three parameters. We also supply the
* the compiler header file version, so
* that we know if the application
* was compiled with a compatible version
* of the library. REQUIRED
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
if (png_ptr == NULL)
{
fclose(fp);
return false;
}
/* Allocate/initialize the memory
* for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return false;
}
/* Set error handling if you are
* using the setjmp/longjmp method
* (this is the normal method of
* doing things with libpng).
* REQUIRED unless you set up
* your own error handlers in
* the png_create_read_struct()
* earlier.
*/
if (setjmp(png_jmpbuf(png_ptr))) {
/* Free all of the memory associated
* with the png_ptr and info_ptr */
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fp);
/* If we get here, we had a
* problem reading the file */
return false;
}
/* Set up the output control if
* you are using standard C streams */
png_init_io(png_ptr, fp);
/* If we have already
* read some of the signature */
png_set_sig_bytes(png_ptr, sig_read);
/*
* If you have enough memory to read
* in the entire image at once, and
* you need to specify only
* transforms that can be controlled
* with one of the PNG_TRANSFORM_*
* bits (this presently excludes
* dithering, filling, setting
* background, and doing gamma
* adjustment), then you can read the
* entire image (including pixels)
* into the info structure with this
* call
*
* PNG_TRANSFORM_STRIP_16 |
* PNG_TRANSFORM_PACKING forces 8 bit
* PNG_TRANSFORM_EXPAND forces to
* expand a palette into RGB
*/
png_read_png(png_ptr,
info_ptr,
PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND,
(png_voidp)NULL);
png_read_png(png_ptr, info_ptr,
PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING |
PNG_TRANSFORM_EXPAND,
(png_voidp)NULL);
outWidth = png_get_image_width(png_ptr, info_ptr);
outHeight = png_get_image_height(png_ptr, info_ptr);
switch (png_get_color_type(png_ptr, info_ptr)) {
switch (png_get_color_type(png_ptr, info_ptr))
{
case PNG_COLOR_TYPE_RGBA:
outHasAlpha = true;
break;
@ -194,17 +140,19 @@ bool loadPngImage(const char* name,
fclose(fp);
return false;
}
unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
*outData = (unsigned char*)malloc(row_bytes * outHeight);
*outData = (unsigned char *)malloc(row_bytes * outHeight);
png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
for (int i = 0; i < outHeight; i++) {
for (int i = 0; i < outHeight; i++)
{
memcpy(*outData + (row_bytes * i), row_pointers[i], row_bytes);
}
/* Clean up after the read,
* and free any memory allocated */
* and free any memory allocated */
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
/* Close the file */
@ -217,9 +165,9 @@ bool loadPngImage(const char* name,
#endif
}
bool loadTGA(const char* filename, STGA& tgaFile)
bool loadTGA(const char *filename, STGA &tgaFile)
{
FILE* file;
FILE *file;
unsigned char type[4];
unsigned char info[6];
@ -233,7 +181,8 @@ bool loadTGA(const char* filename, STGA& tgaFile)
fread(&info, sizeof(char), 6, file);
// image type either 2 (color) or 3 (greyscale)
if (type[1] != 0 || (type[2] != 2 && type[2] != 3)) {
if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
{
fclose(file);
return false;
}
@ -242,7 +191,8 @@ bool loadTGA(const char* filename, STGA& tgaFile)
tgaFile.height = info[2] + info[3] * 256;
tgaFile.byteCount = info[4] / 8;
if (tgaFile.byteCount != 3 && tgaFile.byteCount != 4) {
if (tgaFile.byteCount != 3 && tgaFile.byteCount != 4)
{
fclose(file);
return false;
}
@ -250,17 +200,21 @@ bool loadTGA(const char* filename, STGA& tgaFile)
long imageSize = tgaFile.width * tgaFile.height * tgaFile.byteCount;
// allocate memory for image data
unsigned char* tempBuf = new unsigned char[imageSize];
unsigned char *tempBuf = new unsigned char[imageSize];
tgaFile.data = new unsigned char[tgaFile.width * tgaFile.height * 4];
// read in image data
fread(tempBuf, sizeof(unsigned char), imageSize, file);
// swap line order and convert to RBGA
for (int i = 0; i < tgaFile.height; i++) {
unsigned char* source = tempBuf + tgaFile.width * (tgaFile.height - 1 - i) * tgaFile.byteCount;
unsigned char* destination = tgaFile.data + tgaFile.width * i * 4;
for (int j = 0; j < tgaFile.width; j++) {
for (int i = 0; i < tgaFile.height; i++)
{
unsigned char *source = tempBuf + tgaFile.width *
(tgaFile.height - 1 - i) *
tgaFile.byteCount;
unsigned char *destination = tgaFile.data + tgaFile.width * i * 4;
for (int j = 0; j < tgaFile.width; j++)
{
destination[0] = source[2];
destination[1] = source[1];
destination[2] = source[0];
@ -277,4 +231,3 @@ bool loadTGA(const char* filename, STGA& tgaFile)
return true;
}

View File

@ -13,7 +13,7 @@ typedef struct _STGA
{
_STGA()
{
data = (unsigned char*)0;
data = (unsigned char *)0;
width = 0;
height = 0;
byteCount = 0;
@ -34,21 +34,14 @@ typedef struct _STGA
int width;
int height;
unsigned char byteCount;
unsigned char* data;
unsigned char *data;
} STGA;
bool loadPngImage(const char* name,
int& outWidth,
int& outHeight,
bool& outHasAlpha,
GLubyte** outData);
bool loadTGA(const char* filename, STGA& tgaFile);
void glLogErrors (void);
bool srgb_available (void);
bool float_texture_available (void);
bool gl_version_at_least (int maj, int min);
bool loadPngImage(const char *name, int &outWidth, int &outHeight,
bool &outHasAlpha, GLubyte **outData);
bool loadTGA(const char *filename, STGA &tgaFile);
void gl_log_errors(void);
bool gl_srgb_available(void);
bool gl_float_texture_available(void);
#endif // __SHADER_HELPERS_H