2
0

shader.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/osdep.h"
  28. #include "ui/shader.h"
  29. #include "ui/shader/texture-blit-vert.h"
  30. #include "ui/shader/texture-blit-flip-vert.h"
  31. #include "ui/shader/texture-blit-frag.h"
  32. struct QemuGLShader {
  33. GLint texture_blit_prog;
  34. GLint texture_blit_flip_prog;
  35. GLint texture_blit_vao;
  36. };
  37. /* ---------------------------------------------------------------------- */
  38. static GLuint qemu_gl_init_texture_blit(GLint texture_blit_prog)
  39. {
  40. static const GLfloat in_position[] = {
  41. -1, -1,
  42. 1, -1,
  43. -1, 1,
  44. 1, 1,
  45. };
  46. GLint l_position;
  47. GLuint vao, buffer;
  48. glGenVertexArrays(1, &vao);
  49. glBindVertexArray(vao);
  50. /* this is the VBO that holds the vertex data */
  51. glGenBuffers(1, &buffer);
  52. glBindBuffer(GL_ARRAY_BUFFER, buffer);
  53. glBufferData(GL_ARRAY_BUFFER, sizeof(in_position), in_position,
  54. GL_STATIC_DRAW);
  55. l_position = glGetAttribLocation(texture_blit_prog, "in_position");
  56. glVertexAttribPointer(l_position, 2, GL_FLOAT, GL_FALSE, 0, 0);
  57. glEnableVertexAttribArray(l_position);
  58. glBindBuffer(GL_ARRAY_BUFFER, 0);
  59. glBindVertexArray(0);
  60. return vao;
  61. }
  62. void qemu_gl_run_texture_blit(QemuGLShader *gls, bool flip)
  63. {
  64. glUseProgram(flip
  65. ? gls->texture_blit_flip_prog
  66. : gls->texture_blit_prog);
  67. glBindVertexArray(gls->texture_blit_vao);
  68. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  69. }
  70. /* ---------------------------------------------------------------------- */
  71. static GLuint qemu_gl_create_compile_shader(GLenum type, const GLchar *src)
  72. {
  73. GLuint shader;
  74. GLint status, length;
  75. char *errmsg;
  76. shader = glCreateShader(type);
  77. glShaderSource(shader, 1, &src, 0);
  78. glCompileShader(shader);
  79. glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  80. if (!status) {
  81. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
  82. errmsg = g_malloc(length);
  83. glGetShaderInfoLog(shader, length, &length, errmsg);
  84. fprintf(stderr, "%s: compile %s error\n%s\n", __func__,
  85. (type == GL_VERTEX_SHADER) ? "vertex" : "fragment",
  86. errmsg);
  87. g_free(errmsg);
  88. return 0;
  89. }
  90. return shader;
  91. }
  92. static GLuint qemu_gl_create_link_program(GLuint vert, GLuint frag)
  93. {
  94. GLuint program;
  95. GLint status, length;
  96. char *errmsg;
  97. program = glCreateProgram();
  98. glAttachShader(program, vert);
  99. glAttachShader(program, frag);
  100. glLinkProgram(program);
  101. glGetProgramiv(program, GL_LINK_STATUS, &status);
  102. if (!status) {
  103. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
  104. errmsg = g_malloc(length);
  105. glGetProgramInfoLog(program, length, &length, errmsg);
  106. fprintf(stderr, "%s: link program: %s\n", __func__, errmsg);
  107. g_free(errmsg);
  108. return 0;
  109. }
  110. return program;
  111. }
  112. static GLuint qemu_gl_create_compile_link_program(const GLchar *vert_src,
  113. const GLchar *frag_src)
  114. {
  115. GLuint vert_shader, frag_shader, program;
  116. vert_shader = qemu_gl_create_compile_shader(GL_VERTEX_SHADER, vert_src);
  117. frag_shader = qemu_gl_create_compile_shader(GL_FRAGMENT_SHADER, frag_src);
  118. if (!vert_shader || !frag_shader) {
  119. return 0;
  120. }
  121. program = qemu_gl_create_link_program(vert_shader, frag_shader);
  122. glDeleteShader(vert_shader);
  123. glDeleteShader(frag_shader);
  124. return program;
  125. }
  126. /* ---------------------------------------------------------------------- */
  127. QemuGLShader *qemu_gl_init_shader(void)
  128. {
  129. QemuGLShader *gls = g_new0(QemuGLShader, 1);
  130. gls->texture_blit_prog = qemu_gl_create_compile_link_program
  131. (texture_blit_vert_src, texture_blit_frag_src);
  132. gls->texture_blit_flip_prog = qemu_gl_create_compile_link_program
  133. (texture_blit_flip_vert_src, texture_blit_frag_src);
  134. if (!gls->texture_blit_prog || !gls->texture_blit_flip_prog) {
  135. exit(1);
  136. }
  137. gls->texture_blit_vao =
  138. qemu_gl_init_texture_blit(gls->texture_blit_prog);
  139. return gls;
  140. }
  141. void qemu_gl_fini_shader(QemuGLShader *gls)
  142. {
  143. if (!gls) {
  144. return;
  145. }
  146. g_free(gls);
  147. }