Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
render/gles2: fix y-inverted output when rendering to buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Jun 30, 2020
1 parent 463ce79 commit 51fb54d
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions render/gles2/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,12 @@ static void gles2_scissor(struct wlr_renderer *wlr_renderer,
PUSH_GLES2_DEBUG;
if (box != NULL) {
struct wlr_box gl_box;
wlr_box_transform(&gl_box, box, WL_OUTPUT_TRANSFORM_FLIPPED_180,
renderer->viewport_width, renderer->viewport_height);
if (renderer->current_buffer != NULL) {
memcpy(&gl_box, box, sizeof(gl_box));
} else {
wlr_box_transform(&gl_box, box, WL_OUTPUT_TRANSFORM_FLIPPED_180,
renderer->viewport_width, renderer->viewport_height);
}

glScissor(gl_box.x, gl_box.y, gl_box.width, gl_box.height);
glEnable(GL_SCISSOR_TEST);
Expand All @@ -229,6 +233,12 @@ static void gles2_scissor(struct wlr_renderer *wlr_renderer,
POP_GLES2_DEBUG;
}

static const float flip_180[9] = {
1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
};

static bool gles2_render_subtexture_with_matrix(
struct wlr_renderer *wlr_renderer, struct wlr_texture *wlr_texture,
const struct wlr_fbox *box, const float matrix[static 9],
Expand Down Expand Up @@ -261,10 +271,15 @@ static bool gles2_render_subtexture_with_matrix(
abort();
}

float gl_matrix[9];
if (renderer->current_buffer != NULL) {
wlr_matrix_multiply(gl_matrix, flip_180, matrix);
} else {
memcpy(gl_matrix, matrix, sizeof(gl_matrix));
}
// OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set
// to GL_FALSE
float transposition[9];
wlr_matrix_transpose(transposition, matrix);
wlr_matrix_transpose(gl_matrix, gl_matrix);

PUSH_GLES2_DEBUG;

Expand All @@ -275,7 +290,7 @@ static bool gles2_render_subtexture_with_matrix(

glUseProgram(shader->program);

glUniformMatrix3fv(shader->proj, 1, GL_FALSE, transposition);
glUniformMatrix3fv(shader->proj, 1, GL_FALSE, gl_matrix);
glUniform1i(shader->invert_y, texture->inverted_y);
glUniform1i(shader->tex, 0);
glUniform1f(shader->alpha, alpha);
Expand Down Expand Up @@ -313,15 +328,20 @@ static void gles2_render_quad_with_matrix(struct wlr_renderer *wlr_renderer,
struct wlr_gles2_renderer *renderer =
gles2_get_renderer_in_context(wlr_renderer);

float gl_matrix[9];
if (renderer->current_buffer != NULL) {
wlr_matrix_multiply(gl_matrix, flip_180, matrix);
} else {
memcpy(gl_matrix, matrix, sizeof(gl_matrix));
}
// OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set
// to GL_FALSE
float transposition[9];
wlr_matrix_transpose(transposition, matrix);
wlr_matrix_transpose(gl_matrix, gl_matrix);

PUSH_GLES2_DEBUG;
glUseProgram(renderer->shaders.quad.program);

glUniformMatrix3fv(renderer->shaders.quad.proj, 1, GL_FALSE, transposition);
glUniformMatrix3fv(renderer->shaders.quad.proj, 1, GL_FALSE, gl_matrix);
glUniform4f(renderer->shaders.quad.color, color[0], color[1], color[2], color[3]);

glVertexAttribPointer(renderer->shaders.quad.pos_attrib, 2, GL_FLOAT, GL_FALSE,
Expand All @@ -341,10 +361,15 @@ static void gles2_render_ellipse_with_matrix(struct wlr_renderer *wlr_renderer,
struct wlr_gles2_renderer *renderer =
gles2_get_renderer_in_context(wlr_renderer);

float gl_matrix[9];
if (renderer->current_buffer != NULL) {
wlr_matrix_multiply(gl_matrix, flip_180, matrix);
} else {
memcpy(gl_matrix, matrix, sizeof(gl_matrix));
}
// OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set
// to GL_FALSE
float transposition[9];
wlr_matrix_transpose(transposition, matrix);
wlr_matrix_transpose(gl_matrix, gl_matrix);

static const GLfloat texcoord[] = {
1, 0, // top right
Expand All @@ -356,7 +381,7 @@ static void gles2_render_ellipse_with_matrix(struct wlr_renderer *wlr_renderer,
PUSH_GLES2_DEBUG;
glUseProgram(renderer->shaders.ellipse.program);

glUniformMatrix3fv(renderer->shaders.ellipse.proj, 1, GL_FALSE, transposition);
glUniformMatrix3fv(renderer->shaders.ellipse.proj, 1, GL_FALSE, gl_matrix);
glUniform4f(renderer->shaders.ellipse.color, color[0], color[1], color[2], color[3]);

glVertexAttribPointer(renderer->shaders.ellipse.pos_attrib, 2, GL_FLOAT,
Expand Down Expand Up @@ -478,7 +503,11 @@ static bool gles2_read_pixels(struct wlr_renderer *wlr_renderer,
// one glReadPixels call
glReadPixels(src_x, renderer->viewport_height - height - src_y,
width, height, fmt->gl_format, fmt->gl_type, p);
*flags = WLR_RENDERER_READ_PIXELS_Y_INVERT;
if (renderer->current_buffer != NULL) {
*flags = 0;
} else {
*flags = WLR_RENDERER_READ_PIXELS_Y_INVERT;
}
} else {
// Unfortunately GLES2 doesn't support GL_PACK_*, so we have to read
// the lines out row by row
Expand Down Expand Up @@ -513,6 +542,7 @@ static bool gles2_blit_dmabuf(struct wlr_renderer *wlr_renderer,
goto restore_context_out;
}

// TODO: get inverted_y right when current_buffer != NULL
// This is to take into account y-inversion on both buffers rather than
// just the source buffer.
bool src_inverted_y =
Expand Down

0 comments on commit 51fb54d

Please sign in to comment.