copy_effects.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**************************************************************************/
  2. /* copy_effects.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifdef GLES3_ENABLED
  31. #include "copy_effects.h"
  32. #include "../storage/texture_storage.h"
  33. using namespace GLES3;
  34. CopyEffects *CopyEffects::singleton = nullptr;
  35. CopyEffects *CopyEffects::get_singleton() {
  36. return singleton;
  37. }
  38. CopyEffects::CopyEffects() {
  39. singleton = this;
  40. copy.shader.initialize();
  41. copy.shader_version = copy.shader.version_create();
  42. copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_DEFAULT);
  43. { // Screen Triangle.
  44. glGenBuffers(1, &screen_triangle);
  45. glBindBuffer(GL_ARRAY_BUFFER, screen_triangle);
  46. const float qv[6] = {
  47. -1.0f,
  48. -1.0f,
  49. 3.0f,
  50. -1.0f,
  51. -1.0f,
  52. 3.0f,
  53. };
  54. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, qv, GL_STATIC_DRAW);
  55. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  56. glGenVertexArrays(1, &screen_triangle_array);
  57. glBindVertexArray(screen_triangle_array);
  58. glBindBuffer(GL_ARRAY_BUFFER, screen_triangle);
  59. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
  60. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  61. glBindVertexArray(0);
  62. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  63. }
  64. { // Screen Quad
  65. glGenBuffers(1, &quad);
  66. glBindBuffer(GL_ARRAY_BUFFER, quad);
  67. const float qv[12] = {
  68. -1.0f,
  69. -1.0f,
  70. 1.0f,
  71. -1.0f,
  72. 1.0f,
  73. 1.0f,
  74. -1.0f,
  75. -1.0f,
  76. 1.0f,
  77. 1.0f,
  78. -1.0f,
  79. 1.0f,
  80. };
  81. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 12, qv, GL_STATIC_DRAW);
  82. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  83. glGenVertexArrays(1, &quad_array);
  84. glBindVertexArray(quad_array);
  85. glBindBuffer(GL_ARRAY_BUFFER, quad);
  86. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
  87. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  88. glBindVertexArray(0);
  89. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  90. }
  91. }
  92. CopyEffects::~CopyEffects() {
  93. singleton = nullptr;
  94. glDeleteBuffers(1, &screen_triangle);
  95. glDeleteVertexArrays(1, &screen_triangle_array);
  96. glDeleteBuffers(1, &quad);
  97. glDeleteVertexArrays(1, &quad_array);
  98. copy.shader.version_free(copy.shader_version);
  99. }
  100. void CopyEffects::copy_to_rect(const Rect2 &p_rect) {
  101. bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION);
  102. if (!success) {
  103. return;
  104. }
  105. copy.shader.version_set_uniform(CopyShaderGLES3::COPY_SECTION, p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y, copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION);
  106. draw_screen_quad();
  107. }
  108. void CopyEffects::copy_to_rect_3d(const Rect2 &p_rect, float p_layer, int p_type, float p_lod) {
  109. ERR_FAIL_COND(p_type != Texture::TYPE_LAYERED && p_type != Texture::TYPE_3D);
  110. CopyShaderGLES3::ShaderVariant variant = p_type == Texture::TYPE_LAYERED
  111. ? CopyShaderGLES3::MODE_COPY_SECTION_2D_ARRAY
  112. : CopyShaderGLES3::MODE_COPY_SECTION_3D;
  113. bool success = copy.shader.version_bind_shader(copy.shader_version, variant);
  114. if (!success) {
  115. return;
  116. }
  117. copy.shader.version_set_uniform(CopyShaderGLES3::COPY_SECTION, p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y, copy.shader_version, variant);
  118. copy.shader.version_set_uniform(CopyShaderGLES3::LAYER, p_layer, copy.shader_version, variant);
  119. copy.shader.version_set_uniform(CopyShaderGLES3::LOD, p_lod, copy.shader_version, variant);
  120. draw_screen_quad();
  121. }
  122. void CopyEffects::copy_to_and_from_rect(const Rect2 &p_rect) {
  123. bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION_SOURCE);
  124. if (!success) {
  125. return;
  126. }
  127. copy.shader.version_set_uniform(CopyShaderGLES3::COPY_SECTION, p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y, copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION_SOURCE);
  128. copy.shader.version_set_uniform(CopyShaderGLES3::SOURCE_SECTION, p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y, copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION_SOURCE);
  129. draw_screen_quad();
  130. }
  131. void CopyEffects::copy_screen(float p_multiply) {
  132. bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_SCREEN);
  133. if (!success) {
  134. return;
  135. }
  136. copy.shader.version_set_uniform(CopyShaderGLES3::MULTIPLY, p_multiply, copy.shader_version, CopyShaderGLES3::MODE_SCREEN);
  137. draw_screen_triangle();
  138. }
  139. void CopyEffects::copy_cube_to_rect(const Rect2 &p_rect) {
  140. bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_CUBE_TO_OCTAHEDRAL);
  141. if (!success) {
  142. return;
  143. }
  144. copy.shader.version_set_uniform(CopyShaderGLES3::COPY_SECTION, p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y, copy.shader_version, CopyShaderGLES3::MODE_CUBE_TO_OCTAHEDRAL);
  145. draw_screen_quad();
  146. }
  147. void CopyEffects::copy_cube_to_panorama(float p_mip_level) {
  148. bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_CUBE_TO_PANORAMA);
  149. if (!success) {
  150. return;
  151. }
  152. copy.shader.version_set_uniform(CopyShaderGLES3::MIP_LEVEL, p_mip_level, copy.shader_version, CopyShaderGLES3::MODE_CUBE_TO_PANORAMA);
  153. draw_screen_quad();
  154. }
  155. // Intended for efficiently mipmapping textures.
  156. void CopyEffects::bilinear_blur(GLuint p_source_texture, int p_mipmap_count, const Rect2i &p_region) {
  157. GLuint framebuffers[2];
  158. glGenFramebuffers(2, framebuffers);
  159. glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffers[0]);
  160. glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, 0);
  161. Rect2i source_region = p_region;
  162. Rect2i dest_region = p_region;
  163. for (int i = 1; i < p_mipmap_count; i++) {
  164. dest_region.position.x >>= 1;
  165. dest_region.position.y >>= 1;
  166. dest_region.size = Size2i(dest_region.size.x >> 1, dest_region.size.y >> 1).maxi(1);
  167. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffers[i % 2]);
  168. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, i);
  169. glBlitFramebuffer(source_region.position.x, source_region.position.y, source_region.position.x + source_region.size.x, source_region.position.y + source_region.size.y,
  170. dest_region.position.x, dest_region.position.y, dest_region.position.x + dest_region.size.x, dest_region.position.y + dest_region.size.y, GL_COLOR_BUFFER_BIT, GL_LINEAR);
  171. glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffers[i % 2]);
  172. source_region = dest_region;
  173. }
  174. glBindFramebuffer(GL_READ_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  175. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  176. glDeleteFramebuffers(2, framebuffers);
  177. }
  178. // Intended for approximating a gaussian blur. Used for 2D backbuffer mipmaps. Slightly less efficient than bilinear_blur().
  179. void CopyEffects::gaussian_blur(GLuint p_source_texture, int p_mipmap_count, const Rect2i &p_region, const Size2i &p_size) {
  180. GLuint framebuffer;
  181. glGenFramebuffers(1, &framebuffer);
  182. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  183. glActiveTexture(GL_TEXTURE0);
  184. glBindTexture(GL_TEXTURE_2D, p_source_texture);
  185. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  186. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  187. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  188. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  189. Size2i base_size = p_size;
  190. Rect2i source_region = p_region;
  191. Rect2i dest_region = p_region;
  192. Size2 float_size = Size2(p_size);
  193. Rect2 normalized_source_region = Rect2(p_region);
  194. normalized_source_region.position = normalized_source_region.position / float_size;
  195. normalized_source_region.size = normalized_source_region.size / float_size;
  196. Rect2 normalized_dest_region = Rect2(p_region);
  197. for (int i = 1; i < p_mipmap_count; i++) {
  198. dest_region.position.x >>= 1;
  199. dest_region.position.y >>= 1;
  200. dest_region.size = Size2i(dest_region.size.x >> 1, dest_region.size.y >> 1).maxi(1);
  201. base_size.x >>= 1;
  202. base_size.y >>= 1;
  203. glBindTexture(GL_TEXTURE_2D, p_source_texture);
  204. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, i - 1);
  205. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i - 1);
  206. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, i);
  207. #ifdef DEV_ENABLED
  208. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  209. if (status != GL_FRAMEBUFFER_COMPLETE) {
  210. WARN_PRINT("Could not bind Gaussian blur framebuffer, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
  211. }
  212. #endif
  213. glViewport(0, 0, base_size.x, base_size.y);
  214. bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_GAUSSIAN_BLUR);
  215. if (!success) {
  216. return;
  217. }
  218. float_size = Size2(base_size);
  219. normalized_dest_region.position = Size2(dest_region.position) / float_size;
  220. normalized_dest_region.size = Size2(dest_region.size) / float_size;
  221. copy.shader.version_set_uniform(CopyShaderGLES3::COPY_SECTION, normalized_dest_region.position.x, normalized_dest_region.position.y, normalized_dest_region.size.x, normalized_dest_region.size.y, copy.shader_version, CopyShaderGLES3::MODE_GAUSSIAN_BLUR);
  222. copy.shader.version_set_uniform(CopyShaderGLES3::SOURCE_SECTION, normalized_source_region.position.x, normalized_source_region.position.y, normalized_source_region.size.x, normalized_source_region.size.y, copy.shader_version, CopyShaderGLES3::MODE_GAUSSIAN_BLUR);
  223. copy.shader.version_set_uniform(CopyShaderGLES3::PIXEL_SIZE, 1.0 / float_size.x, 1.0 / float_size.y, copy.shader_version, CopyShaderGLES3::MODE_GAUSSIAN_BLUR);
  224. draw_screen_quad();
  225. source_region = dest_region;
  226. normalized_source_region = normalized_dest_region;
  227. }
  228. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  229. glDeleteFramebuffers(1, &framebuffer);
  230. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  231. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  233. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, p_mipmap_count - 1);
  234. glBindTexture(GL_TEXTURE_2D, 0);
  235. glViewport(0, 0, p_size.x, p_size.y);
  236. }
  237. void CopyEffects::set_color(const Color &p_color, const Rect2i &p_region) {
  238. bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_SIMPLE_COLOR);
  239. if (!success) {
  240. return;
  241. }
  242. copy.shader.version_set_uniform(CopyShaderGLES3::COPY_SECTION, p_region.position.x, p_region.position.y, p_region.size.x, p_region.size.y, copy.shader_version, CopyShaderGLES3::MODE_SIMPLE_COLOR);
  243. copy.shader.version_set_uniform(CopyShaderGLES3::COLOR_IN, p_color, copy.shader_version, CopyShaderGLES3::MODE_SIMPLE_COLOR);
  244. draw_screen_quad();
  245. }
  246. void CopyEffects::draw_screen_triangle() {
  247. glBindVertexArray(screen_triangle_array);
  248. glDrawArrays(GL_TRIANGLES, 0, 3);
  249. glBindVertexArray(0);
  250. }
  251. void CopyEffects::draw_screen_quad() {
  252. glBindVertexArray(quad_array);
  253. glDrawArrays(GL_TRIANGLES, 0, 6);
  254. glBindVertexArray(0);
  255. }
  256. #endif // GLES3_ENABLED