From 46e19e149f3b129a22c440caba853188df67deab Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 10 Oct 2017 15:54:49 +0200 Subject: opengl: move shader init from console-gl.c to shader.c With the upcoming dmabuf support in qemu there will be more users of the shaders than just console-gl.c. So rename ConsoleGLState to QemuGLShader, rename some functions too, move code from console-gl.c to shaders.c. No functional change. Signed-off-by: Gerd Hoffmann Message-id: 20171010135453.6704-3-kraxel@redhat.com --- ui/console-gl.c | 45 ++++++--------------------------------------- 1 file changed, 6 insertions(+), 39 deletions(-) (limited to 'ui/console-gl.c') diff --git a/ui/console-gl.c b/ui/console-gl.c index 5165e21646..9b50daedbd 100644 --- a/ui/console-gl.c +++ b/ui/console-gl.c @@ -29,40 +29,8 @@ #include "ui/console.h" #include "ui/shader.h" -#include "shader/texture-blit-vert.h" -#include "shader/texture-blit-frag.h" - -struct ConsoleGLState { - GLint texture_blit_prog; - GLint texture_blit_vao; -}; - /* ---------------------------------------------------------------------- */ -ConsoleGLState *console_gl_init_context(void) -{ - ConsoleGLState *gls = g_new0(ConsoleGLState, 1); - - gls->texture_blit_prog = qemu_gl_create_compile_link_program - (texture_blit_vert_src, texture_blit_frag_src); - if (!gls->texture_blit_prog) { - exit(1); - } - - gls->texture_blit_vao = - qemu_gl_init_texture_blit(gls->texture_blit_prog); - - return gls; -} - -void console_gl_fini_context(ConsoleGLState *gls) -{ - if (!gls) { - return; - } - g_free(gls); -} - bool console_gl_check_format(DisplayChangeListener *dcl, pixman_format_code_t format) { @@ -76,7 +44,7 @@ bool console_gl_check_format(DisplayChangeListener *dcl, } } -void surface_gl_create_texture(ConsoleGLState *gls, +void surface_gl_create_texture(QemuGLShader *gls, DisplaySurface *surface) { assert(gls); @@ -116,7 +84,7 @@ void surface_gl_create_texture(ConsoleGLState *gls, glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } -void surface_gl_update_texture(ConsoleGLState *gls, +void surface_gl_update_texture(QemuGLShader *gls, DisplaySurface *surface, int x, int y, int w, int h) { @@ -133,7 +101,7 @@ void surface_gl_update_texture(ConsoleGLState *gls, + surface_bytes_per_pixel(surface) * x); } -void surface_gl_render_texture(ConsoleGLState *gls, +void surface_gl_render_texture(QemuGLShader *gls, DisplaySurface *surface) { assert(gls); @@ -141,11 +109,10 @@ void surface_gl_render_texture(ConsoleGLState *gls, glClearColor(0.1f, 0.1f, 0.1f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); - qemu_gl_run_texture_blit(gls->texture_blit_prog, - gls->texture_blit_vao); + qemu_gl_run_texture_blit(gls); } -void surface_gl_destroy_texture(ConsoleGLState *gls, +void surface_gl_destroy_texture(QemuGLShader *gls, DisplaySurface *surface) { if (!surface || !surface->texture) { @@ -155,7 +122,7 @@ void surface_gl_destroy_texture(ConsoleGLState *gls, surface->texture = 0; } -void surface_gl_setup_viewport(ConsoleGLState *gls, +void surface_gl_setup_viewport(QemuGLShader *gls, DisplaySurface *surface, int ww, int wh) { -- cgit 1.4.1 From 2e1d70b9e03ca3f1c6185b54010bc9e47e0a0d0c Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 10 Oct 2017 15:54:50 +0200 Subject: opengl: add flipping vertex shader Add vertex shader which flips the texture upside down while blitting it. Add argument to qemu_gl_run_texture_blit() to enable flipping. Signed-off-by: Gerd Hoffmann Message-id: 20171010135453.6704-4-kraxel@redhat.com --- Makefile | 4 +++- include/ui/shader.h | 2 +- ui/console-gl.c | 2 +- ui/shader.c | 12 +++++++++--- ui/shader/texture-blit-flip.vert | 10 ++++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 ui/shader/texture-blit-flip.vert (limited to 'ui/console-gl.c') diff --git a/Makefile b/Makefile index e124f1c5a0..0d293ad294 100644 --- a/Makefile +++ b/Makefile @@ -673,7 +673,9 @@ ui/shader/%-frag.h: $(SRC_PATH)/ui/shader/%.frag $(SRC_PATH)/scripts/shaderinclu "FRAG","$@") ui/shader.o: $(SRC_PATH)/ui/shader.c \ - ui/shader/texture-blit-vert.h ui/shader/texture-blit-frag.h + ui/shader/texture-blit-vert.h \ + ui/shader/texture-blit-flip-vert.h \ + ui/shader/texture-blit-frag.h # documentation MAKEINFO=makeinfo diff --git a/include/ui/shader.h b/include/ui/shader.h index 369e49865f..4c5acb2ce8 100644 --- a/include/ui/shader.h +++ b/include/ui/shader.h @@ -5,7 +5,7 @@ typedef struct QemuGLShader QemuGLShader; -void qemu_gl_run_texture_blit(QemuGLShader *gls); +void qemu_gl_run_texture_blit(QemuGLShader *gls, bool flip); QemuGLShader *qemu_gl_init_shader(void); void qemu_gl_fini_shader(QemuGLShader *gls); diff --git a/ui/console-gl.c b/ui/console-gl.c index 9b50daedbd..5b77e7aa88 100644 --- a/ui/console-gl.c +++ b/ui/console-gl.c @@ -109,7 +109,7 @@ void surface_gl_render_texture(QemuGLShader *gls, glClearColor(0.1f, 0.1f, 0.1f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); - qemu_gl_run_texture_blit(gls); + qemu_gl_run_texture_blit(gls, false); } void surface_gl_destroy_texture(QemuGLShader *gls, diff --git a/ui/shader.c b/ui/shader.c index d36e7af232..008458bf94 100644 --- a/ui/shader.c +++ b/ui/shader.c @@ -29,10 +29,12 @@ #include "ui/shader.h" #include "shader/texture-blit-vert.h" +#include "shader/texture-blit-flip-vert.h" #include "shader/texture-blit-frag.h" struct QemuGLShader { GLint texture_blit_prog; + GLint texture_blit_flip_prog; GLint texture_blit_vao; }; @@ -68,9 +70,11 @@ static GLuint qemu_gl_init_texture_blit(GLint texture_blit_prog) return vao; } -void qemu_gl_run_texture_blit(QemuGLShader *gls) +void qemu_gl_run_texture_blit(QemuGLShader *gls, bool flip) { - glUseProgram(gls->texture_blit_prog); + glUseProgram(flip + ? gls->texture_blit_flip_prog + : gls->texture_blit_prog); glBindVertexArray(gls->texture_blit_vao); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } @@ -150,7 +154,9 @@ QemuGLShader *qemu_gl_init_shader(void) gls->texture_blit_prog = qemu_gl_create_compile_link_program (texture_blit_vert_src, texture_blit_frag_src); - if (!gls->texture_blit_prog) { + gls->texture_blit_flip_prog = qemu_gl_create_compile_link_program + (texture_blit_flip_vert_src, texture_blit_frag_src); + if (!gls->texture_blit_prog || !gls->texture_blit_flip_prog) { exit(1); } diff --git a/ui/shader/texture-blit-flip.vert b/ui/shader/texture-blit-flip.vert new file mode 100644 index 0000000000..ba081fa5a6 --- /dev/null +++ b/ui/shader/texture-blit-flip.vert @@ -0,0 +1,10 @@ + +#version 300 es + +in vec2 in_position; +out vec2 ex_tex_coord; + +void main(void) { + gl_Position = vec4(in_position, 0.0, 1.0); + ex_tex_coord = vec2(1.0 + in_position.x, 1.0 + in_position.y) * 0.5; +} -- cgit 1.4.1