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) { |
119 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION); |
120 | if (!success) { |
121 | return; |
122 | } |
123 | |
124 | 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); |
125 | draw_screen_quad(); |
126 | } |
127 | |
128 | void CopyEffects::copy_screen() { |
129 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_DEFAULT); |
130 | if (!success) { |
131 | return; |
132 | } |
133 | |
134 | draw_screen_triangle(); |
135 | } |
136 | |
137 | // Intended for efficiently mipmapping textures. |
138 | void CopyEffects::bilinear_blur(GLuint p_source_texture, int p_mipmap_count, const Rect2i &p_region) { |
139 | GLuint framebuffers[2]; |
140 | glGenFramebuffers(2, framebuffers); |
141 | glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffers[0]); |
142 | glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, 0); |
143 | |
144 | Rect2i source_region = p_region; |
145 | Rect2i dest_region = p_region; |
146 | for (int i = 1; i < p_mipmap_count; i++) { |
147 | dest_region.position.x >>= 1; |
148 | dest_region.position.y >>= 1; |
149 | dest_region.size.x = MAX(1, dest_region.size.x >> 1); |
150 | dest_region.size.y = MAX(1, dest_region.size.y >> 1); |
151 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffers[i % 2]); |
152 | glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, i); |
153 | 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, |
154 | 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); |
155 | glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffers[i % 2]); |
156 | source_region = dest_region; |
157 | } |
158 | glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); |
159 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
160 | glDeleteFramebuffers(2, framebuffers); |
161 | } |
162 | |
163 | // Intended for approximating a gaussian blur. Used for 2D backbuffer mipmaps. Slightly less efficient than bilinear_blur(). |
164 | void CopyEffects::gaussian_blur(GLuint p_source_texture, int p_mipmap_count, const Rect2i &p_region, const Size2i &p_size) { |
165 | GLuint framebuffer; |
166 | glGenFramebuffers(1, &framebuffer); |
167 | glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
168 | |
169 | glActiveTexture(GL_TEXTURE0); |
170 | glBindTexture(GL_TEXTURE_2D, p_source_texture); |
171 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
172 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
173 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
174 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
175 | |
176 | Size2i base_size = p_size; |
177 | |
178 | Rect2i source_region = p_region; |
179 | Rect2i dest_region = p_region; |
180 | |
181 | Size2 float_size = Size2(p_size); |
182 | Rect2 normalized_source_region = Rect2(p_region); |
183 | normalized_source_region.position = normalized_source_region.position / float_size; |
184 | normalized_source_region.size = normalized_source_region.size / float_size; |
185 | Rect2 normalized_dest_region = Rect2(p_region); |
186 | for (int i = 1; i < p_mipmap_count; i++) { |
187 | dest_region.position.x >>= 1; |
188 | dest_region.position.y >>= 1; |
189 | dest_region.size.x = MAX(1, dest_region.size.x >> 1); |
190 | dest_region.size.y = MAX(1, dest_region.size.y >> 1); |
191 | base_size.x >>= 1; |
192 | base_size.y >>= 1; |
193 | |
194 | glBindTexture(GL_TEXTURE_2D, p_source_texture); |
195 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, i - 1); |
196 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i - 1); |
197 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_source_texture, i); |
198 | #ifdef DEV_ENABLED |
199 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
200 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
201 | WARN_PRINT("Could not bind Gaussian blur framebuffer, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status)); |
202 | } |
203 | #endif |
204 | |
205 | glViewport(0, 0, base_size.x, base_size.y); |
206 | |
207 | bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_GAUSSIAN_BLUR); |
208 | if (!success) { |
209 | return; |
210 | } |
211 | |
212 | float_size = Size2(base_size); |
213 | normalized_dest_region.position = Size2(dest_region.position) / float_size; |
214 | normalized_dest_region.size = Size2(dest_region.size) / float_size; |
215 | |
216 | 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); |
217 | 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); |
218 | 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); |
219 | |
220 | draw_screen_quad(); |
221 | |
222 | source_region = dest_region; |
223 | normalized_source_region = normalized_dest_region; |
224 | } |
225 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
226 | glDeleteFramebuffers(1, &framebuffer); |
227 | |
228 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
229 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
230 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); |
231 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, p_mipmap_count - 1); |
232 | glBindTexture(GL_TEXTURE_2D, 0); |
233 | |
234 | glViewport(0, 0, p_size.x, p_size.y); |
235 | } |
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 | |
243 | 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); |
244 | copy.shader.version_set_uniform(CopyShaderGLES3::COLOR_IN, p_color, copy.shader_version, CopyShaderGLES3::MODE_SIMPLE_COLOR); |
245 | draw_screen_quad(); |
246 | } |
247 | |
248 | void CopyEffects::draw_screen_triangle() { |
249 | glBindVertexArray(screen_triangle_array); |
250 | glDrawArrays(GL_TRIANGLES, 0, 3); |
251 | glBindVertexArray(0); |
252 | } |
253 | |
254 | void CopyEffects::draw_screen_quad() { |
255 | glBindVertexArray(quad_array); |
256 | glDrawArrays(GL_TRIANGLES, 0, 6); |
257 | glBindVertexArray(0); |
258 | } |
259 | |
260 | #endif // GLES3_ENABLED |
261 | |