]> git.proxmox.com Git - mirror_qemu.git/blame - ui/shader.c
opengl: add shader helper functions.
[mirror_qemu.git] / ui / shader.c
CommitLineData
985e1c9b
GH
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
32GLuint qemu_gl_create_compile_shader(GLenum type, const GLchar *src)
33{
34 GLuint shader;
35 GLint status, length;
36 char *errmsg;
37
38 shader = glCreateShader(type);
39 glShaderSource(shader, 1, &src, 0);
40 glCompileShader(shader);
41
42 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
43 if (!status) {
44 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
45 errmsg = malloc(length);
46 glGetShaderInfoLog(shader, length, &length, errmsg);
47 fprintf(stderr, "%s: compile %s error\n%s\n", __func__,
48 (type == GL_VERTEX_SHADER) ? "vertex" : "fragment",
49 errmsg);
50 free(errmsg);
51 return 0;
52 }
53 return shader;
54}
55
56GLuint qemu_gl_create_link_program(GLuint vert, GLuint frag)
57{
58 GLuint program;
59 GLint status, length;
60 char *errmsg;
61
62 program = glCreateProgram();
63 glAttachShader(program, vert);
64 glAttachShader(program, frag);
65 glLinkProgram(program);
66
67 glGetProgramiv(program, GL_LINK_STATUS, &status);
68 if (!status) {
69 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
70 errmsg = malloc(length);
71 glGetProgramInfoLog(program, length, &length, errmsg);
72 fprintf(stderr, "%s: link program: %s\n", __func__, errmsg);
73 free(errmsg);
74 return 0;
75 }
76 return program;
77}
78
79GLuint qemu_gl_create_compile_link_program(const GLchar *vert_src,
80 const GLchar *frag_src)
81{
82 GLuint vert_shader, frag_shader, program;
83
84 vert_shader = qemu_gl_create_compile_shader(GL_VERTEX_SHADER, vert_src);
85 frag_shader = qemu_gl_create_compile_shader(GL_FRAGMENT_SHADER, frag_src);
86 if (!vert_shader || !frag_shader) {
87 return 0;
88 }
89
90 program = qemu_gl_create_link_program(vert_shader, frag_shader);
91 glDeleteShader(vert_shader);
92 glDeleteShader(frag_shader);
93
94 return program;
95}