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

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