mpv/video/out/opengl/utils.h

118 lines
3.5 KiB
C
Raw Normal View History

#pragma once
#include <stdbool.h>
#include <math.h>
#include "video/out/vo.h"
#include "ra.h"
// A 3x2 matrix, with the translation part separate.
struct gl_transform {
// row-major, e.g. in mathematical notation:
// | m[0][0] m[0][1] |
// | m[1][0] m[1][1] |
float m[2][2];
float t[2];
};
vo_opengl: refactor pass_read_video and texture binding This is a pretty major rewrite of the internal texture binding mechanic, which makes it more flexible. In general, the difference between the old and current approaches is that now, all texture description is held in a struct img_tex and only explicitly bound with pass_bind. (Once bound, a texture unit is assumed to be set in stone and no longer tied to the img_tex) This approach makes the code inside pass_read_video significantly more flexible and cuts down on the number of weird special cases and spaghetti logic. It also has some improvements, e.g. cutting down greatly on the number of unnecessary conversion passes inside pass_read_video (which was previously mostly done to cope with the fact that the alternative would have resulted in a combinatorial explosion of code complexity). Some other notable changes (and potential improvements): - texture expansion is now *always* handled in pass_read_video, and the colormatrix never does this anymore. (Which means the code could probably be removed from the colormatrix generation logic, modulo some other VOs) - struct fbo_tex now stores both its "physical" and "logical" (configured) size, which cuts down on the amount of width/height baggage on some function calls - vo_opengl can now technically support textures with different bit depths (e.g. 10 bit luma, 8 bit chroma) - but the APIs it queries inside img_format.c doesn't export this (nor does ffmpeg support it, really) so the status quo of using the same tex_mul for all planes is kept. - dumb_mode is now only needed because of the indirect_fbo being in the main rendering pipeline. If we reintroduce p->use_indirect and thread a transform through the entire program this could be skipped where unnecessary, allowing for the removal of dumb_mode. But I'm not sure how to do this in a clean way. (Which is part of why it got introduced to begin with) - It would be trivial to resurrect source-shader now (it would just be one extra 'if' inside pass_read_video).
2016-03-05 10:29:19 +00:00
static const struct gl_transform identity_trans = {
.m = {{1.0, 0.0}, {0.0, 1.0}},
.t = {0.0, 0.0},
};
void gl_transform_ortho(struct gl_transform *t, float x0, float x1,
float y0, float y1);
// This treats m as an affine transformation, in other words m[2][n] gets
// added to the output.
static inline void gl_transform_vec(struct gl_transform t, float *x, float *y)
{
float vx = *x, vy = *y;
*x = vx * t.m[0][0] + vy * t.m[0][1] + t.t[0];
*y = vx * t.m[1][0] + vy * t.m[1][1] + t.t[1];
}
struct mp_rect_f {
float x0, y0, x1, y1;
};
// Semantic equality (fuzzy comparison)
static inline bool mp_rect_f_seq(struct mp_rect_f a, struct mp_rect_f b)
{
return fabs(a.x0 - b.x0) < 1e-6 && fabs(a.x1 - b.x1) < 1e-6 &&
fabs(a.y0 - b.y0) < 1e-6 && fabs(a.y1 - b.y1) < 1e-6;
}
static inline void gl_transform_rect(struct gl_transform t, struct mp_rect_f *r)
{
gl_transform_vec(t, &r->x0, &r->y0);
gl_transform_vec(t, &r->x1, &r->y1);
}
vo_opengl: refactor pass_read_video and texture binding This is a pretty major rewrite of the internal texture binding mechanic, which makes it more flexible. In general, the difference between the old and current approaches is that now, all texture description is held in a struct img_tex and only explicitly bound with pass_bind. (Once bound, a texture unit is assumed to be set in stone and no longer tied to the img_tex) This approach makes the code inside pass_read_video significantly more flexible and cuts down on the number of weird special cases and spaghetti logic. It also has some improvements, e.g. cutting down greatly on the number of unnecessary conversion passes inside pass_read_video (which was previously mostly done to cope with the fact that the alternative would have resulted in a combinatorial explosion of code complexity). Some other notable changes (and potential improvements): - texture expansion is now *always* handled in pass_read_video, and the colormatrix never does this anymore. (Which means the code could probably be removed from the colormatrix generation logic, modulo some other VOs) - struct fbo_tex now stores both its "physical" and "logical" (configured) size, which cuts down on the amount of width/height baggage on some function calls - vo_opengl can now technically support textures with different bit depths (e.g. 10 bit luma, 8 bit chroma) - but the APIs it queries inside img_format.c doesn't export this (nor does ffmpeg support it, really) so the status quo of using the same tex_mul for all planes is kept. - dumb_mode is now only needed because of the indirect_fbo being in the main rendering pipeline. If we reintroduce p->use_indirect and thread a transform through the entire program this could be skipped where unnecessary, allowing for the removal of dumb_mode. But I'm not sure how to do this in a clean way. (Which is part of why it got introduced to begin with) - It would be trivial to resurrect source-shader now (it would just be one extra 'if' inside pass_read_video).
2016-03-05 10:29:19 +00:00
static inline bool gl_transform_eq(struct gl_transform a, struct gl_transform b)
{
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
if (a.m[x][y] != b.m[x][y])
return false;
}
}
return a.t[0] == b.t[0] && a.t[1] == b.t[1];
}
void gl_transform_trans(struct gl_transform t, struct gl_transform *x);
struct fbodst {
struct ra_tex *tex;
bool flip; // mirror vertically
};
void gl_transform_ortho_fbodst(struct gl_transform *t, struct fbodst fbo);
// A pool of buffers, which can grow as needed
struct ra_buf_pool {
struct ra_buf_params current_params;
struct ra_buf **buffers;
int num_buffers;
int index;
};
void ra_buf_pool_uninit(struct ra *ra, struct ra_buf_pool *pool);
// Note: params->initial_data is *not* supported
struct ra_buf *ra_buf_pool_get(struct ra *ra, struct ra_buf_pool *pool,
const struct ra_buf_params *params);
// Helper that wraps ra_tex_upload using texture upload buffers to ensure that
// params->buf is always set. This is intended for RA-internal usage.
bool ra_tex_upload_pbo(struct ra *ra, struct ra_buf_pool *pbo,
const struct ra_tex_upload_params *params);
struct fbotex {
struct ra *ra;
struct ra_tex *tex;
int lw, lh; // logical (configured) size, <= than texture size
struct fbodst fbo;
};
void fbotex_uninit(struct fbotex *fbo);
bool fbotex_change(struct fbotex *fbo, struct ra *ra, struct mp_log *log,
int w, int h, const struct ra_format *fmt, int flags);
#define FBOTEX_FUZZY_W 1
#define FBOTEX_FUZZY_H 2
#define FBOTEX_FUZZY (FBOTEX_FUZZY_W | FBOTEX_FUZZY_H)
// A wrapper around ra_timer that does result pooling, averaging etc.
struct timer_pool;
struct timer_pool *timer_pool_create(struct ra *ra);
void timer_pool_destroy(struct timer_pool *pool);
void timer_pool_start(struct timer_pool *pool);
void timer_pool_stop(struct timer_pool *pool);
struct mp_pass_perf timer_pool_measure(struct timer_pool *pool);
// print a multi line string with line numbers (e.g. for shader sources)
// log, lev: module and log level, as in mp_msg()
void mp_log_source(struct mp_log *log, int lev, const char *src);