| 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 | |
| 31 | #ifdef GLES3_ENABLED |
| 32 | |
| 33 | #include "copy_effects.h" |
| 34 | #include "../storage/texture_storage.h" |
| 35 | |
| 36 | using namespace GLES3; |
| 37 | |
| 38 | CopyEffects *CopyEffects::singleton = nullptr; |
| 39 | |
| 40 | CopyEffects *CopyEffects::get_singleton() { |
| 41 | return singleton; |
| 42 | } |
| 43 | |
| 44 | CopyEffects::CopyEffects() { |
| 45 | singleton = this; |
| 46 | |
| 47 | copy.shader.initialize(); |
| 48 | copy.shader_version = copy.shader.version_create(); |
| 49 | copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_DEFAULT); |
| 50 | |
| 51 | { // Screen Triangle. |
| 52 | glGenBuffers(1, &screen_triangle); |
| 53 | glBindBuffer(GL_ARRAY_BUFFER, screen_triangle); |
| 54 | |
| 55 | const float qv[6] = { |
| 56 | -1.0f, |
| 57 | -1.0f, |
| 58 | 3.0f, |
| 59 | -1.0f, |
| 60 | -1.0f, |
| 61 | 3.0f, |
| 62 | }; |
| 63 | |
| 64 | glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, qv, GL_STATIC_DRAW); |
| 65 | glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind |
| 66 | |
| 67 | glGenVertexArrays(1, &screen_triangle_array); |
| 68 | glBindVertexArray(screen_triangle_array); |
| 69 | glBindBuffer(GL_ARRAY_BUFFER, screen_triangle); |
| 70 | glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr); |
| 71 | glEnableVertexAttribArray(RS::ARRAY_VERTEX); |
| 72 | glBindVertexArray(0); |
| 73 | glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind |
| 74 | } |
| 75 | |
| 76 | { // Screen Quad |
| 77 | |
| 78 | glGenBuffers(1, &quad); |
| 79 | glBindBuffer(GL_ARRAY_BUFFER, quad); |
| 80 | |
| 81 | const float qv[12] = { |
| 82 | -1.0f, |
| 83 | -1.0f, |
| 84 | 1.0f, |
| 85 | -1.0f, |
| 86 | 1.0f, |
| 87 | 1.0f, |
| 88 | -1.0f, |
| 89 | -1.0f, |
| 90 | 1.0f, |
| 91 | 1.0f, |
| 92 | -1.0f, |
| 93 | 1.0f, |
| 94 | }; |
| 95 | |
| 96 | glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 12, qv, GL_STATIC_DRAW); |
| 97 | glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind |
| 98 | |
| 99 | glGenVertexArrays(1, &quad_array); |
| 100 | glBindVertexArray(quad_array); |
| 101 | glBindBuffer(GL_ARRAY_BUFFER, quad); |
| 102 | glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr); |
| 103 | glEnableVertexAttribArray(RS::ARRAY_VERTEX); |
| 104 | glBindVertexArray(0); |
| 105 | glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | CopyEffects::~CopyEffects() { |
| 110 | singleton = nullptr; |
| 111 | glDeleteBuffers(1, &screen_triangle); |
| 112 | glDeleteVertexArrays(1, &screen_triangle_array); |
| 113 | glDeleteBuffers(1, &quad); |
| 114 | glDeleteVertexArrays(1, &quad_array); |
| 115 | copy.shader.version_free(copy.shader_version); |
| 116 | } |
| 117 | |
| 118 | void CopyEffects::copy_to_rect(const Rect2 &p_rect, bool p_linear_to_srgb) { |
| 119 | uint64_t specializations = p_linear_to_srgb ? CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0; |
| 120 | |
| 121 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION, specializations); |
| 122 | if (!success) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | 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, specializations); |
| 127 | draw_screen_quad(); |
| 128 | } |
| 129 | |
| 130 | void CopyEffects::copy_to_rect_3d(const Rect2 &p_rect, float p_layer, int p_type, float p_lod, bool p_linear_to_srgb) { |
| 131 | ERR_FAIL_COND(p_type != Texture::TYPE_LAYERED && p_type != Texture::TYPE_3D); |
| 132 | |
| 133 | CopyShaderGLES3::ShaderVariant variant = p_type == Texture::TYPE_LAYERED |
| 134 | ? CopyShaderGLES3::MODE_COPY_SECTION_2D_ARRAY |
| 135 | : CopyShaderGLES3::MODE_COPY_SECTION_3D; |
| 136 | uint64_t specializations = p_linear_to_srgb ? CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0; |
| 137 | |
| 138 | bool success = copy.shader.version_bind_shader(copy.shader_version, variant, specializations); |
| 139 | if (!success) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | 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, specializations); |
| 144 | copy.shader.version_set_uniform(CopyShaderGLES3::LAYER, p_layer, copy.shader_version, variant, specializations); |
| 145 | copy.shader.version_set_uniform(CopyShaderGLES3::LOD, p_lod, copy.shader_version, variant, specializations); |
| 146 | draw_screen_quad(); |
| 147 | } |
| 148 | |
| 149 | void CopyEffects::copy_with_lens_distortion(const Rect2 &p_rect, float p_layer, const Vector2 &p_eye_center, float p_k1, float p_k2, float p_upscale, float p_aspect_ration, bool p_linear_to_srgb) { |
| 150 | CopyShaderGLES3::ShaderVariant variant = CopyShaderGLES3::MODE_LENS_DISTORTION; |
| 151 | |
| 152 | uint64_t specializations = p_linear_to_srgb ? CopyShaderGLES3::CONVERT_LINEAR_TO_SRGB : 0; |
| 153 | |
| 154 | bool success = copy.shader.version_bind_shader(copy.shader_version, variant, specializations); |
| 155 | if (!success) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | 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, specializations); |
| 160 | copy.shader.version_set_uniform(CopyShaderGLES3::LAYER, p_layer, copy.shader_version, variant, specializations); |
| 161 | copy.shader.version_set_uniform(CopyShaderGLES3::LOD, 0.0, copy.shader_version, variant, specializations); |
| 162 | copy.shader.version_set_uniform(CopyShaderGLES3::EYE_CENTER, p_eye_center.x, p_eye_center.y, copy.shader_version, variant, specializations); |
| 163 | copy.shader.version_set_uniform(CopyShaderGLES3::K1, p_k1, copy.shader_version, variant, specializations); |
| 164 | copy.shader.version_set_uniform(CopyShaderGLES3::K2, p_k1, copy.shader_version, variant, specializations); |
| 165 | copy.shader.version_set_uniform(CopyShaderGLES3::UPSCALE, p_upscale, copy.shader_version, variant, specializations); |
| 166 | copy.shader.version_set_uniform(CopyShaderGLES3::ASPECT_RATIO, p_aspect_ration, copy.shader_version, variant, specializations); |
| 167 | |
| 168 | draw_screen_quad(); |
| 169 | } |
| 170 | |
| 171 | void CopyEffects::copy_to_and_from_rect(const Rect2 &p_rect) { |
| 172 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION_SOURCE); |
| 173 | if (!success) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | 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); |
| 178 | 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); |
| 179 | |
| 180 | draw_screen_quad(); |
| 181 | } |
| 182 | |
| 183 | void CopyEffects::copy_screen(float p_multiply) { |
| 184 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_SCREEN); |
| 185 | if (!success) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | copy.shader.version_set_uniform(CopyShaderGLES3::MULTIPLY, p_multiply, copy.shader_version, CopyShaderGLES3::MODE_SCREEN); |
| 190 | |
| 191 | draw_screen_triangle(); |
| 192 | } |
| 193 | |
| 194 | void CopyEffects::copy_cube_to_rect(const Rect2 &p_rect) { |
| 195 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_CUBE_TO_OCTAHEDRAL); |
| 196 | if (!success) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | 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); |
| 201 | draw_screen_quad(); |
| 202 | } |
| 203 | |
| 204 | void CopyEffects::copy_cube_to_panorama(float p_mip_level) { |
| 205 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_CUBE_TO_PANORAMA); |
| 206 | if (!success) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | copy.shader.version_set_uniform(CopyShaderGLES3::MIP_LEVEL, p_mip_level, copy.shader_version, CopyShaderGLES3::MODE_CUBE_TO_PANORAMA); |
| 211 | draw_screen_quad(); |
| 212 | } |
| 213 | |
| 214 | // Intended for efficiently mipmapping textures. |
| 215 | void CopyEffects::bilinear_blur(GLuint p_source_texture, int p_mipmap_count, const Rect2i &p_region) { |
| 216 | GLuint framebuffers[2]; |
| 217 | glGenFramebuffers(2, framebuffers); |
| 218 | glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffers[0]); |
| 219 | glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, 0); |
| 220 | |
| 221 | Rect2i source_region = p_region; |
| 222 | Rect2i dest_region = p_region; |
| 223 | for (int i = 1; i < p_mipmap_count; i++) { |
| 224 | dest_region.position.x >>= 1; |
| 225 | dest_region.position.y >>= 1; |
| 226 | dest_region.size = Size2i(dest_region.size.x >> 1, dest_region.size.y >> 1).maxi(1); |
| 227 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffers[i % 2]); |
| 228 | glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, i); |
| 229 | 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, |
| 230 | 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); |
| 231 | glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffers[i % 2]); |
| 232 | source_region = dest_region; |
| 233 | } |
| 234 | glBindFramebuffer(GL_READ_FRAMEBUFFER, GLES3::TextureStorage::system_fbo); |
| 235 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, GLES3::TextureStorage::system_fbo); |
| 236 | glDeleteFramebuffers(2, framebuffers); |
| 237 | } |
| 238 | |
| 239 | // Intended for approximating a gaussian blur. Used for 2D backbuffer mipmaps. Slightly less efficient than bilinear_blur(). |
| 240 | void CopyEffects::gaussian_blur(GLuint p_source_texture, int p_mipmap_count, const Rect2i &p_region, const Size2i &p_size) { |
| 241 | GLuint framebuffer; |
| 242 | glGenFramebuffers(1, &framebuffer); |
| 243 | glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 244 | |
| 245 | glActiveTexture(GL_TEXTURE0); |
| 246 | glBindTexture(GL_TEXTURE_2D, p_source_texture); |
| 247 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 248 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 249 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 250 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 251 | |
| 252 | Size2i base_size = p_size; |
| 253 | |
| 254 | Rect2i source_region = p_region; |
| 255 | Rect2i dest_region = p_region; |
| 256 | |
| 257 | Size2 float_size = Size2(p_size); |
| 258 | Rect2 normalized_source_region = Rect2(p_region); |
| 259 | normalized_source_region.position = normalized_source_region.position / float_size; |
| 260 | normalized_source_region.size = normalized_source_region.size / float_size; |
| 261 | Rect2 normalized_dest_region = Rect2(p_region); |
| 262 | for (int i = 1; i < p_mipmap_count; i++) { |
| 263 | dest_region.position.x >>= 1; |
| 264 | dest_region.position.y >>= 1; |
| 265 | dest_region.size = Size2i(dest_region.size.x >> 1, dest_region.size.y >> 1).maxi(1); |
| 266 | base_size.x >>= 1; |
| 267 | base_size.y >>= 1; |
| 268 | |
| 269 | glBindTexture(GL_TEXTURE_2D, p_source_texture); |
| 270 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, i - 1); |
| 271 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i); |
| 272 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, i); |
| 273 | #ifdef DEV_ENABLED |
| 274 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 275 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 276 | WARN_PRINT("Could not bind Gaussian blur framebuffer, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status)); |
| 277 | } |
| 278 | #endif |
| 279 | |
| 280 | glViewport(0, 0, base_size.x, base_size.y); |
| 281 | |
| 282 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_GAUSSIAN_BLUR); |
| 283 | if (!success) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | float_size = Size2(base_size); |
| 288 | normalized_dest_region.position = Size2(dest_region.position) / float_size; |
| 289 | normalized_dest_region.size = Size2(dest_region.size) / float_size; |
| 290 | |
| 291 | 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); |
| 292 | 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); |
| 293 | 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); |
| 294 | |
| 295 | draw_screen_quad(); |
| 296 | |
| 297 | source_region = dest_region; |
| 298 | normalized_source_region = normalized_dest_region; |
| 299 | } |
| 300 | glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo); |
| 301 | glDeleteFramebuffers(1, &framebuffer); |
| 302 | |
| 303 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 304 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
| 305 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); |
| 306 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, p_mipmap_count - 1); |
| 307 | glBindTexture(GL_TEXTURE_2D, 0); |
| 308 | |
| 309 | glViewport(0, 0, p_size.x, p_size.y); |
| 310 | } |
| 311 | |
| 312 | void CopyEffects::set_color(const Color &p_color, const Rect2i &p_region) { |
| 313 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_SIMPLE_COLOR); |
| 314 | if (!success) { |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | 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); |
| 319 | copy.shader.version_set_uniform(CopyShaderGLES3::COLOR_IN, p_color, copy.shader_version, CopyShaderGLES3::MODE_SIMPLE_COLOR); |
| 320 | draw_screen_quad(); |
| 321 | } |
| 322 | |
| 323 | void CopyEffects::draw_screen_triangle() { |
| 324 | glBindVertexArray(screen_triangle_array); |
| 325 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 326 | glBindVertexArray(0); |
| 327 | } |
| 328 | |
| 329 | void CopyEffects::draw_screen_quad() { |
| 330 | glBindVertexArray(quad_array); |
| 331 | glDrawArrays(GL_TRIANGLES, 0, 6); |
| 332 | glBindVertexArray(0); |
| 333 | } |
| 334 | |
| 335 | #endif // GLES3_ENABLED |