post_effects.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**************************************************************************/
  2. /* post_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 "post_effects.h"
  32. #include "../storage/texture_storage.h"
  33. using namespace GLES3;
  34. PostEffects *PostEffects::singleton = nullptr;
  35. PostEffects *PostEffects::get_singleton() {
  36. return singleton;
  37. }
  38. PostEffects::PostEffects() {
  39. singleton = this;
  40. post.shader.initialize();
  41. post.shader_version = post.shader.version_create();
  42. post.shader.version_bind_shader(post.shader_version, PostShaderGLES3::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. }
  65. PostEffects::~PostEffects() {
  66. singleton = nullptr;
  67. glDeleteBuffers(1, &screen_triangle);
  68. glDeleteVertexArrays(1, &screen_triangle_array);
  69. post.shader.version_free(post.shader_version);
  70. }
  71. void PostEffects::_draw_screen_triangle() {
  72. glBindVertexArray(screen_triangle_array);
  73. glDrawArrays(GL_TRIANGLES, 0, 3);
  74. glBindVertexArray(0);
  75. }
  76. void PostEffects::post_copy(GLuint p_dest_framebuffer, Size2i p_dest_size, GLuint p_source_color, Size2i p_source_size, float p_luminance_multiplier, const Glow::GLOWLEVEL *p_glow_buffers, float p_glow_intensity, uint32_t p_view, bool p_use_multiview, uint64_t p_spec_constants) {
  77. glDisable(GL_DEPTH_TEST);
  78. glDepthMask(GL_FALSE);
  79. glDisable(GL_BLEND);
  80. glBindFramebuffer(GL_FRAMEBUFFER, p_dest_framebuffer);
  81. glViewport(0, 0, p_dest_size.x, p_dest_size.y);
  82. PostShaderGLES3::ShaderVariant mode = PostShaderGLES3::MODE_DEFAULT;
  83. uint64_t flags = p_spec_constants;
  84. if (p_use_multiview) {
  85. flags |= PostShaderGLES3::USE_MULTIVIEW;
  86. }
  87. if (p_glow_buffers != nullptr) {
  88. flags |= PostShaderGLES3::USE_GLOW;
  89. }
  90. if (p_luminance_multiplier != 1.0) {
  91. flags |= PostShaderGLES3::USE_LUMINANCE_MULTIPLIER;
  92. }
  93. bool success = post.shader.version_bind_shader(post.shader_version, mode, flags);
  94. if (!success) {
  95. return;
  96. }
  97. GLenum texture_target = p_use_multiview ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
  98. glActiveTexture(GL_TEXTURE0);
  99. glBindTexture(texture_target, p_source_color);
  100. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  101. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  102. if (p_glow_buffers != nullptr) {
  103. glActiveTexture(GL_TEXTURE1);
  104. glBindTexture(GL_TEXTURE_2D, p_glow_buffers[0].color);
  105. post.shader.version_set_uniform(PostShaderGLES3::PIXEL_SIZE, 1.0 / p_source_size.x, 1.0 / p_source_size.y, post.shader_version, mode, flags);
  106. post.shader.version_set_uniform(PostShaderGLES3::GLOW_INTENSITY, p_glow_intensity, post.shader_version, mode, flags);
  107. }
  108. post.shader.version_set_uniform(PostShaderGLES3::VIEW, float(p_view), post.shader_version, mode, flags);
  109. post.shader.version_set_uniform(PostShaderGLES3::LUMINANCE_MULTIPLIER, p_luminance_multiplier, post.shader_version, mode, flags);
  110. _draw_screen_triangle();
  111. // Reset state
  112. if (p_glow_buffers != nullptr) {
  113. glActiveTexture(GL_TEXTURE1);
  114. glBindTexture(GL_TEXTURE_2D, 0);
  115. }
  116. // Return back to nearest
  117. glActiveTexture(GL_TEXTURE0);
  118. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  119. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  120. glBindTexture(texture_target, 0);
  121. glDisable(GL_BLEND);
  122. glEnable(GL_DEPTH_TEST);
  123. glDepthMask(GL_TRUE);
  124. glUseProgram(0);
  125. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  126. }
  127. #endif // GLES3_ENABLED