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

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