Add qemu 2.4.0
[kvmfornfv.git] / qemu / ui / shader.c
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
32 void qemu_gl_run_texture_blit(GLint texture_blit_prog)
33 {
34     GLfloat in_position[] = {
35         -1, -1,
36         1,  -1,
37         -1,  1,
38         1,   1,
39     };
40     GLint l_position;
41
42     glUseProgram(texture_blit_prog);
43     l_position = glGetAttribLocation(texture_blit_prog, "in_position");
44     glVertexAttribPointer(l_position, 2, GL_FLOAT, GL_FALSE, 0, in_position);
45     glEnableVertexAttribArray(l_position);
46     glDrawArrays(GL_TRIANGLE_STRIP, l_position, 4);
47 }
48
49 /* ---------------------------------------------------------------------- */
50
51 GLuint qemu_gl_create_compile_shader(GLenum type, const GLchar *src)
52 {
53     GLuint shader;
54     GLint status, length;
55     char *errmsg;
56
57     shader = glCreateShader(type);
58     glShaderSource(shader, 1, &src, 0);
59     glCompileShader(shader);
60
61     glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
62     if (!status) {
63         glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
64         errmsg = malloc(length);
65         glGetShaderInfoLog(shader, length, &length, errmsg);
66         fprintf(stderr, "%s: compile %s error\n%s\n", __func__,
67                 (type == GL_VERTEX_SHADER) ? "vertex" : "fragment",
68                 errmsg);
69         free(errmsg);
70         return 0;
71     }
72     return shader;
73 }
74
75 GLuint qemu_gl_create_link_program(GLuint vert, GLuint frag)
76 {
77     GLuint program;
78     GLint status, length;
79     char *errmsg;
80
81     program = glCreateProgram();
82     glAttachShader(program, vert);
83     glAttachShader(program, frag);
84     glLinkProgram(program);
85
86     glGetProgramiv(program, GL_LINK_STATUS, &status);
87     if (!status) {
88         glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
89         errmsg = malloc(length);
90         glGetProgramInfoLog(program, length, &length, errmsg);
91         fprintf(stderr, "%s: link program: %s\n", __func__, errmsg);
92         free(errmsg);
93         return 0;
94     }
95     return program;
96 }
97
98 GLuint qemu_gl_create_compile_link_program(const GLchar *vert_src,
99                                            const GLchar *frag_src)
100 {
101     GLuint vert_shader, frag_shader, program;
102
103     vert_shader = qemu_gl_create_compile_shader(GL_VERTEX_SHADER, vert_src);
104     frag_shader = qemu_gl_create_compile_shader(GL_FRAGMENT_SHADER, frag_src);
105     if (!vert_shader || !frag_shader) {
106         return 0;
107     }
108
109     program = qemu_gl_create_link_program(vert_shader, frag_shader);
110     glDeleteShader(vert_shader);
111     glDeleteShader(frag_shader);
112
113     return program;
114 }