Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU opengl shader helper functions |
| 3 | * |
| 4 | * Copyright (c) 2014 Red Hat |
| 5 | * |
| 6 | * Authors: |
| 7 | * Gerd Hoffmann <kraxel@redhat.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 27 | #include "qemu/osdep.h" |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 28 | #include "ui/shader.h" |
| 29 | |
Marc-André Lureau | 2b1ccdf | 2019-07-16 23:21:02 +0400 | [diff] [blame] | 30 | #include "ui/shader/texture-blit-vert.h" |
| 31 | #include "ui/shader/texture-blit-flip-vert.h" |
| 32 | #include "ui/shader/texture-blit-frag.h" |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 33 | |
| 34 | struct QemuGLShader { |
| 35 | GLint texture_blit_prog; |
Gerd Hoffmann | 2e1d70b | 2017-10-10 15:54:50 +0200 | [diff] [blame] | 36 | GLint texture_blit_flip_prog; |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 37 | GLint texture_blit_vao; |
| 38 | }; |
| 39 | |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 40 | /* ---------------------------------------------------------------------- */ |
| 41 | |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 42 | static GLuint qemu_gl_init_texture_blit(GLint texture_blit_prog) |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 43 | { |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 44 | static const GLfloat in_position[] = { |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 45 | -1, -1, |
| 46 | 1, -1, |
| 47 | -1, 1, |
| 48 | 1, 1, |
| 49 | }; |
| 50 | GLint l_position; |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 51 | GLuint vao, buffer; |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 52 | |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 53 | glGenVertexArrays(1, &vao); |
| 54 | glBindVertexArray(vao); |
| 55 | |
| 56 | /* this is the VBO that holds the vertex data */ |
| 57 | glGenBuffers(1, &buffer); |
| 58 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
| 59 | glBufferData(GL_ARRAY_BUFFER, sizeof(in_position), in_position, |
| 60 | GL_STATIC_DRAW); |
| 61 | |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 62 | l_position = glGetAttribLocation(texture_blit_prog, "in_position"); |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 63 | glVertexAttribPointer(l_position, 2, GL_FLOAT, GL_FALSE, 0, 0); |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 64 | glEnableVertexAttribArray(l_position); |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 65 | |
| 66 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 67 | glBindVertexArray(0); |
| 68 | |
| 69 | return vao; |
| 70 | } |
| 71 | |
Gerd Hoffmann | 2e1d70b | 2017-10-10 15:54:50 +0200 | [diff] [blame] | 72 | void qemu_gl_run_texture_blit(QemuGLShader *gls, bool flip) |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 73 | { |
Gerd Hoffmann | 2e1d70b | 2017-10-10 15:54:50 +0200 | [diff] [blame] | 74 | glUseProgram(flip |
| 75 | ? gls->texture_blit_flip_prog |
| 76 | : gls->texture_blit_prog); |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 77 | glBindVertexArray(gls->texture_blit_vao); |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 78 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* ---------------------------------------------------------------------- */ |
| 82 | |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 83 | static GLuint qemu_gl_create_compile_shader(GLenum type, const GLchar *src) |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 84 | { |
| 85 | GLuint shader; |
| 86 | GLint status, length; |
| 87 | char *errmsg; |
| 88 | |
| 89 | shader = glCreateShader(type); |
| 90 | glShaderSource(shader, 1, &src, 0); |
| 91 | glCompileShader(shader); |
| 92 | |
| 93 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 94 | if (!status) { |
| 95 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); |
Md Haris Iqbal | 42ddb8a | 2016-04-05 18:51:39 +0530 | [diff] [blame] | 96 | errmsg = g_malloc(length); |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 97 | glGetShaderInfoLog(shader, length, &length, errmsg); |
| 98 | fprintf(stderr, "%s: compile %s error\n%s\n", __func__, |
| 99 | (type == GL_VERTEX_SHADER) ? "vertex" : "fragment", |
| 100 | errmsg); |
Md Haris Iqbal | 42ddb8a | 2016-04-05 18:51:39 +0530 | [diff] [blame] | 101 | g_free(errmsg); |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | return shader; |
| 105 | } |
| 106 | |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 107 | static GLuint qemu_gl_create_link_program(GLuint vert, GLuint frag) |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 108 | { |
| 109 | GLuint program; |
| 110 | GLint status, length; |
| 111 | char *errmsg; |
| 112 | |
| 113 | program = glCreateProgram(); |
| 114 | glAttachShader(program, vert); |
| 115 | glAttachShader(program, frag); |
| 116 | glLinkProgram(program); |
| 117 | |
| 118 | glGetProgramiv(program, GL_LINK_STATUS, &status); |
| 119 | if (!status) { |
| 120 | glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length); |
Md Haris Iqbal | 42ddb8a | 2016-04-05 18:51:39 +0530 | [diff] [blame] | 121 | errmsg = g_malloc(length); |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 122 | glGetProgramInfoLog(program, length, &length, errmsg); |
| 123 | fprintf(stderr, "%s: link program: %s\n", __func__, errmsg); |
Md Haris Iqbal | 42ddb8a | 2016-04-05 18:51:39 +0530 | [diff] [blame] | 124 | g_free(errmsg); |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | return program; |
| 128 | } |
| 129 | |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 130 | static GLuint qemu_gl_create_compile_link_program(const GLchar *vert_src, |
| 131 | const GLchar *frag_src) |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 132 | { |
Marc-André Lureau | 8fe496a | 2022-02-17 11:36:45 +0400 | [diff] [blame] | 133 | GLuint vert_shader, frag_shader, program = 0; |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 134 | |
| 135 | vert_shader = qemu_gl_create_compile_shader(GL_VERTEX_SHADER, vert_src); |
| 136 | frag_shader = qemu_gl_create_compile_shader(GL_FRAGMENT_SHADER, frag_src); |
| 137 | if (!vert_shader || !frag_shader) { |
Marc-André Lureau | 8fe496a | 2022-02-17 11:36:45 +0400 | [diff] [blame] | 138 | goto end; |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | program = qemu_gl_create_link_program(vert_shader, frag_shader); |
Marc-André Lureau | 8fe496a | 2022-02-17 11:36:45 +0400 | [diff] [blame] | 142 | |
| 143 | end: |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 144 | glDeleteShader(vert_shader); |
| 145 | glDeleteShader(frag_shader); |
| 146 | |
| 147 | return program; |
| 148 | } |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 149 | |
| 150 | /* ---------------------------------------------------------------------- */ |
| 151 | |
| 152 | QemuGLShader *qemu_gl_init_shader(void) |
| 153 | { |
| 154 | QemuGLShader *gls = g_new0(QemuGLShader, 1); |
| 155 | |
| 156 | gls->texture_blit_prog = qemu_gl_create_compile_link_program |
| 157 | (texture_blit_vert_src, texture_blit_frag_src); |
Gerd Hoffmann | 2e1d70b | 2017-10-10 15:54:50 +0200 | [diff] [blame] | 158 | gls->texture_blit_flip_prog = qemu_gl_create_compile_link_program |
| 159 | (texture_blit_flip_vert_src, texture_blit_frag_src); |
| 160 | if (!gls->texture_blit_prog || !gls->texture_blit_flip_prog) { |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 161 | exit(1); |
| 162 | } |
| 163 | |
| 164 | gls->texture_blit_vao = |
| 165 | qemu_gl_init_texture_blit(gls->texture_blit_prog); |
| 166 | |
| 167 | return gls; |
| 168 | } |
| 169 | |
| 170 | void qemu_gl_fini_shader(QemuGLShader *gls) |
| 171 | { |
| 172 | if (!gls) { |
| 173 | return; |
| 174 | } |
Marc-André Lureau | 532042d | 2022-02-17 11:37:47 +0400 | [diff] [blame] | 175 | glDeleteProgram(gls->texture_blit_prog); |
| 176 | glDeleteProgram(gls->texture_blit_flip_prog); |
| 177 | glDeleteProgram(gls->texture_blit_vao); |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 178 | g_free(gls); |
| 179 | } |