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 | */ |
| 27 | #include "qemu-common.h" |
| 28 | #include "ui/shader.h" |
| 29 | |
| 30 | /* ---------------------------------------------------------------------- */ |
| 31 | |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 32 | GLuint qemu_gl_init_texture_blit(GLint texture_blit_prog) |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 33 | { |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 34 | static const GLfloat in_position[] = { |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 35 | -1, -1, |
| 36 | 1, -1, |
| 37 | -1, 1, |
| 38 | 1, 1, |
| 39 | }; |
| 40 | GLint l_position; |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 41 | GLuint vao, buffer; |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 42 | |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 43 | glGenVertexArrays(1, &vao); |
| 44 | glBindVertexArray(vao); |
| 45 | |
| 46 | /* this is the VBO that holds the vertex data */ |
| 47 | glGenBuffers(1, &buffer); |
| 48 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
| 49 | glBufferData(GL_ARRAY_BUFFER, sizeof(in_position), in_position, |
| 50 | GL_STATIC_DRAW); |
| 51 | |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 52 | l_position = glGetAttribLocation(texture_blit_prog, "in_position"); |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 53 | glVertexAttribPointer(l_position, 2, GL_FLOAT, GL_FALSE, 0, 0); |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 54 | glEnableVertexAttribArray(l_position); |
Gerd Hoffmann | c046d82 | 2015-07-10 14:40:01 +0200 | [diff] [blame] | 55 | |
| 56 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 57 | glBindVertexArray(0); |
| 58 | |
| 59 | return vao; |
| 60 | } |
| 61 | |
| 62 | void qemu_gl_run_texture_blit(GLint texture_blit_prog, |
| 63 | GLint texture_blit_vao) |
| 64 | { |
| 65 | glUseProgram(texture_blit_prog); |
| 66 | glBindVertexArray(texture_blit_vao); |
| 67 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
Gerd Hoffmann | cd2bc88 | 2015-01-09 11:40:23 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* ---------------------------------------------------------------------- */ |
| 71 | |
Gerd Hoffmann | 985e1c9 | 2015-04-24 07:48:45 +0200 | [diff] [blame] | 72 | GLuint qemu_gl_create_compile_shader(GLenum type, const GLchar *src) |
| 73 | { |
| 74 | GLuint shader; |
| 75 | GLint status, length; |
| 76 | char *errmsg; |
| 77 | |
| 78 | shader = glCreateShader(type); |
| 79 | glShaderSource(shader, 1, &src, 0); |
| 80 | glCompileShader(shader); |
| 81 | |
| 82 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 83 | if (!status) { |
| 84 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); |
| 85 | errmsg = malloc(length); |
| 86 | glGetShaderInfoLog(shader, length, &length, errmsg); |
| 87 | fprintf(stderr, "%s: compile %s error\n%s\n", __func__, |
| 88 | (type == GL_VERTEX_SHADER) ? "vertex" : "fragment", |
| 89 | errmsg); |
| 90 | free(errmsg); |
| 91 | return 0; |
| 92 | } |
| 93 | return shader; |
| 94 | } |
| 95 | |
| 96 | GLuint qemu_gl_create_link_program(GLuint vert, GLuint frag) |
| 97 | { |
| 98 | GLuint program; |
| 99 | GLint status, length; |
| 100 | char *errmsg; |
| 101 | |
| 102 | program = glCreateProgram(); |
| 103 | glAttachShader(program, vert); |
| 104 | glAttachShader(program, frag); |
| 105 | glLinkProgram(program); |
| 106 | |
| 107 | glGetProgramiv(program, GL_LINK_STATUS, &status); |
| 108 | if (!status) { |
| 109 | glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length); |
| 110 | errmsg = malloc(length); |
| 111 | glGetProgramInfoLog(program, length, &length, errmsg); |
| 112 | fprintf(stderr, "%s: link program: %s\n", __func__, errmsg); |
| 113 | free(errmsg); |
| 114 | return 0; |
| 115 | } |
| 116 | return program; |
| 117 | } |
| 118 | |
| 119 | GLuint qemu_gl_create_compile_link_program(const GLchar *vert_src, |
| 120 | const GLchar *frag_src) |
| 121 | { |
| 122 | GLuint vert_shader, frag_shader, program; |
| 123 | |
| 124 | vert_shader = qemu_gl_create_compile_shader(GL_VERTEX_SHADER, vert_src); |
| 125 | frag_shader = qemu_gl_create_compile_shader(GL_FRAGMENT_SHADER, frag_src); |
| 126 | if (!vert_shader || !frag_shader) { |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | program = qemu_gl_create_link_program(vert_shader, frag_shader); |
| 131 | glDeleteShader(vert_shader); |
| 132 | glDeleteShader(frag_shader); |
| 133 | |
| 134 | return program; |
| 135 | } |