From ba50fbdc3e75001b70eec770826aeb46085040cb Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Fri, 19 Apr 2019 11:23:51 +1000 Subject: [PATCH] [client] egl: implement pixel perfect upscaling --- VERSION | 2 +- client/renderers/EGL/desktop.c | 17 +++++++---- client/renderers/EGL/desktop.h | 2 +- client/renderers/EGL/egl.c | 5 +++- client/renderers/EGL/shader/desktop_rgb.frag | 11 +++++-- client/renderers/EGL/shader/desktop_yuv.frag | 31 +++++++++++++++----- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/VERSION b/VERSION index a3ee9c51..984db311 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -a12-157-g972ff93e6c+1 \ No newline at end of file +a12-158-g6f77ba8aea+1 \ No newline at end of file diff --git a/client/renderers/EGL/desktop.c b/client/renderers/EGL/desktop.c index caf3651e..f25e03a6 100644 --- a/client/renderers/EGL/desktop.c +++ b/client/renderers/EGL/desktop.c @@ -47,6 +47,8 @@ struct EGL_Desktop // uniforms GLint uDesktopPos; + GLint uDesktopSize; + GLint uNearest; GLint uNV, uNVGain; // internals @@ -200,9 +202,11 @@ bool egl_desktop_perform_update(EGL_Desktop * desktop, const bool sourceChanged) { if (desktop->shader) { - desktop->uDesktopPos = egl_shader_get_uniform_location(desktop->shader, "position"); - desktop->uNV = egl_shader_get_uniform_location(desktop->shader, "nv" ); - desktop->uNVGain = egl_shader_get_uniform_location(desktop->shader, "nvGain" ); + desktop->uDesktopPos = egl_shader_get_uniform_location(desktop->shader, "position"); + desktop->uDesktopSize = egl_shader_get_uniform_location(desktop->shader, "size" ); + desktop->uNearest = egl_shader_get_uniform_location(desktop->shader, "nearest" ); + desktop->uNV = egl_shader_get_uniform_location(desktop->shader, "nv" ); + desktop->uNVGain = egl_shader_get_uniform_location(desktop->shader, "nvGain" ); } if (!egl_texture_setup( @@ -232,13 +236,16 @@ bool egl_desktop_perform_update(EGL_Desktop * desktop, const bool sourceChanged) return true; } -void egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, const float scaleX, const float scaleY) +void egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, const float scaleX, const float scaleY, const bool nearest) { if (!desktop->shader) return; egl_shader_use(desktop->shader); - glUniform4f(desktop->uDesktopPos, x, y, scaleX, scaleY); + glUniform4f(desktop->uDesktopPos , x, y, scaleX, scaleY); + glUniform1i(desktop->uNearest , nearest ? 1 : 0); + glUniform2f(desktop->uDesktopSize, desktop->width, desktop->height); + if (desktop->nvGain) { glUniform1i(desktop->uNV, 1); diff --git a/client/renderers/EGL/desktop.h b/client/renderers/EGL/desktop.h index cdffa2e2..3835ab04 100644 --- a/client/renderers/EGL/desktop.h +++ b/client/renderers/EGL/desktop.h @@ -30,4 +30,4 @@ void egl_desktop_free(EGL_Desktop ** desktop); bool egl_desktop_prepare_update(EGL_Desktop * desktop, const bool sourceChanged, const LG_RendererFormat format, const uint8_t * data); bool egl_desktop_perform_update(EGL_Desktop * desktop, const bool sourceChanged); -void egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, const float scaleX, const float scaleY); \ No newline at end of file +void egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, const float scaleX, const float scaleY, const bool nearest); \ No newline at end of file diff --git a/client/renderers/EGL/egl.c b/client/renderers/EGL/egl.c index d4ca6088..4929140a 100644 --- a/client/renderers/EGL/egl.c +++ b/client/renderers/EGL/egl.c @@ -86,6 +86,7 @@ struct Inst float scaleX , scaleY; float splashRatio; float screenScaleX, screenScaleY; + bool useNearest; float mouseWidth , mouseHeight; float mouseScaleX, mouseScaleY; @@ -239,6 +240,8 @@ bool egl_on_frame_event(void * opaque, const LG_RendererFormat format, const uin if (this->sourceChanged) memcpy(&this->format, &format, sizeof(LG_RendererFormat)); + this->useNearest = this->width < format.width || this->height < format.height; + if (!egl_desktop_prepare_update(this->desktop, this->sourceChanged, format, data)) { DEBUG_INFO("Failed to prepare to update the desktop"); @@ -420,7 +423,7 @@ bool egl_render(void * opaque, SDL_Window * window) glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - egl_desktop_render(this->desktop, this->translateX, this->translateY, this->scaleX, this->scaleY); + egl_desktop_render(this->desktop, this->translateX, this->translateY, this->scaleX, this->scaleY, this->useNearest); egl_cursor_render(this->cursor); if (!this->waitDone) diff --git a/client/renderers/EGL/shader/desktop_rgb.frag b/client/renderers/EGL/shader/desktop_rgb.frag index 9b14b9d3..6c5cbaba 100644 --- a/client/renderers/EGL/shader/desktop_rgb.frag +++ b/client/renderers/EGL/shader/desktop_rgb.frag @@ -4,12 +4,19 @@ in highp vec2 uv; out highp vec4 color; uniform sampler2D sampler1; -uniform int nv; + +uniform int nearest; +uniform highp vec2 size; + +uniform int nv; uniform highp float nvGain; void main() { - color = texture(sampler1, uv); + if(nearest == 1) + color = texture(sampler1, uv); + else + color = texelFetch(sampler1, ivec2(uv * size), 0); if (nv == 1) { diff --git a/client/renderers/EGL/shader/desktop_yuv.frag b/client/renderers/EGL/shader/desktop_yuv.frag index 94c7ca2a..a0408670 100644 --- a/client/renderers/EGL/shader/desktop_yuv.frag +++ b/client/renderers/EGL/shader/desktop_yuv.frag @@ -3,7 +3,10 @@ in highp vec2 uv; out highp vec4 color; -uniform int nv; +uniform int nearest; +uniform highp vec2 size; + +uniform int nv; uniform highp float nvGain; uniform sampler2D sampler1; @@ -12,12 +15,26 @@ uniform sampler2D sampler3; void main() { - highp vec4 yuv = vec4( - texture(sampler1, uv).r, - texture(sampler2, uv).r, - texture(sampler3, uv).r, - 1.0 - ); + highp vec4 yuv; + if(nearest == 1) + { + yuv = vec4( + texture(sampler1, uv).r, + texture(sampler2, uv).r, + texture(sampler3, uv).r, + 1.0 + ); + } + else + { + highp ivec2 px = ivec2(uv * size); + yuv = vec4( + texelFetch(sampler1, px, 0).r, + texelFetch(sampler2, px, 0).r, + texelFetch(sampler3, px, 0).r, + 1.0 + ); + } highp mat4 yuv_to_rgb = mat4( 1.0, 0.0 , 1.402, -0.701,