shader.c 6.4 KB

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