Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPV: Support using framebuffer ( OpenGL ES 2.0 / 3.0) #247

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions wiliwili/include/utils/ps4_mpv_shaders.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// Created by fang on 2023/12/6.
//

#pragma once

const int PS4_MPV_SHADER_VERT_LENGTH = 561;
const int PS4_MPV_SHADER_FRAG_LENGTH = 770;

const unsigned char PS4_MPV_SHADER_VERT[] = {
0x71, 0xbc, 0x91, 0xe8, 0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x9d, 0x00, 0x00, 0x00, 0x23, 0x76, 0x65, 0x72, 0x73, 0x69,
0x6f, 0x6e, 0x20, 0x31, 0x30, 0x30, 0x0a, 0x61, 0x74, 0x74, 0x72,
0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20,
0x61, 0x50, 0x6f, 0x73, 0x3b, 0x0a, 0x61, 0x74, 0x74, 0x72, 0x69,
0x62, 0x75, 0x74, 0x65, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61,
0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a, 0x76,
0x61, 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, 0x76, 0x65, 0x63, 0x32,
0x20, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a,
0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29,
0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f,
0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x76, 0x65,
0x63, 0x34, 0x28, 0x61, 0x50, 0x6f, 0x73, 0x2c, 0x20, 0x31, 0x2e,
0x30, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x54, 0x65, 0x78, 0x43,
0x6f, 0x6f, 0x72, 0x64, 0x20, 0x3d, 0x20, 0x61, 0x54, 0x65, 0x78,
0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a, 0x7d, 0x80, 0x01, 0x00,
0x00, 0x00, 0x04, 0xef, 0xbe, 0x9c, 0xfa, 0xdf, 0x45, 0x6e, 0x1a,
0x60, 0xba, 0x00, 0x01, 0x00, 0x02, 0xb8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x53, 0x68, 0x64, 0x72, 0x07, 0x00, 0x02,
0x00, 0x01, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0xff,
0xff, 0xff, 0xff, 0x42, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x01, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x17, 0x00,
0x02, 0x10, 0x00, 0x04, 0x03, 0x00, 0x01, 0x08, 0x02, 0x00, 0x0f,
0x00, 0x00, 0x00, 0xff, 0x03, 0xeb, 0xbe, 0x09, 0x00, 0x00, 0x00,
0x00, 0x21, 0x80, 0xbe, 0xf2, 0x02, 0x00, 0x7e, 0x80, 0x02, 0x02,
0x7e, 0xcf, 0x08, 0x00, 0xf8, 0x04, 0x05, 0x06, 0x00, 0x0f, 0x02,
0x00, 0xf8, 0x08, 0x09, 0x01, 0x00, 0x00, 0x00, 0x81, 0xbf, 0x39,
0x6a, 0x06, 0x00, 0x08, 0x00, 0x00, 0x6b, 0x01, 0x03, 0x01, 0x00,
0x90, 0x50, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00,
0x00, 0x17, 0x00, 0x02, 0x10, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0xef, 0xbe, 0x00, 0x00, 0x4f, 0x72, 0x62, 0x53, 0x68,
0x64, 0x72, 0x07, 0x45, 0x28, 0x00, 0x00, 0x03, 0x02, 0x0c, 0x03,
0x9c, 0xfa, 0xdf, 0x45, 0x6e, 0x1a, 0x60, 0xba, 0xe9, 0x9c, 0xb6,
0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x02, 0x24, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00,
0x00, 0x01, 0x24, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00,
0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x01, 0x24, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00,
0x03, 0x2e, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00,
0x00, 0x40, 0x00, 0x00, 0x00, 0x61, 0x50, 0x6f, 0x73, 0x00, 0x41,
0x50, 0x4f, 0x53, 0x00, 0x61, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f,
0x72, 0x64, 0x00, 0x41, 0x54, 0x45, 0x58, 0x43, 0x4f, 0x4f, 0x52,
0x44, 0x00, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x00,
0x54, 0x45, 0x58, 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x00, 0x67, 0x6c,
0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x28,
0x6e, 0x6f, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x29, 0x00, 0x00, 0x00
};

const unsigned char PS4_MPV_SHADER_FRAG[] = {
0x71, 0xbc, 0x91, 0xe8, 0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0xce, 0x00, 0x00, 0x00, 0x23, 0x76, 0x65, 0x72, 0x73, 0x69,
0x6f, 0x6e, 0x20, 0x31, 0x30, 0x30, 0x0a, 0x70, 0x72, 0x65, 0x63,
0x69, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75,
0x6d, 0x70, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x3b, 0x0a, 0x76,
0x61, 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, 0x76, 0x65, 0x63, 0x32,
0x20, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a,
0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x73, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x72, 0x32, 0x44, 0x20, 0x6f, 0x75, 0x72, 0x54,
0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x3b, 0x0a, 0x75, 0x6e, 0x69,
0x66, 0x6f, 0x72, 0x6d, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20,
0x41, 0x6c, 0x70, 0x68, 0x61, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64,
0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20,
0x20, 0x20, 0x67, 0x6c, 0x5f, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f,
0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75,
0x72, 0x65, 0x32, 0x44, 0x28, 0x6f, 0x75, 0x72, 0x54, 0x65, 0x78,
0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x54, 0x65, 0x78, 0x43, 0x6f,
0x6f, 0x72, 0x64, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x67, 0x6c,
0x5f, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2e,
0x61, 0x20, 0x3d, 0x20, 0x41, 0x6c, 0x70, 0x68, 0x61, 0x3b, 0x0a,
0x7d, 0x0a, 0x20, 0x02, 0x00, 0x00, 0x00, 0x04, 0xef, 0xbe, 0x7f,
0x79, 0x7f, 0x72, 0xcc, 0x71, 0x93, 0xd2, 0x01, 0x01, 0x00, 0x02,
0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x68,
0x64, 0x72, 0x07, 0x00, 0x02, 0x00, 0x02, 0x14, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xa4, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x50, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x81, 0x00, 0x0c,
0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x0f, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1b, 0x01, 0x02,
0x00, 0x00, 0x00, 0x04, 0x03, 0x01, 0x00, 0x0c, 0x00, 0x02, 0x00,
0x10, 0x00, 0x0f, 0x00, 0x00, 0x00, 0xff, 0x03, 0xeb, 0xbe, 0x10,
0x00, 0x00, 0x00, 0x7e, 0x04, 0x92, 0xbe, 0x7e, 0x0a, 0xfe, 0xbe,
0x10, 0x03, 0xfc, 0xbe, 0x00, 0x03, 0x80, 0xc0, 0x00, 0x00, 0x08,
0xc8, 0x01, 0x00, 0x09, 0xc8, 0x00, 0x01, 0x0c, 0xc8, 0x01, 0x01,
0x0d, 0xc8, 0x00, 0x07, 0x80, 0xf0, 0x02, 0x00, 0x61, 0x00, 0x7f,
0x00, 0x8c, 0xbf, 0x00, 0x01, 0x00, 0xc2, 0x7f, 0x00, 0x8c, 0xbf,
0x00, 0x02, 0x06, 0x7e, 0x12, 0x04, 0xfe, 0xbe, 0x70, 0x0f, 0x8c,
0xbf, 0x00, 0x03, 0x00, 0x5e, 0x02, 0x07, 0x02, 0x5e, 0x0f, 0x1c,
0x00, 0xf8, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x81, 0xbf, 0x6a,
0x08, 0x00, 0x04, 0x00, 0x51, 0x41, 0x00, 0xcd, 0x68, 0x00, 0x00,
0x0c, 0x00, 0x00, 0x00, 0x1b, 0x01, 0x02, 0x00, 0x00, 0x00, 0x04,
0x03, 0x01, 0x00, 0x0c, 0x00, 0x02, 0x00, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xef, 0xbe, 0x00, 0x00, 0x4f,
0x72, 0x62, 0x53, 0x68, 0x64, 0x72, 0x07, 0x41, 0x5c, 0x00, 0x00,
0x03, 0x04, 0x0c, 0x03, 0x7f, 0x79, 0x7f, 0x72, 0xcc, 0x71, 0x93,
0xd2, 0x54, 0xae, 0x71, 0xe6, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x05, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x16,
0x04, 0x6a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00,
0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x01, 0x24, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00,
0x03, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00,
0x00, 0x23, 0x00, 0x00, 0x00, 0x6f, 0x75, 0x72, 0x54, 0x65, 0x78,
0x74, 0x75, 0x72, 0x65, 0x00, 0x5f, 0x5f, 0x47, 0x4c, 0x4f, 0x42,
0x41, 0x4c, 0x5f, 0x43, 0x42, 0x5f, 0x5f, 0x00, 0x41, 0x6c, 0x70,
0x68, 0x61, 0x00, 0x28, 0x6e, 0x6f, 0x5f, 0x6e, 0x61, 0x6d, 0x65,
0x29, 0x00, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x00,
0x54, 0x45, 0x58, 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x00, 0x67, 0x6c,
0x5f, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00
};
42 changes: 28 additions & 14 deletions wiliwili/include/view/mpv_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#if defined(MPV_SW_RENDER)
#elif defined(BOREALIS_USE_DEKO3D)
#include <mpv/render_dk3d.h>
#else
#elif defined(BOREALIS_USE_OPENGL)
#include <mpv/render_gl.h>
#if defined(__PSV__) || defined(PS4)
#include <GLES2/gl2.h>
Expand All @@ -25,14 +25,27 @@
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#endif
#include <nanovg_gl.h>
#if defined(USE_GL2)
// 对于 OpenGL 2.0 平台,不支持独立创建 framebuffer
#define MPV_NO_FB
#endif
#if !defined(MPV_NO_FB) && !defined(MPV_SW_RENDER) && !defined(USE_GL2)
// 将视频绘制到独立的 framebuffer
#define MPV_USE_FB
#if !defined(USE_GLES2) && !defined(USE_GLES3)
// 虽然 gles3 理论时也支持 vao 但是部分平台上实际不支持(比如 ANGLE)
#define MPV_USE_VAO
#endif
struct GLShader {
GLuint prog;
GLuint vao;
GLuint vbo;
GLuint ebo;
#ifdef MPV_USE_VAO
GLuint vao;
#endif
};
#endif
#endif

typedef enum MpvEventEnum {
MPV_LOADED,
Expand Down Expand Up @@ -163,11 +176,12 @@ class MPVCore : public brls::Singleton<MPVCore> {
* wiliwili 的 UWP 版本因为用了dx12,所以使用此方式绘制。
* 在向新的平台移植时推荐优先使用此方式绘制,便于移植,之后再考虑优化问题。
* 2. MPV_NO_FB: 绘制到默认的 framebuffer 上; 性能适中。
* 适合 树莓派、PSV 等 OpenGL <ES> 2.0 的平台;因为不支持独立创建 framebuffer,所以让 mpv 先绘制到整个屏幕上,再用UI来覆盖。
* 使用 deko3d (switch only) 时,采用 MPV_NO_FB 方式绘制,因为实现起来比较简单。
* 使用 OpenGL ES 3.0 时,也需要采用 MPV_NO_FB 方式绘制,因为独立绘制的 GLSL 脚本没有做适配。
* 3. 默认绘制方式: 将视频绘制到独立的 framebuffer 上,需要显示在屏幕上时再贴到默认的 framebuffer 上的某个位置;性能最佳。
* 支持图形API OpenGL 3.2+。
* 适合 树莓派 等只支持 OpenGL 2.0 的平台;因为不支持独立创建 framebuffer,
* 所以让 mpv 先绘制到整个屏幕上,再用UI来覆盖。
* 使用 deko3d (switch only) 时, 暂时也采用 MPV_NO_FB 方式绘制。
* 3. 默认绘制方式: 将视频绘制到独立的 framebuffer 上,
* 需要显示在屏幕上时再贴到默认的 framebuffer 上的某个位置;性能最佳。
* 支持图形API OpenGL 3.2+ / OpenGL ES 2.0+。
* 在部分平台上支持 direct rendering, 减少 CPU 和 GPU 间的拷贝进一步提升了性能。
*/
void draw(brls::Rect rect, float alpha = 1.0);
Expand Down Expand Up @@ -286,15 +300,17 @@ class MPVCore : public brls::Singleton<MPVCore> {
{ MPV_RENDER_PARAM_INVALID,
nullptr }};
#elif defined(MPV_NO_FB)
GLint default_framebuffer = 0;
mpv_opengl_fbo mpv_fbo{0, 1920, 1080};
int flip_y{1};
mpv_render_param mpv_params[3] = {{MPV_RENDER_PARAM_OPENGL_FBO, &mpv_fbo},
{MPV_RENDER_PARAM_FLIP_Y, &flip_y},
{ MPV_RENDER_PARAM_INVALID,
nullptr }};
#else
GLuint media_framebuffer = 0;
GLuint media_texture = 0;
GLint default_framebuffer = 0;
GLuint media_framebuffer = 0;
GLuint media_texture = 0;
GLShader shader{0};
mpv_opengl_fbo mpv_fbo{0, 1920, 1080};
int flip_y{1};
Expand All @@ -319,11 +335,9 @@ class MPVCore : public brls::Singleton<MPVCore> {
/// Will be called in main thread to get events from mpv core
void eventMainLoop();

void deleteFrameBuffer();

void deleteShader();
void initializeVideo();

void initializeGL();
void uninitializeVideo();

void init();

Expand Down
2 changes: 2 additions & 0 deletions wiliwili/source/utils/config_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,8 @@ void ProgramConfig::save() {
}

void ProgramConfig::init() {
brls::Logger::info("wiliwili {}", APPVersion::instance().git_tag);

// Set min_threads and max_threads of http thread pool
curl_global_init(CURL_GLOBAL_DEFAULT);
cpr::async::startup(THREAD_POOL_MIN_THREAD_NUM, THREAD_POOL_MAX_THREAD_NUM,
Expand Down
Loading