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

202 lines
7.6 KiB

  1. /*
  2. * Copyright (C) 2021 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_SHADER_INPUT_HPP
  20. #define ANTKEEPER_GL_SHADER_INPUT_HPP
  21. #include "utility/fundamental-types.hpp"
  22. #include <string>
  23. namespace gl {
  24. class shader_program;
  25. class texture_1d;
  26. class texture_2d;
  27. class texture_3d;
  28. class texture_cube;
  29. enum class shader_variable_type;
  30. /**
  31. * Port through which data can be uploaded to shader variables.
  32. */
  33. class shader_input
  34. {
  35. public:
  36. /**
  37. * Returns the type of data which can be passed through this input.
  38. */
  39. shader_variable_type get_data_type() const;
  40. /**
  41. * Returns `true` if the input data is stored in an array.
  42. */
  43. bool is_array() const;
  44. /**
  45. * Returns the number of elements the array can contain, or `1` if the data is not stored in an array.
  46. */
  47. std::size_t get_element_count() const;
  48. /**
  49. * Uploads a value to the shader.
  50. *
  51. * @param value Value to upload.
  52. * @return `true` if the value was uploaded successfully, `false` otherwise.
  53. */
  54. ///@{
  55. bool upload(const bool& value) const;
  56. bool upload(const bool2& value) const;
  57. bool upload(const bool3& value) const;
  58. bool upload(const bool4& value) const;
  59. bool upload(const int& value) const;
  60. bool upload(const int2& value) const;
  61. bool upload(const int3& value) const;
  62. bool upload(const int4& value) const;
  63. bool upload(const unsigned int& value) const;
  64. bool upload(const uint2& value) const;
  65. bool upload(const uint3& value) const;
  66. bool upload(const uint4& value) const;
  67. bool upload(const float& value) const;
  68. bool upload(const float2& value) const;
  69. bool upload(const float3& value) const;
  70. bool upload(const float4& value) const;
  71. bool upload(const float2x2& value) const;
  72. bool upload(const float3x3& value) const;
  73. bool upload(const float4x4& value) const;
  74. bool upload(const texture_1d* value) const;
  75. bool upload(const texture_2d* value) const;
  76. bool upload(const texture_3d* value) const;
  77. bool upload(const texture_cube* value) const;
  78. ///@}
  79. /**
  80. * Uploads a single array element to the shader.
  81. *
  82. * @param index Index of an array element.
  83. * @param values Value to upload.
  84. * @return `true` if the value was uploaded successfully, `false` otherwise.
  85. */
  86. ///@{
  87. bool upload(std::size_t index, const bool& value) const;
  88. bool upload(std::size_t index, const bool2& value) const;
  89. bool upload(std::size_t index, const bool3& value) const;
  90. bool upload(std::size_t index, const bool4& value) const;
  91. bool upload(std::size_t index, const int& value) const;
  92. bool upload(std::size_t index, const int2& value) const;
  93. bool upload(std::size_t index, const int3& value) const;
  94. bool upload(std::size_t index, const int4& value) const;
  95. bool upload(std::size_t index, const unsigned int& value) const;
  96. bool upload(std::size_t index, const uint2& value) const;
  97. bool upload(std::size_t index, const uint3& value) const;
  98. bool upload(std::size_t index, const uint4& value) const;
  99. bool upload(std::size_t index, const float& value) const;
  100. bool upload(std::size_t index, const float2& value) const;
  101. bool upload(std::size_t index, const float3& value) const;
  102. bool upload(std::size_t index, const float4& value) const;
  103. bool upload(std::size_t index, const float2x2& value) const;
  104. bool upload(std::size_t index, const float3x3& value) const;
  105. bool upload(std::size_t index, const float4x4& value) const;
  106. bool upload(std::size_t index, const texture_1d* value) const;
  107. bool upload(std::size_t index, const texture_2d* value) const;
  108. bool upload(std::size_t index, const texture_3d* value) const;
  109. bool upload(std::size_t index, const texture_cube* value) const;
  110. ///@}
  111. /**
  112. * Uploads a range of array elements to the shader.
  113. *
  114. * @param index Index of the first array element.
  115. * @param values Pointer to an array of values.
  116. * @param count Number of elements to upload.
  117. * @return `true` if the value was fed successfully, `false` otherwise.
  118. */
  119. ///@{
  120. bool upload(std::size_t index, const bool* values, std::size_t count) const;
  121. bool upload(std::size_t index, const bool2* values, std::size_t count) const;
  122. bool upload(std::size_t index, const bool3* values, std::size_t count) const;
  123. bool upload(std::size_t index, const bool4* values, std::size_t count) const;
  124. bool upload(std::size_t index, const int* values, std::size_t count) const;
  125. bool upload(std::size_t index, const int2* values, std::size_t count) const;
  126. bool upload(std::size_t index, const int3* values, std::size_t count) const;
  127. bool upload(std::size_t index, const int4* values, std::size_t count) const;
  128. bool upload(std::size_t index, const unsigned int* values, std::size_t count) const;
  129. bool upload(std::size_t index, const uint2* values, std::size_t count) const;
  130. bool upload(std::size_t index, const uint3* values, std::size_t count) const;
  131. bool upload(std::size_t index, const uint4* values, std::size_t count) const;
  132. bool upload(std::size_t index, const float* values, std::size_t count) const;
  133. bool upload(std::size_t index, const float2* values, std::size_t count) const;
  134. bool upload(std::size_t index, const float3* values, std::size_t count) const;
  135. bool upload(std::size_t index, const float4* values, std::size_t count) const;
  136. bool upload(std::size_t index, const float2x2* values, std::size_t count) const;
  137. bool upload(std::size_t index, const float3x3* values, std::size_t count) const;
  138. bool upload(std::size_t index, const float4x4* values, std::size_t count) const;
  139. bool upload(std::size_t index, const texture_1d** values, std::size_t count) const;
  140. bool upload(std::size_t index, const texture_2d** values, std::size_t count) const;
  141. bool upload(std::size_t index, const texture_3d** values, std::size_t count) const;
  142. bool upload(std::size_t index, const texture_cube** values, std::size_t count) const;
  143. ///@}
  144. private:
  145. friend class shader_program;
  146. /**
  147. * Creates a shader input.
  148. *
  149. * @param program Shader program with which this input is associated.
  150. * @param gl_uniform_location Location of the shader uniform with which this shader input is associated.
  151. * @param name Name of the input.
  152. * @param data_type Type of data which can be passed through this input.
  153. * @param element_count Number of elements which the array can contain, or `0` if input data is not stored in an array.
  154. * @param texture_unit Texture unit to which texture shader variables can be bound, or `-1` if the data type is not a texture type.
  155. */
  156. shader_input(shader_program* program, std::size_t input_index, int gl_uniform_location, const std::string& name, shader_variable_type data_type, std::size_t element_count, int texture_unit);
  157. /**
  158. * Destroys a shader input.
  159. */
  160. ~shader_input();
  161. shader_program* program;
  162. std::size_t input_index;
  163. int gl_uniform_location;
  164. std::string name;
  165. shader_variable_type data_type;
  166. std::size_t element_count;
  167. int texture_unit;
  168. };
  169. inline shader_variable_type shader_input::get_data_type() const
  170. {
  171. return data_type;
  172. }
  173. inline bool shader_input::is_array() const
  174. {
  175. return (element_count > 1);
  176. }
  177. inline std::size_t shader_input::get_element_count() const
  178. {
  179. return element_count;
  180. }
  181. } // namespace gl
  182. #endif // ANTKEEPER_GL_SHADER_INPUT_HPP