💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
9.2 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <engine/gl/shader-program.hpp>
  20. #include <engine/gl/shader-object.hpp>
  21. #include <engine/gl/opengl/gl-shader-variables.hpp>
  22. #include <glad/gl.h>
  23. #include <stdexcept>
  24. #include <string_view>
  25. namespace gl {
  26. shader_program::shader_program()
  27. {
  28. // Create an OpenGL shader program
  29. gl_program_id = glCreateProgram();
  30. // Handle OpenGL errors
  31. if (!gl_program_id)
  32. {
  33. throw std::runtime_error("Unable to create OpenGL shader program");
  34. }
  35. }
  36. shader_program::~shader_program()
  37. {
  38. // Detach all shader objects
  39. detach_all();
  40. // Delete the OpenGL shader program
  41. glDeleteProgram(gl_program_id);
  42. }
  43. void shader_program::attach(const shader_object& object)
  44. {
  45. if (attached_objects.find(&object) != attached_objects.end())
  46. {
  47. throw std::runtime_error("OpenGL shader object already attached to the shader program");
  48. }
  49. else
  50. {
  51. // Check that both the OpenGL shader program and OpenGL shader object are valid
  52. if (glIsProgram(gl_program_id) != GL_TRUE)
  53. {
  54. throw std::runtime_error("Invalid OpenGL shader program");
  55. }
  56. if (glIsShader(object.gl_shader_id) != GL_TRUE)
  57. {
  58. throw std::runtime_error("Invalid OpenGL shader object");
  59. }
  60. // Attach the OpenGL shader object to the OpenGL shader program
  61. glAttachShader(gl_program_id, object.gl_shader_id);
  62. // Add shader object to set of attached objects
  63. attached_objects.insert(&object);
  64. }
  65. }
  66. void shader_program::detach(const shader_object& object)
  67. {
  68. if (attached_objects.find(&object) == attached_objects.end())
  69. {
  70. throw std::runtime_error("Shader object is not attached to the shader program.");
  71. }
  72. else
  73. {
  74. // Check that both the OpenGL shader program and OpenGL shader object are valid
  75. if (glIsProgram(gl_program_id) != GL_TRUE)
  76. {
  77. throw std::runtime_error("Invalid OpenGL shader program");
  78. }
  79. if (glIsShader(object.gl_shader_id) != GL_TRUE)
  80. {
  81. throw std::runtime_error("Invalid OpenGL shader object");
  82. }
  83. // Detach the OpenGL shader object from the OpenGL shader program
  84. glDetachShader(gl_program_id, object.gl_shader_id);
  85. // Remove shader object from set of attached objects
  86. attached_objects.erase(&object);
  87. }
  88. }
  89. void shader_program::detach_all()
  90. {
  91. while (!attached_objects.empty())
  92. {
  93. detach(**attached_objects.begin());
  94. }
  95. }
  96. bool shader_program::link()
  97. {
  98. m_linked = false;
  99. info_log.clear();
  100. variable_map.clear();
  101. // Check that the OpenGL shader program is valid
  102. if (glIsProgram(gl_program_id) != GL_TRUE)
  103. {
  104. throw std::runtime_error("Invalid OpenGL shader program");
  105. }
  106. // Link OpenGL shader program
  107. glLinkProgram(gl_program_id);
  108. // Get OpenGL shader program linking status
  109. GLint gl_link_status;
  110. glGetProgramiv(gl_program_id, GL_LINK_STATUS, &gl_link_status);
  111. m_linked = (gl_link_status == GL_TRUE);
  112. // Populate info log string
  113. if (m_linked)
  114. {
  115. // Load shader variables
  116. load_variables();
  117. }
  118. else
  119. {
  120. // Get OpenGL shader program info log length
  121. GLint gl_info_log_length;
  122. glGetProgramiv(gl_program_id, GL_INFO_LOG_LENGTH, &gl_info_log_length);
  123. if (gl_info_log_length > 0)
  124. {
  125. // Resize string to accommodate OpenGL shader program info log
  126. info_log.resize(gl_info_log_length);
  127. // Read OpenGL shader program info log into string
  128. glGetProgramInfoLog(gl_program_id, gl_info_log_length, &gl_info_log_length, info_log.data());
  129. // Remove redundant null terminator from string
  130. info_log.pop_back();
  131. }
  132. }
  133. return m_linked;
  134. }
  135. void shader_program::load_variables()
  136. {
  137. variable_map.clear();
  138. // Get maximum uniform name length
  139. GLint max_uniform_name_length = 0;
  140. glGetProgramiv(gl_program_id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_uniform_name_length);
  141. // Allocate uniform name buffer
  142. std::string uniform_name(static_cast<std::size_t>(max_uniform_name_length), '\0');
  143. // Get number of active uniforms in the shader
  144. GLint active_uniform_count = 0;
  145. glGetProgramiv(gl_program_id, GL_ACTIVE_UNIFORMS, &active_uniform_count);
  146. // Init texture unit index
  147. GLint texture_index = 0;
  148. // For each active uniform
  149. for (GLint uniform_index = 0; uniform_index < active_uniform_count; ++uniform_index)
  150. {
  151. // Get uniform info
  152. GLsizei uniform_name_length;
  153. GLint uniform_size;
  154. GLenum uniform_type;
  155. glGetActiveUniform(gl_program_id, static_cast<GLuint>(uniform_index), static_cast<GLsizei>(max_uniform_name_length), &uniform_name_length, &uniform_size, &uniform_type, uniform_name.data());
  156. // Get uniform location
  157. const GLint uniform_location = glGetUniformLocation(gl_program_id, uniform_name.c_str());
  158. if (uniform_location == -1)
  159. {
  160. throw std::runtime_error("Unable to get shader uniform location");
  161. }
  162. // Get length of variable name by stripping array notation from uniform name
  163. std::size_t uniform_base_name_length = static_cast<std::size_t>(uniform_name_length);
  164. if (std::size_t i = std::string_view(uniform_name.c_str(), uniform_name_length).find_first_of("["); i != std::string::npos)
  165. {
  166. uniform_base_name_length = i;
  167. }
  168. // Hash variable name to get variable key
  169. const hash::fnv1a32_t variable_key = hash::fnv1a32<char>({uniform_name.c_str(), uniform_base_name_length});
  170. // Get size of variable
  171. const std::size_t variable_size = static_cast<std::size_t>(uniform_size);
  172. // Construct shader variable
  173. std::unique_ptr<const gl::shader_variable> variable;
  174. switch (uniform_type)
  175. {
  176. case GL_BOOL:
  177. variable = std::make_unique<const gl_shader_bool>(variable_size, uniform_location);
  178. break;
  179. case GL_BOOL_VEC2:
  180. variable = std::make_unique<const gl_shader_bvec2>(variable_size, uniform_location);
  181. break;
  182. case GL_BOOL_VEC3:
  183. variable = std::make_unique<const gl_shader_bvec3>(variable_size, uniform_location);
  184. break;
  185. case GL_BOOL_VEC4:
  186. variable = std::make_unique<const gl_shader_bvec4>(variable_size, uniform_location);
  187. break;
  188. case GL_INT:
  189. variable = std::make_unique<const gl_shader_int>(variable_size, uniform_location);
  190. break;
  191. case GL_INT_VEC2:
  192. variable = std::make_unique<const gl_shader_ivec2>(variable_size, uniform_location);
  193. break;
  194. case GL_INT_VEC3:
  195. variable = std::make_unique<const gl_shader_ivec3>(variable_size, uniform_location);
  196. break;
  197. case GL_INT_VEC4:
  198. variable = std::make_unique<const gl_shader_ivec4>(variable_size, uniform_location);
  199. break;
  200. case GL_UNSIGNED_INT:
  201. variable = std::make_unique<const gl_shader_uint>(variable_size, uniform_location);
  202. break;
  203. case GL_UNSIGNED_INT_VEC2:
  204. variable = std::make_unique<const gl_shader_uvec2>(variable_size, uniform_location);
  205. break;
  206. case GL_UNSIGNED_INT_VEC3:
  207. variable = std::make_unique<const gl_shader_uvec3>(variable_size, uniform_location);
  208. break;
  209. case GL_UNSIGNED_INT_VEC4:
  210. variable = std::make_unique<const gl_shader_uvec4>(variable_size, uniform_location);
  211. break;
  212. case GL_FLOAT:
  213. variable = std::make_unique<const gl_shader_float>(variable_size, uniform_location);
  214. break;
  215. case GL_FLOAT_VEC2:
  216. variable = std::make_unique<const gl_shader_fvec2>(variable_size, uniform_location);
  217. break;
  218. case GL_FLOAT_VEC3:
  219. variable = std::make_unique<const gl_shader_fvec3>(variable_size, uniform_location);
  220. break;
  221. case GL_FLOAT_VEC4:
  222. variable = std::make_unique<const gl_shader_fvec4>(variable_size, uniform_location);
  223. break;
  224. case GL_FLOAT_MAT2:
  225. variable = std::make_unique<const gl_shader_fmat2>(variable_size, uniform_location);
  226. break;
  227. case GL_FLOAT_MAT3:
  228. variable = std::make_unique<const gl_shader_fmat3>(variable_size, uniform_location);
  229. break;
  230. case GL_FLOAT_MAT4:
  231. variable = std::make_unique<const gl_shader_fmat4>(variable_size, uniform_location);
  232. break;
  233. case GL_SAMPLER_1D:
  234. case GL_SAMPLER_1D_SHADOW:
  235. variable = std::make_unique<const gl_shader_texture_1d>(variable_size, uniform_location, texture_index);
  236. texture_index += uniform_size;
  237. break;
  238. case GL_SAMPLER_2D:
  239. case GL_SAMPLER_2D_SHADOW:
  240. variable = std::make_unique<const gl_shader_texture_2d>(variable_size, uniform_location, texture_index);
  241. texture_index += uniform_size;
  242. break;
  243. case GL_SAMPLER_3D:
  244. variable = std::make_unique<const gl_shader_texture_3d>(variable_size, uniform_location, texture_index);
  245. texture_index += uniform_size;
  246. break;
  247. case GL_SAMPLER_CUBE:
  248. variable = std::make_unique<const gl_shader_texture_cube>(variable_size, uniform_location, texture_index);
  249. texture_index += uniform_size;
  250. break;
  251. default:
  252. throw std::runtime_error("Unsupported to shader uniform type");
  253. break;
  254. }
  255. // Map variable to variable key
  256. variable_map.emplace(variable_key, std::move(variable));
  257. }
  258. }
  259. } // namespace gl