| 1 | /**************************************************************************/ |
| 2 | /* glow.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 | |
| 31 | #ifdef GLES3_ENABLED |
| 32 | |
| 33 | #include "glow.h" |
| 34 | #include "../storage/texture_storage.h" |
| 35 | |
| 36 | using namespace GLES3; |
| 37 | |
| 38 | Glow *Glow::singleton = nullptr; |
| 39 | |
| 40 | Glow *Glow::get_singleton() { |
| 41 | return singleton; |
| 42 | } |
| 43 | |
| 44 | Glow::Glow() { |
| 45 | singleton = this; |
| 46 | |
| 47 | glow.shader.initialize(); |
| 48 | glow.shader_version = glow.shader.version_create(); |
| 49 | |
| 50 | { // Screen Triangle. |
| 51 | glGenBuffers(1, &screen_triangle); |
| 52 | glBindBuffer(GL_ARRAY_BUFFER, screen_triangle); |
| 53 | |
| 54 | const float qv[6] = { |
| 55 | -1.0f, |
| 56 | -1.0f, |
| 57 | 3.0f, |
| 58 | -1.0f, |
| 59 | -1.0f, |
| 60 | 3.0f, |
| 61 | }; |
| 62 | |
| 63 | glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, qv, GL_STATIC_DRAW); |
| 64 | glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind |
| 65 | |
| 66 | glGenVertexArrays(1, &screen_triangle_array); |
| 67 | glBindVertexArray(screen_triangle_array); |
| 68 | glBindBuffer(GL_ARRAY_BUFFER, screen_triangle); |
| 69 | glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr); |
| 70 | glEnableVertexAttribArray(RS::ARRAY_VERTEX); |
| 71 | glBindVertexArray(0); |
| 72 | glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | Glow::~Glow() { |
| 77 | glDeleteBuffers(1, &screen_triangle); |
| 78 | glDeleteVertexArrays(1, &screen_triangle_array); |
| 79 | |
| 80 | glow.shader.version_free(glow.shader_version); |
| 81 | |
| 82 | singleton = nullptr; |
| 83 | } |
| 84 | |
| 85 | void Glow::_draw_screen_triangle() { |
| 86 | glBindVertexArray(screen_triangle_array); |
| 87 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 88 | glBindVertexArray(0); |
| 89 | } |
| 90 | |
| 91 | void Glow::process_glow(GLuint p_source_color, Size2i p_size, const Glow::GLOWLEVEL *p_glow_buffers, uint32_t p_view, bool p_use_multiview) { |
| 92 | ERR_FAIL_COND(p_source_color == 0); |
| 93 | ERR_FAIL_COND(p_glow_buffers[3].color == 0); |
| 94 | |
| 95 | // Reset some OpenGL state... |
| 96 | glDisable(GL_BLEND); |
| 97 | glDisable(GL_DEPTH_TEST); |
| 98 | glDepthMask(GL_FALSE); |
| 99 | |
| 100 | // Start with our filter pass |
| 101 | { |
| 102 | glBindFramebuffer(GL_FRAMEBUFFER, p_glow_buffers[0].fbo); |
| 103 | glViewport(0, 0, p_glow_buffers[0].size.x, p_glow_buffers[0].size.y); |
| 104 | |
| 105 | glActiveTexture(GL_TEXTURE0); |
| 106 | glBindTexture(p_use_multiview ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D, p_source_color); |
| 107 | |
| 108 | uint64_t specialization = p_use_multiview ? GlowShaderGLES3::USE_MULTIVIEW : 0; |
| 109 | bool success = glow.shader.version_bind_shader(glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization); |
| 110 | if (!success) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | glow.shader.version_set_uniform(GlowShaderGLES3::PIXEL_SIZE, 1.0 / p_glow_buffers[0].size.x, 1.0 / p_glow_buffers[0].size.y, glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization); |
| 115 | glow.shader.version_set_uniform(GlowShaderGLES3::VIEW, float(p_view), glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization); |
| 116 | glow.shader.version_set_uniform(GlowShaderGLES3::LUMINANCE_MULTIPLIER, luminance_multiplier, glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization); |
| 117 | glow.shader.version_set_uniform(GlowShaderGLES3::GLOW_BLOOM, glow_bloom, glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization); |
| 118 | glow.shader.version_set_uniform(GlowShaderGLES3::GLOW_HDR_THRESHOLD, glow_hdr_bleed_threshold, glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization); |
| 119 | glow.shader.version_set_uniform(GlowShaderGLES3::GLOW_HDR_SCALE, glow_hdr_bleed_scale, glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization); |
| 120 | glow.shader.version_set_uniform(GlowShaderGLES3::GLOW_LUMINANCE_CAP, glow_hdr_luminance_cap, glow.shader_version, GlowShaderGLES3::MODE_FILTER, specialization); |
| 121 | |
| 122 | _draw_screen_triangle(); |
| 123 | } |
| 124 | |
| 125 | // Continue with downsampling |
| 126 | { |
| 127 | bool success = glow.shader.version_bind_shader(glow.shader_version, GlowShaderGLES3::MODE_DOWNSAMPLE, 0); |
| 128 | if (!success) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | for (int i = 1; i < 4; i++) { |
| 133 | glBindFramebuffer(GL_FRAMEBUFFER, p_glow_buffers[i].fbo); |
| 134 | glViewport(0, 0, p_glow_buffers[i].size.x, p_glow_buffers[i].size.y); |
| 135 | |
| 136 | glActiveTexture(GL_TEXTURE0); |
| 137 | glBindTexture(GL_TEXTURE_2D, p_glow_buffers[i - 1].color); |
| 138 | |
| 139 | glow.shader.version_set_uniform(GlowShaderGLES3::PIXEL_SIZE, 1.0 / p_glow_buffers[i].size.x, 1.0 / p_glow_buffers[i].size.y, glow.shader_version, GlowShaderGLES3::MODE_DOWNSAMPLE); |
| 140 | |
| 141 | _draw_screen_triangle(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Now upsample |
| 146 | { |
| 147 | bool success = glow.shader.version_bind_shader(glow.shader_version, GlowShaderGLES3::MODE_UPSAMPLE, 0); |
| 148 | if (!success) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | for (int i = 2; i >= 0; i--) { |
| 153 | glBindFramebuffer(GL_FRAMEBUFFER, p_glow_buffers[i].fbo); |
| 154 | glViewport(0, 0, p_glow_buffers[i].size.x, p_glow_buffers[i].size.y); |
| 155 | |
| 156 | glActiveTexture(GL_TEXTURE0); |
| 157 | glBindTexture(GL_TEXTURE_2D, p_glow_buffers[i + 1].color); |
| 158 | |
| 159 | glow.shader.version_set_uniform(GlowShaderGLES3::PIXEL_SIZE, 1.0 / p_glow_buffers[i].size.x, 1.0 / p_glow_buffers[i].size.y, glow.shader_version, GlowShaderGLES3::MODE_UPSAMPLE); |
| 160 | |
| 161 | _draw_screen_triangle(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | glDisable(GL_BLEND); |
| 166 | glEnable(GL_DEPTH_TEST); |
| 167 | glDepthMask(GL_TRUE); |
| 168 | glUseProgram(0); |
| 169 | glBindTexture(GL_TEXTURE_2D, 0); |
| 170 | glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo); |
| 171 | } |
| 172 | |
| 173 | #endif // GLES3_ENABLED |