Skip to content

Commit

Permalink
#2467 handle scaling in shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Nov 9, 2023
1 parent 7f46d5a commit 267339f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
30 changes: 17 additions & 13 deletions xpra/client/gl/backing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions xpra/client/gl/shaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ 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;
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 {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};
Expand All @@ -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;
Expand Down

0 comments on commit 267339f

Please sign in to comment.