💿🐜 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.

526 lines
15 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. #ifndef ANTKEEPER_GL_GL_SHADER_VARIABLES_HPP
  20. #define ANTKEEPER_GL_GL_SHADER_VARIABLES_HPP
  21. #include <engine/gl/shader-variable.hpp>
  22. #include <glad/gl.h>
  23. #include <vector>
  24. namespace gl {
  25. /**
  26. * Boolean shader variable implementation using OpenGL.
  27. */
  28. class gl_shader_bool: public shader_variable
  29. {
  30. public:
  31. gl_shader_bool(std::size_t size, GLint gl_uniform_location);
  32. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  33. {
  34. return shader_variable_type::bvec1;
  35. }
  36. void update(bool value) const noexcept override;
  37. void update(bool value, std::size_t index) const override;
  38. void update(std::span<const bool> values, std::size_t index = 0) const override;
  39. private:
  40. GLint gl_uniform_location;
  41. mutable std::vector<GLint> ivalues;
  42. };
  43. /**
  44. * 2-dimensional boolean vector shader variable implementation using OpenGL.
  45. */
  46. class gl_shader_bvec2: public shader_variable
  47. {
  48. public:
  49. gl_shader_bvec2(std::size_t size, GLint gl_uniform_location);
  50. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  51. {
  52. return shader_variable_type::bvec2;
  53. }
  54. void update(const math::bvec2& value) const noexcept override;
  55. void update(const math::bvec2& value, std::size_t index) const override;
  56. void update(std::span<const math::bvec2> values, std::size_t index = 0) const override;
  57. private:
  58. GLint gl_uniform_location;
  59. mutable std::vector<GLint> ivalues;
  60. };
  61. /**
  62. * 3-dimensional boolean vector shader variable implementation using OpenGL.
  63. */
  64. class gl_shader_bvec3: public shader_variable
  65. {
  66. public:
  67. gl_shader_bvec3(std::size_t size, GLint gl_uniform_location);
  68. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  69. {
  70. return shader_variable_type::bvec3;
  71. }
  72. void update(const math::bvec3& value) const noexcept override;
  73. void update(const math::bvec3& value, std::size_t index) const override;
  74. void update(std::span<const math::bvec3> values, std::size_t index = 0) const override;
  75. private:
  76. GLint gl_uniform_location;
  77. mutable std::vector<GLint> ivalues;
  78. };
  79. /**
  80. * 4-dimensional boolean vector shader variable implementation using OpenGL.
  81. */
  82. class gl_shader_bvec4: public shader_variable
  83. {
  84. public:
  85. gl_shader_bvec4(std::size_t size, GLint gl_uniform_location);
  86. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  87. {
  88. return shader_variable_type::bvec4;
  89. }
  90. void update(const math::bvec4& value) const noexcept override;
  91. void update(const math::bvec4& value, std::size_t index) const override;
  92. void update(std::span<const math::bvec4> values, std::size_t index = 0) const override;
  93. private:
  94. GLint gl_uniform_location;
  95. mutable std::vector<GLint> ivalues;
  96. };
  97. /**
  98. * Signed integer shader variable implementation using OpenGL.
  99. */
  100. class gl_shader_int: public shader_variable
  101. {
  102. public:
  103. gl_shader_int(std::size_t size, GLint gl_uniform_location);
  104. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  105. {
  106. return shader_variable_type::ivec1;
  107. }
  108. void update(int value) const noexcept override;
  109. void update(int value, std::size_t index) const override;
  110. void update(std::span<const int> values, std::size_t index = 0) const override;
  111. private:
  112. GLint gl_uniform_location;
  113. };
  114. /**
  115. * 2-dimensional signed integer vector shader variable implementation using OpenGL.
  116. */
  117. class gl_shader_ivec2: public shader_variable
  118. {
  119. public:
  120. gl_shader_ivec2(std::size_t size, GLint gl_uniform_location);
  121. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  122. {
  123. return shader_variable_type::ivec2;
  124. }
  125. void update(const math::ivec2& value) const noexcept override;
  126. void update(const math::ivec2& value, std::size_t index) const override;
  127. void update(std::span<const math::ivec2> values, std::size_t index = 0) const override;
  128. private:
  129. GLint gl_uniform_location;
  130. };
  131. /**
  132. * 3-dimensional signed integer vector shader variable implementation using OpenGL.
  133. */
  134. class gl_shader_ivec3: public shader_variable
  135. {
  136. public:
  137. gl_shader_ivec3(std::size_t size, GLint gl_uniform_location);
  138. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  139. {
  140. return shader_variable_type::ivec3;
  141. }
  142. void update(const math::ivec3& value) const noexcept override;
  143. void update(const math::ivec3& value, std::size_t index) const override;
  144. void update(std::span<const math::ivec3> values, std::size_t index = 0) const override;
  145. private:
  146. GLint gl_uniform_location;
  147. };
  148. /**
  149. * 4-dimensional signed integer vector shader variable implementation using OpenGL.
  150. */
  151. class gl_shader_ivec4: public shader_variable
  152. {
  153. public:
  154. gl_shader_ivec4(std::size_t size, GLint gl_uniform_location);
  155. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  156. {
  157. return shader_variable_type::ivec4;
  158. }
  159. void update(const math::ivec4& value) const noexcept override;
  160. void update(const math::ivec4& value, std::size_t index) const override;
  161. void update(std::span<const math::ivec4> values, std::size_t index = 0) const override;
  162. private:
  163. GLint gl_uniform_location;
  164. };
  165. /**
  166. * Unsigned integer shader variable implementation using OpenGL.
  167. */
  168. class gl_shader_uint: public shader_variable
  169. {
  170. public:
  171. gl_shader_uint(std::size_t size, GLint gl_uniform_location);
  172. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  173. {
  174. return shader_variable_type::uvec1;
  175. }
  176. void update(unsigned int value) const noexcept override;
  177. void update(unsigned int value, std::size_t index) const override;
  178. void update(std::span<const unsigned int> values, std::size_t index = 0) const override;
  179. private:
  180. GLint gl_uniform_location;
  181. };
  182. /**
  183. * 2-dimensional unsigned integer vector shader variable implementation using OpenGL.
  184. */
  185. class gl_shader_uvec2: public shader_variable
  186. {
  187. public:
  188. gl_shader_uvec2(std::size_t size, GLint gl_uniform_location);
  189. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  190. {
  191. return shader_variable_type::uvec2;
  192. }
  193. void update(const math::uvec2& value) const noexcept override;
  194. void update(const math::uvec2& value, std::size_t index) const override;
  195. void update(std::span<const math::uvec2> values, std::size_t index = 0) const override;
  196. private:
  197. GLint gl_uniform_location;
  198. };
  199. /**
  200. * 3-dimensional unsigned integer vector shader variable implementation using OpenGL.
  201. */
  202. class gl_shader_uvec3: public shader_variable
  203. {
  204. public:
  205. gl_shader_uvec3(std::size_t size, GLint gl_uniform_location);
  206. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  207. {
  208. return shader_variable_type::uvec3;
  209. }
  210. void update(const math::uvec3& value) const noexcept override;
  211. void update(const math::uvec3& value, std::size_t index) const override;
  212. void update(std::span<const math::uvec3> values, std::size_t index = 0) const override;
  213. private:
  214. GLint gl_uniform_location;
  215. };
  216. /**
  217. * 4-dimensional unsigned integer vector shader variable implementation using OpenGL.
  218. */
  219. class gl_shader_uvec4: public shader_variable
  220. {
  221. public:
  222. gl_shader_uvec4(std::size_t size, GLint gl_uniform_location);
  223. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  224. {
  225. return shader_variable_type::uvec4;
  226. }
  227. void update(const math::uvec4& value) const noexcept override;
  228. void update(const math::uvec4& value, std::size_t index) const override;
  229. void update(std::span<const math::uvec4> values, std::size_t index = 0) const override;
  230. private:
  231. GLint gl_uniform_location;
  232. };
  233. /**
  234. * Floating-point shader variable implementation using OpenGL.
  235. */
  236. class gl_shader_float: public shader_variable
  237. {
  238. public:
  239. gl_shader_float(std::size_t size, GLint gl_uniform_location);
  240. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  241. {
  242. return shader_variable_type::fvec1;
  243. }
  244. void update(float value) const noexcept override;
  245. void update(float value, std::size_t index) const override;
  246. void update(std::span<const float> values, std::size_t index = 0) const override;
  247. private:
  248. GLint gl_uniform_location;
  249. };
  250. /**
  251. * 2-dimensional floating-point vector shader variable implementation using OpenGL.
  252. */
  253. class gl_shader_fvec2: public shader_variable
  254. {
  255. public:
  256. gl_shader_fvec2(std::size_t size, GLint gl_uniform_location);
  257. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  258. {
  259. return shader_variable_type::fvec2;
  260. }
  261. void update(const math::fvec2& value) const noexcept override;
  262. void update(const math::fvec2& value, std::size_t index) const override;
  263. void update(std::span<const math::fvec2> values, std::size_t index = 0) const override;
  264. private:
  265. GLint gl_uniform_location;
  266. };
  267. /**
  268. * 3-dimensional floating-point vector shader variable implementation using OpenGL.
  269. */
  270. class gl_shader_fvec3: public shader_variable
  271. {
  272. public:
  273. gl_shader_fvec3(std::size_t size, GLint gl_uniform_location);
  274. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  275. {
  276. return shader_variable_type::fvec3;
  277. }
  278. void update(const math::fvec3& value) const noexcept override;
  279. void update(const math::fvec3& value, std::size_t index) const override;
  280. void update(std::span<const math::fvec3> values, std::size_t index = 0) const override;
  281. private:
  282. GLint gl_uniform_location;
  283. };
  284. /**
  285. * 4-dimensional floating-point vector shader variable implementation using OpenGL.
  286. */
  287. class gl_shader_fvec4: public shader_variable
  288. {
  289. public:
  290. gl_shader_fvec4(std::size_t size, GLint gl_uniform_location);
  291. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  292. {
  293. return shader_variable_type::fvec4;
  294. }
  295. void update(const math::fvec4& value) const noexcept override;
  296. void update(const math::fvec4& value, std::size_t index) const override;
  297. void update(std::span<const math::fvec4> values, std::size_t index = 0) const override;
  298. private:
  299. GLint gl_uniform_location;
  300. };
  301. /**
  302. * 2x2 floating-point matrix shader variable implementation using OpenGL.
  303. */
  304. class gl_shader_fmat2: public shader_variable
  305. {
  306. public:
  307. gl_shader_fmat2(std::size_t size, GLint gl_uniform_location);
  308. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  309. {
  310. return shader_variable_type::fmat2;
  311. }
  312. void update(const math::fmat2& value) const noexcept override;
  313. void update(const math::fmat2& value, std::size_t index) const override;
  314. void update(std::span<const math::fmat2> values, std::size_t index = 0) const override;
  315. private:
  316. GLint gl_uniform_location;
  317. };
  318. /**
  319. * 3x3 floating-point matrix shader variable implementation using OpenGL.
  320. */
  321. class gl_shader_fmat3: public shader_variable
  322. {
  323. public:
  324. gl_shader_fmat3(std::size_t size, GLint gl_uniform_location);
  325. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  326. {
  327. return shader_variable_type::fmat3;
  328. }
  329. void update(const math::fmat3& value) const noexcept override;
  330. void update(const math::fmat3& value, std::size_t index) const override;
  331. void update(std::span<const math::fmat3> values, std::size_t index = 0) const override;
  332. private:
  333. GLint gl_uniform_location;
  334. };
  335. /**
  336. * 4x4 floating-point matrix shader variable implementation using OpenGL.
  337. */
  338. class gl_shader_fmat4: public shader_variable
  339. {
  340. public:
  341. gl_shader_fmat4(std::size_t size, GLint gl_uniform_location);
  342. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  343. {
  344. return shader_variable_type::fmat4;
  345. }
  346. void update(const math::fmat4& value) const noexcept override;
  347. void update(const math::fmat4& value, std::size_t index) const override;
  348. void update(std::span<const math::fmat4> values, std::size_t index = 0) const override;
  349. private:
  350. GLint gl_uniform_location;
  351. };
  352. /**
  353. * 1-dimensional texture shader variable implementation using OpenGL.
  354. */
  355. class gl_shader_texture_1d: public shader_variable
  356. {
  357. public:
  358. gl_shader_texture_1d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_index);
  359. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  360. {
  361. return shader_variable_type::texture_1d;
  362. }
  363. void update(const texture_1d& value) const noexcept override;
  364. void update(const texture_1d& value, std::size_t index) const override;
  365. void update(std::span<const texture_1d* const> values, std::size_t index = 0) const override;
  366. void update(std::span<const std::shared_ptr<texture_1d>> values, std::size_t index = 0) const override;
  367. private:
  368. GLint gl_uniform_location;
  369. std::vector<GLint> gl_texture_unit_indices;
  370. };
  371. /**
  372. * 2-dimensional texture shader variable implementation using OpenGL.
  373. */
  374. class gl_shader_texture_2d: public shader_variable
  375. {
  376. public:
  377. gl_shader_texture_2d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_unit_index);
  378. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  379. {
  380. return shader_variable_type::texture_2d;
  381. }
  382. void update(const texture_2d& value) const noexcept override;
  383. void update(const texture_2d& value, std::size_t index) const override;
  384. void update(std::span<const texture_2d* const> values, std::size_t index = 0) const override;
  385. void update(std::span<const std::shared_ptr<texture_2d>> values, std::size_t index = 0) const override;
  386. private:
  387. GLint gl_uniform_location;
  388. std::vector<GLint> gl_texture_unit_indices;
  389. };
  390. /**
  391. * 3-dimensional texture shader variable implementation using OpenGL.
  392. */
  393. class gl_shader_texture_3d: public shader_variable
  394. {
  395. public:
  396. gl_shader_texture_3d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_index);
  397. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  398. {
  399. return shader_variable_type::texture_3d;
  400. }
  401. void update(const texture_3d& value) const noexcept override;
  402. void update(const texture_3d& value, std::size_t index) const override;
  403. void update(std::span<const texture_3d* const> values, std::size_t index = 0) const override;
  404. void update(std::span<const std::shared_ptr<texture_3d>> values, std::size_t index = 0) const override;
  405. private:
  406. GLint gl_uniform_location;
  407. std::vector<GLint> gl_texture_unit_indices;
  408. };
  409. /**
  410. * Cube texture shader variable implementation using OpenGL.
  411. */
  412. class gl_shader_texture_cube: public shader_variable
  413. {
  414. public:
  415. gl_shader_texture_cube(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_index);
  416. [[nodiscard]] inline constexpr shader_variable_type type() const noexcept override
  417. {
  418. return shader_variable_type::texture_cube;
  419. }
  420. void update(const texture_cube& value) const noexcept override;
  421. void update(const texture_cube& value, std::size_t index) const override;
  422. void update(std::span<const texture_cube* const> values, std::size_t index = 0) const override;
  423. void update(std::span<const std::shared_ptr<texture_cube>> values, std::size_t index = 0) const override;
  424. private:
  425. GLint gl_uniform_location;
  426. std::vector<GLint> gl_texture_unit_indices;
  427. };
  428. } // namespace gl
  429. #endif // ANTKEEPER_GL_GL_SHADER_VARIABLES_HPP