mirror of
https://repo.dec05eba.com/gpu-screen-recorder
synced 2026-04-04 10:36:37 +09:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db04b2e55e | ||
|
|
5029906c34 | ||
|
|
9de04e74ea | ||
|
|
54e5ec6193 | ||
|
|
01040796ee | ||
|
|
152d3d7536 | ||
|
|
ff01f69006 | ||
|
|
6910009e07 | ||
|
|
4e614a18bc |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -4,8 +4,8 @@ compile_commands.json
|
||||
tests/sibs-build/
|
||||
tests/compile_commands.json
|
||||
|
||||
external/wlr-export-dmabuf-unstable-v1-client-protocol.h
|
||||
external/wlr-export-dmabuf-unstable-v1-protocol.c
|
||||
**/xdg-output-unstable-v1-client-protocol.h
|
||||
**/xdg-output-unstable-v1-protocol.c
|
||||
|
||||
.clangd/
|
||||
.cache/
|
||||
|
||||
@@ -87,6 +87,7 @@ These are the dependencies needed to build GPU Screen Recorder:
|
||||
* libcap
|
||||
* wayland-client
|
||||
* wayland-egl
|
||||
* wayland-scanner
|
||||
|
||||
## Runtime dependencies
|
||||
There are also additional dependencies needed at runtime depending on your GPU vendor:
|
||||
|
||||
4
TODO
4
TODO
@@ -260,4 +260,6 @@ External texture doesn't work on nvidia x11, probably because of glx context (re
|
||||
|
||||
Add option to save replay buffer on disk instead of ram.
|
||||
|
||||
nvfbc capture cursor with cursor.h instead and composite that on top. This allows us to also always get a cursor in direct capture mode. This could possible give better performance as well.
|
||||
nvfbc capture cursor with cursor.h instead and composite that on top. This allows us to also always get a cursor in direct capture mode. This could possible give better performance as well.
|
||||
|
||||
Maybe remove external shader code and make a simple external to internal texture converter (compute shader), to reduce texture sampling. Maybe this is faster?
|
||||
@@ -6,7 +6,8 @@
|
||||
#include "vec2.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#define GSR_COLOR_CONVERSION_MAX_SHADERS 12
|
||||
#define GSR_COLOR_CONVERSION_MAX_COMPUTE_SHADERS 12
|
||||
#define GSR_COLOR_CONVERSION_MAX_GRAPHICS_SHADERS 6
|
||||
#define GSR_COLOR_CONVERSION_MAX_FRAMEBUFFERS 2
|
||||
|
||||
typedef enum {
|
||||
@@ -37,12 +38,17 @@ typedef enum {
|
||||
GSR_ROT_270
|
||||
} gsr_rotation;
|
||||
|
||||
typedef struct {
|
||||
int rotation_matrix;
|
||||
int offset;
|
||||
} gsr_color_graphics_uniforms;
|
||||
|
||||
typedef struct {
|
||||
int rotation_matrix;
|
||||
int source_position;
|
||||
int target_position;
|
||||
int scale;
|
||||
} gsr_color_uniforms;
|
||||
} gsr_color_compute_uniforms;
|
||||
|
||||
typedef struct {
|
||||
gsr_egl *egl;
|
||||
@@ -58,8 +64,15 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
gsr_color_conversion_params params;
|
||||
gsr_color_uniforms uniforms[GSR_COLOR_CONVERSION_MAX_SHADERS];
|
||||
gsr_shader shaders[GSR_COLOR_CONVERSION_MAX_SHADERS];
|
||||
gsr_color_compute_uniforms compute_uniforms[GSR_COLOR_CONVERSION_MAX_COMPUTE_SHADERS];
|
||||
gsr_shader compute_shaders[GSR_COLOR_CONVERSION_MAX_COMPUTE_SHADERS];
|
||||
|
||||
/* These are only loader if compute shaders (of the same type) fail to load */
|
||||
gsr_color_graphics_uniforms graphics_uniforms[GSR_COLOR_CONVERSION_MAX_GRAPHICS_SHADERS];
|
||||
gsr_shader graphics_shaders[GSR_COLOR_CONVERSION_MAX_GRAPHICS_SHADERS];
|
||||
|
||||
bool compute_shaders_failed_to_load;
|
||||
bool external_compute_shaders_failed_to_load;
|
||||
|
||||
unsigned int framebuffers[GSR_COLOR_CONVERSION_MAX_FRAMEBUFFERS];
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef GSR_SHADER_H
|
||||
#define GSR_SHADER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct gsr_egl gsr_egl;
|
||||
|
||||
typedef struct {
|
||||
@@ -16,4 +18,6 @@ int gsr_shader_bind_attribute_location(gsr_shader *self, const char *attribute,
|
||||
void gsr_shader_use(gsr_shader *self);
|
||||
void gsr_shader_use_none(gsr_shader *self);
|
||||
|
||||
void gsr_shader_enable_debug_output(bool enable);
|
||||
|
||||
#endif /* GSR_SHADER_H */
|
||||
|
||||
@@ -207,7 +207,7 @@ static uint32_t plane_get_properties(int drmfd, uint32_t plane_id, int *x, int *
|
||||
return property_mask;
|
||||
}
|
||||
|
||||
/* Returns 0 if not found */
|
||||
/* Returns NULL if not found */
|
||||
static const connector_crtc_pair* get_connector_pair_by_crtc_id(const connector_to_crtc_map *c2crtc_map, uint32_t crtc_id) {
|
||||
for(int i = 0; i < c2crtc_map->num_maps; ++i) {
|
||||
if(c2crtc_map->maps[i].crtc_id == crtc_id)
|
||||
@@ -433,81 +433,6 @@ static double clock_get_monotonic_seconds(void) {
|
||||
return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001;
|
||||
}
|
||||
|
||||
// static bool readlink_realpath(const char *filepath, char *buffer) {
|
||||
// char symlinked_path[PATH_MAX];
|
||||
// ssize_t bytes_written = readlink(filepath, symlinked_path, sizeof(symlinked_path) - 1);
|
||||
// if(bytes_written == -1 && errno == EINVAL) {
|
||||
// /* Not a symlink */
|
||||
// snprintf(symlinked_path, sizeof(symlinked_path), "%s", filepath);
|
||||
// } else if(bytes_written == -1) {
|
||||
// return false;
|
||||
// } else {
|
||||
// symlinked_path[bytes_written] = '\0';
|
||||
// }
|
||||
|
||||
// if(!realpath(symlinked_path, buffer))
|
||||
// return false;
|
||||
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// static void file_get_directory(char *filepath) {
|
||||
// char *end = strrchr(filepath, '/');
|
||||
// if(end == NULL)
|
||||
// filepath[0] = '\0';
|
||||
// else
|
||||
// *end = '\0';
|
||||
// }
|
||||
|
||||
// static bool string_ends_with(const char *str, const char *ends_with) {
|
||||
// const int len = strlen(str);
|
||||
// const int ends_with_len = strlen(ends_with);
|
||||
// return len >= ends_with_len && memcmp(str + len - ends_with_len, ends_with, ends_with_len) == 0;
|
||||
// }
|
||||
|
||||
// This is not foolproof, but the assumption is that gsr-kms-server and gpu-screen-recorder are installed in the same directory
|
||||
// in a location that only the root user can write to (usually /usr/bin or /usr/local/bin) and if the client runs from that location
|
||||
// and is called gpu-screen-recorder then gsr-kms-server can only be used by a malicious program if the malicious program
|
||||
// had root access, to modify that program install directory.
|
||||
// static bool is_remote_peer_program_gpu_screen_recorder(int socket_fd) {
|
||||
// // TODO: Use SO_PEERPIDFD on kernel >= 6.5 to avoid a race condition in the /proc/<pid> check
|
||||
// struct ucred cred;
|
||||
// socklen_t ucred_len = sizeof(cred);
|
||||
// if(getsockopt(socket_fd, SOL_SOCKET, SO_PEERCRED, &cred, &ucred_len) == -1) {
|
||||
// fprintf(stderr, "kms server error: failed to get peer credentials, error: %s\n", strerror(errno));
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// char self_directory[PATH_MAX];
|
||||
// if(!readlink_realpath("/proc/self/exe", self_directory)) {
|
||||
// fprintf(stderr, "kms server error: failed to resolve /proc/self/exe\n");
|
||||
// return false;
|
||||
// }
|
||||
// file_get_directory(self_directory);
|
||||
|
||||
// char peer_directory[PATH_MAX];
|
||||
// char peer_exe_path[PATH_MAX];
|
||||
// snprintf(peer_exe_path, sizeof(peer_exe_path), "/proc/%d/exe", (int)cred.pid);
|
||||
// if(!readlink_realpath(peer_exe_path, peer_directory)) {
|
||||
// fprintf(stderr, "kms server error: failed to resolve /proc/self/exe\n");
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// if(!string_ends_with(peer_directory, "/gpu-screen-recorder")) {
|
||||
// fprintf(stderr, "kms server error: only gpu-screen-recorder can use gsr-kms-server. client program location is %s\n", peer_directory);
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// file_get_directory(peer_directory);
|
||||
|
||||
// if(strcmp(self_directory, peer_directory) != 0) {
|
||||
// fprintf(stderr, "kms server error: the client program is in directory %s but only programs in %s can run gsr-kms-server\n", peer_directory, self_directory);
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// return true;
|
||||
// }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int res = 0;
|
||||
int socket_fd = 0;
|
||||
@@ -579,11 +504,6 @@ int main(int argc, char **argv) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
// if(!is_remote_peer_program_gpu_screen_recorder(socket_fd)) {
|
||||
// res = 3;
|
||||
// goto done;
|
||||
// }
|
||||
|
||||
for(;;) {
|
||||
gsr_kms_request request;
|
||||
request.version = 0;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
project('gpu-screen-recorder', ['c', 'cpp'], version : '5.3.6', default_options : ['warning_level=2'])
|
||||
project('gpu-screen-recorder', ['c', 'cpp'], version : '5.3.8', default_options : ['warning_level=2'])
|
||||
|
||||
add_project_arguments('-Wshadow', language : ['c', 'cpp'])
|
||||
if get_option('buildtype') == 'debug'
|
||||
@@ -41,6 +41,9 @@ src = [
|
||||
'src/main.cpp',
|
||||
]
|
||||
|
||||
subdir('protocol')
|
||||
src += protocol_src
|
||||
|
||||
dep = [
|
||||
dependency('threads'),
|
||||
dependency('libavcodec'),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "gpu-screen-recorder"
|
||||
type = "executable"
|
||||
version = "5.3.6"
|
||||
version = "5.3.8"
|
||||
platforms = ["posix"]
|
||||
|
||||
[config]
|
||||
|
||||
25
protocol/meson.build
Normal file
25
protocol/meson.build
Normal file
@@ -0,0 +1,25 @@
|
||||
wayland_scanner = dependency('wayland-scanner', native: true)
|
||||
wayland_scanner_path = wayland_scanner.get_variable(pkgconfig: 'wayland_scanner')
|
||||
wayland_scanner_prog = find_program(wayland_scanner_path, native: true)
|
||||
|
||||
wayland_scanner_code = generator(
|
||||
wayland_scanner_prog,
|
||||
output: '@BASENAME@-protocol.c',
|
||||
arguments: ['private-code', '@INPUT@', '@OUTPUT@'],
|
||||
)
|
||||
|
||||
wayland_scanner_client = generator(
|
||||
wayland_scanner_prog,
|
||||
output: '@BASENAME@-client-protocol.h',
|
||||
arguments: ['client-header', '@INPUT@', '@OUTPUT@'],
|
||||
)
|
||||
|
||||
protocols = [
|
||||
'xdg-output-unstable-v1.xml',
|
||||
]
|
||||
|
||||
protocol_src = []
|
||||
foreach xml : protocols
|
||||
protocol_src += wayland_scanner_code.process(xml)
|
||||
protocol_src += wayland_scanner_client.process(xml)
|
||||
endforeach
|
||||
222
protocol/xdg-output-unstable-v1.xml
Normal file
222
protocol/xdg-output-unstable-v1.xml
Normal file
@@ -0,0 +1,222 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<protocol name="xdg_output_unstable_v1">
|
||||
|
||||
<copyright>
|
||||
Copyright © 2017 Red Hat Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice (including the next
|
||||
paragraph) shall be included in all copies or substantial portions of the
|
||||
Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
</copyright>
|
||||
|
||||
<description summary="Protocol to describe output regions">
|
||||
This protocol aims at describing outputs in a way which is more in line
|
||||
with the concept of an output on desktop oriented systems.
|
||||
|
||||
Some information are more specific to the concept of an output for
|
||||
a desktop oriented system and may not make sense in other applications,
|
||||
such as IVI systems for example.
|
||||
|
||||
Typically, the global compositor space on a desktop system is made of
|
||||
a contiguous or overlapping set of rectangular regions.
|
||||
|
||||
The logical_position and logical_size events defined in this protocol
|
||||
might provide information identical to their counterparts already
|
||||
available from wl_output, in which case the information provided by this
|
||||
protocol should be preferred to their equivalent in wl_output. The goal is
|
||||
to move the desktop specific concepts (such as output location within the
|
||||
global compositor space, etc.) out of the core wl_output protocol.
|
||||
|
||||
Warning! The protocol described in this file is experimental and
|
||||
backward incompatible changes may be made. Backward compatible
|
||||
changes may be added together with the corresponding interface
|
||||
version bump.
|
||||
Backward incompatible changes are done by bumping the version
|
||||
number in the protocol and interface names and resetting the
|
||||
interface version. Once the protocol is to be declared stable,
|
||||
the 'z' prefix and the version number in the protocol and
|
||||
interface names are removed and the interface version number is
|
||||
reset.
|
||||
</description>
|
||||
|
||||
<interface name="zxdg_output_manager_v1" version="3">
|
||||
<description summary="manage xdg_output objects">
|
||||
A global factory interface for xdg_output objects.
|
||||
</description>
|
||||
|
||||
<request name="destroy" type="destructor">
|
||||
<description summary="destroy the xdg_output_manager object">
|
||||
Using this request a client can tell the server that it is not
|
||||
going to use the xdg_output_manager object anymore.
|
||||
|
||||
Any objects already created through this instance are not affected.
|
||||
</description>
|
||||
</request>
|
||||
|
||||
<request name="get_xdg_output">
|
||||
<description summary="create an xdg output from a wl_output">
|
||||
This creates a new xdg_output object for the given wl_output.
|
||||
</description>
|
||||
<arg name="id" type="new_id" interface="zxdg_output_v1"/>
|
||||
<arg name="output" type="object" interface="wl_output"/>
|
||||
</request>
|
||||
</interface>
|
||||
|
||||
<interface name="zxdg_output_v1" version="3">
|
||||
<description summary="compositor logical output region">
|
||||
An xdg_output describes part of the compositor geometry.
|
||||
|
||||
This typically corresponds to a monitor that displays part of the
|
||||
compositor space.
|
||||
|
||||
For objects version 3 onwards, after all xdg_output properties have been
|
||||
sent (when the object is created and when properties are updated), a
|
||||
wl_output.done event is sent. This allows changes to the output
|
||||
properties to be seen as atomic, even if they happen via multiple events.
|
||||
</description>
|
||||
|
||||
<request name="destroy" type="destructor">
|
||||
<description summary="destroy the xdg_output object">
|
||||
Using this request a client can tell the server that it is not
|
||||
going to use the xdg_output object anymore.
|
||||
</description>
|
||||
</request>
|
||||
|
||||
<event name="logical_position">
|
||||
<description summary="position of the output within the global compositor space">
|
||||
The position event describes the location of the wl_output within
|
||||
the global compositor space.
|
||||
|
||||
The logical_position event is sent after creating an xdg_output
|
||||
(see xdg_output_manager.get_xdg_output) and whenever the location
|
||||
of the output changes within the global compositor space.
|
||||
</description>
|
||||
<arg name="x" type="int"
|
||||
summary="x position within the global compositor space"/>
|
||||
<arg name="y" type="int"
|
||||
summary="y position within the global compositor space"/>
|
||||
</event>
|
||||
|
||||
<event name="logical_size">
|
||||
<description summary="size of the output in the global compositor space">
|
||||
The logical_size event describes the size of the output in the
|
||||
global compositor space.
|
||||
|
||||
Most regular Wayland clients should not pay attention to the
|
||||
logical size and would rather rely on xdg_shell interfaces.
|
||||
|
||||
Some clients such as Xwayland, however, need this to configure
|
||||
their surfaces in the global compositor space as the compositor
|
||||
may apply a different scale from what is advertised by the output
|
||||
scaling property (to achieve fractional scaling, for example).
|
||||
|
||||
For example, for a wl_output mode 3840×2160 and a scale factor 2:
|
||||
|
||||
- A compositor not scaling the monitor viewport in its compositing space
|
||||
will advertise a logical size of 3840×2160,
|
||||
|
||||
- A compositor scaling the monitor viewport with scale factor 2 will
|
||||
advertise a logical size of 1920×1080,
|
||||
|
||||
- A compositor scaling the monitor viewport using a fractional scale of
|
||||
1.5 will advertise a logical size of 2560×1440.
|
||||
|
||||
For example, for a wl_output mode 1920×1080 and a 90 degree rotation,
|
||||
the compositor will advertise a logical size of 1080x1920.
|
||||
|
||||
The logical_size event is sent after creating an xdg_output
|
||||
(see xdg_output_manager.get_xdg_output) and whenever the logical
|
||||
size of the output changes, either as a result of a change in the
|
||||
applied scale or because of a change in the corresponding output
|
||||
mode(see wl_output.mode) or transform (see wl_output.transform).
|
||||
</description>
|
||||
<arg name="width" type="int"
|
||||
summary="width in global compositor space"/>
|
||||
<arg name="height" type="int"
|
||||
summary="height in global compositor space"/>
|
||||
</event>
|
||||
|
||||
<event name="done" deprecated-since="3">
|
||||
<description summary="all information about the output have been sent">
|
||||
This event is sent after all other properties of an xdg_output
|
||||
have been sent.
|
||||
|
||||
This allows changes to the xdg_output properties to be seen as
|
||||
atomic, even if they happen via multiple events.
|
||||
|
||||
For objects version 3 onwards, this event is deprecated. Compositors
|
||||
are not required to send it anymore and must send wl_output.done
|
||||
instead.
|
||||
</description>
|
||||
</event>
|
||||
|
||||
<!-- Version 2 additions -->
|
||||
|
||||
<event name="name" since="2">
|
||||
<description summary="name of this output">
|
||||
Many compositors will assign names to their outputs, show them to the
|
||||
user, allow them to be configured by name, etc. The client may wish to
|
||||
know this name as well to offer the user similar behaviors.
|
||||
|
||||
The naming convention is compositor defined, but limited to
|
||||
alphanumeric characters and dashes (-). Each name is unique among all
|
||||
wl_output globals, but if a wl_output global is destroyed the same name
|
||||
may be reused later. The names will also remain consistent across
|
||||
sessions with the same hardware and software configuration.
|
||||
|
||||
Examples of names include 'HDMI-A-1', 'WL-1', 'X11-1', etc. However, do
|
||||
not assume that the name is a reflection of an underlying DRM
|
||||
connector, X11 connection, etc.
|
||||
|
||||
The name event is sent after creating an xdg_output (see
|
||||
xdg_output_manager.get_xdg_output). This event is only sent once per
|
||||
xdg_output, and the name does not change over the lifetime of the
|
||||
wl_output global.
|
||||
|
||||
This event is deprecated, instead clients should use wl_output.name.
|
||||
Compositors must still support this event.
|
||||
</description>
|
||||
<arg name="name" type="string" summary="output name"/>
|
||||
</event>
|
||||
|
||||
<event name="description" since="2">
|
||||
<description summary="human-readable description of this output">
|
||||
Many compositors can produce human-readable descriptions of their
|
||||
outputs. The client may wish to know this description as well, to
|
||||
communicate the user for various purposes.
|
||||
|
||||
The description is a UTF-8 string with no convention defined for its
|
||||
contents. Examples might include 'Foocorp 11" Display' or 'Virtual X11
|
||||
output via :1'.
|
||||
|
||||
The description event is sent after creating an xdg_output (see
|
||||
xdg_output_manager.get_xdg_output) and whenever the description
|
||||
changes. The description is optional, and may not be sent at all.
|
||||
|
||||
For objects of version 2 and lower, this event is only sent once per
|
||||
xdg_output, and the description does not change over the lifetime of
|
||||
the wl_output global.
|
||||
|
||||
This event is deprecated, instead clients should use
|
||||
wl_output.description. Compositors must still support this event.
|
||||
</description>
|
||||
<arg name="description" type="string" summary="output description"/>
|
||||
</event>
|
||||
|
||||
</interface>
|
||||
</protocol>
|
||||
@@ -5,18 +5,25 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define SHADER_INDEX_Y 0
|
||||
#define SHADER_INDEX_UV 1
|
||||
#define SHADER_INDEX_Y_EXTERNAL 2
|
||||
#define SHADER_INDEX_UV_EXTERNAL 3
|
||||
#define SHADER_INDEX_RGB 4
|
||||
#define SHADER_INDEX_RGB_EXTERNAL 5
|
||||
#define SHADER_INDEX_Y_BLEND 6
|
||||
#define SHADER_INDEX_UV_BLEND 7
|
||||
#define SHADER_INDEX_Y_EXTERNAL_BLEND 8
|
||||
#define SHADER_INDEX_UV_EXTERNAL_BLEND 9
|
||||
#define SHADER_INDEX_RGB_BLEND 10
|
||||
#define SHADER_INDEX_RGB_EXTERNAL_BLEND 11
|
||||
#define COMPUTE_SHADER_INDEX_Y 0
|
||||
#define COMPUTE_SHADER_INDEX_UV 1
|
||||
#define COMPUTE_SHADER_INDEX_Y_EXTERNAL 2
|
||||
#define COMPUTE_SHADER_INDEX_UV_EXTERNAL 3
|
||||
#define COMPUTE_SHADER_INDEX_RGB 4
|
||||
#define COMPUTE_SHADER_INDEX_RGB_EXTERNAL 5
|
||||
#define COMPUTE_SHADER_INDEX_Y_BLEND 6
|
||||
#define COMPUTE_SHADER_INDEX_UV_BLEND 7
|
||||
#define COMPUTE_SHADER_INDEX_Y_EXTERNAL_BLEND 8
|
||||
#define COMPUTE_SHADER_INDEX_UV_EXTERNAL_BLEND 9
|
||||
#define COMPUTE_SHADER_INDEX_RGB_BLEND 10
|
||||
#define COMPUTE_SHADER_INDEX_RGB_EXTERNAL_BLEND 11
|
||||
|
||||
#define GRAPHICS_SHADER_INDEX_Y 0
|
||||
#define GRAPHICS_SHADER_INDEX_UV 1
|
||||
#define GRAPHICS_SHADER_INDEX_Y_EXTERNAL 2
|
||||
#define GRAPHICS_SHADER_INDEX_UV_EXTERNAL 3
|
||||
#define GRAPHICS_SHADER_INDEX_RGB 4
|
||||
#define GRAPHICS_SHADER_INDEX_RGB_EXTERNAL 5
|
||||
|
||||
/* https://en.wikipedia.org/wiki/YCbCr, see study/color_space_transform_matrix.png */
|
||||
|
||||
@@ -78,18 +85,35 @@ static const char* color_format_range_get_transform_matrix(gsr_destination_color
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int load_compute_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_uniforms *uniforms, int max_local_size_dim, gsr_destination_color color_format, gsr_color_range color_range, bool external_texture, bool alpha_blending) {
|
||||
static void get_compute_shader_header(char *header, size_t header_size, bool external_texture) {
|
||||
if(external_texture) {
|
||||
snprintf(header, header_size,
|
||||
"#version 310 es\n"
|
||||
"#extension GL_ARB_compute_shader: enable\n"
|
||||
"#extension GL_OES_EGL_image_external : enable\n"
|
||||
"#extension GL_OES_EGL_image_external_essl3 : require\n"
|
||||
"layout(binding = 0) uniform highp samplerExternalOES img_input;\n"
|
||||
"layout(binding = 1) uniform highp sampler2D img_background;\n");
|
||||
} else {
|
||||
snprintf(header, header_size,
|
||||
"#version 420\n"
|
||||
"#extension GL_ARB_compute_shader: enable\n"
|
||||
"layout(binding = 0) uniform highp sampler2D img_input;\n"
|
||||
"layout(binding = 1) uniform highp sampler2D img_background;\n");
|
||||
}
|
||||
}
|
||||
|
||||
static int load_compute_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_compute_uniforms *uniforms, int max_local_size_dim, gsr_destination_color color_format, gsr_color_range color_range, bool external_texture, bool alpha_blending) {
|
||||
const char *color_transform_matrix = color_format_range_get_transform_matrix(color_format, color_range);
|
||||
|
||||
char header[512];
|
||||
get_compute_shader_header(header, sizeof(header), external_texture);
|
||||
|
||||
char compute_shader[2048];
|
||||
snprintf(compute_shader, sizeof(compute_shader),
|
||||
"#version 310 es\n"
|
||||
"#extension GL_OES_EGL_image_external : enable\n"
|
||||
"#extension GL_OES_EGL_image_external_essl3 : require\n"
|
||||
"precision highp float;\n"
|
||||
"%s"
|
||||
"layout (local_size_x = %d, local_size_y = %d, local_size_z = 1) in;\n"
|
||||
"layout(binding = 0) uniform highp %s img_input;\n"
|
||||
"layout(binding = 1) uniform highp sampler2D img_background;\n"
|
||||
"precision highp float;\n"
|
||||
"uniform ivec2 source_position;\n"
|
||||
"uniform ivec2 target_position;\n"
|
||||
"uniform vec2 scale;\n"
|
||||
@@ -103,13 +127,13 @@ static int load_compute_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_uni
|
||||
" ivec2 output_size = textureSize(img_background, 0);\n"
|
||||
" vec2 rotated_texel_coord = vec2(texel_coord - source_position - size_shift) * rotation_matrix + vec2(size_shift) + 0.5;\n"
|
||||
" vec2 output_texel_coord = vec2(texel_coord - source_position + target_position) + 0.5;\n"
|
||||
" vec4 source_color = texelFetch(img_input, ivec2(rotated_texel_coord), 0);\n"
|
||||
" vec4 source_color = texture(img_input, rotated_texel_coord/vec2(size));\n"
|
||||
" vec4 source_color_yuv = RGBtoYUV * vec4(source_color.rgb, 1.0);\n"
|
||||
" vec4 output_color_yuv = %s;\n"
|
||||
" float y_color = mix(output_color_yuv.r, source_color_yuv.r, source_color.a);\n"
|
||||
" imageStore(img_output, texel_coord + target_position, vec4(y_color, 1.0, 1.0, 1.0));\n"
|
||||
"}\n", max_local_size_dim, max_local_size_dim, external_texture ? "samplerExternalOES" : "sampler2D", color_transform_matrix,
|
||||
alpha_blending ? "texelFetch(img_background, ivec2(output_texel_coord), 0)" : "source_color_yuv");
|
||||
"}\n", header, max_local_size_dim, max_local_size_dim, color_transform_matrix,
|
||||
alpha_blending ? "texture(img_background, output_texel_coord/vec2(output_size))" : "source_color_yuv");
|
||||
|
||||
if(gsr_shader_init(shader, egl, NULL, NULL, compute_shader) != 0)
|
||||
return -1;
|
||||
@@ -121,18 +145,17 @@ static int load_compute_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_uni
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_compute_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_uniforms *uniforms, int max_local_size_dim, gsr_destination_color color_format, gsr_color_range color_range, bool external_texture, bool alpha_blending) {
|
||||
static int load_compute_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_compute_uniforms *uniforms, int max_local_size_dim, gsr_destination_color color_format, gsr_color_range color_range, bool external_texture, bool alpha_blending) {
|
||||
const char *color_transform_matrix = color_format_range_get_transform_matrix(color_format, color_range);
|
||||
|
||||
char header[512];
|
||||
get_compute_shader_header(header, sizeof(header), external_texture);
|
||||
|
||||
char compute_shader[2048];
|
||||
snprintf(compute_shader, sizeof(compute_shader),
|
||||
"#version 310 es\n"
|
||||
"#extension GL_OES_EGL_image_external : enable\n"
|
||||
"#extension GL_OES_EGL_image_external_essl3 : require\n"
|
||||
"precision highp float;\n"
|
||||
"%s"
|
||||
"layout (local_size_x = %d, local_size_y = %d, local_size_z = 1) in;\n"
|
||||
"layout(binding = 0) uniform highp %s img_input;\n"
|
||||
"layout(binding = 1) uniform highp sampler2D img_background;\n"
|
||||
"precision highp float;\n"
|
||||
"uniform ivec2 source_position;\n"
|
||||
"uniform ivec2 target_position;\n"
|
||||
"uniform vec2 scale;\n"
|
||||
@@ -146,13 +169,13 @@ static int load_compute_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_un
|
||||
" ivec2 output_size = textureSize(img_background, 0);\n"
|
||||
" vec2 rotated_texel_coord = vec2(texel_coord - source_position - size_shift) * rotation_matrix + vec2(size_shift) + 0.5;\n"
|
||||
" vec2 output_texel_coord = vec2(texel_coord - source_position + target_position) + 0.5;\n"
|
||||
" vec4 source_color = texelFetch(img_input, ivec2(rotated_texel_coord) << 1, 0);\n"
|
||||
" vec4 source_color = texture(img_input, rotated_texel_coord/vec2(size>>1));\n" // size/2
|
||||
" vec4 source_color_yuv = RGBtoYUV * vec4(source_color.rgb, 1.0);\n"
|
||||
" vec4 output_color_yuv = %s;\n"
|
||||
" vec2 uv_color = mix(output_color_yuv.rg, source_color_yuv.gb, source_color.a);\n"
|
||||
" imageStore(img_output, texel_coord + target_position, vec4(uv_color, 1.0, 1.0));\n"
|
||||
"}\n", max_local_size_dim, max_local_size_dim, external_texture ? "samplerExternalOES" : "sampler2D", color_transform_matrix,
|
||||
alpha_blending ? "texelFetch(img_background, ivec2(output_texel_coord), 0)" : "source_color_yuv");
|
||||
"}\n", header, max_local_size_dim, max_local_size_dim, color_transform_matrix,
|
||||
alpha_blending ? "texture(img_background, output_texel_coord/vec2(output_size))" : "source_color_yuv");
|
||||
|
||||
if(gsr_shader_init(shader, egl, NULL, NULL, compute_shader) != 0)
|
||||
return -1;
|
||||
@@ -164,16 +187,14 @@ static int load_compute_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_un
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_compute_shader_rgb(gsr_shader *shader, gsr_egl *egl, gsr_color_uniforms *uniforms, int max_local_size_dim, bool external_texture, bool alpha_blending) {
|
||||
static int load_compute_shader_rgb(gsr_shader *shader, gsr_egl *egl, gsr_color_compute_uniforms *uniforms, int max_local_size_dim, bool external_texture, bool alpha_blending) {
|
||||
char header[512];
|
||||
get_compute_shader_header(header, sizeof(header), external_texture);
|
||||
|
||||
char compute_shader[2048];
|
||||
snprintf(compute_shader, sizeof(compute_shader),
|
||||
"#version 310 es\n"
|
||||
"#extension GL_OES_EGL_image_external : enable\n"
|
||||
"#extension GL_OES_EGL_image_external_essl3 : require\n"
|
||||
"precision highp float;\n"
|
||||
"%s"
|
||||
"layout (local_size_x = %d, local_size_y = %d, local_size_z = 1) in;\n"
|
||||
"layout(binding = 0) uniform highp %s img_input;\n"
|
||||
"layout(binding = 1) uniform highp sampler2D img_background;\n"
|
||||
"uniform ivec2 source_position;\n"
|
||||
"uniform ivec2 target_position;\n"
|
||||
"uniform vec2 scale;\n"
|
||||
@@ -186,12 +207,12 @@ static int load_compute_shader_rgb(gsr_shader *shader, gsr_egl *egl, gsr_color_u
|
||||
" ivec2 output_size = textureSize(img_background, 0);\n"
|
||||
" vec2 rotated_texel_coord = vec2(texel_coord - source_position - size_shift) * rotation_matrix + vec2(size_shift) + 0.5;\n"
|
||||
" vec2 output_texel_coord = vec2(texel_coord - source_position + target_position) + 0.5;\n"
|
||||
" vec4 source_color = texelFetch(img_input, ivec2(rotated_texel_coord), 0);\n"
|
||||
" vec4 source_color = texture(img_input, rotated_texel_coord/vec2(size));\n"
|
||||
" vec4 output_color = %s;\n"
|
||||
" vec3 color = mix(output_color.rgb, source_color.rgb, source_color.a);\n"
|
||||
" imageStore(img_output, texel_coord + target_position, vec4(color, 1.0));\n"
|
||||
"}\n", max_local_size_dim, max_local_size_dim, external_texture ? "samplerExternalOES" : "sampler2D",
|
||||
alpha_blending ? "texelFetch(img_background, ivec2(output_texel_coord), 0)" : "source_color");
|
||||
"}\n", header, max_local_size_dim, max_local_size_dim,
|
||||
alpha_blending ? "texture(img_background, output_texel_coord/vec2(output_size))" : "source_color");
|
||||
|
||||
if(gsr_shader_init(shader, egl, NULL, NULL, compute_shader) != 0)
|
||||
return -1;
|
||||
@@ -203,6 +224,190 @@ static int load_compute_shader_rgb(gsr_shader *shader, gsr_egl *egl, gsr_color_u
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_graphics_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, gsr_destination_color color_format, gsr_color_range color_range, bool external_texture) {
|
||||
const char *color_transform_matrix = color_format_range_get_transform_matrix(color_format, color_range);
|
||||
|
||||
char vertex_shader[2048];
|
||||
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||
"#version 300 es \n"
|
||||
"in vec2 pos; \n"
|
||||
"in vec2 texcoords; \n"
|
||||
"out vec2 texcoords_out; \n"
|
||||
"uniform vec2 offset; \n"
|
||||
"uniform float rotation; \n"
|
||||
"uniform mat2 rotation_matrix; \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||
" gl_Position = vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0); \n"
|
||||
"} \n");
|
||||
|
||||
const char *main_code =
|
||||
main_code =
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor.x = (RGBtoYUV * vec4(pixel.rgb, 1.0)).x; \n"
|
||||
" FragColor.w = pixel.a; \n";
|
||||
|
||||
char fragment_shader[2048];
|
||||
if(external_texture) {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
"#extension GL_OES_EGL_image_external : enable \n"
|
||||
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||
"precision highp float; \n"
|
||||
"in vec2 texcoords_out; \n"
|
||||
"uniform samplerExternalOES tex1; \n"
|
||||
"out vec4 FragColor; \n"
|
||||
"%s"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", color_transform_matrix, main_code);
|
||||
} else {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
"precision highp float; \n"
|
||||
"in vec2 texcoords_out; \n"
|
||||
"uniform sampler2D tex1; \n"
|
||||
"out vec4 FragColor; \n"
|
||||
"%s"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", color_transform_matrix, main_code);
|
||||
}
|
||||
|
||||
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader, NULL) != 0)
|
||||
return -1;
|
||||
|
||||
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int load_graphics_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, gsr_destination_color color_format, gsr_color_range color_range, bool external_texture) {
|
||||
const char *color_transform_matrix = color_format_range_get_transform_matrix(color_format, color_range);
|
||||
|
||||
char vertex_shader[2048];
|
||||
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||
"#version 300 es \n"
|
||||
"in vec2 pos; \n"
|
||||
"in vec2 texcoords; \n"
|
||||
"out vec2 texcoords_out; \n"
|
||||
"uniform vec2 offset; \n"
|
||||
"uniform float rotation; \n"
|
||||
"uniform mat2 rotation_matrix; \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||
" gl_Position = (vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0)) * vec4(0.5, 0.5, 1.0, 1.0) - vec4(0.5, 0.5, 0.0, 0.0); \n"
|
||||
"} \n");
|
||||
|
||||
const char *main_code =
|
||||
main_code =
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor.xy = (RGBtoYUV * vec4(pixel.rgb, 1.0)).yz; \n"
|
||||
" FragColor.w = pixel.a; \n";
|
||||
|
||||
char fragment_shader[2048];
|
||||
if(external_texture) {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
"#extension GL_OES_EGL_image_external : enable \n"
|
||||
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||
"precision highp float; \n"
|
||||
"in vec2 texcoords_out; \n"
|
||||
"uniform samplerExternalOES tex1; \n"
|
||||
"out vec4 FragColor; \n"
|
||||
"%s"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", color_transform_matrix, main_code);
|
||||
} else {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
"precision highp float; \n"
|
||||
"in vec2 texcoords_out; \n"
|
||||
"uniform sampler2D tex1; \n"
|
||||
"out vec4 FragColor; \n"
|
||||
"%s"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", color_transform_matrix, main_code);
|
||||
}
|
||||
|
||||
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader, NULL) != 0)
|
||||
return -1;
|
||||
|
||||
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int load_graphics_shader_rgb(gsr_shader *shader, gsr_egl *egl, gsr_color_graphics_uniforms *uniforms, bool external_texture) {
|
||||
char vertex_shader[2048];
|
||||
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||
"#version 300 es \n"
|
||||
"in vec2 pos; \n"
|
||||
"in vec2 texcoords; \n"
|
||||
"out vec2 texcoords_out; \n"
|
||||
"uniform vec2 offset; \n"
|
||||
"uniform float rotation; \n"
|
||||
"uniform mat2 rotation_matrix; \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" texcoords_out = vec2(texcoords.x - 0.5, texcoords.y - 0.5) * rotation_matrix + vec2(0.5, 0.5); \n"
|
||||
" gl_Position = vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0); \n"
|
||||
"} \n");
|
||||
|
||||
const char *main_code =
|
||||
main_code =
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor = pixel; \n";
|
||||
|
||||
char fragment_shader[2048];
|
||||
if(external_texture) {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
"#extension GL_OES_EGL_image_external : enable \n"
|
||||
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||
"precision highp float; \n"
|
||||
"in vec2 texcoords_out; \n"
|
||||
"uniform samplerExternalOES tex1; \n"
|
||||
"out vec4 FragColor; \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", main_code);
|
||||
} else {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
"precision highp float; \n"
|
||||
"in vec2 texcoords_out; \n"
|
||||
"uniform sampler2D tex1; \n"
|
||||
"out vec4 FragColor; \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", main_code);
|
||||
}
|
||||
|
||||
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader, NULL) != 0)
|
||||
return -1;
|
||||
|
||||
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||
uniforms->rotation_matrix = egl->glGetUniformLocation(shader->program_id, "rotation_matrix");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_framebuffers(gsr_color_conversion *self) {
|
||||
/* TODO: Only generate the necessary amount of framebuffers (self->params.num_destination_textures) */
|
||||
const unsigned int draw_buffer = GL_COLOR_ATTACHMENT0;
|
||||
@@ -252,6 +457,140 @@ static int create_vertices(gsr_color_conversion *self) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool gsr_color_conversion_load_compute_shaders(gsr_color_conversion *self) {
|
||||
switch(self->params.destination_color) {
|
||||
case GSR_DESTINATION_COLOR_NV12:
|
||||
case GSR_DESTINATION_COLOR_P010: {
|
||||
if(load_compute_shader_y(&self->compute_shaders[COMPUTE_SHADER_INDEX_Y], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_Y], self->max_local_size_dim, self->params.destination_color, self->params.color_range, false, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_compute_shader_uv(&self->compute_shaders[COMPUTE_SHADER_INDEX_UV], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_UV], self->max_local_size_dim, self->params.destination_color, self->params.color_range, false, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV compute shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_compute_shader_y(&self->compute_shaders[COMPUTE_SHADER_INDEX_Y_BLEND], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_Y_BLEND], self->max_local_size_dim, self->params.destination_color, self->params.color_range, false, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_compute_shader_uv(&self->compute_shaders[COMPUTE_SHADER_INDEX_UV_BLEND], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_UV_BLEND], self->max_local_size_dim, self->params.destination_color, self->params.color_range, false, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV compute shader\n");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
if(load_compute_shader_rgb(&self->compute_shaders[COMPUTE_SHADER_INDEX_RGB], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_RGB], self->max_local_size_dim, false, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_compute_shader_rgb(&self->compute_shaders[COMPUTE_SHADER_INDEX_RGB_BLEND], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_RGB_BLEND], self->max_local_size_dim, false, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool gsr_color_conversion_load_external_compute_shaders(gsr_color_conversion *self) {
|
||||
switch(self->params.destination_color) {
|
||||
case GSR_DESTINATION_COLOR_NV12:
|
||||
case GSR_DESTINATION_COLOR_P010: {
|
||||
if(load_compute_shader_y(&self->compute_shaders[COMPUTE_SHADER_INDEX_Y_EXTERNAL], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_Y_EXTERNAL], self->max_local_size_dim, self->params.destination_color, self->params.color_range, true, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_compute_shader_uv(&self->compute_shaders[COMPUTE_SHADER_INDEX_UV_EXTERNAL], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_UV_EXTERNAL], self->max_local_size_dim, self->params.destination_color, self->params.color_range, true, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV compute shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_compute_shader_y(&self->compute_shaders[COMPUTE_SHADER_INDEX_Y_EXTERNAL_BLEND], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_Y_EXTERNAL_BLEND], self->max_local_size_dim, self->params.destination_color, self->params.color_range, true, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_compute_shader_uv(&self->compute_shaders[COMPUTE_SHADER_INDEX_UV_EXTERNAL_BLEND], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_UV_EXTERNAL_BLEND], self->max_local_size_dim, self->params.destination_color, self->params.color_range, true, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV compute shader\n");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
if(load_compute_shader_rgb(&self->compute_shaders[COMPUTE_SHADER_INDEX_RGB_EXTERNAL], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_RGB_EXTERNAL], self->max_local_size_dim, true, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_compute_shader_rgb(&self->compute_shaders[COMPUTE_SHADER_INDEX_RGB_EXTERNAL_BLEND], self->params.egl, &self->compute_uniforms[COMPUTE_SHADER_INDEX_RGB_EXTERNAL_BLEND], self->max_local_size_dim, true, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool gsr_color_conversion_load_graphics_shaders(gsr_color_conversion *self) {
|
||||
switch(self->params.destination_color) {
|
||||
case GSR_DESTINATION_COLOR_NV12:
|
||||
case GSR_DESTINATION_COLOR_P010: {
|
||||
if(load_graphics_shader_y(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_Y], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_Y], self->params.destination_color, self->params.color_range, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y graphics shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_graphics_shader_uv(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_UV], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_UV], self->params.destination_color, self->params.color_range, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV graphics shader\n");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
if(load_graphics_shader_rgb(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_RGB], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_RGB], false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y graphics shader\n");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool gsr_color_conversion_load_external_graphics_shaders(gsr_color_conversion *self) {
|
||||
switch(self->params.destination_color) {
|
||||
case GSR_DESTINATION_COLOR_NV12:
|
||||
case GSR_DESTINATION_COLOR_P010: {
|
||||
if(load_graphics_shader_y(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_Y_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_Y_EXTERNAL], self->params.destination_color, self->params.color_range, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y graphics shader\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(load_graphics_shader_uv(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_UV_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_UV_EXTERNAL], self->params.destination_color, self->params.color_range, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV graphics shader\n");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
if(load_graphics_shader_rgb(&self->graphics_shaders[GRAPHICS_SHADER_INDEX_RGB_EXTERNAL], self->params.egl, &self->graphics_uniforms[GRAPHICS_SHADER_INDEX_RGB_EXTERNAL], true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y graphics shader\n");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int gsr_color_conversion_init(gsr_color_conversion *self, const gsr_color_conversion_params *params) {
|
||||
assert(params);
|
||||
assert(params->egl);
|
||||
@@ -263,88 +602,40 @@ int gsr_color_conversion_init(gsr_color_conversion *self, const gsr_color_conver
|
||||
self->params.egl->glGetIntegerv(GL_MAX_COMPUTE_FIXED_GROUP_INVOCATIONS, &max_compute_work_group_invocations);
|
||||
self->max_local_size_dim = sqrt(max_compute_work_group_invocations);
|
||||
|
||||
switch(params->destination_color) {
|
||||
switch(self->params.destination_color) {
|
||||
case GSR_DESTINATION_COLOR_NV12:
|
||||
case GSR_DESTINATION_COLOR_P010: {
|
||||
if(self->params.num_destination_textures != 2) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: expected 2 destination textures for destination color NV12/P010, got %d destination texture(s)\n", self->params.num_destination_textures);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(load_compute_shader_y(&self->shaders[SHADER_INDEX_Y], self->params.egl, &self->uniforms[SHADER_INDEX_Y], self->max_local_size_dim, params->destination_color, params->color_range, false, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_compute_shader_uv(&self->shaders[SHADER_INDEX_UV], self->params.egl, &self->uniforms[SHADER_INDEX_UV], self->max_local_size_dim, params->destination_color, params->color_range, false, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_compute_shader_y(&self->shaders[SHADER_INDEX_Y_BLEND], self->params.egl, &self->uniforms[SHADER_INDEX_Y_BLEND], self->max_local_size_dim, params->destination_color, params->color_range, false, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_compute_shader_uv(&self->shaders[SHADER_INDEX_UV_BLEND], self->params.egl, &self->uniforms[SHADER_INDEX_UV_BLEND], self->max_local_size_dim, params->destination_color, params->color_range, false, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(self->params.load_external_image_shader) {
|
||||
if(load_compute_shader_y(&self->shaders[SHADER_INDEX_Y_EXTERNAL], self->params.egl, &self->uniforms[SHADER_INDEX_Y_EXTERNAL], self->max_local_size_dim, params->destination_color, params->color_range, true, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_compute_shader_uv(&self->shaders[SHADER_INDEX_UV_EXTERNAL], self->params.egl, &self->uniforms[SHADER_INDEX_UV_EXTERNAL], self->max_local_size_dim, params->destination_color, params->color_range, true, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_compute_shader_y(&self->shaders[SHADER_INDEX_Y_EXTERNAL_BLEND], self->params.egl, &self->uniforms[SHADER_INDEX_Y_EXTERNAL_BLEND], self->max_local_size_dim, params->destination_color, params->color_range, true, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_compute_shader_uv(&self->shaders[SHADER_INDEX_UV_EXTERNAL_BLEND], self->params.egl, &self->uniforms[SHADER_INDEX_UV_EXTERNAL_BLEND], self->max_local_size_dim, params->destination_color, params->color_range, true, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
if(self->params.num_destination_textures != 1) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: expected 1 destination textures for destination color RGB8, got %d destination texture(s)\n", self->params.num_destination_textures);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(load_compute_shader_rgb(&self->shaders[SHADER_INDEX_RGB], self->params.egl, &self->uniforms[SHADER_INDEX_RGB], self->max_local_size_dim, false, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_compute_shader_rgb(&self->shaders[SHADER_INDEX_RGB_BLEND], self->params.egl, &self->uniforms[SHADER_INDEX_RGB_BLEND], self->max_local_size_dim, false, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(self->params.load_external_image_shader) {
|
||||
if(load_compute_shader_rgb(&self->shaders[SHADER_INDEX_RGB_EXTERNAL], self->params.egl, &self->uniforms[SHADER_INDEX_RGB_EXTERNAL], self->max_local_size_dim, true, false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_compute_shader_rgb(&self->shaders[SHADER_INDEX_RGB_EXTERNAL_BLEND], self->params.egl, &self->uniforms[SHADER_INDEX_RGB_EXTERNAL_BLEND], self->max_local_size_dim, true, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y compute shader\n");
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!gsr_color_conversion_load_compute_shaders(self)) {
|
||||
self->compute_shaders_failed_to_load = true;
|
||||
fprintf(stderr, "gsr info: failed to load one or more compute shaders, run gpu-screen-recorder with the '-gl-debug yes' option to see why. Falling back to slower graphics shader instead\n");
|
||||
if(!gsr_color_conversion_load_graphics_shaders(self))
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(self->params.load_external_image_shader) {
|
||||
if(!gsr_color_conversion_load_external_compute_shaders(self)) {
|
||||
self->external_compute_shaders_failed_to_load = true;
|
||||
fprintf(stderr, "gsr info: failed to load one or more external compute shaders, run gpu-screen-recorder with the '-gl-debug yes' option to see why. Falling back to slower graphics shader instead\n");
|
||||
if(!gsr_color_conversion_load_external_graphics_shaders(self))
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if(load_framebuffers(self) != 0)
|
||||
goto err;
|
||||
|
||||
@@ -377,14 +668,18 @@ void gsr_color_conversion_deinit(gsr_color_conversion *self) {
|
||||
self->framebuffers[i] = 0;
|
||||
}
|
||||
|
||||
for(int i = 0; i < GSR_COLOR_CONVERSION_MAX_SHADERS; ++i) {
|
||||
gsr_shader_deinit(&self->shaders[i]);
|
||||
for(int i = 0; i < GSR_COLOR_CONVERSION_MAX_COMPUTE_SHADERS; ++i) {
|
||||
gsr_shader_deinit(&self->compute_shaders[i]);
|
||||
}
|
||||
|
||||
for(int i = 0; i < GSR_COLOR_CONVERSION_MAX_GRAPHICS_SHADERS; ++i) {
|
||||
gsr_shader_deinit(&self->graphics_shaders[i]);
|
||||
}
|
||||
|
||||
self->params.egl = NULL;
|
||||
}
|
||||
|
||||
static void gsr_color_conversion_apply_rotation(gsr_rotation rotation, float rotation_matrix[2][2], vec2i *source_position, vec2i texture_size, vec2f scale) {
|
||||
static void gsr_color_conversion_apply_rotation(gsr_rotation rotation, float rotation_matrix[2][2]) {
|
||||
/*
|
||||
rotation_matrix[0][0] = cos(angle);
|
||||
rotation_matrix[0][1] = -sin(angle);
|
||||
@@ -405,8 +700,6 @@ static void gsr_color_conversion_apply_rotation(gsr_rotation rotation, float rot
|
||||
rotation_matrix[0][1] = -1.0f;
|
||||
rotation_matrix[1][0] = 1.0f;
|
||||
rotation_matrix[1][1] = 0.0f;
|
||||
source_position->x += (((double)texture_size.x*0.5 - (double)texture_size.y*0.5) * scale.x + 0.5);
|
||||
source_position->y += (((double)texture_size.y*0.5 - (double)texture_size.x*0.5) * scale.y + 0.5);
|
||||
break;
|
||||
case GSR_ROT_180:
|
||||
rotation_matrix[0][0] = -1.0f;
|
||||
@@ -419,8 +712,6 @@ static void gsr_color_conversion_apply_rotation(gsr_rotation rotation, float rot
|
||||
rotation_matrix[0][1] = 1.0f;
|
||||
rotation_matrix[1][0] = -1.0f;
|
||||
rotation_matrix[1][1] = 0.0f;
|
||||
source_position->x += (((double)texture_size.x*0.5 - (double)texture_size.y*0.5) * scale.x + 0.5);
|
||||
source_position->y += (((double)texture_size.y*0.5 - (double)texture_size.x*0.5) * scale.y + 0.5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -465,33 +756,33 @@ static unsigned int color_component_get_color_format(gsr_color_component color_c
|
||||
return GL_RGBA8;
|
||||
}
|
||||
|
||||
static int color_component_get_shader_index(gsr_color_component color_component, bool external_texture, bool alpha_blending) {
|
||||
static int color_component_get_COMPUTE_SHADER_INDEX(gsr_color_component color_component, bool external_texture, bool alpha_blending) {
|
||||
switch(color_component) {
|
||||
case GSR_COLOR_COMP_Y: {
|
||||
if(external_texture)
|
||||
return alpha_blending ? SHADER_INDEX_Y_EXTERNAL_BLEND : SHADER_INDEX_Y_EXTERNAL;
|
||||
return alpha_blending ? COMPUTE_SHADER_INDEX_Y_EXTERNAL_BLEND : COMPUTE_SHADER_INDEX_Y_EXTERNAL;
|
||||
else
|
||||
return alpha_blending ? SHADER_INDEX_Y_BLEND : SHADER_INDEX_Y;
|
||||
return alpha_blending ? COMPUTE_SHADER_INDEX_Y_BLEND : COMPUTE_SHADER_INDEX_Y;
|
||||
}
|
||||
case GSR_COLOR_COMP_UV: {
|
||||
if(external_texture)
|
||||
return alpha_blending ? SHADER_INDEX_UV_EXTERNAL_BLEND : SHADER_INDEX_UV_EXTERNAL;
|
||||
return alpha_blending ? COMPUTE_SHADER_INDEX_UV_EXTERNAL_BLEND : COMPUTE_SHADER_INDEX_UV_EXTERNAL;
|
||||
else
|
||||
return alpha_blending ? SHADER_INDEX_UV_BLEND : SHADER_INDEX_UV;
|
||||
return alpha_blending ? COMPUTE_SHADER_INDEX_UV_BLEND : COMPUTE_SHADER_INDEX_UV;
|
||||
}
|
||||
case GSR_COLOR_COMP_RGB: {
|
||||
if(external_texture)
|
||||
return alpha_blending ? SHADER_INDEX_RGB_EXTERNAL_BLEND : SHADER_INDEX_RGB_EXTERNAL;
|
||||
return alpha_blending ? COMPUTE_SHADER_INDEX_RGB_EXTERNAL_BLEND : COMPUTE_SHADER_INDEX_RGB_EXTERNAL;
|
||||
else
|
||||
return alpha_blending ? SHADER_INDEX_RGB_BLEND : SHADER_INDEX_RGB;
|
||||
return alpha_blending ? COMPUTE_SHADER_INDEX_RGB_BLEND : COMPUTE_SHADER_INDEX_RGB;
|
||||
}
|
||||
}
|
||||
assert(false);
|
||||
return SHADER_INDEX_RGB;
|
||||
return COMPUTE_SHADER_INDEX_RGB;
|
||||
}
|
||||
|
||||
static void gsr_color_conversion_dispatch_compute_shader(gsr_color_conversion *self, bool external_texture, bool alpha_blending, float rotation_matrix[2][2], vec2i source_position, vec2i destination_pos, vec2i destination_size, vec2f scale, bool use_16bit_colors, gsr_color_component color_component) {
|
||||
const int shader_index = color_component_get_shader_index(color_component, external_texture, alpha_blending);
|
||||
const int compute_shader_index = color_component_get_COMPUTE_SHADER_INDEX(color_component, external_texture, alpha_blending);
|
||||
const int destination_texture_index = color_component_get_destination_texture_index(color_component);
|
||||
const unsigned int color_format = color_component_get_color_format(color_component, use_16bit_colors);
|
||||
|
||||
@@ -499,8 +790,8 @@ static void gsr_color_conversion_dispatch_compute_shader(gsr_color_conversion *s
|
||||
self->params.egl->glBindTexture(GL_TEXTURE_2D, self->params.destination_textures[destination_texture_index]);
|
||||
self->params.egl->glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
gsr_color_uniforms *uniform = &self->uniforms[shader_index];
|
||||
gsr_shader_use(&self->shaders[shader_index]);
|
||||
gsr_color_compute_uniforms *uniform = &self->compute_uniforms[compute_shader_index];
|
||||
gsr_shader_use(&self->compute_shaders[compute_shader_index]);
|
||||
self->params.egl->glUniformMatrix2fv(uniform->rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||
self->params.egl->glUniform2i(uniform->source_position, source_position.x, source_position.y);
|
||||
self->params.egl->glUniform2i(uniform->target_position, destination_pos.x, destination_pos.y);
|
||||
@@ -511,34 +802,156 @@ static void gsr_color_conversion_dispatch_compute_shader(gsr_color_conversion *s
|
||||
self->params.egl->glDispatchCompute(max_int(1, num_groups_x), max_int(1, num_groups_y), 1);
|
||||
}
|
||||
|
||||
static void gsr_color_conversion_draw_graphics(gsr_color_conversion *self, unsigned int texture_id, bool external_texture, float rotation_matrix[2][2], vec2i source_position, vec2i source_size, vec2i destination_pos, vec2i texture_size, vec2f scale, gsr_source_color source_color) {
|
||||
/* TODO: Do not call this every frame? */
|
||||
vec2i dest_texture_size = {0, 0};
|
||||
self->params.egl->glBindTexture(GL_TEXTURE_2D, self->params.destination_textures[0]);
|
||||
self->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &dest_texture_size.x);
|
||||
self->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &dest_texture_size.y);
|
||||
self->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
const int texture_target = external_texture ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
|
||||
|
||||
self->params.egl->glBindTexture(texture_target, texture_id);
|
||||
gsr_color_conversion_swizzle_texture_source(self, source_color);
|
||||
|
||||
const vec2f pos_norm = {
|
||||
((float)destination_pos.x / (dest_texture_size.x == 0 ? 1.0f : (float)dest_texture_size.x)) * 2.0f,
|
||||
((float)destination_pos.y / (dest_texture_size.y == 0 ? 1.0f : (float)dest_texture_size.y)) * 2.0f,
|
||||
};
|
||||
|
||||
const vec2f size_norm = {
|
||||
((float)source_size.x / (dest_texture_size.x == 0 ? 1.0f : (float)dest_texture_size.x)) * 2.0f * scale.x,
|
||||
((float)source_size.y / (dest_texture_size.y == 0 ? 1.0f : (float)dest_texture_size.y)) * 2.0f * scale.y,
|
||||
};
|
||||
|
||||
const vec2f texture_pos_norm = {
|
||||
(float)source_position.x / (texture_size.x == 0 ? 1.0f : (float)texture_size.x),
|
||||
(float)source_position.y / (texture_size.y == 0 ? 1.0f : (float)texture_size.y),
|
||||
};
|
||||
|
||||
const vec2f texture_size_norm = {
|
||||
(float)source_size.x / (texture_size.x == 0 ? 1.0f : (float)texture_size.x),
|
||||
(float)source_size.y / (texture_size.y == 0 ? 1.0f : (float)texture_size.y),
|
||||
};
|
||||
|
||||
const float vertices[] = {
|
||||
-1.0f + 0.0f, -1.0f + 0.0f + size_norm.y, texture_pos_norm.x, texture_pos_norm.y + texture_size_norm.y,
|
||||
-1.0f + 0.0f, -1.0f + 0.0f, texture_pos_norm.x, texture_pos_norm.y,
|
||||
-1.0f + 0.0f + size_norm.x, -1.0f + 0.0f, texture_pos_norm.x + texture_size_norm.x, texture_pos_norm.y,
|
||||
|
||||
-1.0f + 0.0f, -1.0f + 0.0f + size_norm.y, texture_pos_norm.x, texture_pos_norm.y + texture_size_norm.y,
|
||||
-1.0f + 0.0f + size_norm.x, -1.0f + 0.0f, texture_pos_norm.x + texture_size_norm.x, texture_pos_norm.y,
|
||||
-1.0f + 0.0f + size_norm.x, -1.0f + 0.0f + size_norm.y, texture_pos_norm.x + texture_size_norm.x, texture_pos_norm.y + texture_size_norm.y
|
||||
};
|
||||
|
||||
self->params.egl->glBindVertexArray(self->vertex_array_object_id);
|
||||
self->params.egl->glViewport(0, 0, dest_texture_size.x, dest_texture_size.y);
|
||||
|
||||
/* TODO: this, also cleanup */
|
||||
//self->params.egl->glBindBuffer(GL_ARRAY_BUFFER, self->vertex_buffer_object_id);
|
||||
self->params.egl->glBufferSubData(GL_ARRAY_BUFFER, 0, 24 * sizeof(float), vertices);
|
||||
|
||||
switch(self->params.destination_color) {
|
||||
case GSR_DESTINATION_COLOR_NV12:
|
||||
case GSR_DESTINATION_COLOR_P010: {
|
||||
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); // TODO: Do this in a separate clear_ function. We want to do that when using multiple drm to create the final image (multiple monitors for example)
|
||||
|
||||
int shader_index = external_texture ? GRAPHICS_SHADER_INDEX_Y_EXTERNAL : GRAPHICS_SHADER_INDEX_Y;
|
||||
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
||||
if(self->params.num_destination_textures > 1) {
|
||||
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[1]);
|
||||
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
shader_index = external_texture ? GRAPHICS_SHADER_INDEX_UV_EXTERNAL : GRAPHICS_SHADER_INDEX_UV;
|
||||
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); // TODO: Do this in a separate clear_ function. We want to do that when using multiple drm to create the final image (multiple monitors for example)
|
||||
|
||||
const int shader_index = external_texture ? GRAPHICS_SHADER_INDEX_RGB_EXTERNAL : GRAPHICS_SHADER_INDEX_RGB;
|
||||
gsr_shader_use(&self->graphics_shaders[shader_index]);
|
||||
self->params.egl->glUniformMatrix2fv(self->graphics_uniforms[shader_index].rotation_matrix, 1, GL_TRUE, (const float*)rotation_matrix);
|
||||
self->params.egl->glUniform2f(self->graphics_uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||
self->params.egl->glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
self->params.egl->glBindVertexArray(0);
|
||||
self->params.egl->glUseProgram(0);
|
||||
gsr_color_conversion_swizzle_reset(self, source_color);
|
||||
self->params.egl->glBindTexture(texture_target, 0);
|
||||
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
void gsr_color_conversion_draw(gsr_color_conversion *self, unsigned int texture_id, vec2i destination_pos, vec2i destination_size, vec2i source_pos, vec2i source_size, vec2i texture_size, gsr_rotation rotation, gsr_source_color source_color, bool external_texture, bool alpha_blending) {
|
||||
assert(!external_texture || self->params.load_external_image_shader);
|
||||
if(external_texture && !self->params.load_external_image_shader) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_draw: external texture not loaded\n");
|
||||
return;
|
||||
}
|
||||
|
||||
vec2f scale = {0.0f, 0.0f};
|
||||
if(source_size.x > 0 && source_size.y > 0)
|
||||
scale = (vec2f){ (double)destination_size.x/(double)source_size.x, (double)destination_size.y/(double)source_size.y };
|
||||
|
||||
vec2i source_position = {0, 0};
|
||||
float rotation_matrix[2][2] = {{0, 0}, {0, 0}};
|
||||
gsr_color_conversion_apply_rotation(rotation, rotation_matrix, &source_position, texture_size, scale);
|
||||
|
||||
source_position.x -= (source_pos.x * scale.x + 0.5);
|
||||
source_position.y -= (source_pos.y * scale.y + 0.5);
|
||||
gsr_color_conversion_apply_rotation(rotation, rotation_matrix);
|
||||
|
||||
const int texture_target = external_texture ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
|
||||
self->params.egl->glBindTexture(texture_target, texture_id);
|
||||
gsr_color_conversion_swizzle_texture_source(self, source_color);
|
||||
|
||||
switch(self->params.destination_color) {
|
||||
case GSR_DESTINATION_COLOR_NV12:
|
||||
case GSR_DESTINATION_COLOR_P010: {
|
||||
const bool use_16bit_colors = self->params.destination_color == GSR_DESTINATION_COLOR_P010;
|
||||
gsr_color_conversion_dispatch_compute_shader(self, external_texture, alpha_blending, rotation_matrix, source_position, destination_pos, destination_size, scale, use_16bit_colors, GSR_COLOR_COMP_Y);
|
||||
gsr_color_conversion_dispatch_compute_shader(self, external_texture, alpha_blending, rotation_matrix, (vec2i){source_position.x/2, source_position.y/2},
|
||||
(vec2i){destination_pos.x/2, destination_pos.y/2}, (vec2i){destination_size.x/2, destination_size.y/2}, scale, use_16bit_colors, GSR_COLOR_COMP_UV);
|
||||
break;
|
||||
const bool use_graphics_shader = external_texture ? self->external_compute_shaders_failed_to_load : self->compute_shaders_failed_to_load;
|
||||
if(use_graphics_shader) {
|
||||
source_position.x += source_pos.x;
|
||||
source_position.y += source_pos.y;
|
||||
gsr_color_conversion_draw_graphics(self, texture_id, external_texture, rotation_matrix, source_position, source_size, destination_pos, texture_size, scale, source_color);
|
||||
// TODO: Is glFlush and glFinish needed here for graphics garbage?
|
||||
} else {
|
||||
switch(rotation) {
|
||||
case GSR_ROT_0:
|
||||
break;
|
||||
case GSR_ROT_90:
|
||||
source_position.x += (((double)texture_size.x*0.5 - (double)texture_size.y*0.5) * scale.x);
|
||||
source_position.y += (((double)texture_size.y*0.5 - (double)texture_size.x*0.5) * scale.y);
|
||||
break;
|
||||
case GSR_ROT_180:
|
||||
break;
|
||||
case GSR_ROT_270:
|
||||
source_position.x += (((double)texture_size.x*0.5 - (double)texture_size.y*0.5) * scale.x);
|
||||
source_position.y += (((double)texture_size.y*0.5 - (double)texture_size.x*0.5) * scale.y);
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
gsr_color_conversion_dispatch_compute_shader(self, external_texture, alpha_blending, rotation_matrix, source_position, destination_pos, destination_size, scale, false, GSR_COLOR_COMP_RGB);
|
||||
break;
|
||||
source_position.x -= (source_pos.x * scale.x + 0.5);
|
||||
source_position.y -= (source_pos.y * scale.y + 0.5);
|
||||
|
||||
switch(self->params.destination_color) {
|
||||
case GSR_DESTINATION_COLOR_NV12:
|
||||
case GSR_DESTINATION_COLOR_P010: {
|
||||
const bool use_16bit_colors = self->params.destination_color == GSR_DESTINATION_COLOR_P010;
|
||||
gsr_color_conversion_dispatch_compute_shader(self, external_texture, alpha_blending, rotation_matrix, source_position, destination_pos, destination_size, scale, use_16bit_colors, GSR_COLOR_COMP_Y);
|
||||
gsr_color_conversion_dispatch_compute_shader(self, external_texture, alpha_blending, rotation_matrix, (vec2i){source_position.x/2, source_position.y/2},
|
||||
(vec2i){destination_pos.x/2, destination_pos.y/2}, (vec2i){destination_size.x/2, destination_size.y/2}, scale, use_16bit_colors, GSR_COLOR_COMP_UV);
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
gsr_color_conversion_dispatch_compute_shader(self, external_texture, alpha_blending, rotation_matrix, source_position, destination_pos, destination_size, scale, false, GSR_COLOR_COMP_RGB);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3706,6 +3706,11 @@ int main(int argc, char **argv) {
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
gsr_shader_enable_debug_output(gl_debug);
|
||||
#ifndef NDEBUG
|
||||
gsr_shader_enable_debug_output(true);
|
||||
#endif
|
||||
|
||||
if(egl.gpu_info.is_steam_deck) {
|
||||
fprintf(stderr, "gsr warning: steam deck has multiple driver issues. One of them has been reported here: https://github.com/ValveSoftware/SteamOS/issues/1609\n"
|
||||
"If you have issues with GPU Screen Recorder on steam deck that you don't have on a desktop computer then report the issue to Valve and/or AMD.\n");
|
||||
|
||||
18
src/shader.c
18
src/shader.c
@@ -3,14 +3,16 @@
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
static bool print_compile_errors = false;
|
||||
|
||||
static int min_int(int a, int b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
static unsigned int loader_shader(gsr_egl *egl, unsigned int type, const char *source) {
|
||||
static unsigned int load_shader(gsr_egl *egl, unsigned int type, const char *source) {
|
||||
unsigned int shader_id = egl->glCreateShader(type);
|
||||
if(shader_id == 0) {
|
||||
fprintf(stderr, "gsr error: loader_shader: failed to create shader, error: %d\n", egl->glGetError());
|
||||
fprintf(stderr, "gsr error: load_shader: failed to create shader, error: %d\n", egl->glGetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -23,7 +25,7 @@ static unsigned int loader_shader(gsr_egl *egl, unsigned int type, const char *s
|
||||
int info_length = 0;
|
||||
egl->glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &info_length);
|
||||
|
||||
if(info_length > 1) {
|
||||
if(info_length > 1 && print_compile_errors) {
|
||||
char info_log[4096];
|
||||
egl->glGetShaderInfoLog(shader_id, min_int(4096, info_length), NULL, info_log);
|
||||
fprintf(stderr, "gsr error: loader shader: failed to compile shader, error:\n%s\nshader source:\n%s\n", info_log, source);
|
||||
@@ -45,19 +47,19 @@ static unsigned int load_program(gsr_egl *egl, const char *vertex_shader, const
|
||||
bool success = false;
|
||||
|
||||
if(vertex_shader) {
|
||||
vertex_shader_id = loader_shader(egl, GL_VERTEX_SHADER, vertex_shader);
|
||||
vertex_shader_id = load_shader(egl, GL_VERTEX_SHADER, vertex_shader);
|
||||
if(vertex_shader_id == 0)
|
||||
goto done;
|
||||
}
|
||||
|
||||
if(fragment_shader) {
|
||||
fragment_shader_id = loader_shader(egl, GL_FRAGMENT_SHADER, fragment_shader);
|
||||
fragment_shader_id = load_shader(egl, GL_FRAGMENT_SHADER, fragment_shader);
|
||||
if(fragment_shader_id == 0)
|
||||
goto done;
|
||||
}
|
||||
|
||||
if(compute_shader) {
|
||||
compute_shader_id = loader_shader(egl, GL_COMPUTE_SHADER, compute_shader);
|
||||
compute_shader_id = load_shader(egl, GL_COMPUTE_SHADER, compute_shader);
|
||||
if(compute_shader_id == 0)
|
||||
goto done;
|
||||
}
|
||||
@@ -151,3 +153,7 @@ void gsr_shader_use(gsr_shader *self) {
|
||||
void gsr_shader_use_none(gsr_shader *self) {
|
||||
self->egl->glUseProgram(0);
|
||||
}
|
||||
|
||||
void gsr_shader_enable_debug_output(bool enable) {
|
||||
print_compile_errors = enable;
|
||||
}
|
||||
|
||||
@@ -9,19 +9,23 @@
|
||||
#include <stdint.h>
|
||||
#include <wayland-client.h>
|
||||
#include <wayland-egl.h>
|
||||
#include "xdg-output-unstable-v1-client-protocol.h"
|
||||
|
||||
#define GSR_MAX_OUTPUTS 32
|
||||
|
||||
typedef struct gsr_window_wayland gsr_window_wayland;
|
||||
|
||||
typedef struct {
|
||||
uint32_t wl_name;
|
||||
void *output;
|
||||
struct wl_output *output;
|
||||
struct zxdg_output_v1 *xdg_output;
|
||||
vec2i pos;
|
||||
vec2i size;
|
||||
int32_t transform;
|
||||
char *name;
|
||||
} gsr_wayland_output;
|
||||
|
||||
typedef struct {
|
||||
struct gsr_window_wayland {
|
||||
void *display;
|
||||
void *window;
|
||||
void *registry;
|
||||
@@ -29,7 +33,8 @@ typedef struct {
|
||||
void *compositor;
|
||||
gsr_wayland_output outputs[GSR_MAX_OUTPUTS];
|
||||
int num_outputs;
|
||||
} gsr_window_wayland;
|
||||
struct zxdg_output_manager_v1 *xdg_output_manager;
|
||||
};
|
||||
|
||||
static void output_handle_geometry(void *data, struct wl_output *wl_output,
|
||||
int32_t x, int32_t y, int32_t phys_width, int32_t phys_height,
|
||||
@@ -95,7 +100,7 @@ static const struct wl_output_listener output_listener = {
|
||||
static void registry_add_object(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
|
||||
(void)version;
|
||||
gsr_window_wayland *window_wayland = data;
|
||||
if (strcmp(interface, "wl_compositor") == 0) {
|
||||
if(strcmp(interface, "wl_compositor") == 0) {
|
||||
if(window_wayland->compositor) {
|
||||
wl_compositor_destroy(window_wayland->compositor);
|
||||
window_wayland->compositor = NULL;
|
||||
@@ -103,7 +108,7 @@ static void registry_add_object(void *data, struct wl_registry *registry, uint32
|
||||
window_wayland->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
|
||||
} else if(strcmp(interface, wl_output_interface.name) == 0) {
|
||||
if(version < 4) {
|
||||
fprintf(stderr, "gsr warning: wl output interface version is < 4, expected >= 4 to capture a monitor. Using KMS capture instead\n");
|
||||
fprintf(stderr, "gsr warning: wl output interface version is < 4, expected >= 4 to capture a monitor\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -123,6 +128,17 @@ static void registry_add_object(void *data, struct wl_registry *registry, uint32
|
||||
.name = NULL,
|
||||
};
|
||||
wl_output_add_listener(gsr_output->output, &output_listener, gsr_output);
|
||||
} else if(strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
|
||||
if(version < 1) {
|
||||
fprintf(stderr, "gsr warning: xdg output interface version is < 1, expected >= 1 to capture a monitor\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if(window_wayland->xdg_output_manager) {
|
||||
zxdg_output_manager_v1_destroy(window_wayland->xdg_output_manager);
|
||||
window_wayland->xdg_output_manager = NULL;
|
||||
}
|
||||
window_wayland->xdg_output_manager = wl_registry_bind(registry, name, &zxdg_output_manager_v1_interface, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,6 +154,60 @@ static struct wl_registry_listener registry_listener = {
|
||||
.global_remove = registry_remove_object,
|
||||
};
|
||||
|
||||
static void xdg_output_logical_position(void *data, struct zxdg_output_v1 *zxdg_output_v1, int32_t x, int32_t y) {
|
||||
(void)zxdg_output_v1;
|
||||
gsr_wayland_output *gsr_xdg_output = data;
|
||||
gsr_xdg_output->pos.x = x;
|
||||
gsr_xdg_output->pos.y = y;
|
||||
}
|
||||
|
||||
static void xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output, int32_t width, int32_t height) {
|
||||
(void)data;
|
||||
(void)xdg_output;
|
||||
(void)width;
|
||||
(void)height;
|
||||
}
|
||||
|
||||
static void xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output) {
|
||||
(void)data;
|
||||
(void)xdg_output;
|
||||
}
|
||||
|
||||
static void xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output, const char *name) {
|
||||
(void)data;
|
||||
(void)xdg_output;
|
||||
(void)name;
|
||||
}
|
||||
|
||||
static void xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output, const char *description) {
|
||||
(void)data;
|
||||
(void)xdg_output;
|
||||
(void)description;
|
||||
}
|
||||
|
||||
static const struct zxdg_output_v1_listener xdg_output_listener = {
|
||||
.logical_position = xdg_output_logical_position,
|
||||
.logical_size = xdg_output_handle_logical_size,
|
||||
.done = xdg_output_handle_done,
|
||||
.name = xdg_output_handle_name,
|
||||
.description = xdg_output_handle_description,
|
||||
};
|
||||
|
||||
static void gsr_window_wayland_set_monitor_outputs_from_xdg_output(gsr_window_wayland *self) {
|
||||
if(!self->xdg_output_manager) {
|
||||
fprintf(stderr, "gsr warning: zxdg_output_manager not found. registered monitor positions might be incorrect\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for(int i = 0; i < self->num_outputs; ++i) {
|
||||
self->outputs[i].xdg_output = zxdg_output_manager_v1_get_xdg_output(self->xdg_output_manager, self->outputs[i].output);
|
||||
zxdg_output_v1_add_listener(self->outputs[i].xdg_output, &xdg_output_listener, &self->outputs[i]);
|
||||
}
|
||||
|
||||
// Fetch xdg_output
|
||||
wl_display_roundtrip(self->display);
|
||||
}
|
||||
|
||||
static void gsr_window_wayland_deinit(gsr_window_wayland *self) {
|
||||
if(self->window) {
|
||||
wl_egl_window_destroy(self->window);
|
||||
@@ -159,9 +229,19 @@ static void gsr_window_wayland_deinit(gsr_window_wayland *self) {
|
||||
free(self->outputs[i].name);
|
||||
self->outputs[i].name = NULL;
|
||||
}
|
||||
|
||||
if(self->outputs[i].xdg_output) {
|
||||
zxdg_output_v1_destroy(self->outputs[i].xdg_output);
|
||||
self->outputs[i].output = NULL;
|
||||
}
|
||||
}
|
||||
self->num_outputs = 0;
|
||||
|
||||
if(self->xdg_output_manager) {
|
||||
zxdg_output_manager_v1_destroy(self->xdg_output_manager);
|
||||
self->xdg_output_manager = NULL;
|
||||
}
|
||||
|
||||
if(self->compositor) {
|
||||
wl_compositor_destroy(self->compositor);
|
||||
self->compositor = NULL;
|
||||
@@ -194,6 +274,8 @@ static bool gsr_window_wayland_init(gsr_window_wayland *self) {
|
||||
// Fetch wl_output
|
||||
wl_display_roundtrip(self->display);
|
||||
|
||||
gsr_window_wayland_set_monitor_outputs_from_xdg_output(self);
|
||||
|
||||
if(!self->compositor) {
|
||||
fprintf(stderr, "gsr error: gsr_window_wayland_init failed: failed to find compositor\n");
|
||||
goto fail;
|
||||
|
||||
Reference in New Issue
Block a user