summaryrefslogtreecommitdiffstats
path: root/video/out/opengl/utils.c
diff options
context:
space:
mode:
authorBin Jin <bjin1990@gmail.com>2015-10-28 01:37:55 +0000
committerwm4 <wm4@nowhere>2015-11-05 17:38:20 +0100
commit27dc834f37cd2427798c8cb582a574409865d1e7 (patch)
treefcc4fdfb0a4c8b20958ee110d5d8068439779848 /video/out/opengl/utils.c
parent3f73d6352306d470821f3ea5078b7b7f8031f0d7 (diff)
downloadmpv-27dc834f37cd2427798c8cb582a574409865d1e7.tar.bz2
mpv-27dc834f37cd2427798c8cb582a574409865d1e7.tar.xz
vo_opengl: implement NNEDI3 prescaler
Implement NNEDI3, a neural network based deinterlacer. The shader is reimplemented in GLSL and supports both 8x4 and 8x6 sampling window now. This allows the shader to be licensed under LGPL2.1 so that it can be used in mpv. The current implementation supports uploading the NN weights (up to 51kb with placebo setting) in two different way, via uniform buffer object or hard coding into shader source. UBO requires OpenGL 3.1, which only guarantee 16kb per block. But I find that 64kb seems to be a default setting for recent card/driver (which nnedi3 is targeting), so I think we're fine here (with default nnedi3 setting the size of weights is 9kb). Hard-coding into shader requires OpenGL 3.3, for the "intBitsToFloat()" built-in function. This is necessary to precisely represent these weights in GLSL. I tried several human readable floating point number format (with really high precision as for single precision float), but for some reason they are not working nicely, bad pixels (with NaN value) could be produced with some weights set. We could also add support to upload these weights with texture, just for compatibility reason (etc. upscaling a still image with a low end graphics card). But as I tested, it's rather slow even with 1D texture (we probably had to use 2D texture due to dimension size limitation). Since there is always better choice to do NNEDI3 upscaling for still image (vapoursynth plugin), it's not implemented in this commit. If this turns out to be a popular demand from the user, it should be easy to add it later. For those who wants to optimize the performance a bit further, the bottleneck seems to be: 1. overhead to upload and access these weights, (in particular, the shader code will be regenerated for each frame, it's on CPU though). 2. "dot()" performance in the main loop. 3. "exp()" performance in the main loop, there are various fast implementation with some bit tricks (probably with the help of the intBitsToFloat function). The code is tested with nvidia card and driver (355.11), on Linux. Closes #2230
Diffstat (limited to 'video/out/opengl/utils.c')
-rw-r--r--video/out/opengl/utils.c29
1 files changed, 27 insertions, 2 deletions
diff --git a/video/out/opengl/utils.c b/video/out/opengl/utils.c
index 0026090df9..6ddb333e47 100644
--- a/video/out/opengl/utils.c
+++ b/video/out/opengl/utils.c
@@ -482,6 +482,7 @@ enum uniform_type {
UT_i,
UT_f,
UT_m,
+ UT_buffer,
};
struct sc_uniform {
@@ -493,6 +494,10 @@ struct sc_uniform {
union {
GLfloat f[9];
GLint i[4];
+ struct {
+ char* text;
+ GLint binding;
+ } buffer;
} v;
};
@@ -535,8 +540,11 @@ void gl_sc_reset(struct gl_shader_cache *sc)
{
sc->text[0] = '\0';
sc->header_text[0] = '\0';
- for (int n = 0; n < sc->num_uniforms; n++)
+ for (int n = 0; n < sc->num_uniforms; n++) {
talloc_free(sc->uniforms[n].name);
+ if (sc->uniforms[n].type == UT_buffer)
+ talloc_free(sc->uniforms[n].v.buffer.text);
+ }
sc->num_uniforms = 0;
}
@@ -697,6 +705,15 @@ void gl_sc_uniform_mat3(struct gl_shader_cache *sc, char *name,
transpose3x3(&u->v.f[0]);
}
+void gl_sc_uniform_buffer(struct gl_shader_cache *sc, char *name,
+ const char *text, int binding)
+{
+ struct sc_uniform *u = find_uniform(sc, name);
+ u->type = UT_buffer;
+ u->v.buffer.text = talloc_strdup(sc, text);
+ u->v.buffer.binding = binding;
+}
+
// This will call glBindAttribLocation() on the shader before it's linked
// (OpenGL requires this to happen before linking). Basically, it associates
// the input variable names with the fields in the vao.
@@ -723,6 +740,11 @@ static const char *vao_glsl_type(const struct gl_vao_entry *e)
// Assumes program is current (gl->UseProgram(program)).
static void update_uniform(GL *gl, GLuint program, struct sc_uniform *u)
{
+ if (u->type == UT_buffer) {
+ GLuint idx = gl->GetUniformBlockIndex(program, u->name);
+ gl->UniformBlockBinding(program, idx, u->v.buffer.binding);
+ return;
+ }
GLint loc = gl->GetUniformLocation(program, u->name);
if (loc < 0)
return;
@@ -885,7 +907,10 @@ void gl_sc_gen_shader_and_reset(struct gl_shader_cache *sc)
ADD(frag, "%s", frag_vaos);
for (int n = 0; n < sc->num_uniforms; n++) {
struct sc_uniform *u = &sc->uniforms[n];
- ADD(frag, "uniform %s %s;\n", u->glsl_type, u->name);
+ if (u->type == UT_buffer)
+ ADD(frag, "uniform %s { %s };\n", u->name, u->v.buffer.text);
+ else
+ ADD(frag, "uniform %s %s;\n", u->glsl_type, u->name);
}
// custom shader header
if (sc->header_text[0]) {