2
0

shader.c 4.2 KB

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