From 267339f45ae5020a0ade390d47d32cdea685919a Mon Sep 17 00:00:00 2001 From: totaam Date: Thu, 9 Nov 2023 20:43:31 +0700 Subject: [PATCH] #2467 handle scaling in shaders --- xpra/client/gl/backing.py | 30 +++++++++++++++++------------- xpra/client/gl/shaders.py | 6 ++++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/xpra/client/gl/backing.py b/xpra/client/gl/backing.py index 8261fedfc1..3ccbe3c0db 100644 --- a/xpra/client/gl/backing.py +++ b/xpra/client/gl/backing.py @@ -1272,16 +1272,8 @@ def do_gl_paint_planar(self, context, shader: str, flush: int, encoding: str, im self.gl_init(context) pbo = options.boolget("pbo") scaling = enc_width != width or enc_height != height - log.warn(f"{pbo=}, {scaling=}") self.update_planar_textures(enc_width, enc_height, img, pixel_format, scaling=scaling, pbo=pbo) - - # Update FBO texture - x_scale, y_scale = 1.0, 1.0 - if scaling: - x_scale = width/enc_width - y_scale = height/enc_height - - self.render_planar_update(x, y, enc_width, enc_height, x_scale, y_scale, shader) + self.render_planar_update(x, y, enc_width, enc_height, width, height, shader) self.paint_box(encoding, x, y, width, height) fire_paint_callbacks(callbacks, True) # Present it on screen @@ -1387,9 +1379,9 @@ def update_planar_textures(self, width: int, height: int, img, pixel_format, sca glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0) # glActiveTexture(GL_TEXTURE0) #redundant, we always call render_planar_update afterwards - def render_planar_update(self, rx: int, ry: int, rw: int, rh: int, x_scale=1.0, y_scale=1.0, shader="YUV_to_RGB") -> None: + def render_planar_update(self, rx: int, ry: int, rw: int, rh: int, width: int, height: int, shader="YUV_to_RGB") -> None: log("%s.render_planar_update%s pixel_format=%s", - self, (rx, ry, rw, rh, x_scale, y_scale, shader), self.planar_pixel_format) + self, (rx, ry, rw, rh, width, height, shader), self.planar_pixel_format) if self.planar_pixel_format not in ("YUV420P", "YUV422P", "YUV444P", "GBRP", "NV12", "GBRP16", "YUV444P16"): # not ready to render yet return @@ -1407,9 +1399,18 @@ def render_planar_update(self, rx: int, ry: int, rw: int, rh: int, x_scale=1.0, glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, self.textures[TEX_FBO], 0) glDrawBuffer(GL_COLOR_ATTACHMENT0) - wh = self.render_size[1] + ww, wh = self.render_size # the region we're updating: - glViewport(rx, wh-ry-rh, rw, rh) + + def clampx(v: int) -> int: + return min(ww, max(0, v)) + + def clampy(v: int) -> int: + return min(wh, max(0, v)) + + viewport = clampx(rx), clampy(wh-ry-height), clampx(width), clampy(height) + glViewport(*viewport) + log("viewport: %s for size=%s render_size=%s", viewport, self.size, self.render_size) program = self.programs[shader] glUseProgram(program) @@ -1427,6 +1428,9 @@ def render_planar_update(self, rx: int, ry: int, rw: int, rh: int, x_scale=1.0, viewport_pos = glGetUniformLocation(program, "viewport_pos") glUniform2f(viewport_pos, rx, ry) + scaling = glGetUniformLocation(program, "scaling") + glUniform2f(scaling, width / rw, height / rh) + pos_buffer = self.set_vao(position) glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) diff --git a/xpra/client/gl/shaders.py b/xpra/client/gl/shaders.py index e0ab72b68c..c41890c917 100644 --- a/xpra/client/gl/shaders.py +++ b/xpra/client/gl/shaders.py @@ -45,6 +45,7 @@ def gen_YUV_to_RGB(cs="bt601", full_range=True): #version {GLSL_VERSION} layout(origin_upper_left) in vec4 gl_FragCoord; uniform vec2 viewport_pos; +uniform vec2 scaling; uniform sampler2DRect Y; uniform sampler2DRect U; uniform sampler2DRect V; @@ -52,7 +53,7 @@ def gen_YUV_to_RGB(cs="bt601", full_range=True): void main() {{ - vec2 pos = gl_FragCoord.xy-viewport_pos.xy; + vec2 pos = (gl_FragCoord.xy-viewport_pos.xy)/scaling; highp float y = texture(Y, pos).r {ymult}; highp float u = (texture(U, pos/2.0).r - 0.5) {umult}; highp float v = (texture(V, pos/2.0).r - 0.5) {vmult}; @@ -76,13 +77,14 @@ def gen_NV12_to_RGB(cs="bt601"): #version {GLSL_VERSION} layout(origin_upper_left) in vec4 gl_FragCoord; uniform vec2 viewport_pos; +uniform vec2 scaling; uniform sampler2DRect Y; uniform sampler2DRect UV; layout(location = 0) out vec4 frag_color; void main() {{ - vec2 pos = gl_FragCoord.xy-viewport_pos.xy; + vec2 pos = (gl_FragCoord.xy-viewport_pos.xy)/scaling; highp float y = texture(Y, pos).r; highp float u = texture(UV, pos).r - 0.5; highp float v = texture(UV, pos).g - 0.5;