1 /*************************************************************************/
2 /*  rasterizer_storage_gles2.cpp                                         */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 #include "rasterizer_storage_gles2.h"
32 
33 #include "core/math/transform.h"
34 #include "core/project_settings.h"
35 #include "rasterizer_canvas_gles2.h"
36 #include "rasterizer_scene_gles2.h"
37 #include "servers/visual/shader_language.h"
38 
39 GLuint RasterizerStorageGLES2::system_fbo = 0;
40 
41 /* TEXTURE API */
42 
43 #define _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
44 #define _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
45 #define _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
46 
47 #define _EXT_COMPRESSED_RED_RGTC1_EXT 0x8DBB
48 #define _EXT_COMPRESSED_RED_RGTC1 0x8DBB
49 #define _EXT_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC
50 #define _EXT_COMPRESSED_RG_RGTC2 0x8DBD
51 #define _EXT_COMPRESSED_SIGNED_RG_RGTC2 0x8DBE
52 #define _EXT_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
53 #define _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
54 #define _EXT_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
55 #define _EXT_ETC1_RGB8_OES 0x8D64
56 
57 #define _EXT_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
58 #define _EXT_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
59 #define _EXT_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
60 #define _EXT_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
61 
62 #define _EXT_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
63 #define _EXT_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
64 #define _EXT_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
65 #define _EXT_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
66 
67 #define _EXT_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C
68 #define _EXT_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D
69 #define _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E
70 #define _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F
71 
72 #define _GL_TEXTURE_EXTERNAL_OES 0x8D65
73 
74 #ifdef GLES_OVER_GL
75 #define _GL_HALF_FLOAT_OES 0x140B
76 #else
77 #define _GL_HALF_FLOAT_OES 0x8D61
78 #endif
79 
80 #define _EXT_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
81 
82 #define _RED_OES 0x1903
83 
84 #define _DEPTH_COMPONENT24_OES 0x81A6
85 
86 #ifndef GLES_OVER_GL
87 #define glClearDepth glClearDepthf
88 
89 // enable extensions manually for android and ios
90 #ifndef UWP_ENABLED
91 #include <dlfcn.h> // needed to load extensions
92 #endif
93 
94 #ifdef IPHONE_ENABLED
95 
96 #include <OpenGLES/ES2/glext.h>
97 //void *glRenderbufferStorageMultisampleAPPLE;
98 //void *glResolveMultisampleFramebufferAPPLE;
99 #define glRenderbufferStorageMultisample glRenderbufferStorageMultisampleAPPLE
100 #elif defined(ANDROID_ENABLED)
101 
102 #include <GLES2/gl2ext.h>
103 PFNGLRENDERBUFFERSTORAGEMULTISAMPLEEXTPROC glRenderbufferStorageMultisampleEXT;
104 PFNGLFRAMEBUFFERTEXTURE2DMULTISAMPLEEXTPROC glFramebufferTexture2DMultisampleEXT;
105 #define glRenderbufferStorageMultisample glRenderbufferStorageMultisampleEXT
106 #define glFramebufferTexture2DMultisample glFramebufferTexture2DMultisampleEXT
107 
108 #elif defined(UWP_ENABLED)
109 #include <GLES2/gl2ext.h>
110 #define glRenderbufferStorageMultisample glRenderbufferStorageMultisampleANGLE
111 #define glFramebufferTexture2DMultisample glFramebufferTexture2DMultisampleANGLE
112 #endif
113 
114 #define GL_TEXTURE_3D 0x806F
115 #define GL_MAX_SAMPLES 0x8D57
116 #endif //!GLES_OVER_GL
117 
bind_quad_array() const118 void RasterizerStorageGLES2::bind_quad_array() const {
119 	glBindBuffer(GL_ARRAY_BUFFER, resources.quadie);
120 	glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
121 	glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
122 
123 	glEnableVertexAttribArray(VS::ARRAY_VERTEX);
124 	glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
125 }
126 
_get_gl_image_and_format(const Ref<Image> & p_image,Image::Format p_format,uint32_t p_flags,Image::Format & r_real_format,GLenum & r_gl_format,GLenum & r_gl_internal_format,GLenum & r_gl_type,bool & r_compressed,bool p_force_decompress) const127 Ref<Image> RasterizerStorageGLES2::_get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool p_force_decompress) const {
128 
129 	r_gl_format = 0;
130 	Ref<Image> image = p_image;
131 	r_compressed = false;
132 	r_real_format = p_format;
133 
134 	bool need_decompress = false;
135 
136 	switch (p_format) {
137 
138 		case Image::FORMAT_L8: {
139 
140 			r_gl_internal_format = GL_LUMINANCE;
141 			r_gl_format = GL_LUMINANCE;
142 			r_gl_type = GL_UNSIGNED_BYTE;
143 		} break;
144 		case Image::FORMAT_LA8: {
145 			r_gl_internal_format = GL_LUMINANCE_ALPHA;
146 			r_gl_format = GL_LUMINANCE_ALPHA;
147 			r_gl_type = GL_UNSIGNED_BYTE;
148 		} break;
149 		case Image::FORMAT_R8: {
150 
151 			r_gl_internal_format = GL_ALPHA;
152 			r_gl_format = GL_ALPHA;
153 			r_gl_type = GL_UNSIGNED_BYTE;
154 
155 		} break;
156 		case Image::FORMAT_RG8: {
157 			ERR_PRINT("RG texture not supported, converting to RGB8.");
158 			if (image.is_valid())
159 				image->convert(Image::FORMAT_RGB8);
160 			r_real_format = Image::FORMAT_RGB8;
161 			r_gl_internal_format = GL_RGB;
162 			r_gl_format = GL_RGB;
163 			r_gl_type = GL_UNSIGNED_BYTE;
164 
165 		} break;
166 		case Image::FORMAT_RGB8: {
167 
168 			r_gl_internal_format = GL_RGB;
169 			r_gl_format = GL_RGB;
170 			r_gl_type = GL_UNSIGNED_BYTE;
171 
172 		} break;
173 		case Image::FORMAT_RGBA8: {
174 
175 			r_gl_format = GL_RGBA;
176 			r_gl_internal_format = GL_RGBA;
177 			r_gl_type = GL_UNSIGNED_BYTE;
178 
179 		} break;
180 		case Image::FORMAT_RGBA4444: {
181 
182 			r_gl_internal_format = GL_RGBA;
183 			r_gl_format = GL_RGBA;
184 			r_gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
185 
186 		} break;
187 		case Image::FORMAT_RGBA5551: {
188 
189 			r_gl_internal_format = GL_RGB5_A1;
190 			r_gl_format = GL_RGBA;
191 			r_gl_type = GL_UNSIGNED_SHORT_5_5_5_1;
192 
193 		} break;
194 		case Image::FORMAT_RF: {
195 			if (!config.float_texture_supported) {
196 				ERR_PRINT("R float texture not supported, converting to RGB8.");
197 				if (image.is_valid())
198 					image->convert(Image::FORMAT_RGB8);
199 				r_real_format = Image::FORMAT_RGB8;
200 				r_gl_internal_format = GL_RGB;
201 				r_gl_format = GL_RGB;
202 				r_gl_type = GL_UNSIGNED_BYTE;
203 			} else {
204 				r_gl_internal_format = GL_ALPHA;
205 				r_gl_format = GL_ALPHA;
206 				r_gl_type = GL_FLOAT;
207 			}
208 		} break;
209 		case Image::FORMAT_RGF: {
210 			ERR_PRINT("RG float texture not supported, converting to RGB8.");
211 			if (image.is_valid())
212 				image->convert(Image::FORMAT_RGB8);
213 			r_real_format = Image::FORMAT_RGB8;
214 			r_gl_internal_format = GL_RGB;
215 			r_gl_format = GL_RGB;
216 			r_gl_type = GL_UNSIGNED_BYTE;
217 		} break;
218 		case Image::FORMAT_RGBF: {
219 			if (!config.float_texture_supported) {
220 				ERR_PRINT("RGB float texture not supported, converting to RGB8.");
221 				if (image.is_valid())
222 					image->convert(Image::FORMAT_RGB8);
223 				r_real_format = Image::FORMAT_RGB8;
224 				r_gl_internal_format = GL_RGB;
225 				r_gl_format = GL_RGB;
226 				r_gl_type = GL_UNSIGNED_BYTE;
227 			} else {
228 				r_gl_internal_format = GL_RGB;
229 				r_gl_format = GL_RGB;
230 				r_gl_type = GL_FLOAT;
231 			}
232 		} break;
233 		case Image::FORMAT_RGBAF: {
234 			if (!config.float_texture_supported) {
235 				ERR_PRINT("RGBA float texture not supported, converting to RGBA8.");
236 				if (image.is_valid())
237 					image->convert(Image::FORMAT_RGBA8);
238 				r_real_format = Image::FORMAT_RGBA8;
239 				r_gl_internal_format = GL_RGBA;
240 				r_gl_format = GL_RGBA;
241 				r_gl_type = GL_UNSIGNED_BYTE;
242 			} else {
243 				r_gl_internal_format = GL_RGBA;
244 				r_gl_format = GL_RGBA;
245 				r_gl_type = GL_FLOAT;
246 			}
247 		} break;
248 		case Image::FORMAT_RH: {
249 			need_decompress = true;
250 		} break;
251 		case Image::FORMAT_RGH: {
252 			need_decompress = true;
253 		} break;
254 		case Image::FORMAT_RGBH: {
255 			need_decompress = true;
256 		} break;
257 		case Image::FORMAT_RGBAH: {
258 			need_decompress = true;
259 		} break;
260 		case Image::FORMAT_RGBE9995: {
261 			r_gl_internal_format = GL_RGB;
262 			r_gl_format = GL_RGB;
263 			r_gl_type = GL_UNSIGNED_BYTE;
264 
265 			if (image.is_valid())
266 
267 				image = image->rgbe_to_srgb();
268 
269 			return image;
270 
271 		} break;
272 		case Image::FORMAT_DXT1: {
273 
274 			if (config.s3tc_supported) {
275 				r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT;
276 				r_gl_format = GL_RGBA;
277 				r_gl_type = GL_UNSIGNED_BYTE;
278 				r_compressed = true;
279 			} else {
280 				need_decompress = true;
281 			}
282 
283 		} break;
284 		case Image::FORMAT_DXT3: {
285 
286 			if (config.s3tc_supported) {
287 				r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT;
288 				r_gl_format = GL_RGBA;
289 				r_gl_type = GL_UNSIGNED_BYTE;
290 				r_compressed = true;
291 			} else {
292 				need_decompress = true;
293 			}
294 
295 		} break;
296 		case Image::FORMAT_DXT5: {
297 
298 			if (config.s3tc_supported) {
299 				r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
300 				r_gl_format = GL_RGBA;
301 				r_gl_type = GL_UNSIGNED_BYTE;
302 				r_compressed = true;
303 			} else {
304 				need_decompress = true;
305 			}
306 
307 		} break;
308 		case Image::FORMAT_RGTC_R: {
309 
310 			if (config.rgtc_supported) {
311 
312 				r_gl_internal_format = _EXT_COMPRESSED_RED_RGTC1_EXT;
313 				r_gl_format = GL_RGBA;
314 				r_gl_type = GL_UNSIGNED_BYTE;
315 				r_compressed = true;
316 
317 			} else {
318 
319 				need_decompress = true;
320 			}
321 
322 		} break;
323 		case Image::FORMAT_RGTC_RG: {
324 
325 			if (config.rgtc_supported) {
326 
327 				r_gl_internal_format = _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT;
328 				r_gl_format = GL_RGBA;
329 				r_gl_type = GL_UNSIGNED_BYTE;
330 				r_compressed = true;
331 			} else {
332 
333 				need_decompress = true;
334 			}
335 
336 		} break;
337 		case Image::FORMAT_BPTC_RGBA: {
338 
339 			if (config.bptc_supported) {
340 
341 				r_gl_internal_format = _EXT_COMPRESSED_RGBA_BPTC_UNORM;
342 				r_gl_format = GL_RGBA;
343 				r_gl_type = GL_UNSIGNED_BYTE;
344 				r_compressed = true;
345 
346 			} else {
347 
348 				need_decompress = true;
349 			}
350 		} break;
351 		case Image::FORMAT_BPTC_RGBF: {
352 
353 			if (config.bptc_supported) {
354 
355 				r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
356 				r_gl_format = GL_RGB;
357 				r_gl_type = GL_FLOAT;
358 				r_compressed = true;
359 			} else {
360 
361 				need_decompress = true;
362 			}
363 		} break;
364 		case Image::FORMAT_BPTC_RGBFU: {
365 			if (config.bptc_supported) {
366 
367 				r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
368 				r_gl_format = GL_RGB;
369 				r_gl_type = GL_FLOAT;
370 				r_compressed = true;
371 			} else {
372 
373 				need_decompress = true;
374 			}
375 		} break;
376 		case Image::FORMAT_PVRTC2: {
377 
378 			if (config.pvrtc_supported) {
379 
380 				r_gl_internal_format = _EXT_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
381 				r_gl_format = GL_RGBA;
382 				r_gl_type = GL_UNSIGNED_BYTE;
383 				r_compressed = true;
384 
385 			} else {
386 
387 				need_decompress = true;
388 			}
389 		} break;
390 		case Image::FORMAT_PVRTC2A: {
391 
392 			if (config.pvrtc_supported) {
393 
394 				r_gl_internal_format = _EXT_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
395 				r_gl_format = GL_RGBA;
396 				r_gl_type = GL_UNSIGNED_BYTE;
397 				r_compressed = true;
398 
399 			} else {
400 
401 				need_decompress = true;
402 			}
403 
404 		} break;
405 		case Image::FORMAT_PVRTC4: {
406 
407 			if (config.pvrtc_supported) {
408 
409 				r_gl_internal_format = _EXT_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
410 				r_gl_format = GL_RGBA;
411 				r_gl_type = GL_UNSIGNED_BYTE;
412 				r_compressed = true;
413 
414 			} else {
415 
416 				need_decompress = true;
417 			}
418 
419 		} break;
420 		case Image::FORMAT_PVRTC4A: {
421 
422 			if (config.pvrtc_supported) {
423 
424 				r_gl_internal_format = _EXT_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
425 				r_gl_format = GL_RGBA;
426 				r_gl_type = GL_UNSIGNED_BYTE;
427 				r_compressed = true;
428 
429 			} else {
430 
431 				need_decompress = true;
432 			}
433 
434 		} break;
435 		case Image::FORMAT_ETC: {
436 
437 			if (config.etc1_supported) {
438 				r_gl_internal_format = _EXT_ETC1_RGB8_OES;
439 				r_gl_format = GL_RGBA;
440 				r_gl_type = GL_UNSIGNED_BYTE;
441 				r_compressed = true;
442 			} else {
443 				need_decompress = true;
444 			}
445 		} break;
446 		case Image::FORMAT_ETC2_R11: {
447 
448 			need_decompress = true;
449 		} break;
450 		case Image::FORMAT_ETC2_R11S: {
451 
452 			need_decompress = true;
453 		} break;
454 		case Image::FORMAT_ETC2_RG11: {
455 
456 			need_decompress = true;
457 		} break;
458 		case Image::FORMAT_ETC2_RG11S: {
459 
460 			need_decompress = true;
461 		} break;
462 		case Image::FORMAT_ETC2_RGB8: {
463 
464 			need_decompress = true;
465 		} break;
466 		case Image::FORMAT_ETC2_RGBA8: {
467 
468 			need_decompress = true;
469 		} break;
470 		case Image::FORMAT_ETC2_RGB8A1: {
471 
472 			need_decompress = true;
473 		} break;
474 		default: {
475 
476 			ERR_FAIL_V(Ref<Image>());
477 		}
478 	}
479 
480 	if (need_decompress || p_force_decompress) {
481 
482 		if (!image.is_null()) {
483 
484 			image = image->duplicate();
485 			image->decompress();
486 			ERR_FAIL_COND_V(image->is_compressed(), image);
487 			switch (image->get_format()) {
488 				case Image::FORMAT_RGB8: {
489 					r_gl_format = GL_RGB;
490 					r_gl_internal_format = GL_RGB;
491 					r_gl_type = GL_UNSIGNED_BYTE;
492 					r_real_format = Image::FORMAT_RGB8;
493 					r_compressed = false;
494 				} break;
495 				case Image::FORMAT_RGBA8: {
496 					r_gl_format = GL_RGBA;
497 					r_gl_internal_format = GL_RGBA;
498 					r_gl_type = GL_UNSIGNED_BYTE;
499 					r_real_format = Image::FORMAT_RGBA8;
500 					r_compressed = false;
501 				} break;
502 				default: {
503 					image->convert(Image::FORMAT_RGBA8);
504 					r_gl_format = GL_RGBA;
505 					r_gl_internal_format = GL_RGBA;
506 					r_gl_type = GL_UNSIGNED_BYTE;
507 					r_real_format = Image::FORMAT_RGBA8;
508 					r_compressed = false;
509 
510 				} break;
511 			}
512 		}
513 
514 		return image;
515 	}
516 
517 	return p_image;
518 }
519 
520 static const GLenum _cube_side_enum[6] = {
521 
522 	GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
523 	GL_TEXTURE_CUBE_MAP_POSITIVE_X,
524 	GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
525 	GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
526 	GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
527 	GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
528 
529 };
530 
texture_create()531 RID RasterizerStorageGLES2::texture_create() {
532 
533 	Texture *texture = memnew(Texture);
534 	ERR_FAIL_COND_V(!texture, RID());
535 	glGenTextures(1, &texture->tex_id);
536 	texture->active = false;
537 	texture->total_data_size = 0;
538 
539 	return texture_owner.make_rid(texture);
540 }
541 
texture_allocate(RID p_texture,int p_width,int p_height,int p_depth_3d,Image::Format p_format,VisualServer::TextureType p_type,uint32_t p_flags)542 void RasterizerStorageGLES2::texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VisualServer::TextureType p_type, uint32_t p_flags) {
543 	GLenum format;
544 	GLenum internal_format;
545 	GLenum type;
546 
547 	bool compressed = false;
548 
549 	if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
550 		p_flags &= ~VS::TEXTURE_FLAG_MIPMAPS; // no mipies for video
551 	}
552 
553 	Texture *texture = texture_owner.getornull(p_texture);
554 	ERR_FAIL_COND(!texture);
555 	texture->width = p_width;
556 	texture->height = p_height;
557 	texture->format = p_format;
558 	texture->flags = p_flags;
559 	texture->stored_cube_sides = 0;
560 	texture->type = p_type;
561 
562 	switch (p_type) {
563 		case VS::TEXTURE_TYPE_2D: {
564 			texture->target = GL_TEXTURE_2D;
565 			texture->images.resize(1);
566 		} break;
567 		case VS::TEXTURE_TYPE_EXTERNAL: {
568 #ifdef ANDROID_ENABLED
569 			texture->target = _GL_TEXTURE_EXTERNAL_OES;
570 #else
571 			texture->target = GL_TEXTURE_2D;
572 #endif
573 			texture->images.resize(0);
574 		} break;
575 		case VS::TEXTURE_TYPE_CUBEMAP: {
576 			texture->target = GL_TEXTURE_CUBE_MAP;
577 			texture->images.resize(6);
578 		} break;
579 		case VS::TEXTURE_TYPE_2D_ARRAY:
580 		case VS::TEXTURE_TYPE_3D: {
581 			texture->target = GL_TEXTURE_3D;
582 			ERR_PRINT("3D textures and Texture Arrays are not supported in GLES2. Please switch to the GLES3 backend.");
583 			return;
584 		} break;
585 		default: {
586 			ERR_PRINT("Unknown texture type!");
587 			return;
588 		}
589 	}
590 
591 	if (p_type != VS::TEXTURE_TYPE_EXTERNAL) {
592 		texture->alloc_width = texture->width;
593 		texture->alloc_height = texture->height;
594 		texture->resize_to_po2 = false;
595 		if (!config.support_npot_repeat_mipmap) {
596 			int po2_width = next_power_of_2(p_width);
597 			int po2_height = next_power_of_2(p_height);
598 
599 			bool is_po2 = p_width == po2_width && p_height == po2_height;
600 
601 			if (!is_po2 && (p_flags & VS::TEXTURE_FLAG_REPEAT || p_flags & VS::TEXTURE_FLAG_MIPMAPS)) {
602 
603 				if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
604 					//not supported
605 					ERR_PRINT("Streaming texture for non power of 2 or has mipmaps on this hardware: " + texture->path + "'. Mipmaps and repeat disabled.");
606 					texture->flags &= ~(VS::TEXTURE_FLAG_REPEAT | VS::TEXTURE_FLAG_MIPMAPS);
607 				} else {
608 					texture->alloc_height = po2_height;
609 					texture->alloc_width = po2_width;
610 					texture->resize_to_po2 = true;
611 				}
612 			}
613 		}
614 
615 		Image::Format real_format;
616 		_get_gl_image_and_format(Ref<Image>(),
617 				texture->format,
618 				texture->flags,
619 				real_format,
620 				format,
621 				internal_format,
622 				type,
623 				compressed,
624 				texture->resize_to_po2);
625 
626 		texture->gl_format_cache = format;
627 		texture->gl_type_cache = type;
628 		texture->gl_internal_format_cache = internal_format;
629 		texture->data_size = 0;
630 		texture->mipmaps = 1;
631 
632 		texture->compressed = compressed;
633 	}
634 
635 	glActiveTexture(GL_TEXTURE0);
636 	glBindTexture(texture->target, texture->tex_id);
637 
638 	if (p_type == VS::TEXTURE_TYPE_EXTERNAL) {
639 		glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
640 		glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
641 		glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
642 		glTexParameteri(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
643 	} else if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
644 		//prealloc if video
645 		glTexImage2D(texture->target, 0, internal_format, texture->alloc_width, texture->alloc_height, 0, format, type, NULL);
646 	}
647 
648 	texture->active = true;
649 }
650 
texture_set_data(RID p_texture,const Ref<Image> & p_image,int p_layer)651 void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer) {
652 	Texture *texture = texture_owner.getornull(p_texture);
653 
654 	ERR_FAIL_COND(!texture);
655 	if (texture->target == GL_TEXTURE_3D) {
656 		// Target is set to a 3D texture or array texture, exit early to avoid spamming errors
657 		return;
658 	}
659 	ERR_FAIL_COND(!texture->active);
660 	ERR_FAIL_COND(texture->render_target);
661 	ERR_FAIL_COND(texture->format != p_image->get_format());
662 	ERR_FAIL_COND(p_image.is_null());
663 	ERR_FAIL_COND(texture->type == VS::TEXTURE_TYPE_EXTERNAL);
664 
665 	GLenum type;
666 	GLenum format;
667 	GLenum internal_format;
668 	bool compressed = false;
669 
670 	if (config.keep_original_textures && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
671 		texture->images.write[p_layer] = p_image;
672 	}
673 
674 	Image::Format real_format;
675 	Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, real_format, format, internal_format, type, compressed, texture->resize_to_po2);
676 
677 	if (texture->resize_to_po2) {
678 		if (p_image->is_compressed()) {
679 			ERR_PRINTS("Texture '" + texture->path + "' is required to be a power of 2 because it uses either mipmaps or repeat, so it was decompressed. This will hurt performance and memory usage.");
680 		}
681 
682 		if (img == p_image) {
683 			img = img->duplicate();
684 		}
685 		img->resize_to_po2(false);
686 	}
687 
688 	if (config.shrink_textures_x2 && (p_image->has_mipmaps() || !p_image->is_compressed()) && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
689 
690 		texture->alloc_height = MAX(1, texture->alloc_height / 2);
691 		texture->alloc_width = MAX(1, texture->alloc_width / 2);
692 
693 		if (texture->alloc_width == img->get_width() / 2 && texture->alloc_height == img->get_height() / 2) {
694 
695 			img->shrink_x2();
696 		} else if (img->get_format() <= Image::FORMAT_RGBA8) {
697 
698 			img->resize(texture->alloc_width, texture->alloc_height, Image::INTERPOLATE_BILINEAR);
699 		}
700 	}
701 
702 	GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_layer] : GL_TEXTURE_2D;
703 
704 	texture->data_size = img->get_data().size();
705 	PoolVector<uint8_t>::Read read = img->get_data().read();
706 	ERR_FAIL_COND(!read.ptr());
707 
708 	glActiveTexture(GL_TEXTURE0);
709 	glBindTexture(texture->target, texture->tex_id);
710 
711 	texture->ignore_mipmaps = compressed && !img->has_mipmaps();
712 
713 	if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && !texture->ignore_mipmaps)
714 		if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
715 			glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR);
716 		} else {
717 			glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_LINEAR);
718 		}
719 	else {
720 		if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
721 			glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
722 		} else {
723 			glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
724 		}
725 	}
726 
727 	if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
728 
729 		glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
730 
731 	} else {
732 
733 		glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
734 	}
735 
736 	if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
737 
738 		if (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT) {
739 			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
740 			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
741 		} else {
742 			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
743 			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
744 		}
745 	} else {
746 
747 		//glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
748 		glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
749 		glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
750 	}
751 
752 	int mipmaps = ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && img->has_mipmaps()) ? img->get_mipmap_count() + 1 : 1;
753 
754 	int w = img->get_width();
755 	int h = img->get_height();
756 
757 	int tsize = 0;
758 
759 	for (int i = 0; i < mipmaps; i++) {
760 
761 		int size, ofs;
762 		img->get_mipmap_offset_and_size(i, ofs, size);
763 
764 		if (compressed) {
765 			glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
766 
767 			int bw = w;
768 			int bh = h;
769 
770 			glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]);
771 		} else {
772 
773 			glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
774 			if (texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
775 				glTexSubImage2D(blit_target, i, 0, 0, w, h, format, type, &read[ofs]);
776 			} else {
777 				glTexImage2D(blit_target, i, internal_format, w, h, 0, format, type, &read[ofs]);
778 			}
779 		}
780 
781 		tsize += size;
782 
783 		w = MAX(1, w >> 1);
784 		h = MAX(1, h >> 1);
785 	}
786 
787 	info.texture_mem -= texture->total_data_size;
788 	texture->total_data_size = tsize;
789 	info.texture_mem += texture->total_data_size;
790 
791 	// printf("texture: %i x %i - size: %i - total: %i\n", texture->width, texture->height, tsize, info.texture_mem);
792 
793 	texture->stored_cube_sides |= (1 << p_layer);
794 
795 	if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && mipmaps == 1 && !texture->ignore_mipmaps && (texture->type != VS::TEXTURE_TYPE_CUBEMAP || texture->stored_cube_sides == (1 << 6) - 1)) {
796 		//generate mipmaps if they were requested and the image does not contain them
797 		glGenerateMipmap(texture->target);
798 	}
799 
800 	texture->mipmaps = mipmaps;
801 }
802 
texture_set_data_partial(RID p_texture,const Ref<Image> & p_image,int src_x,int src_y,int src_w,int src_h,int dst_x,int dst_y,int p_dst_mip,int p_layer)803 void RasterizerStorageGLES2::texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer) {
804 	// TODO
805 	ERR_PRINT("Not implemented (ask Karroffel to do it :p)");
806 }
807 
texture_get_data(RID p_texture,int p_layer) const808 Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, int p_layer) const {
809 
810 	Texture *texture = texture_owner.getornull(p_texture);
811 
812 	ERR_FAIL_COND_V(!texture, Ref<Image>());
813 	ERR_FAIL_COND_V(!texture->active, Ref<Image>());
814 	ERR_FAIL_COND_V(texture->data_size == 0 && !texture->render_target, Ref<Image>());
815 
816 	if (texture->type == VS::TEXTURE_TYPE_CUBEMAP && p_layer < 6 && p_layer >= 0 && !texture->images[p_layer].is_null()) {
817 		return texture->images[p_layer];
818 	}
819 
820 #ifdef GLES_OVER_GL
821 
822 	Image::Format real_format;
823 	GLenum gl_format;
824 	GLenum gl_internal_format;
825 	GLenum gl_type;
826 	bool compressed;
827 	_get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed, false);
828 
829 	PoolVector<uint8_t> data;
830 
831 	int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, texture->mipmaps > 1);
832 
833 	data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
834 	PoolVector<uint8_t>::Write wb = data.write();
835 
836 	glActiveTexture(GL_TEXTURE0);
837 
838 	glBindTexture(texture->target, texture->tex_id);
839 
840 	glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
841 
842 	for (int i = 0; i < texture->mipmaps; i++) {
843 
844 		int ofs = Image::get_image_mipmap_offset(texture->alloc_width, texture->alloc_height, real_format, i);
845 
846 		if (texture->compressed) {
847 			glPixelStorei(GL_PACK_ALIGNMENT, 4);
848 			glGetCompressedTexImage(texture->target, i, &wb[ofs]);
849 		} else {
850 			glPixelStorei(GL_PACK_ALIGNMENT, 1);
851 			glGetTexImage(texture->target, i, texture->gl_format_cache, texture->gl_type_cache, &wb[ofs]);
852 		}
853 	}
854 
855 	wb.release();
856 
857 	data.resize(data_size);
858 
859 	Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, texture->mipmaps > 1, real_format, data));
860 
861 	return Ref<Image>(img);
862 #else
863 
864 	Image::Format real_format;
865 	GLenum gl_format;
866 	GLenum gl_internal_format;
867 	GLenum gl_type;
868 	bool compressed;
869 	_get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed, texture->resize_to_po2);
870 
871 	PoolVector<uint8_t> data;
872 
873 	int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false);
874 
875 	data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
876 	PoolVector<uint8_t>::Write wb = data.write();
877 
878 	GLuint temp_framebuffer;
879 	glGenFramebuffers(1, &temp_framebuffer);
880 
881 	GLuint temp_color_texture;
882 	glGenTextures(1, &temp_color_texture);
883 
884 	glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer);
885 
886 	glBindTexture(GL_TEXTURE_2D, temp_color_texture);
887 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
888 
889 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
890 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
891 	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0);
892 
893 	glDepthMask(GL_FALSE);
894 	glDisable(GL_DEPTH_TEST);
895 	glDisable(GL_CULL_FACE);
896 	glDisable(GL_BLEND);
897 	glDepthFunc(GL_LEQUAL);
898 	glColorMask(1, 1, 1, 1);
899 	glActiveTexture(GL_TEXTURE0);
900 	glBindTexture(GL_TEXTURE_2D, texture->tex_id);
901 
902 	glViewport(0, 0, texture->alloc_width, texture->alloc_height);
903 
904 	shaders.copy.bind();
905 
906 	glClearColor(0.0, 0.0, 0.0, 0.0);
907 	glClear(GL_COLOR_BUFFER_BIT);
908 	bind_quad_array();
909 	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
910 	glBindBuffer(GL_ARRAY_BUFFER, 0);
911 
912 	glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &wb[0]);
913 
914 	glDeleteTextures(1, &temp_color_texture);
915 
916 	glBindFramebuffer(GL_FRAMEBUFFER, 0);
917 	glDeleteFramebuffers(1, &temp_framebuffer);
918 
919 	wb.release();
920 
921 	data.resize(data_size);
922 
923 	Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, false, Image::FORMAT_RGBA8, data));
924 	if (!texture->compressed) {
925 		img->convert(real_format);
926 	}
927 
928 	return Ref<Image>(img);
929 
930 #endif
931 }
932 
texture_set_flags(RID p_texture,uint32_t p_flags)933 void RasterizerStorageGLES2::texture_set_flags(RID p_texture, uint32_t p_flags) {
934 
935 	Texture *texture = texture_owner.getornull(p_texture);
936 	ERR_FAIL_COND(!texture);
937 
938 	bool had_mipmaps = texture->flags & VS::TEXTURE_FLAG_MIPMAPS;
939 
940 	texture->flags = p_flags;
941 
942 	glActiveTexture(GL_TEXTURE0);
943 	glBindTexture(texture->target, texture->tex_id);
944 
945 	if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
946 
947 		if (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT) {
948 			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
949 			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
950 		} else {
951 			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
952 			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
953 		}
954 	} else {
955 		//glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
956 		glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
957 		glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
958 	}
959 
960 	if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && !texture->ignore_mipmaps) {
961 		if (!had_mipmaps && texture->mipmaps == 1) {
962 			glGenerateMipmap(texture->target);
963 		}
964 		if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
965 			glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR);
966 		} else {
967 			glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_LINEAR);
968 		}
969 
970 	} else {
971 		if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
972 			glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
973 		} else {
974 			glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
975 		}
976 	}
977 
978 	if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
979 
980 		glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
981 
982 	} else {
983 
984 		glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
985 	}
986 }
987 
texture_get_flags(RID p_texture) const988 uint32_t RasterizerStorageGLES2::texture_get_flags(RID p_texture) const {
989 	Texture *texture = texture_owner.getornull(p_texture);
990 
991 	ERR_FAIL_COND_V(!texture, 0);
992 
993 	return texture->flags;
994 }
995 
texture_get_format(RID p_texture) const996 Image::Format RasterizerStorageGLES2::texture_get_format(RID p_texture) const {
997 	Texture *texture = texture_owner.getornull(p_texture);
998 
999 	ERR_FAIL_COND_V(!texture, Image::FORMAT_L8);
1000 
1001 	return texture->format;
1002 }
1003 
texture_get_type(RID p_texture) const1004 VisualServer::TextureType RasterizerStorageGLES2::texture_get_type(RID p_texture) const {
1005 	Texture *texture = texture_owner.getornull(p_texture);
1006 
1007 	ERR_FAIL_COND_V(!texture, VS::TEXTURE_TYPE_2D);
1008 
1009 	return texture->type;
1010 }
1011 
texture_get_texid(RID p_texture) const1012 uint32_t RasterizerStorageGLES2::texture_get_texid(RID p_texture) const {
1013 	Texture *texture = texture_owner.getornull(p_texture);
1014 
1015 	ERR_FAIL_COND_V(!texture, 0);
1016 
1017 	return texture->tex_id;
1018 }
1019 
texture_bind(RID p_texture,uint32_t p_texture_no)1020 void RasterizerStorageGLES2::texture_bind(RID p_texture, uint32_t p_texture_no) {
1021 	Texture *texture = texture_owner.getornull(p_texture);
1022 
1023 	ERR_FAIL_COND(!texture);
1024 
1025 	glActiveTexture(GL_TEXTURE0 + p_texture_no);
1026 	glBindTexture(texture->target, texture->tex_id);
1027 }
1028 
texture_get_width(RID p_texture) const1029 uint32_t RasterizerStorageGLES2::texture_get_width(RID p_texture) const {
1030 	Texture *texture = texture_owner.getornull(p_texture);
1031 
1032 	ERR_FAIL_COND_V(!texture, 0);
1033 
1034 	return texture->width;
1035 }
1036 
texture_get_height(RID p_texture) const1037 uint32_t RasterizerStorageGLES2::texture_get_height(RID p_texture) const {
1038 	Texture *texture = texture_owner.getornull(p_texture);
1039 
1040 	ERR_FAIL_COND_V(!texture, 0);
1041 
1042 	return texture->height;
1043 }
1044 
texture_get_depth(RID p_texture) const1045 uint32_t RasterizerStorageGLES2::texture_get_depth(RID p_texture) const {
1046 	Texture *texture = texture_owner.getornull(p_texture);
1047 
1048 	ERR_FAIL_COND_V(!texture, 0);
1049 
1050 	return texture->depth;
1051 }
1052 
texture_set_size_override(RID p_texture,int p_width,int p_height,int p_depth)1053 void RasterizerStorageGLES2::texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth) {
1054 	Texture *texture = texture_owner.getornull(p_texture);
1055 
1056 	ERR_FAIL_COND(!texture);
1057 	ERR_FAIL_COND(texture->render_target);
1058 
1059 	ERR_FAIL_COND(p_width <= 0 || p_width > 16384);
1060 	ERR_FAIL_COND(p_height <= 0 || p_height > 16384);
1061 	//real texture size is in alloc width and height
1062 	texture->width = p_width;
1063 	texture->height = p_height;
1064 }
1065 
texture_set_path(RID p_texture,const String & p_path)1066 void RasterizerStorageGLES2::texture_set_path(RID p_texture, const String &p_path) {
1067 	Texture *texture = texture_owner.getornull(p_texture);
1068 	ERR_FAIL_COND(!texture);
1069 
1070 	texture->path = p_path;
1071 }
1072 
texture_get_path(RID p_texture) const1073 String RasterizerStorageGLES2::texture_get_path(RID p_texture) const {
1074 	Texture *texture = texture_owner.getornull(p_texture);
1075 	ERR_FAIL_COND_V(!texture, "");
1076 
1077 	return texture->path;
1078 }
1079 
texture_debug_usage(List<VS::TextureInfo> * r_info)1080 void RasterizerStorageGLES2::texture_debug_usage(List<VS::TextureInfo> *r_info) {
1081 	List<RID> textures;
1082 	texture_owner.get_owned_list(&textures);
1083 
1084 	for (List<RID>::Element *E = textures.front(); E; E = E->next()) {
1085 
1086 		Texture *t = texture_owner.getornull(E->get());
1087 		if (!t)
1088 			continue;
1089 		VS::TextureInfo tinfo;
1090 		tinfo.path = t->path;
1091 		tinfo.format = t->format;
1092 		tinfo.width = t->alloc_width;
1093 		tinfo.height = t->alloc_height;
1094 		tinfo.depth = 0;
1095 		tinfo.bytes = t->total_data_size;
1096 		r_info->push_back(tinfo);
1097 	}
1098 }
1099 
texture_set_shrink_all_x2_on_set_data(bool p_enable)1100 void RasterizerStorageGLES2::texture_set_shrink_all_x2_on_set_data(bool p_enable) {
1101 	config.shrink_textures_x2 = p_enable;
1102 }
1103 
textures_keep_original(bool p_enable)1104 void RasterizerStorageGLES2::textures_keep_original(bool p_enable) {
1105 	config.keep_original_textures = p_enable;
1106 }
1107 
texture_size_with_proxy(RID p_texture) const1108 Size2 RasterizerStorageGLES2::texture_size_with_proxy(RID p_texture) const {
1109 
1110 	const Texture *texture = texture_owner.getornull(p_texture);
1111 	ERR_FAIL_COND_V(!texture, Size2());
1112 	if (texture->proxy) {
1113 		return Size2(texture->proxy->width, texture->proxy->height);
1114 	} else {
1115 		return Size2(texture->width, texture->height);
1116 	}
1117 }
1118 
texture_set_proxy(RID p_texture,RID p_proxy)1119 void RasterizerStorageGLES2::texture_set_proxy(RID p_texture, RID p_proxy) {
1120 	Texture *texture = texture_owner.getornull(p_texture);
1121 	ERR_FAIL_COND(!texture);
1122 
1123 	if (texture->proxy) {
1124 		texture->proxy->proxy_owners.erase(texture);
1125 		texture->proxy = NULL;
1126 	}
1127 
1128 	if (p_proxy.is_valid()) {
1129 		Texture *proxy = texture_owner.get(p_proxy);
1130 		ERR_FAIL_COND(!proxy);
1131 		ERR_FAIL_COND(proxy == texture);
1132 		proxy->proxy_owners.insert(texture);
1133 		texture->proxy = proxy;
1134 	}
1135 }
1136 
texture_set_force_redraw_if_visible(RID p_texture,bool p_enable)1137 void RasterizerStorageGLES2::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
1138 
1139 	Texture *texture = texture_owner.getornull(p_texture);
1140 	ERR_FAIL_COND(!texture);
1141 
1142 	texture->redraw_if_visible = p_enable;
1143 }
1144 
texture_set_detect_3d_callback(RID p_texture,VisualServer::TextureDetectCallback p_callback,void * p_userdata)1145 void RasterizerStorageGLES2::texture_set_detect_3d_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
1146 	Texture *texture = texture_owner.get(p_texture);
1147 	ERR_FAIL_COND(!texture);
1148 
1149 	texture->detect_3d = p_callback;
1150 	texture->detect_3d_ud = p_userdata;
1151 }
1152 
texture_set_detect_srgb_callback(RID p_texture,VisualServer::TextureDetectCallback p_callback,void * p_userdata)1153 void RasterizerStorageGLES2::texture_set_detect_srgb_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
1154 	Texture *texture = texture_owner.get(p_texture);
1155 	ERR_FAIL_COND(!texture);
1156 
1157 	texture->detect_srgb = p_callback;
1158 	texture->detect_srgb_ud = p_userdata;
1159 }
1160 
texture_set_detect_normal_callback(RID p_texture,VisualServer::TextureDetectCallback p_callback,void * p_userdata)1161 void RasterizerStorageGLES2::texture_set_detect_normal_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
1162 	Texture *texture = texture_owner.get(p_texture);
1163 	ERR_FAIL_COND(!texture);
1164 
1165 	texture->detect_normal = p_callback;
1166 	texture->detect_normal_ud = p_userdata;
1167 }
1168 
texture_create_radiance_cubemap(RID p_source,int p_resolution) const1169 RID RasterizerStorageGLES2::texture_create_radiance_cubemap(RID p_source, int p_resolution) const {
1170 
1171 	return RID();
1172 }
1173 
sky_create()1174 RID RasterizerStorageGLES2::sky_create() {
1175 	Sky *sky = memnew(Sky);
1176 	sky->radiance = 0;
1177 	return sky_owner.make_rid(sky);
1178 }
1179 
sky_set_texture(RID p_sky,RID p_panorama,int p_radiance_size)1180 void RasterizerStorageGLES2::sky_set_texture(RID p_sky, RID p_panorama, int p_radiance_size) {
1181 	Sky *sky = sky_owner.getornull(p_sky);
1182 	ERR_FAIL_COND(!sky);
1183 
1184 	if (sky->panorama.is_valid()) {
1185 		sky->panorama = RID();
1186 		glDeleteTextures(1, &sky->radiance);
1187 		sky->radiance = 0;
1188 	}
1189 
1190 	sky->panorama = p_panorama;
1191 	if (!sky->panorama.is_valid()) {
1192 		return; // the panorama was cleared
1193 	}
1194 
1195 	Texture *texture = texture_owner.getornull(sky->panorama);
1196 	if (!texture) {
1197 		sky->panorama = RID();
1198 		ERR_FAIL_COND(!texture);
1199 	}
1200 
1201 	// glBindVertexArray(0) and more
1202 	{
1203 		glBindBuffer(GL_ARRAY_BUFFER, 0);
1204 		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1205 		glDisable(GL_CULL_FACE);
1206 		glDisable(GL_DEPTH_TEST);
1207 		glDisable(GL_SCISSOR_TEST);
1208 		glDisable(GL_BLEND);
1209 
1210 		for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
1211 			glDisableVertexAttribArray(i);
1212 		}
1213 	}
1214 
1215 	glActiveTexture(GL_TEXTURE0);
1216 	glBindTexture(texture->target, texture->tex_id);
1217 
1218 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1219 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1220 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1221 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //need this for proper sampling
1222 
1223 	glActiveTexture(GL_TEXTURE1);
1224 	glBindTexture(GL_TEXTURE_2D, resources.radical_inverse_vdc_cache_tex);
1225 
1226 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1227 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1228 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1229 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1230 
1231 	// New cubemap that will hold the mipmaps with different roughness values
1232 	glActiveTexture(GL_TEXTURE2);
1233 	glGenTextures(1, &sky->radiance);
1234 	glBindTexture(GL_TEXTURE_CUBE_MAP, sky->radiance);
1235 
1236 	int size = p_radiance_size / 2; //divide by two because its a cubemap (this is an approximation because GLES3 uses a dual paraboloid)
1237 
1238 	GLenum internal_format = GL_RGB;
1239 	GLenum format = GL_RGB;
1240 	GLenum type = GL_UNSIGNED_BYTE;
1241 
1242 	// Set the initial (empty) mipmaps
1243 	// Mobile hardware (PowerVR specially) prefers this approach,
1244 	// the previous approach with manual lod levels kills the game.
1245 	for (int i = 0; i < 6; i++) {
1246 		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, size, size, 0, format, type, NULL);
1247 	}
1248 
1249 	glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
1250 
1251 	// No filters for now
1252 	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1253 	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1254 	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1255 	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1256 
1257 	// Framebuffer
1258 
1259 	glBindFramebuffer(GL_FRAMEBUFFER, resources.mipmap_blur_fbo);
1260 
1261 	int mipmaps = 6;
1262 	int lod = 0;
1263 	int mm_level = mipmaps;
1264 	size = p_radiance_size / 2;
1265 	shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, true);
1266 	shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_DIRECT_WRITE, true);
1267 	shaders.cubemap_filter.bind();
1268 
1269 	// third, render to the framebuffer using separate textures, then copy to mipmaps
1270 	while (size >= 1) {
1271 
1272 		//make framebuffer size the texture size, need to use a separate texture for compatibility
1273 		glActiveTexture(GL_TEXTURE3);
1274 		glBindTexture(GL_TEXTURE_2D, resources.mipmap_blur_color);
1275 		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
1276 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, resources.mipmap_blur_color, 0);
1277 
1278 		if (lod == 1) {
1279 			//bind panorama for smaller lods
1280 
1281 			glActiveTexture(GL_TEXTURE0);
1282 			glBindTexture(GL_TEXTURE_CUBE_MAP, sky->radiance);
1283 			shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, false);
1284 			shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_DIRECT_WRITE, false);
1285 			shaders.cubemap_filter.bind();
1286 		}
1287 		glViewport(0, 0, size, size);
1288 		bind_quad_array();
1289 
1290 		glActiveTexture(GL_TEXTURE2); //back to panorama
1291 
1292 		for (int i = 0; i < 6; i++) {
1293 
1294 			shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::FACE_ID, i);
1295 
1296 			float roughness = mm_level >= 0 ? lod / (float)(mipmaps - 1) : 1;
1297 			roughness = MIN(1.0, roughness); //keep max at 1
1298 			shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::ROUGHNESS, roughness);
1299 			shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::Z_FLIP, false);
1300 
1301 			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1302 
1303 			glCopyTexSubImage2D(_cube_side_enum[i], lod, 0, 0, 0, 0, size, size);
1304 		}
1305 
1306 		size >>= 1;
1307 
1308 		mm_level--;
1309 
1310 		lod++;
1311 	}
1312 
1313 	shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, false);
1314 	shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_DIRECT_WRITE, false);
1315 
1316 	// restore ranges
1317 	glActiveTexture(GL_TEXTURE2); //back to panorama
1318 
1319 	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
1320 	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1321 	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1322 	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1323 
1324 	glBindTexture(GL_TEXTURE_2D, 0);
1325 	glActiveTexture(GL_TEXTURE3); //back to panorama
1326 	glBindTexture(GL_TEXTURE_2D, 0);
1327 	glActiveTexture(GL_TEXTURE1);
1328 	glBindTexture(GL_TEXTURE_2D, 0);
1329 	glActiveTexture(GL_TEXTURE0);
1330 	glBindTexture(GL_TEXTURE_2D, 0);
1331 
1332 	//reset flags on Sky Texture that may have changed
1333 	texture_set_flags(sky->panorama, texture->flags);
1334 
1335 	// Framebuffer did its job. thank mr framebuffer
1336 	glActiveTexture(GL_TEXTURE0); //back to panorama
1337 	glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
1338 }
1339 
1340 /* SHADER API */
1341 
shader_create()1342 RID RasterizerStorageGLES2::shader_create() {
1343 
1344 	Shader *shader = memnew(Shader);
1345 	shader->mode = VS::SHADER_SPATIAL;
1346 	shader->shader = &scene->state.scene_shader;
1347 	RID rid = shader_owner.make_rid(shader);
1348 	_shader_make_dirty(shader);
1349 	shader->self = rid;
1350 
1351 	return rid;
1352 }
1353 
_shader_make_dirty(Shader * p_shader)1354 void RasterizerStorageGLES2::_shader_make_dirty(Shader *p_shader) {
1355 	if (p_shader->dirty_list.in_list())
1356 		return;
1357 
1358 	_shader_dirty_list.add(&p_shader->dirty_list);
1359 }
1360 
shader_set_code(RID p_shader,const String & p_code)1361 void RasterizerStorageGLES2::shader_set_code(RID p_shader, const String &p_code) {
1362 
1363 	Shader *shader = shader_owner.getornull(p_shader);
1364 	ERR_FAIL_COND(!shader);
1365 
1366 	shader->code = p_code;
1367 
1368 	String mode_string = ShaderLanguage::get_shader_type(p_code);
1369 	VS::ShaderMode mode;
1370 
1371 	if (mode_string == "canvas_item")
1372 		mode = VS::SHADER_CANVAS_ITEM;
1373 	else if (mode_string == "particles")
1374 		mode = VS::SHADER_PARTICLES;
1375 	else
1376 		mode = VS::SHADER_SPATIAL;
1377 
1378 	if (shader->custom_code_id && mode != shader->mode) {
1379 		shader->shader->free_custom_shader(shader->custom_code_id);
1380 		shader->custom_code_id = 0;
1381 	}
1382 
1383 	shader->mode = mode;
1384 
1385 	// TODO handle all shader types
1386 	if (mode == VS::SHADER_CANVAS_ITEM) {
1387 		shader->shader = &canvas->state.canvas_shader;
1388 
1389 	} else if (mode == VS::SHADER_SPATIAL) {
1390 		shader->shader = &scene->state.scene_shader;
1391 	} else {
1392 		return;
1393 	}
1394 
1395 	if (shader->custom_code_id == 0) {
1396 		shader->custom_code_id = shader->shader->create_custom_shader();
1397 	}
1398 
1399 	_shader_make_dirty(shader);
1400 }
1401 
shader_get_code(RID p_shader) const1402 String RasterizerStorageGLES2::shader_get_code(RID p_shader) const {
1403 
1404 	const Shader *shader = shader_owner.get(p_shader);
1405 	ERR_FAIL_COND_V(!shader, "");
1406 
1407 	return shader->code;
1408 }
1409 
_update_shader(Shader * p_shader) const1410 void RasterizerStorageGLES2::_update_shader(Shader *p_shader) const {
1411 
1412 	_shader_dirty_list.remove(&p_shader->dirty_list);
1413 
1414 	p_shader->valid = false;
1415 
1416 	p_shader->uniforms.clear();
1417 
1418 	if (p_shader->code == String()) {
1419 		return; //just invalid, but no error
1420 	}
1421 
1422 	ShaderCompilerGLES2::GeneratedCode gen_code;
1423 	ShaderCompilerGLES2::IdentifierActions *actions = NULL;
1424 
1425 	switch (p_shader->mode) {
1426 
1427 		case VS::SHADER_CANVAS_ITEM: {
1428 
1429 			p_shader->canvas_item.light_mode = Shader::CanvasItem::LIGHT_MODE_NORMAL;
1430 			p_shader->canvas_item.blend_mode = Shader::CanvasItem::BLEND_MODE_MIX;
1431 
1432 			p_shader->canvas_item.uses_screen_texture = false;
1433 			p_shader->canvas_item.uses_screen_uv = false;
1434 			p_shader->canvas_item.uses_time = false;
1435 			p_shader->canvas_item.uses_modulate = false;
1436 			p_shader->canvas_item.uses_color = false;
1437 			p_shader->canvas_item.uses_vertex = false;
1438 			p_shader->canvas_item.batch_flags = 0;
1439 
1440 			shaders.actions_canvas.render_mode_values["blend_add"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_ADD);
1441 			shaders.actions_canvas.render_mode_values["blend_mix"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MIX);
1442 			shaders.actions_canvas.render_mode_values["blend_sub"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_SUB);
1443 			shaders.actions_canvas.render_mode_values["blend_mul"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MUL);
1444 			shaders.actions_canvas.render_mode_values["blend_premul_alpha"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_PMALPHA);
1445 
1446 			shaders.actions_canvas.render_mode_values["unshaded"] = Pair<int *, int>(&p_shader->canvas_item.light_mode, Shader::CanvasItem::LIGHT_MODE_UNSHADED);
1447 			shaders.actions_canvas.render_mode_values["light_only"] = Pair<int *, int>(&p_shader->canvas_item.light_mode, Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY);
1448 
1449 			shaders.actions_canvas.usage_flag_pointers["SCREEN_UV"] = &p_shader->canvas_item.uses_screen_uv;
1450 			shaders.actions_canvas.usage_flag_pointers["SCREEN_PIXEL_SIZE"] = &p_shader->canvas_item.uses_screen_uv;
1451 			shaders.actions_canvas.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->canvas_item.uses_screen_texture;
1452 			shaders.actions_canvas.usage_flag_pointers["TIME"] = &p_shader->canvas_item.uses_time;
1453 			shaders.actions_canvas.usage_flag_pointers["MODULATE"] = &p_shader->canvas_item.uses_modulate;
1454 			shaders.actions_canvas.usage_flag_pointers["COLOR"] = &p_shader->canvas_item.uses_color;
1455 			shaders.actions_canvas.usage_flag_pointers["VERTEX"] = &p_shader->canvas_item.uses_vertex;
1456 
1457 			actions = &shaders.actions_canvas;
1458 			actions->uniforms = &p_shader->uniforms;
1459 		} break;
1460 
1461 		case VS::SHADER_SPATIAL: {
1462 			p_shader->spatial.blend_mode = Shader::Spatial::BLEND_MODE_MIX;
1463 			p_shader->spatial.depth_draw_mode = Shader::Spatial::DEPTH_DRAW_OPAQUE;
1464 			p_shader->spatial.cull_mode = Shader::Spatial::CULL_MODE_BACK;
1465 			p_shader->spatial.uses_alpha = false;
1466 			p_shader->spatial.uses_alpha_scissor = false;
1467 			p_shader->spatial.uses_discard = false;
1468 			p_shader->spatial.unshaded = false;
1469 			p_shader->spatial.no_depth_test = false;
1470 			p_shader->spatial.uses_sss = false;
1471 			p_shader->spatial.uses_time = false;
1472 			p_shader->spatial.uses_vertex_lighting = false;
1473 			p_shader->spatial.uses_screen_texture = false;
1474 			p_shader->spatial.uses_depth_texture = false;
1475 			p_shader->spatial.uses_vertex = false;
1476 			p_shader->spatial.writes_modelview_or_projection = false;
1477 			p_shader->spatial.uses_world_coordinates = false;
1478 
1479 			shaders.actions_scene.render_mode_values["blend_add"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_ADD);
1480 			shaders.actions_scene.render_mode_values["blend_mix"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_MIX);
1481 			shaders.actions_scene.render_mode_values["blend_sub"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_SUB);
1482 			shaders.actions_scene.render_mode_values["blend_mul"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_MUL);
1483 
1484 			shaders.actions_scene.render_mode_values["depth_draw_opaque"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_OPAQUE);
1485 			shaders.actions_scene.render_mode_values["depth_draw_always"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_ALWAYS);
1486 			shaders.actions_scene.render_mode_values["depth_draw_never"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_NEVER);
1487 			shaders.actions_scene.render_mode_values["depth_draw_alpha_prepass"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS);
1488 
1489 			shaders.actions_scene.render_mode_values["cull_front"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_FRONT);
1490 			shaders.actions_scene.render_mode_values["cull_back"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_BACK);
1491 			shaders.actions_scene.render_mode_values["cull_disabled"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_DISABLED);
1492 
1493 			shaders.actions_scene.render_mode_flags["unshaded"] = &p_shader->spatial.unshaded;
1494 			shaders.actions_scene.render_mode_flags["depth_test_disable"] = &p_shader->spatial.no_depth_test;
1495 
1496 			shaders.actions_scene.render_mode_flags["vertex_lighting"] = &p_shader->spatial.uses_vertex_lighting;
1497 
1498 			shaders.actions_scene.render_mode_flags["world_vertex_coords"] = &p_shader->spatial.uses_world_coordinates;
1499 
1500 			shaders.actions_scene.usage_flag_pointers["ALPHA"] = &p_shader->spatial.uses_alpha;
1501 			shaders.actions_scene.usage_flag_pointers["ALPHA_SCISSOR"] = &p_shader->spatial.uses_alpha_scissor;
1502 
1503 			shaders.actions_scene.usage_flag_pointers["SSS_STRENGTH"] = &p_shader->spatial.uses_sss;
1504 			shaders.actions_scene.usage_flag_pointers["DISCARD"] = &p_shader->spatial.uses_discard;
1505 			shaders.actions_scene.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->spatial.uses_screen_texture;
1506 			shaders.actions_scene.usage_flag_pointers["DEPTH_TEXTURE"] = &p_shader->spatial.uses_depth_texture;
1507 			shaders.actions_scene.usage_flag_pointers["TIME"] = &p_shader->spatial.uses_time;
1508 
1509 			shaders.actions_scene.write_flag_pointers["MODELVIEW_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection;
1510 			shaders.actions_scene.write_flag_pointers["PROJECTION_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection;
1511 			shaders.actions_scene.write_flag_pointers["VERTEX"] = &p_shader->spatial.uses_vertex;
1512 
1513 			actions = &shaders.actions_scene;
1514 			actions->uniforms = &p_shader->uniforms;
1515 
1516 			if (p_shader->spatial.uses_screen_texture && p_shader->spatial.uses_depth_texture) {
1517 				ERR_PRINT_ONCE("Using both SCREEN_TEXTURE and DEPTH_TEXTURE is not supported in GLES2");
1518 			}
1519 
1520 			if (p_shader->spatial.uses_depth_texture && !config.support_depth_texture) {
1521 				ERR_PRINT_ONCE("Using DEPTH_TEXTURE is not permitted on this hardware, operation will fail.");
1522 			}
1523 		} break;
1524 
1525 		default: {
1526 			return;
1527 		} break;
1528 	}
1529 
1530 	Error err = shaders.compiler.compile(p_shader->mode, p_shader->code, actions, p_shader->path, gen_code);
1531 	if (err != OK) {
1532 		return;
1533 	}
1534 
1535 	p_shader->shader->set_custom_shader_code(p_shader->custom_code_id, gen_code.vertex, gen_code.vertex_global, gen_code.fragment, gen_code.light, gen_code.fragment_global, gen_code.uniforms, gen_code.texture_uniforms, gen_code.custom_defines);
1536 
1537 	p_shader->texture_count = gen_code.texture_uniforms.size();
1538 	p_shader->texture_hints = gen_code.texture_hints;
1539 
1540 	p_shader->uses_vertex_time = gen_code.uses_vertex_time;
1541 	p_shader->uses_fragment_time = gen_code.uses_fragment_time;
1542 
1543 	// some logic for batching
1544 	if (p_shader->mode == VS::SHADER_CANVAS_ITEM) {
1545 		if (p_shader->canvas_item.uses_modulate | p_shader->canvas_item.uses_color) {
1546 			p_shader->canvas_item.batch_flags |= Shader::CanvasItem::PREVENT_COLOR_BAKING;
1547 		}
1548 		if (p_shader->canvas_item.uses_vertex) {
1549 			p_shader->canvas_item.batch_flags |= Shader::CanvasItem::PREVENT_VERTEX_BAKING;
1550 		}
1551 	}
1552 
1553 	p_shader->shader->set_custom_shader(p_shader->custom_code_id);
1554 	p_shader->shader->bind();
1555 
1556 	// cache uniform locations
1557 
1558 	for (SelfList<Material> *E = p_shader->materials.first(); E; E = E->next()) {
1559 		_material_make_dirty(E->self());
1560 	}
1561 
1562 	p_shader->valid = true;
1563 	p_shader->version++;
1564 }
1565 
update_dirty_shaders()1566 void RasterizerStorageGLES2::update_dirty_shaders() {
1567 	while (_shader_dirty_list.first()) {
1568 		_update_shader(_shader_dirty_list.first()->self());
1569 	}
1570 }
1571 
shader_get_param_list(RID p_shader,List<PropertyInfo> * p_param_list) const1572 void RasterizerStorageGLES2::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
1573 
1574 	Shader *shader = shader_owner.get(p_shader);
1575 	ERR_FAIL_COND(!shader);
1576 
1577 	if (shader->dirty_list.in_list()) {
1578 		_update_shader(shader);
1579 	}
1580 
1581 	Map<int, StringName> order;
1582 
1583 	for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = shader->uniforms.front(); E; E = E->next()) {
1584 
1585 		if (E->get().texture_order >= 0) {
1586 			order[E->get().texture_order + 100000] = E->key();
1587 		} else {
1588 			order[E->get().order] = E->key();
1589 		}
1590 	}
1591 
1592 	for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
1593 
1594 		PropertyInfo pi;
1595 		ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[E->get()];
1596 
1597 		pi.name = E->get();
1598 
1599 		switch (u.type) {
1600 			case ShaderLanguage::TYPE_VOID: {
1601 				pi.type = Variant::NIL;
1602 			} break;
1603 
1604 			case ShaderLanguage::TYPE_BOOL: {
1605 				pi.type = Variant::BOOL;
1606 			} break;
1607 
1608 			// bool vectors
1609 			case ShaderLanguage::TYPE_BVEC2: {
1610 				pi.type = Variant::INT;
1611 				pi.hint = PROPERTY_HINT_FLAGS;
1612 				pi.hint_string = "x,y";
1613 			} break;
1614 			case ShaderLanguage::TYPE_BVEC3: {
1615 				pi.type = Variant::INT;
1616 				pi.hint = PROPERTY_HINT_FLAGS;
1617 				pi.hint_string = "x,y,z";
1618 			} break;
1619 			case ShaderLanguage::TYPE_BVEC4: {
1620 				pi.type = Variant::INT;
1621 				pi.hint = PROPERTY_HINT_FLAGS;
1622 				pi.hint_string = "x,y,z,w";
1623 			} break;
1624 
1625 				// int stuff
1626 			case ShaderLanguage::TYPE_UINT:
1627 			case ShaderLanguage::TYPE_INT: {
1628 				pi.type = Variant::INT;
1629 
1630 				if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
1631 					pi.hint = PROPERTY_HINT_RANGE;
1632 					pi.hint_string = rtos(u.hint_range[0]) + "," + rtos(u.hint_range[1]) + "," + rtos(u.hint_range[2]);
1633 				}
1634 			} break;
1635 
1636 			case ShaderLanguage::TYPE_IVEC2:
1637 			case ShaderLanguage::TYPE_UVEC2:
1638 			case ShaderLanguage::TYPE_IVEC3:
1639 			case ShaderLanguage::TYPE_UVEC3:
1640 			case ShaderLanguage::TYPE_IVEC4:
1641 			case ShaderLanguage::TYPE_UVEC4: {
1642 				pi.type = Variant::POOL_INT_ARRAY;
1643 			} break;
1644 
1645 			case ShaderLanguage::TYPE_FLOAT: {
1646 				pi.type = Variant::REAL;
1647 				if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
1648 					pi.hint = PROPERTY_HINT_RANGE;
1649 					pi.hint_string = rtos(u.hint_range[0]) + "," + rtos(u.hint_range[1]) + "," + rtos(u.hint_range[2]);
1650 				}
1651 			} break;
1652 
1653 			case ShaderLanguage::TYPE_VEC2: {
1654 				pi.type = Variant::VECTOR2;
1655 			} break;
1656 			case ShaderLanguage::TYPE_VEC3: {
1657 				pi.type = Variant::VECTOR3;
1658 			} break;
1659 
1660 			case ShaderLanguage::TYPE_VEC4: {
1661 				if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
1662 					pi.type = Variant::COLOR;
1663 				} else {
1664 					pi.type = Variant::PLANE;
1665 				}
1666 			} break;
1667 
1668 			case ShaderLanguage::TYPE_MAT2: {
1669 				pi.type = Variant::TRANSFORM2D;
1670 			} break;
1671 
1672 			case ShaderLanguage::TYPE_MAT3: {
1673 				pi.type = Variant::BASIS;
1674 			} break;
1675 
1676 			case ShaderLanguage::TYPE_MAT4: {
1677 				pi.type = Variant::TRANSFORM;
1678 			} break;
1679 
1680 			case ShaderLanguage::TYPE_SAMPLER2D:
1681 			case ShaderLanguage::TYPE_SAMPLEREXT:
1682 			case ShaderLanguage::TYPE_ISAMPLER2D:
1683 			case ShaderLanguage::TYPE_USAMPLER2D: {
1684 				pi.type = Variant::OBJECT;
1685 				pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
1686 				pi.hint_string = "Texture";
1687 			} break;
1688 
1689 			case ShaderLanguage::TYPE_SAMPLERCUBE: {
1690 				pi.type = Variant::OBJECT;
1691 				pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
1692 				pi.hint_string = "CubeMap";
1693 			} break;
1694 
1695 			case ShaderLanguage::TYPE_SAMPLER2DARRAY:
1696 			case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
1697 			case ShaderLanguage::TYPE_USAMPLER2DARRAY:
1698 			case ShaderLanguage::TYPE_SAMPLER3D:
1699 			case ShaderLanguage::TYPE_ISAMPLER3D:
1700 			case ShaderLanguage::TYPE_USAMPLER3D: {
1701 				// Not implemented in GLES2
1702 			} break;
1703 		}
1704 
1705 		p_param_list->push_back(pi);
1706 	}
1707 }
1708 
shader_set_default_texture_param(RID p_shader,const StringName & p_name,RID p_texture)1709 void RasterizerStorageGLES2::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) {
1710 
1711 	Shader *shader = shader_owner.get(p_shader);
1712 	ERR_FAIL_COND(!shader);
1713 	ERR_FAIL_COND(p_texture.is_valid() && !texture_owner.owns(p_texture));
1714 
1715 	if (p_texture.is_valid()) {
1716 		shader->default_textures[p_name] = p_texture;
1717 	} else {
1718 		shader->default_textures.erase(p_name);
1719 	}
1720 
1721 	_shader_make_dirty(shader);
1722 }
1723 
shader_get_default_texture_param(RID p_shader,const StringName & p_name) const1724 RID RasterizerStorageGLES2::shader_get_default_texture_param(RID p_shader, const StringName &p_name) const {
1725 
1726 	const Shader *shader = shader_owner.get(p_shader);
1727 	ERR_FAIL_COND_V(!shader, RID());
1728 
1729 	const Map<StringName, RID>::Element *E = shader->default_textures.find(p_name);
1730 
1731 	if (!E) {
1732 		return RID();
1733 	}
1734 
1735 	return E->get();
1736 }
1737 
shader_add_custom_define(RID p_shader,const String & p_define)1738 void RasterizerStorageGLES2::shader_add_custom_define(RID p_shader, const String &p_define) {
1739 
1740 	Shader *shader = shader_owner.get(p_shader);
1741 	ERR_FAIL_COND(!shader);
1742 
1743 	shader->shader->add_custom_define(p_define);
1744 
1745 	_shader_make_dirty(shader);
1746 }
1747 
shader_get_custom_defines(RID p_shader,Vector<String> * p_defines) const1748 void RasterizerStorageGLES2::shader_get_custom_defines(RID p_shader, Vector<String> *p_defines) const {
1749 
1750 	Shader *shader = shader_owner.get(p_shader);
1751 	ERR_FAIL_COND(!shader);
1752 
1753 	shader->shader->get_custom_defines(p_defines);
1754 }
1755 
shader_remove_custom_define(RID p_shader,const String & p_define)1756 void RasterizerStorageGLES2::shader_remove_custom_define(RID p_shader, const String &p_define) {
1757 
1758 	Shader *shader = shader_owner.get(p_shader);
1759 	ERR_FAIL_COND(!shader);
1760 
1761 	shader->shader->remove_custom_define(p_define);
1762 
1763 	_shader_make_dirty(shader);
1764 }
1765 
1766 /* COMMON MATERIAL API */
1767 
_material_make_dirty(Material * p_material) const1768 void RasterizerStorageGLES2::_material_make_dirty(Material *p_material) const {
1769 
1770 	if (p_material->dirty_list.in_list())
1771 		return;
1772 
1773 	_material_dirty_list.add(&p_material->dirty_list);
1774 }
1775 
material_create()1776 RID RasterizerStorageGLES2::material_create() {
1777 
1778 	Material *material = memnew(Material);
1779 
1780 	return material_owner.make_rid(material);
1781 }
1782 
material_set_shader(RID p_material,RID p_shader)1783 void RasterizerStorageGLES2::material_set_shader(RID p_material, RID p_shader) {
1784 
1785 	Material *material = material_owner.get(p_material);
1786 	ERR_FAIL_COND(!material);
1787 
1788 	Shader *shader = shader_owner.getornull(p_shader);
1789 
1790 	if (material->shader) {
1791 		// if a shader is present, remove the old shader
1792 		material->shader->materials.remove(&material->list);
1793 	}
1794 
1795 	material->shader = shader;
1796 
1797 	if (shader) {
1798 		shader->materials.add(&material->list);
1799 	}
1800 
1801 	_material_make_dirty(material);
1802 }
1803 
material_get_shader(RID p_material) const1804 RID RasterizerStorageGLES2::material_get_shader(RID p_material) const {
1805 
1806 	const Material *material = material_owner.get(p_material);
1807 	ERR_FAIL_COND_V(!material, RID());
1808 
1809 	if (material->shader) {
1810 		return material->shader->self;
1811 	}
1812 
1813 	return RID();
1814 }
1815 
material_set_param(RID p_material,const StringName & p_param,const Variant & p_value)1816 void RasterizerStorageGLES2::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
1817 
1818 	Material *material = material_owner.get(p_material);
1819 	ERR_FAIL_COND(!material);
1820 
1821 	if (p_value.get_type() == Variant::NIL) {
1822 		material->params.erase(p_param);
1823 	} else {
1824 		material->params[p_param] = p_value;
1825 	}
1826 
1827 	_material_make_dirty(material);
1828 }
1829 
material_get_param(RID p_material,const StringName & p_param) const1830 Variant RasterizerStorageGLES2::material_get_param(RID p_material, const StringName &p_param) const {
1831 
1832 	const Material *material = material_owner.get(p_material);
1833 	ERR_FAIL_COND_V(!material, RID());
1834 
1835 	if (material->params.has(p_param)) {
1836 		return material->params[p_param];
1837 	}
1838 
1839 	return material_get_param_default(p_material, p_param);
1840 }
1841 
material_get_param_default(RID p_material,const StringName & p_param) const1842 Variant RasterizerStorageGLES2::material_get_param_default(RID p_material, const StringName &p_param) const {
1843 	const Material *material = material_owner.get(p_material);
1844 	ERR_FAIL_COND_V(!material, Variant());
1845 
1846 	if (material->shader) {
1847 		if (material->shader->uniforms.has(p_param)) {
1848 			ShaderLanguage::ShaderNode::Uniform uniform = material->shader->uniforms[p_param];
1849 			Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
1850 			return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
1851 		}
1852 	}
1853 	return Variant();
1854 }
1855 
material_set_line_width(RID p_material,float p_width)1856 void RasterizerStorageGLES2::material_set_line_width(RID p_material, float p_width) {
1857 	Material *material = material_owner.getornull(p_material);
1858 	ERR_FAIL_COND(!material);
1859 
1860 	material->line_width = p_width;
1861 }
1862 
material_set_next_pass(RID p_material,RID p_next_material)1863 void RasterizerStorageGLES2::material_set_next_pass(RID p_material, RID p_next_material) {
1864 	Material *material = material_owner.get(p_material);
1865 	ERR_FAIL_COND(!material);
1866 
1867 	material->next_pass = p_next_material;
1868 }
1869 
material_is_animated(RID p_material)1870 bool RasterizerStorageGLES2::material_is_animated(RID p_material) {
1871 	Material *material = material_owner.get(p_material);
1872 	ERR_FAIL_COND_V(!material, false);
1873 	if (material->dirty_list.in_list()) {
1874 		_update_material(material);
1875 	}
1876 
1877 	bool animated = material->is_animated_cache;
1878 	if (!animated && material->next_pass.is_valid()) {
1879 		animated = material_is_animated(material->next_pass);
1880 	}
1881 	return animated;
1882 }
1883 
material_casts_shadows(RID p_material)1884 bool RasterizerStorageGLES2::material_casts_shadows(RID p_material) {
1885 	Material *material = material_owner.get(p_material);
1886 	ERR_FAIL_COND_V(!material, false);
1887 	if (material->dirty_list.in_list()) {
1888 		_update_material(material);
1889 	}
1890 
1891 	bool casts_shadows = material->can_cast_shadow_cache;
1892 
1893 	if (!casts_shadows && material->next_pass.is_valid()) {
1894 		casts_shadows = material_casts_shadows(material->next_pass);
1895 	}
1896 
1897 	return casts_shadows;
1898 }
1899 
material_add_instance_owner(RID p_material,RasterizerScene::InstanceBase * p_instance)1900 void RasterizerStorageGLES2::material_add_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) {
1901 
1902 	Material *material = material_owner.getornull(p_material);
1903 	ERR_FAIL_COND(!material);
1904 
1905 	Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.find(p_instance);
1906 	if (E) {
1907 		E->get()++;
1908 	} else {
1909 		material->instance_owners[p_instance] = 1;
1910 	}
1911 }
1912 
material_remove_instance_owner(RID p_material,RasterizerScene::InstanceBase * p_instance)1913 void RasterizerStorageGLES2::material_remove_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) {
1914 
1915 	Material *material = material_owner.getornull(p_material);
1916 	ERR_FAIL_COND(!material);
1917 
1918 	Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.find(p_instance);
1919 	ERR_FAIL_COND(!E);
1920 
1921 	E->get()--;
1922 
1923 	if (E->get() == 0) {
1924 		material->instance_owners.erase(E);
1925 	}
1926 }
1927 
material_set_render_priority(RID p_material,int priority)1928 void RasterizerStorageGLES2::material_set_render_priority(RID p_material, int priority) {
1929 	ERR_FAIL_COND(priority < VS::MATERIAL_RENDER_PRIORITY_MIN);
1930 	ERR_FAIL_COND(priority > VS::MATERIAL_RENDER_PRIORITY_MAX);
1931 
1932 	Material *material = material_owner.get(p_material);
1933 	ERR_FAIL_COND(!material);
1934 
1935 	material->render_priority = priority;
1936 }
1937 
_update_material(Material * p_material)1938 void RasterizerStorageGLES2::_update_material(Material *p_material) {
1939 	if (p_material->dirty_list.in_list()) {
1940 		_material_dirty_list.remove(&p_material->dirty_list);
1941 	}
1942 
1943 	if (p_material->shader && p_material->shader->dirty_list.in_list()) {
1944 		_update_shader(p_material->shader);
1945 	}
1946 
1947 	if (p_material->shader && !p_material->shader->valid) {
1948 		return;
1949 	}
1950 
1951 	{
1952 		bool can_cast_shadow = false;
1953 		bool is_animated = false;
1954 
1955 		if (p_material->shader && p_material->shader->mode == VS::SHADER_SPATIAL) {
1956 
1957 			if (p_material->shader->spatial.blend_mode == Shader::Spatial::BLEND_MODE_MIX &&
1958 					(!p_material->shader->spatial.uses_alpha || p_material->shader->spatial.depth_draw_mode == Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) {
1959 				can_cast_shadow = true;
1960 			}
1961 
1962 			if (p_material->shader->spatial.uses_discard && p_material->shader->uses_fragment_time) {
1963 				is_animated = true;
1964 			}
1965 
1966 			if (p_material->shader->spatial.uses_vertex && p_material->shader->uses_vertex_time) {
1967 				is_animated = true;
1968 			}
1969 
1970 			if (can_cast_shadow != p_material->can_cast_shadow_cache || is_animated != p_material->is_animated_cache) {
1971 				p_material->can_cast_shadow_cache = can_cast_shadow;
1972 				p_material->is_animated_cache = is_animated;
1973 
1974 				for (Map<Geometry *, int>::Element *E = p_material->geometry_owners.front(); E; E = E->next()) {
1975 					E->key()->material_changed_notify();
1976 				}
1977 
1978 				for (Map<RasterizerScene::InstanceBase *, int>::Element *E = p_material->instance_owners.front(); E; E = E->next()) {
1979 					E->key()->base_changed(false, true);
1980 				}
1981 			}
1982 		}
1983 	}
1984 
1985 	// uniforms and other things will be set in the use_material method in ShaderGLES2
1986 
1987 	if (p_material->shader && p_material->shader->texture_count > 0) {
1988 
1989 		p_material->textures.resize(p_material->shader->texture_count);
1990 
1991 		for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = p_material->shader->uniforms.front(); E; E = E->next()) {
1992 			if (E->get().texture_order < 0)
1993 				continue; // not a texture, does not go here
1994 
1995 			RID texture;
1996 
1997 			Map<StringName, Variant>::Element *V = p_material->params.find(E->key());
1998 
1999 			if (V) {
2000 				texture = V->get();
2001 			}
2002 
2003 			if (!texture.is_valid()) {
2004 				Map<StringName, RID>::Element *W = p_material->shader->default_textures.find(E->key());
2005 
2006 				if (W) {
2007 					texture = W->get();
2008 				}
2009 			}
2010 
2011 			p_material->textures.write[E->get().texture_order] = Pair<StringName, RID>(E->key(), texture);
2012 		}
2013 	} else {
2014 		p_material->textures.clear();
2015 	}
2016 }
2017 
_material_add_geometry(RID p_material,Geometry * p_geometry)2018 void RasterizerStorageGLES2::_material_add_geometry(RID p_material, Geometry *p_geometry) {
2019 	Material *material = material_owner.getornull(p_material);
2020 	ERR_FAIL_COND(!material);
2021 
2022 	Map<Geometry *, int>::Element *I = material->geometry_owners.find(p_geometry);
2023 
2024 	if (I) {
2025 		I->get()++;
2026 	} else {
2027 		material->geometry_owners[p_geometry] = 1;
2028 	}
2029 }
2030 
_material_remove_geometry(RID p_material,Geometry * p_geometry)2031 void RasterizerStorageGLES2::_material_remove_geometry(RID p_material, Geometry *p_geometry) {
2032 
2033 	Material *material = material_owner.getornull(p_material);
2034 	ERR_FAIL_COND(!material);
2035 
2036 	Map<Geometry *, int>::Element *I = material->geometry_owners.find(p_geometry);
2037 	ERR_FAIL_COND(!I);
2038 
2039 	I->get()--;
2040 
2041 	if (I->get() == 0) {
2042 		material->geometry_owners.erase(I);
2043 	}
2044 }
2045 
update_dirty_materials()2046 void RasterizerStorageGLES2::update_dirty_materials() {
2047 	while (_material_dirty_list.first()) {
2048 
2049 		Material *material = _material_dirty_list.first()->self();
2050 		_update_material(material);
2051 	}
2052 }
2053 
2054 /* MESH API */
2055 
mesh_create()2056 RID RasterizerStorageGLES2::mesh_create() {
2057 
2058 	Mesh *mesh = memnew(Mesh);
2059 
2060 	return mesh_owner.make_rid(mesh);
2061 }
2062 
_unpack_half_floats(const PoolVector<uint8_t> & array,uint32_t & format,int p_vertices)2063 static PoolVector<uint8_t> _unpack_half_floats(const PoolVector<uint8_t> &array, uint32_t &format, int p_vertices) {
2064 
2065 	uint32_t p_format = format;
2066 
2067 	static int src_size[VS::ARRAY_MAX];
2068 	static int dst_size[VS::ARRAY_MAX];
2069 	static int to_convert[VS::ARRAY_MAX];
2070 
2071 	int src_stride = 0;
2072 	int dst_stride = 0;
2073 
2074 	for (int i = 0; i < VS::ARRAY_MAX; i++) {
2075 
2076 		to_convert[i] = 0;
2077 		if (!(p_format & (1 << i))) {
2078 			src_size[i] = 0;
2079 			dst_size[i] = 0;
2080 			continue;
2081 		}
2082 
2083 		switch (i) {
2084 
2085 			case VS::ARRAY_VERTEX: {
2086 
2087 				if (p_format & VS::ARRAY_COMPRESS_VERTEX) {
2088 
2089 					if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
2090 						src_size[i] = 4;
2091 						dst_size[i] = 8;
2092 						to_convert[i] = 2;
2093 					} else {
2094 						src_size[i] = 8;
2095 						dst_size[i] = 12;
2096 						to_convert[i] = 3;
2097 					}
2098 
2099 					format &= ~VS::ARRAY_COMPRESS_VERTEX;
2100 				} else {
2101 
2102 					if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
2103 						src_size[i] = 8;
2104 						dst_size[i] = 8;
2105 					} else {
2106 						src_size[i] = 12;
2107 						dst_size[i] = 12;
2108 					}
2109 				}
2110 
2111 			} break;
2112 			case VS::ARRAY_NORMAL: {
2113 
2114 				if (p_format & VS::ARRAY_COMPRESS_NORMAL) {
2115 					src_size[i] = 4;
2116 					dst_size[i] = 4;
2117 				} else {
2118 					src_size[i] = 12;
2119 					dst_size[i] = 12;
2120 				}
2121 
2122 			} break;
2123 			case VS::ARRAY_TANGENT: {
2124 
2125 				if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
2126 					src_size[i] = 4;
2127 					dst_size[i] = 4;
2128 				} else {
2129 					src_size[i] = 16;
2130 					dst_size[i] = 16;
2131 				}
2132 
2133 			} break;
2134 			case VS::ARRAY_COLOR: {
2135 
2136 				if (p_format & VS::ARRAY_COMPRESS_COLOR) {
2137 					src_size[i] = 4;
2138 					dst_size[i] = 4;
2139 				} else {
2140 					src_size[i] = 16;
2141 					dst_size[i] = 16;
2142 				}
2143 
2144 			} break;
2145 			case VS::ARRAY_TEX_UV: {
2146 
2147 				if (p_format & VS::ARRAY_COMPRESS_TEX_UV) {
2148 					src_size[i] = 4;
2149 					to_convert[i] = 2;
2150 					format &= ~VS::ARRAY_COMPRESS_TEX_UV;
2151 				} else {
2152 					src_size[i] = 8;
2153 				}
2154 
2155 				dst_size[i] = 8;
2156 
2157 			} break;
2158 			case VS::ARRAY_TEX_UV2: {
2159 
2160 				if (p_format & VS::ARRAY_COMPRESS_TEX_UV2) {
2161 					src_size[i] = 4;
2162 					to_convert[i] = 2;
2163 					format &= ~VS::ARRAY_COMPRESS_TEX_UV2;
2164 				} else {
2165 					src_size[i] = 8;
2166 				}
2167 
2168 				dst_size[i] = 8;
2169 
2170 			} break;
2171 			case VS::ARRAY_BONES: {
2172 
2173 				if (p_format & VS::ARRAY_FLAG_USE_16_BIT_BONES) {
2174 					src_size[i] = 8;
2175 					dst_size[i] = 8;
2176 				} else {
2177 					src_size[i] = 4;
2178 					dst_size[i] = 4;
2179 				}
2180 
2181 			} break;
2182 			case VS::ARRAY_WEIGHTS: {
2183 
2184 				if (p_format & VS::ARRAY_COMPRESS_WEIGHTS) {
2185 					src_size[i] = 8;
2186 					dst_size[i] = 8;
2187 				} else {
2188 					src_size[i] = 16;
2189 					dst_size[i] = 16;
2190 				}
2191 
2192 			} break;
2193 			case VS::ARRAY_INDEX: {
2194 
2195 				src_size[i] = 0;
2196 				dst_size[i] = 0;
2197 
2198 			} break;
2199 		}
2200 
2201 		src_stride += src_size[i];
2202 		dst_stride += dst_size[i];
2203 	}
2204 
2205 	PoolVector<uint8_t> ret;
2206 	ret.resize(p_vertices * dst_stride);
2207 
2208 	PoolVector<uint8_t>::Read r = array.read();
2209 	PoolVector<uint8_t>::Write w = ret.write();
2210 
2211 	int src_offset = 0;
2212 	int dst_offset = 0;
2213 
2214 	for (int i = 0; i < VS::ARRAY_MAX; i++) {
2215 
2216 		if (src_size[i] == 0) {
2217 			continue; //no go
2218 		}
2219 		const uint8_t *rptr = r.ptr();
2220 		uint8_t *wptr = w.ptr();
2221 		if (to_convert[i]) { //converting
2222 
2223 			for (int j = 0; j < p_vertices; j++) {
2224 				const uint16_t *src = (const uint16_t *)&rptr[src_stride * j + src_offset];
2225 				float *dst = (float *)&wptr[dst_stride * j + dst_offset];
2226 
2227 				for (int k = 0; k < to_convert[i]; k++) {
2228 
2229 					dst[k] = Math::half_to_float(src[k]);
2230 				}
2231 			}
2232 
2233 		} else {
2234 			//just copy
2235 			for (int j = 0; j < p_vertices; j++) {
2236 				for (int k = 0; k < src_size[i]; k++) {
2237 					wptr[dst_stride * j + dst_offset + k] = rptr[src_stride * j + src_offset + k];
2238 				}
2239 			}
2240 		}
2241 
2242 		src_offset += src_size[i];
2243 		dst_offset += dst_size[i];
2244 	}
2245 
2246 	r.release();
2247 	w.release();
2248 
2249 	return ret;
2250 }
2251 
mesh_add_surface(RID p_mesh,uint32_t p_format,VS::PrimitiveType p_primitive,const PoolVector<uint8_t> & p_array,int p_vertex_count,const PoolVector<uint8_t> & p_index_array,int p_index_count,const AABB & p_aabb,const Vector<PoolVector<uint8_t>> & p_blend_shapes,const Vector<AABB> & p_bone_aabbs)2252 void RasterizerStorageGLES2::mesh_add_surface(RID p_mesh, uint32_t p_format, VS::PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t> > &p_blend_shapes, const Vector<AABB> &p_bone_aabbs) {
2253 
2254 	Mesh *mesh = mesh_owner.getornull(p_mesh);
2255 	ERR_FAIL_COND(!mesh);
2256 
2257 	ERR_FAIL_COND(!(p_format & VS::ARRAY_FORMAT_VERTEX));
2258 
2259 	//must have index and bones, both.
2260 	{
2261 		uint32_t bones_weight = VS::ARRAY_FORMAT_BONES | VS::ARRAY_FORMAT_WEIGHTS;
2262 		ERR_FAIL_COND_MSG((p_format & bones_weight) && (p_format & bones_weight) != bones_weight, "Array must have both bones and weights in format or none.");
2263 	}
2264 
2265 	//bool has_morph = p_blend_shapes.size();
2266 
2267 	Surface::Attrib attribs[VS::ARRAY_MAX];
2268 
2269 	int stride = 0;
2270 	bool uses_half_float = false;
2271 
2272 	for (int i = 0; i < VS::ARRAY_MAX; i++) {
2273 
2274 		attribs[i].index = i;
2275 
2276 		if (!(p_format & (1 << i))) {
2277 			attribs[i].enabled = false;
2278 			attribs[i].integer = false;
2279 			continue;
2280 		}
2281 
2282 		attribs[i].enabled = true;
2283 		attribs[i].offset = stride;
2284 		attribs[i].integer = false;
2285 
2286 		switch (i) {
2287 
2288 			case VS::ARRAY_VERTEX: {
2289 
2290 				if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
2291 					attribs[i].size = 2;
2292 				} else {
2293 					attribs[i].size = (p_format & VS::ARRAY_COMPRESS_VERTEX) ? 4 : 3;
2294 				}
2295 
2296 				if (p_format & VS::ARRAY_COMPRESS_VERTEX) {
2297 					attribs[i].type = _GL_HALF_FLOAT_OES;
2298 					stride += attribs[i].size * 2;
2299 					uses_half_float = true;
2300 				} else {
2301 					attribs[i].type = GL_FLOAT;
2302 					stride += attribs[i].size * 4;
2303 				}
2304 
2305 				attribs[i].normalized = GL_FALSE;
2306 
2307 			} break;
2308 			case VS::ARRAY_NORMAL: {
2309 
2310 				attribs[i].size = 3;
2311 
2312 				if (p_format & VS::ARRAY_COMPRESS_NORMAL) {
2313 					attribs[i].type = GL_BYTE;
2314 					stride += 4; //pad extra byte
2315 					attribs[i].normalized = GL_TRUE;
2316 				} else {
2317 					attribs[i].type = GL_FLOAT;
2318 					stride += 12;
2319 					attribs[i].normalized = GL_FALSE;
2320 				}
2321 
2322 			} break;
2323 			case VS::ARRAY_TANGENT: {
2324 
2325 				attribs[i].size = 4;
2326 
2327 				if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
2328 					attribs[i].type = GL_BYTE;
2329 					stride += 4;
2330 					attribs[i].normalized = GL_TRUE;
2331 				} else {
2332 					attribs[i].type = GL_FLOAT;
2333 					stride += 16;
2334 					attribs[i].normalized = GL_FALSE;
2335 				}
2336 
2337 			} break;
2338 			case VS::ARRAY_COLOR: {
2339 
2340 				attribs[i].size = 4;
2341 
2342 				if (p_format & VS::ARRAY_COMPRESS_COLOR) {
2343 					attribs[i].type = GL_UNSIGNED_BYTE;
2344 					stride += 4;
2345 					attribs[i].normalized = GL_TRUE;
2346 				} else {
2347 					attribs[i].type = GL_FLOAT;
2348 					stride += 16;
2349 					attribs[i].normalized = GL_FALSE;
2350 				}
2351 
2352 			} break;
2353 			case VS::ARRAY_TEX_UV: {
2354 
2355 				attribs[i].size = 2;
2356 
2357 				if (p_format & VS::ARRAY_COMPRESS_TEX_UV) {
2358 					attribs[i].type = _GL_HALF_FLOAT_OES;
2359 					stride += 4;
2360 					uses_half_float = true;
2361 				} else {
2362 					attribs[i].type = GL_FLOAT;
2363 					stride += 8;
2364 				}
2365 
2366 				attribs[i].normalized = GL_FALSE;
2367 
2368 			} break;
2369 			case VS::ARRAY_TEX_UV2: {
2370 
2371 				attribs[i].size = 2;
2372 
2373 				if (p_format & VS::ARRAY_COMPRESS_TEX_UV2) {
2374 					attribs[i].type = _GL_HALF_FLOAT_OES;
2375 					stride += 4;
2376 					uses_half_float = true;
2377 				} else {
2378 					attribs[i].type = GL_FLOAT;
2379 					stride += 8;
2380 				}
2381 				attribs[i].normalized = GL_FALSE;
2382 
2383 			} break;
2384 			case VS::ARRAY_BONES: {
2385 
2386 				attribs[i].size = 4;
2387 
2388 				if (p_format & VS::ARRAY_FLAG_USE_16_BIT_BONES) {
2389 					attribs[i].type = GL_UNSIGNED_SHORT;
2390 					stride += 8;
2391 				} else {
2392 					attribs[i].type = GL_UNSIGNED_BYTE;
2393 					stride += 4;
2394 				}
2395 
2396 				attribs[i].normalized = GL_FALSE;
2397 				attribs[i].integer = true;
2398 
2399 			} break;
2400 			case VS::ARRAY_WEIGHTS: {
2401 
2402 				attribs[i].size = 4;
2403 
2404 				if (p_format & VS::ARRAY_COMPRESS_WEIGHTS) {
2405 
2406 					attribs[i].type = GL_UNSIGNED_SHORT;
2407 					stride += 8;
2408 					attribs[i].normalized = GL_TRUE;
2409 				} else {
2410 					attribs[i].type = GL_FLOAT;
2411 					stride += 16;
2412 					attribs[i].normalized = GL_FALSE;
2413 				}
2414 
2415 			} break;
2416 			case VS::ARRAY_INDEX: {
2417 
2418 				attribs[i].size = 1;
2419 
2420 				if (p_vertex_count >= (1 << 16)) {
2421 					attribs[i].type = GL_UNSIGNED_INT;
2422 					attribs[i].stride = 4;
2423 				} else {
2424 					attribs[i].type = GL_UNSIGNED_SHORT;
2425 					attribs[i].stride = 2;
2426 				}
2427 
2428 				attribs[i].normalized = GL_FALSE;
2429 
2430 			} break;
2431 		}
2432 	}
2433 
2434 	for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
2435 		attribs[i].stride = stride;
2436 	}
2437 
2438 	//validate sizes
2439 	PoolVector<uint8_t> array = p_array;
2440 
2441 	int array_size = stride * p_vertex_count;
2442 	int index_array_size = 0;
2443 	if (array.size() != array_size && array.size() + p_vertex_count * 2 == array_size) {
2444 		//old format, convert
2445 		array = PoolVector<uint8_t>();
2446 
2447 		array.resize(p_array.size() + p_vertex_count * 2);
2448 
2449 		PoolVector<uint8_t>::Write w = array.write();
2450 		PoolVector<uint8_t>::Read r = p_array.read();
2451 
2452 		uint16_t *w16 = (uint16_t *)w.ptr();
2453 		const uint16_t *r16 = (uint16_t *)r.ptr();
2454 
2455 		uint16_t one = Math::make_half_float(1);
2456 
2457 		for (int i = 0; i < p_vertex_count; i++) {
2458 
2459 			*w16++ = *r16++;
2460 			*w16++ = *r16++;
2461 			*w16++ = *r16++;
2462 			*w16++ = one;
2463 			for (int j = 0; j < (stride / 2) - 4; j++) {
2464 				*w16++ = *r16++;
2465 			}
2466 		}
2467 	}
2468 
2469 	ERR_FAIL_COND(array.size() != array_size);
2470 
2471 	if (!config.support_half_float_vertices && uses_half_float) {
2472 
2473 		uint32_t new_format = p_format;
2474 		PoolVector<uint8_t> unpacked_array = _unpack_half_floats(array, new_format, p_vertex_count);
2475 
2476 		mesh_add_surface(p_mesh, new_format, p_primitive, unpacked_array, p_vertex_count, p_index_array, p_index_count, p_aabb, p_blend_shapes, p_bone_aabbs);
2477 		return; //do not go any further, above function used unpacked stuff will be used instead.
2478 	}
2479 
2480 	if (p_format & VS::ARRAY_FORMAT_INDEX) {
2481 
2482 		index_array_size = attribs[VS::ARRAY_INDEX].stride * p_index_count;
2483 	}
2484 
2485 	ERR_FAIL_COND(p_index_array.size() != index_array_size);
2486 
2487 	ERR_FAIL_COND(p_blend_shapes.size() != mesh->blend_shape_count);
2488 
2489 	for (int i = 0; i < p_blend_shapes.size(); i++) {
2490 		ERR_FAIL_COND(p_blend_shapes[i].size() != array_size);
2491 	}
2492 
2493 	// all valid, create stuff
2494 
2495 	Surface *surface = memnew(Surface);
2496 
2497 	surface->active = true;
2498 	surface->array_len = p_vertex_count;
2499 	surface->index_array_len = p_index_count;
2500 	surface->array_byte_size = array.size();
2501 	surface->index_array_byte_size = p_index_array.size();
2502 	surface->primitive = p_primitive;
2503 	surface->mesh = mesh;
2504 	surface->format = p_format;
2505 	surface->skeleton_bone_aabb = p_bone_aabbs;
2506 	surface->skeleton_bone_used.resize(surface->skeleton_bone_aabb.size());
2507 
2508 	surface->aabb = p_aabb;
2509 	surface->max_bone = p_bone_aabbs.size();
2510 #ifdef TOOLS_ENABLED
2511 	surface->blend_shape_data = p_blend_shapes;
2512 	if (surface->blend_shape_data.size()) {
2513 		ERR_PRINT_ONCE("Blend shapes are not supported in OpenGL ES 2.0");
2514 	}
2515 #endif
2516 
2517 	surface->data = array;
2518 	surface->index_data = p_index_array;
2519 	surface->total_data_size += surface->array_byte_size + surface->index_array_byte_size;
2520 
2521 	for (int i = 0; i < surface->skeleton_bone_used.size(); i++) {
2522 		surface->skeleton_bone_used.write[i] = !(surface->skeleton_bone_aabb[i].size.x < 0 || surface->skeleton_bone_aabb[i].size.y < 0 || surface->skeleton_bone_aabb[i].size.z < 0);
2523 	}
2524 
2525 	for (int i = 0; i < VS::ARRAY_MAX; i++) {
2526 		surface->attribs[i] = attribs[i];
2527 	}
2528 
2529 	// Okay, now the OpenGL stuff, wheeeeey \o/
2530 	{
2531 		PoolVector<uint8_t>::Read vr = array.read();
2532 
2533 		glGenBuffers(1, &surface->vertex_id);
2534 		glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
2535 		glBufferData(GL_ARRAY_BUFFER, array_size, vr.ptr(), (p_format & VS::ARRAY_FLAG_USE_DYNAMIC_UPDATE) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
2536 
2537 		glBindBuffer(GL_ARRAY_BUFFER, 0);
2538 
2539 		if (p_format & VS::ARRAY_FORMAT_INDEX) {
2540 			PoolVector<uint8_t>::Read ir = p_index_array.read();
2541 
2542 			glGenBuffers(1, &surface->index_id);
2543 			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_id);
2544 			glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_array_size, ir.ptr(), GL_STATIC_DRAW);
2545 			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
2546 		} else {
2547 			surface->index_id = 0;
2548 		}
2549 
2550 		// TODO generate wireframes
2551 	}
2552 
2553 	{
2554 		// blend shapes
2555 
2556 		for (int i = 0; i < p_blend_shapes.size(); i++) {
2557 
2558 			Surface::BlendShape mt;
2559 
2560 			PoolVector<uint8_t>::Read vr = p_blend_shapes[i].read();
2561 
2562 			surface->total_data_size += array_size;
2563 
2564 			glGenBuffers(1, &mt.vertex_id);
2565 			glBindBuffer(GL_ARRAY_BUFFER, mt.vertex_id);
2566 			glBufferData(GL_ARRAY_BUFFER, array_size, vr.ptr(), GL_STATIC_DRAW);
2567 			glBindBuffer(GL_ARRAY_BUFFER, 0);
2568 
2569 			surface->blend_shapes.push_back(mt);
2570 		}
2571 	}
2572 
2573 	mesh->surfaces.push_back(surface);
2574 	mesh->instance_change_notify(true, true);
2575 
2576 	info.vertex_mem += surface->total_data_size;
2577 }
2578 
mesh_set_blend_shape_count(RID p_mesh,int p_amount)2579 void RasterizerStorageGLES2::mesh_set_blend_shape_count(RID p_mesh, int p_amount) {
2580 	Mesh *mesh = mesh_owner.getornull(p_mesh);
2581 	ERR_FAIL_COND(!mesh);
2582 
2583 	ERR_FAIL_COND(mesh->surfaces.size() != 0);
2584 	ERR_FAIL_COND(p_amount < 0);
2585 
2586 	mesh->blend_shape_count = p_amount;
2587 	mesh->instance_change_notify(true, false);
2588 }
2589 
mesh_get_blend_shape_count(RID p_mesh) const2590 int RasterizerStorageGLES2::mesh_get_blend_shape_count(RID p_mesh) const {
2591 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2592 	ERR_FAIL_COND_V(!mesh, 0);
2593 	return mesh->blend_shape_count;
2594 }
2595 
mesh_set_blend_shape_mode(RID p_mesh,VS::BlendShapeMode p_mode)2596 void RasterizerStorageGLES2::mesh_set_blend_shape_mode(RID p_mesh, VS::BlendShapeMode p_mode) {
2597 	Mesh *mesh = mesh_owner.getornull(p_mesh);
2598 	ERR_FAIL_COND(!mesh);
2599 
2600 	mesh->blend_shape_mode = p_mode;
2601 }
2602 
mesh_get_blend_shape_mode(RID p_mesh) const2603 VS::BlendShapeMode RasterizerStorageGLES2::mesh_get_blend_shape_mode(RID p_mesh) const {
2604 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2605 	ERR_FAIL_COND_V(!mesh, VS::BLEND_SHAPE_MODE_NORMALIZED);
2606 
2607 	return mesh->blend_shape_mode;
2608 }
2609 
mesh_surface_update_region(RID p_mesh,int p_surface,int p_offset,const PoolVector<uint8_t> & p_data)2610 void RasterizerStorageGLES2::mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const PoolVector<uint8_t> &p_data) {
2611 	Mesh *mesh = mesh_owner.getornull(p_mesh);
2612 
2613 	ERR_FAIL_COND(!mesh);
2614 	ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
2615 
2616 	int total_size = p_data.size();
2617 	ERR_FAIL_COND(p_offset + total_size > mesh->surfaces[p_surface]->array_byte_size);
2618 
2619 	PoolVector<uint8_t>::Read r = p_data.read();
2620 
2621 	glBindBuffer(GL_ARRAY_BUFFER, mesh->surfaces[p_surface]->vertex_id);
2622 	glBufferSubData(GL_ARRAY_BUFFER, p_offset, total_size, r.ptr());
2623 	glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
2624 }
2625 
mesh_surface_set_material(RID p_mesh,int p_surface,RID p_material)2626 void RasterizerStorageGLES2::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
2627 	Mesh *mesh = mesh_owner.getornull(p_mesh);
2628 	ERR_FAIL_COND(!mesh);
2629 	ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
2630 
2631 	if (mesh->surfaces[p_surface]->material == p_material)
2632 		return;
2633 
2634 	if (mesh->surfaces[p_surface]->material.is_valid()) {
2635 		_material_remove_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]);
2636 	}
2637 
2638 	mesh->surfaces[p_surface]->material = p_material;
2639 
2640 	if (mesh->surfaces[p_surface]->material.is_valid()) {
2641 		_material_add_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]);
2642 	}
2643 
2644 	mesh->instance_change_notify(false, true);
2645 }
2646 
mesh_surface_get_material(RID p_mesh,int p_surface) const2647 RID RasterizerStorageGLES2::mesh_surface_get_material(RID p_mesh, int p_surface) const {
2648 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2649 	ERR_FAIL_COND_V(!mesh, RID());
2650 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), RID());
2651 
2652 	return mesh->surfaces[p_surface]->material;
2653 }
2654 
mesh_surface_get_array_len(RID p_mesh,int p_surface) const2655 int RasterizerStorageGLES2::mesh_surface_get_array_len(RID p_mesh, int p_surface) const {
2656 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2657 	ERR_FAIL_COND_V(!mesh, 0);
2658 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
2659 
2660 	return mesh->surfaces[p_surface]->array_len;
2661 }
2662 
mesh_surface_get_array_index_len(RID p_mesh,int p_surface) const2663 int RasterizerStorageGLES2::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const {
2664 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2665 	ERR_FAIL_COND_V(!mesh, 0);
2666 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
2667 
2668 	return mesh->surfaces[p_surface]->index_array_len;
2669 }
2670 
mesh_surface_get_array(RID p_mesh,int p_surface) const2671 PoolVector<uint8_t> RasterizerStorageGLES2::mesh_surface_get_array(RID p_mesh, int p_surface) const {
2672 
2673 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2674 	ERR_FAIL_COND_V(!mesh, PoolVector<uint8_t>());
2675 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), PoolVector<uint8_t>());
2676 
2677 	Surface *surface = mesh->surfaces[p_surface];
2678 
2679 	return surface->data;
2680 }
2681 
mesh_surface_get_index_array(RID p_mesh,int p_surface) const2682 PoolVector<uint8_t> RasterizerStorageGLES2::mesh_surface_get_index_array(RID p_mesh, int p_surface) const {
2683 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2684 	ERR_FAIL_COND_V(!mesh, PoolVector<uint8_t>());
2685 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), PoolVector<uint8_t>());
2686 
2687 	Surface *surface = mesh->surfaces[p_surface];
2688 
2689 	return surface->index_data;
2690 }
2691 
mesh_surface_get_format(RID p_mesh,int p_surface) const2692 uint32_t RasterizerStorageGLES2::mesh_surface_get_format(RID p_mesh, int p_surface) const {
2693 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2694 
2695 	ERR_FAIL_COND_V(!mesh, 0);
2696 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
2697 
2698 	return mesh->surfaces[p_surface]->format;
2699 }
2700 
mesh_surface_get_primitive_type(RID p_mesh,int p_surface) const2701 VS::PrimitiveType RasterizerStorageGLES2::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const {
2702 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2703 	ERR_FAIL_COND_V(!mesh, VS::PRIMITIVE_MAX);
2704 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), VS::PRIMITIVE_MAX);
2705 
2706 	return mesh->surfaces[p_surface]->primitive;
2707 }
2708 
mesh_surface_get_aabb(RID p_mesh,int p_surface) const2709 AABB RasterizerStorageGLES2::mesh_surface_get_aabb(RID p_mesh, int p_surface) const {
2710 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2711 	ERR_FAIL_COND_V(!mesh, AABB());
2712 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), AABB());
2713 
2714 	return mesh->surfaces[p_surface]->aabb;
2715 }
2716 
mesh_surface_get_blend_shapes(RID p_mesh,int p_surface) const2717 Vector<PoolVector<uint8_t> > RasterizerStorageGLES2::mesh_surface_get_blend_shapes(RID p_mesh, int p_surface) const {
2718 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2719 	ERR_FAIL_COND_V(!mesh, Vector<PoolVector<uint8_t> >());
2720 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), Vector<PoolVector<uint8_t> >());
2721 #ifndef TOOLS_ENABLED
2722 	ERR_PRINT("OpenGL ES 2.0 does not allow retrieving blend shape data");
2723 #endif
2724 
2725 	return mesh->surfaces[p_surface]->blend_shape_data;
2726 }
mesh_surface_get_skeleton_aabb(RID p_mesh,int p_surface) const2727 Vector<AABB> RasterizerStorageGLES2::mesh_surface_get_skeleton_aabb(RID p_mesh, int p_surface) const {
2728 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2729 	ERR_FAIL_COND_V(!mesh, Vector<AABB>());
2730 	ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), Vector<AABB>());
2731 
2732 	return mesh->surfaces[p_surface]->skeleton_bone_aabb;
2733 }
2734 
mesh_remove_surface(RID p_mesh,int p_surface)2735 void RasterizerStorageGLES2::mesh_remove_surface(RID p_mesh, int p_surface) {
2736 
2737 	Mesh *mesh = mesh_owner.getornull(p_mesh);
2738 	ERR_FAIL_COND(!mesh);
2739 	ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
2740 
2741 	Surface *surface = mesh->surfaces[p_surface];
2742 
2743 	if (surface->material.is_valid()) {
2744 		_material_remove_geometry(surface->material, mesh->surfaces[p_surface]);
2745 	}
2746 
2747 	glDeleteBuffers(1, &surface->vertex_id);
2748 	if (surface->index_id) {
2749 		glDeleteBuffers(1, &surface->index_id);
2750 	}
2751 
2752 	for (int i = 0; i < surface->blend_shapes.size(); i++) {
2753 		glDeleteBuffers(1, &surface->blend_shapes[i].vertex_id);
2754 	}
2755 
2756 	info.vertex_mem -= surface->total_data_size;
2757 
2758 	memdelete(surface);
2759 
2760 	mesh->surfaces.remove(p_surface);
2761 
2762 	mesh->instance_change_notify(true, true);
2763 }
2764 
mesh_get_surface_count(RID p_mesh) const2765 int RasterizerStorageGLES2::mesh_get_surface_count(RID p_mesh) const {
2766 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2767 	ERR_FAIL_COND_V(!mesh, 0);
2768 	return mesh->surfaces.size();
2769 }
2770 
mesh_set_custom_aabb(RID p_mesh,const AABB & p_aabb)2771 void RasterizerStorageGLES2::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
2772 	Mesh *mesh = mesh_owner.getornull(p_mesh);
2773 	ERR_FAIL_COND(!mesh);
2774 
2775 	mesh->custom_aabb = p_aabb;
2776 	mesh->instance_change_notify(true, false);
2777 }
2778 
mesh_get_custom_aabb(RID p_mesh) const2779 AABB RasterizerStorageGLES2::mesh_get_custom_aabb(RID p_mesh) const {
2780 	const Mesh *mesh = mesh_owner.getornull(p_mesh);
2781 	ERR_FAIL_COND_V(!mesh, AABB());
2782 
2783 	return mesh->custom_aabb;
2784 }
2785 
mesh_get_aabb(RID p_mesh,RID p_skeleton) const2786 AABB RasterizerStorageGLES2::mesh_get_aabb(RID p_mesh, RID p_skeleton) const {
2787 	Mesh *mesh = mesh_owner.get(p_mesh);
2788 	ERR_FAIL_COND_V(!mesh, AABB());
2789 
2790 	if (mesh->custom_aabb != AABB())
2791 		return mesh->custom_aabb;
2792 
2793 	Skeleton *sk = NULL;
2794 	if (p_skeleton.is_valid()) {
2795 		sk = skeleton_owner.get(p_skeleton);
2796 	}
2797 
2798 	AABB aabb;
2799 
2800 	if (sk && sk->size != 0) {
2801 
2802 		for (int i = 0; i < mesh->surfaces.size(); i++) {
2803 
2804 			AABB laabb;
2805 			if ((mesh->surfaces[i]->format & VS::ARRAY_FORMAT_BONES) && mesh->surfaces[i]->skeleton_bone_aabb.size()) {
2806 
2807 				int bs = mesh->surfaces[i]->skeleton_bone_aabb.size();
2808 				const AABB *skbones = mesh->surfaces[i]->skeleton_bone_aabb.ptr();
2809 				const bool *skused = mesh->surfaces[i]->skeleton_bone_used.ptr();
2810 
2811 				int sbs = sk->size;
2812 				ERR_CONTINUE(bs > sbs);
2813 				const float *texture = sk->bone_data.ptr();
2814 
2815 				bool first = true;
2816 				if (sk->use_2d) {
2817 					for (int j = 0; j < bs; j++) {
2818 
2819 						if (!skused[j])
2820 							continue;
2821 
2822 						int base_ofs = j * 2 * 4;
2823 
2824 						Transform mtx;
2825 
2826 						mtx.basis[0].x = texture[base_ofs + 0];
2827 						mtx.basis[0].y = texture[base_ofs + 1];
2828 						mtx.origin.x = texture[base_ofs + 3];
2829 						base_ofs += 4;
2830 						mtx.basis[1].x = texture[base_ofs + 0];
2831 						mtx.basis[1].y = texture[base_ofs + 1];
2832 						mtx.origin.y = texture[base_ofs + 3];
2833 
2834 						AABB baabb = mtx.xform(skbones[j]);
2835 
2836 						if (first) {
2837 							laabb = baabb;
2838 							first = false;
2839 						} else {
2840 							laabb.merge_with(baabb);
2841 						}
2842 					}
2843 				} else {
2844 					for (int j = 0; j < bs; j++) {
2845 
2846 						if (!skused[j])
2847 							continue;
2848 
2849 						int base_ofs = j * 3 * 4;
2850 
2851 						Transform mtx;
2852 
2853 						mtx.basis[0].x = texture[base_ofs + 0];
2854 						mtx.basis[0].y = texture[base_ofs + 1];
2855 						mtx.basis[0].z = texture[base_ofs + 2];
2856 						mtx.origin.x = texture[base_ofs + 3];
2857 						base_ofs += 4;
2858 						mtx.basis[1].x = texture[base_ofs + 0];
2859 						mtx.basis[1].y = texture[base_ofs + 1];
2860 						mtx.basis[1].z = texture[base_ofs + 2];
2861 						mtx.origin.y = texture[base_ofs + 3];
2862 						base_ofs += 4;
2863 						mtx.basis[2].x = texture[base_ofs + 0];
2864 						mtx.basis[2].y = texture[base_ofs + 1];
2865 						mtx.basis[2].z = texture[base_ofs + 2];
2866 						mtx.origin.z = texture[base_ofs + 3];
2867 
2868 						AABB baabb = mtx.xform(skbones[j]);
2869 						if (first) {
2870 							laabb = baabb;
2871 							first = false;
2872 						} else {
2873 							laabb.merge_with(baabb);
2874 						}
2875 					}
2876 				}
2877 
2878 			} else {
2879 
2880 				laabb = mesh->surfaces[i]->aabb;
2881 			}
2882 
2883 			if (i == 0)
2884 				aabb = laabb;
2885 			else
2886 				aabb.merge_with(laabb);
2887 		}
2888 	} else {
2889 
2890 		for (int i = 0; i < mesh->surfaces.size(); i++) {
2891 
2892 			if (i == 0)
2893 				aabb = mesh->surfaces[i]->aabb;
2894 			else
2895 				aabb.merge_with(mesh->surfaces[i]->aabb);
2896 		}
2897 	}
2898 
2899 	return aabb;
2900 }
mesh_clear(RID p_mesh)2901 void RasterizerStorageGLES2::mesh_clear(RID p_mesh) {
2902 	Mesh *mesh = mesh_owner.getornull(p_mesh);
2903 	ERR_FAIL_COND(!mesh);
2904 
2905 	while (mesh->surfaces.size()) {
2906 		mesh_remove_surface(p_mesh, 0);
2907 	}
2908 }
2909 
2910 /* MULTIMESH API */
2911 
multimesh_create()2912 RID RasterizerStorageGLES2::multimesh_create() {
2913 	MultiMesh *multimesh = memnew(MultiMesh);
2914 	return multimesh_owner.make_rid(multimesh);
2915 }
2916 
multimesh_allocate(RID p_multimesh,int p_instances,VS::MultimeshTransformFormat p_transform_format,VS::MultimeshColorFormat p_color_format,VS::MultimeshCustomDataFormat p_data)2917 void RasterizerStorageGLES2::multimesh_allocate(RID p_multimesh, int p_instances, VS::MultimeshTransformFormat p_transform_format, VS::MultimeshColorFormat p_color_format, VS::MultimeshCustomDataFormat p_data) {
2918 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
2919 	ERR_FAIL_COND(!multimesh);
2920 
2921 	if (multimesh->size == p_instances && multimesh->transform_format == p_transform_format && multimesh->color_format == p_color_format && multimesh->custom_data_format == p_data) {
2922 		return;
2923 	}
2924 
2925 	multimesh->size = p_instances;
2926 
2927 	multimesh->color_format = p_color_format;
2928 	multimesh->transform_format = p_transform_format;
2929 	multimesh->custom_data_format = p_data;
2930 
2931 	if (multimesh->size) {
2932 		multimesh->data.resize(0);
2933 	}
2934 
2935 	if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
2936 		multimesh->xform_floats = 8;
2937 	} else {
2938 		multimesh->xform_floats = 12;
2939 	}
2940 
2941 	if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
2942 		multimesh->color_floats = 1;
2943 	} else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
2944 		multimesh->color_floats = 4;
2945 	} else {
2946 		multimesh->color_floats = 0;
2947 	}
2948 
2949 	if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
2950 		multimesh->custom_data_floats = 1;
2951 	} else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
2952 		multimesh->custom_data_floats = 4;
2953 	} else {
2954 		multimesh->custom_data_floats = 0;
2955 	}
2956 
2957 	int format_floats = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
2958 
2959 	multimesh->data.resize(format_floats * p_instances);
2960 
2961 	for (int i = 0; i < p_instances * format_floats; i += format_floats) {
2962 		int color_from = 0;
2963 		int custom_data_from = 0;
2964 
2965 		if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
2966 			multimesh->data.write[i + 0] = 1.0;
2967 			multimesh->data.write[i + 1] = 0.0;
2968 			multimesh->data.write[i + 2] = 0.0;
2969 			multimesh->data.write[i + 3] = 0.0;
2970 			multimesh->data.write[i + 4] = 0.0;
2971 			multimesh->data.write[i + 5] = 1.0;
2972 			multimesh->data.write[i + 6] = 0.0;
2973 			multimesh->data.write[i + 7] = 0.0;
2974 			color_from = 8;
2975 			custom_data_from = 8;
2976 		} else {
2977 			multimesh->data.write[i + 0] = 1.0;
2978 			multimesh->data.write[i + 1] = 0.0;
2979 			multimesh->data.write[i + 2] = 0.0;
2980 			multimesh->data.write[i + 3] = 0.0;
2981 			multimesh->data.write[i + 4] = 0.0;
2982 			multimesh->data.write[i + 5] = 1.0;
2983 			multimesh->data.write[i + 6] = 0.0;
2984 			multimesh->data.write[i + 7] = 0.0;
2985 			multimesh->data.write[i + 8] = 0.0;
2986 			multimesh->data.write[i + 9] = 0.0;
2987 			multimesh->data.write[i + 10] = 1.0;
2988 			multimesh->data.write[i + 11] = 0.0;
2989 			color_from = 12;
2990 			custom_data_from = 12;
2991 		}
2992 
2993 		if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
2994 			union {
2995 				uint32_t colu;
2996 				float colf;
2997 			} cu;
2998 
2999 			cu.colu = 0xFFFFFFFF;
3000 			multimesh->data.write[i + color_from + 0] = cu.colf;
3001 			custom_data_from = color_from + 1;
3002 		} else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
3003 			multimesh->data.write[i + color_from + 0] = 1.0;
3004 			multimesh->data.write[i + color_from + 1] = 1.0;
3005 			multimesh->data.write[i + color_from + 2] = 1.0;
3006 			multimesh->data.write[i + color_from + 3] = 1.0;
3007 			custom_data_from = color_from + 4;
3008 		}
3009 
3010 		if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
3011 			union {
3012 				uint32_t colu;
3013 				float colf;
3014 			} cu;
3015 
3016 			cu.colu = 0;
3017 			multimesh->data.write[i + custom_data_from + 0] = cu.colf;
3018 		} else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
3019 			multimesh->data.write[i + custom_data_from + 0] = 0.0;
3020 			multimesh->data.write[i + custom_data_from + 1] = 0.0;
3021 			multimesh->data.write[i + custom_data_from + 2] = 0.0;
3022 			multimesh->data.write[i + custom_data_from + 3] = 0.0;
3023 		}
3024 	}
3025 
3026 	multimesh->dirty_aabb = true;
3027 	multimesh->dirty_data = true;
3028 
3029 	if (!multimesh->update_list.in_list()) {
3030 		multimesh_update_list.add(&multimesh->update_list);
3031 	}
3032 }
3033 
multimesh_get_instance_count(RID p_multimesh) const3034 int RasterizerStorageGLES2::multimesh_get_instance_count(RID p_multimesh) const {
3035 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3036 	ERR_FAIL_COND_V(!multimesh, 0);
3037 
3038 	return multimesh->size;
3039 }
3040 
multimesh_set_mesh(RID p_multimesh,RID p_mesh)3041 void RasterizerStorageGLES2::multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
3042 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3043 	ERR_FAIL_COND(!multimesh);
3044 
3045 	if (multimesh->mesh.is_valid()) {
3046 		Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
3047 		if (mesh) {
3048 			mesh->multimeshes.remove(&multimesh->mesh_list);
3049 		}
3050 	}
3051 
3052 	multimesh->mesh = p_mesh;
3053 
3054 	if (multimesh->mesh.is_valid()) {
3055 		Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
3056 		if (mesh) {
3057 			mesh->multimeshes.add(&multimesh->mesh_list);
3058 		}
3059 	}
3060 
3061 	multimesh->dirty_aabb = true;
3062 
3063 	if (!multimesh->update_list.in_list()) {
3064 		multimesh_update_list.add(&multimesh->update_list);
3065 	}
3066 }
3067 
multimesh_instance_set_transform(RID p_multimesh,int p_index,const Transform & p_transform)3068 void RasterizerStorageGLES2::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
3069 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3070 	ERR_FAIL_COND(!multimesh);
3071 	ERR_FAIL_INDEX(p_index, multimesh->size);
3072 	ERR_FAIL_COND(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D);
3073 
3074 	int stride = multimesh->color_floats + multimesh->custom_data_floats + multimesh->xform_floats;
3075 
3076 	float *dataptr = &multimesh->data.write[stride * p_index];
3077 
3078 	dataptr[0] = p_transform.basis.elements[0][0];
3079 	dataptr[1] = p_transform.basis.elements[0][1];
3080 	dataptr[2] = p_transform.basis.elements[0][2];
3081 	dataptr[3] = p_transform.origin.x;
3082 	dataptr[4] = p_transform.basis.elements[1][0];
3083 	dataptr[5] = p_transform.basis.elements[1][1];
3084 	dataptr[6] = p_transform.basis.elements[1][2];
3085 	dataptr[7] = p_transform.origin.y;
3086 	dataptr[8] = p_transform.basis.elements[2][0];
3087 	dataptr[9] = p_transform.basis.elements[2][1];
3088 	dataptr[10] = p_transform.basis.elements[2][2];
3089 	dataptr[11] = p_transform.origin.z;
3090 
3091 	multimesh->dirty_data = true;
3092 	multimesh->dirty_aabb = true;
3093 
3094 	if (!multimesh->update_list.in_list()) {
3095 		multimesh_update_list.add(&multimesh->update_list);
3096 	}
3097 }
3098 
multimesh_instance_set_transform_2d(RID p_multimesh,int p_index,const Transform2D & p_transform)3099 void RasterizerStorageGLES2::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
3100 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3101 	ERR_FAIL_COND(!multimesh);
3102 	ERR_FAIL_INDEX(p_index, multimesh->size);
3103 	ERR_FAIL_COND(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_3D);
3104 
3105 	int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
3106 	float *dataptr = &multimesh->data.write[stride * p_index];
3107 
3108 	dataptr[0] = p_transform.elements[0][0];
3109 	dataptr[1] = p_transform.elements[1][0];
3110 	dataptr[2] = 0;
3111 	dataptr[3] = p_transform.elements[2][0];
3112 	dataptr[4] = p_transform.elements[0][1];
3113 	dataptr[5] = p_transform.elements[1][1];
3114 	dataptr[6] = 0;
3115 	dataptr[7] = p_transform.elements[2][1];
3116 
3117 	multimesh->dirty_data = true;
3118 	multimesh->dirty_aabb = true;
3119 
3120 	if (!multimesh->update_list.in_list()) {
3121 		multimesh_update_list.add(&multimesh->update_list);
3122 	}
3123 }
3124 
multimesh_instance_set_color(RID p_multimesh,int p_index,const Color & p_color)3125 void RasterizerStorageGLES2::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
3126 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3127 	ERR_FAIL_COND(!multimesh);
3128 	ERR_FAIL_INDEX(p_index, multimesh->size);
3129 	ERR_FAIL_COND(multimesh->color_format == VS::MULTIMESH_COLOR_NONE);
3130 	ERR_FAIL_INDEX(multimesh->color_format, VS::MULTIMESH_COLOR_MAX);
3131 
3132 	int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
3133 	float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats];
3134 
3135 	if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
3136 
3137 		uint8_t *data8 = (uint8_t *)dataptr;
3138 		data8[0] = CLAMP(p_color.r * 255.0, 0, 255);
3139 		data8[1] = CLAMP(p_color.g * 255.0, 0, 255);
3140 		data8[2] = CLAMP(p_color.b * 255.0, 0, 255);
3141 		data8[3] = CLAMP(p_color.a * 255.0, 0, 255);
3142 
3143 	} else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
3144 		dataptr[0] = p_color.r;
3145 		dataptr[1] = p_color.g;
3146 		dataptr[2] = p_color.b;
3147 		dataptr[3] = p_color.a;
3148 	}
3149 
3150 	multimesh->dirty_data = true;
3151 	multimesh->dirty_aabb = true;
3152 
3153 	if (!multimesh->update_list.in_list()) {
3154 		multimesh_update_list.add(&multimesh->update_list);
3155 	}
3156 }
3157 
multimesh_instance_set_custom_data(RID p_multimesh,int p_index,const Color & p_custom_data)3158 void RasterizerStorageGLES2::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_custom_data) {
3159 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3160 	ERR_FAIL_COND(!multimesh);
3161 	ERR_FAIL_INDEX(p_index, multimesh->size);
3162 	ERR_FAIL_COND(multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE);
3163 	ERR_FAIL_INDEX(multimesh->custom_data_format, VS::MULTIMESH_CUSTOM_DATA_MAX);
3164 
3165 	int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
3166 	float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats + multimesh->color_floats];
3167 
3168 	if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
3169 
3170 		uint8_t *data8 = (uint8_t *)dataptr;
3171 		data8[0] = CLAMP(p_custom_data.r * 255.0, 0, 255);
3172 		data8[1] = CLAMP(p_custom_data.g * 255.0, 0, 255);
3173 		data8[2] = CLAMP(p_custom_data.b * 255.0, 0, 255);
3174 		data8[3] = CLAMP(p_custom_data.a * 255.0, 0, 255);
3175 
3176 	} else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
3177 		dataptr[0] = p_custom_data.r;
3178 		dataptr[1] = p_custom_data.g;
3179 		dataptr[2] = p_custom_data.b;
3180 		dataptr[3] = p_custom_data.a;
3181 	}
3182 
3183 	multimesh->dirty_data = true;
3184 	multimesh->dirty_aabb = true;
3185 
3186 	if (!multimesh->update_list.in_list()) {
3187 		multimesh_update_list.add(&multimesh->update_list);
3188 	}
3189 }
3190 
multimesh_get_mesh(RID p_multimesh) const3191 RID RasterizerStorageGLES2::multimesh_get_mesh(RID p_multimesh) const {
3192 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3193 	ERR_FAIL_COND_V(!multimesh, RID());
3194 
3195 	return multimesh->mesh;
3196 }
3197 
multimesh_instance_get_transform(RID p_multimesh,int p_index) const3198 Transform RasterizerStorageGLES2::multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
3199 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3200 	ERR_FAIL_COND_V(!multimesh, Transform());
3201 	ERR_FAIL_INDEX_V(p_index, multimesh->size, Transform());
3202 	ERR_FAIL_COND_V(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D, Transform());
3203 
3204 	int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
3205 	float *dataptr = &multimesh->data.write[stride * p_index];
3206 
3207 	Transform xform;
3208 
3209 	xform.basis.elements[0][0] = dataptr[0];
3210 	xform.basis.elements[0][1] = dataptr[1];
3211 	xform.basis.elements[0][2] = dataptr[2];
3212 	xform.origin.x = dataptr[3];
3213 	xform.basis.elements[1][0] = dataptr[4];
3214 	xform.basis.elements[1][1] = dataptr[5];
3215 	xform.basis.elements[1][2] = dataptr[6];
3216 	xform.origin.y = dataptr[7];
3217 	xform.basis.elements[2][0] = dataptr[8];
3218 	xform.basis.elements[2][1] = dataptr[9];
3219 	xform.basis.elements[2][2] = dataptr[10];
3220 	xform.origin.z = dataptr[11];
3221 
3222 	return xform;
3223 }
3224 
multimesh_instance_get_transform_2d(RID p_multimesh,int p_index) const3225 Transform2D RasterizerStorageGLES2::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
3226 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3227 	ERR_FAIL_COND_V(!multimesh, Transform2D());
3228 	ERR_FAIL_INDEX_V(p_index, multimesh->size, Transform2D());
3229 	ERR_FAIL_COND_V(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_3D, Transform2D());
3230 
3231 	int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
3232 	float *dataptr = &multimesh->data.write[stride * p_index];
3233 
3234 	Transform2D xform;
3235 
3236 	xform.elements[0][0] = dataptr[0];
3237 	xform.elements[1][0] = dataptr[1];
3238 	xform.elements[2][0] = dataptr[3];
3239 	xform.elements[0][1] = dataptr[4];
3240 	xform.elements[1][1] = dataptr[5];
3241 	xform.elements[2][1] = dataptr[7];
3242 
3243 	return xform;
3244 }
3245 
multimesh_instance_get_color(RID p_multimesh,int p_index) const3246 Color RasterizerStorageGLES2::multimesh_instance_get_color(RID p_multimesh, int p_index) const {
3247 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3248 	ERR_FAIL_COND_V(!multimesh, Color());
3249 	ERR_FAIL_INDEX_V(p_index, multimesh->size, Color());
3250 	ERR_FAIL_COND_V(multimesh->color_format == VS::MULTIMESH_COLOR_NONE, Color());
3251 	ERR_FAIL_INDEX_V(multimesh->color_format, VS::MULTIMESH_COLOR_MAX, Color());
3252 
3253 	int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
3254 	float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats];
3255 
3256 	if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
3257 		union {
3258 			uint32_t colu;
3259 			float colf;
3260 		} cu;
3261 
3262 		cu.colf = dataptr[0];
3263 
3264 		return Color::hex(BSWAP32(cu.colu));
3265 
3266 	} else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
3267 		Color c;
3268 		c.r = dataptr[0];
3269 		c.g = dataptr[1];
3270 		c.b = dataptr[2];
3271 		c.a = dataptr[3];
3272 
3273 		return c;
3274 	}
3275 
3276 	return Color();
3277 }
3278 
multimesh_instance_get_custom_data(RID p_multimesh,int p_index) const3279 Color RasterizerStorageGLES2::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
3280 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3281 	ERR_FAIL_COND_V(!multimesh, Color());
3282 	ERR_FAIL_INDEX_V(p_index, multimesh->size, Color());
3283 	ERR_FAIL_COND_V(multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE, Color());
3284 	ERR_FAIL_INDEX_V(multimesh->custom_data_format, VS::MULTIMESH_CUSTOM_DATA_MAX, Color());
3285 
3286 	int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
3287 	float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats + multimesh->color_floats];
3288 
3289 	if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
3290 		union {
3291 			uint32_t colu;
3292 			float colf;
3293 		} cu;
3294 
3295 		cu.colf = dataptr[0];
3296 
3297 		return Color::hex(BSWAP32(cu.colu));
3298 
3299 	} else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
3300 		Color c;
3301 		c.r = dataptr[0];
3302 		c.g = dataptr[1];
3303 		c.b = dataptr[2];
3304 		c.a = dataptr[3];
3305 
3306 		return c;
3307 	}
3308 
3309 	return Color();
3310 }
3311 
multimesh_set_as_bulk_array(RID p_multimesh,const PoolVector<float> & p_array)3312 void RasterizerStorageGLES2::multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array) {
3313 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3314 	ERR_FAIL_COND(!multimesh);
3315 	ERR_FAIL_COND(!multimesh->data.ptr());
3316 
3317 	int dsize = multimesh->data.size();
3318 
3319 	ERR_FAIL_COND(dsize != p_array.size());
3320 
3321 	PoolVector<float>::Read r = p_array.read();
3322 	ERR_FAIL_COND(!r.ptr());
3323 	copymem(multimesh->data.ptrw(), r.ptr(), dsize * sizeof(float));
3324 
3325 	multimesh->dirty_data = true;
3326 	multimesh->dirty_aabb = true;
3327 
3328 	if (!multimesh->update_list.in_list()) {
3329 		multimesh_update_list.add(&multimesh->update_list);
3330 	}
3331 }
3332 
multimesh_set_visible_instances(RID p_multimesh,int p_visible)3333 void RasterizerStorageGLES2::multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
3334 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3335 	ERR_FAIL_COND(!multimesh);
3336 
3337 	multimesh->visible_instances = p_visible;
3338 }
3339 
multimesh_get_visible_instances(RID p_multimesh) const3340 int RasterizerStorageGLES2::multimesh_get_visible_instances(RID p_multimesh) const {
3341 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3342 	ERR_FAIL_COND_V(!multimesh, -1);
3343 
3344 	return multimesh->visible_instances;
3345 }
3346 
multimesh_get_aabb(RID p_multimesh) const3347 AABB RasterizerStorageGLES2::multimesh_get_aabb(RID p_multimesh) const {
3348 	MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
3349 	ERR_FAIL_COND_V(!multimesh, AABB());
3350 
3351 	const_cast<RasterizerStorageGLES2 *>(this)->update_dirty_multimeshes();
3352 
3353 	return multimesh->aabb;
3354 }
3355 
update_dirty_multimeshes()3356 void RasterizerStorageGLES2::update_dirty_multimeshes() {
3357 
3358 	while (multimesh_update_list.first()) {
3359 
3360 		MultiMesh *multimesh = multimesh_update_list.first()->self();
3361 
3362 		if (multimesh->size && multimesh->dirty_aabb) {
3363 
3364 			AABB mesh_aabb;
3365 
3366 			if (multimesh->mesh.is_valid()) {
3367 				mesh_aabb = mesh_get_aabb(multimesh->mesh, RID());
3368 			}
3369 
3370 			mesh_aabb.size += Vector3(0.001, 0.001, 0.001); //in case mesh is empty in one of the sides
3371 
3372 			int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
3373 			int count = multimesh->data.size();
3374 			float *data = multimesh->data.ptrw();
3375 
3376 			AABB aabb;
3377 
3378 			if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
3379 
3380 				for (int i = 0; i < count; i += stride) {
3381 
3382 					float *dataptr = &data[i];
3383 
3384 					Transform xform;
3385 					xform.basis[0][0] = dataptr[0];
3386 					xform.basis[0][1] = dataptr[1];
3387 					xform.origin[0] = dataptr[3];
3388 					xform.basis[1][0] = dataptr[4];
3389 					xform.basis[1][1] = dataptr[5];
3390 					xform.origin[1] = dataptr[7];
3391 
3392 					AABB laabb = xform.xform(mesh_aabb);
3393 
3394 					if (i == 0) {
3395 						aabb = laabb;
3396 					} else {
3397 						aabb.merge_with(laabb);
3398 					}
3399 				}
3400 
3401 			} else {
3402 
3403 				for (int i = 0; i < count; i += stride) {
3404 
3405 					float *dataptr = &data[i];
3406 
3407 					Transform xform;
3408 					xform.basis.elements[0][0] = dataptr[0];
3409 					xform.basis.elements[0][1] = dataptr[1];
3410 					xform.basis.elements[0][2] = dataptr[2];
3411 					xform.origin.x = dataptr[3];
3412 					xform.basis.elements[1][0] = dataptr[4];
3413 					xform.basis.elements[1][1] = dataptr[5];
3414 					xform.basis.elements[1][2] = dataptr[6];
3415 					xform.origin.y = dataptr[7];
3416 					xform.basis.elements[2][0] = dataptr[8];
3417 					xform.basis.elements[2][1] = dataptr[9];
3418 					xform.basis.elements[2][2] = dataptr[10];
3419 					xform.origin.z = dataptr[11];
3420 
3421 					AABB laabb = xform.xform(mesh_aabb);
3422 
3423 					if (i == 0) {
3424 						aabb = laabb;
3425 					} else {
3426 						aabb.merge_with(laabb);
3427 					}
3428 				}
3429 			}
3430 
3431 			multimesh->aabb = aabb;
3432 		}
3433 
3434 		multimesh->dirty_aabb = false;
3435 		multimesh->dirty_data = false;
3436 
3437 		multimesh->instance_change_notify(true, false);
3438 
3439 		multimesh_update_list.remove(multimesh_update_list.first());
3440 	}
3441 }
3442 
3443 /* IMMEDIATE API */
3444 
immediate_create()3445 RID RasterizerStorageGLES2::immediate_create() {
3446 	Immediate *im = memnew(Immediate);
3447 	return immediate_owner.make_rid(im);
3448 }
3449 
immediate_begin(RID p_immediate,VS::PrimitiveType p_primitive,RID p_texture)3450 void RasterizerStorageGLES2::immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture) {
3451 	Immediate *im = immediate_owner.get(p_immediate);
3452 	ERR_FAIL_COND(!im);
3453 	ERR_FAIL_COND(im->building);
3454 
3455 	Immediate::Chunk ic;
3456 	ic.texture = p_texture;
3457 	ic.primitive = p_primitive;
3458 	im->chunks.push_back(ic);
3459 	im->mask = 0;
3460 	im->building = true;
3461 }
3462 
immediate_vertex(RID p_immediate,const Vector3 & p_vertex)3463 void RasterizerStorageGLES2::immediate_vertex(RID p_immediate, const Vector3 &p_vertex) {
3464 	Immediate *im = immediate_owner.get(p_immediate);
3465 	ERR_FAIL_COND(!im);
3466 	ERR_FAIL_COND(!im->building);
3467 
3468 	Immediate::Chunk *c = &im->chunks.back()->get();
3469 
3470 	if (c->vertices.empty() && im->chunks.size() == 1) {
3471 		im->aabb.position = p_vertex;
3472 		im->aabb.size = Vector3();
3473 	} else {
3474 		im->aabb.expand_to(p_vertex);
3475 	}
3476 
3477 	if (im->mask & VS::ARRAY_FORMAT_NORMAL)
3478 		c->normals.push_back(chunk_normal);
3479 	if (im->mask & VS::ARRAY_FORMAT_TANGENT)
3480 		c->tangents.push_back(chunk_tangent);
3481 	if (im->mask & VS::ARRAY_FORMAT_COLOR)
3482 		c->colors.push_back(chunk_color);
3483 	if (im->mask & VS::ARRAY_FORMAT_TEX_UV)
3484 		c->uvs.push_back(chunk_uv);
3485 	if (im->mask & VS::ARRAY_FORMAT_TEX_UV2)
3486 		c->uv2s.push_back(chunk_uv2);
3487 	im->mask |= VS::ARRAY_FORMAT_VERTEX;
3488 	c->vertices.push_back(p_vertex);
3489 }
3490 
immediate_normal(RID p_immediate,const Vector3 & p_normal)3491 void RasterizerStorageGLES2::immediate_normal(RID p_immediate, const Vector3 &p_normal) {
3492 	Immediate *im = immediate_owner.get(p_immediate);
3493 	ERR_FAIL_COND(!im);
3494 	ERR_FAIL_COND(!im->building);
3495 
3496 	im->mask |= VS::ARRAY_FORMAT_NORMAL;
3497 	chunk_normal = p_normal;
3498 }
3499 
immediate_tangent(RID p_immediate,const Plane & p_tangent)3500 void RasterizerStorageGLES2::immediate_tangent(RID p_immediate, const Plane &p_tangent) {
3501 	Immediate *im = immediate_owner.get(p_immediate);
3502 	ERR_FAIL_COND(!im);
3503 	ERR_FAIL_COND(!im->building);
3504 
3505 	im->mask |= VS::ARRAY_FORMAT_TANGENT;
3506 	chunk_tangent = p_tangent;
3507 }
3508 
immediate_color(RID p_immediate,const Color & p_color)3509 void RasterizerStorageGLES2::immediate_color(RID p_immediate, const Color &p_color) {
3510 	Immediate *im = immediate_owner.get(p_immediate);
3511 	ERR_FAIL_COND(!im);
3512 	ERR_FAIL_COND(!im->building);
3513 
3514 	im->mask |= VS::ARRAY_FORMAT_COLOR;
3515 	chunk_color = p_color;
3516 }
3517 
immediate_uv(RID p_immediate,const Vector2 & tex_uv)3518 void RasterizerStorageGLES2::immediate_uv(RID p_immediate, const Vector2 &tex_uv) {
3519 	Immediate *im = immediate_owner.get(p_immediate);
3520 	ERR_FAIL_COND(!im);
3521 	ERR_FAIL_COND(!im->building);
3522 
3523 	im->mask |= VS::ARRAY_FORMAT_TEX_UV;
3524 	chunk_uv = tex_uv;
3525 }
3526 
immediate_uv2(RID p_immediate,const Vector2 & tex_uv)3527 void RasterizerStorageGLES2::immediate_uv2(RID p_immediate, const Vector2 &tex_uv) {
3528 	Immediate *im = immediate_owner.get(p_immediate);
3529 	ERR_FAIL_COND(!im);
3530 	ERR_FAIL_COND(!im->building);
3531 
3532 	im->mask |= VS::ARRAY_FORMAT_TEX_UV2;
3533 	chunk_uv2 = tex_uv;
3534 }
3535 
immediate_end(RID p_immediate)3536 void RasterizerStorageGLES2::immediate_end(RID p_immediate) {
3537 	Immediate *im = immediate_owner.get(p_immediate);
3538 	ERR_FAIL_COND(!im);
3539 	ERR_FAIL_COND(!im->building);
3540 
3541 	im->building = false;
3542 	im->instance_change_notify(true, false);
3543 }
3544 
immediate_clear(RID p_immediate)3545 void RasterizerStorageGLES2::immediate_clear(RID p_immediate) {
3546 	Immediate *im = immediate_owner.get(p_immediate);
3547 	ERR_FAIL_COND(!im);
3548 	ERR_FAIL_COND(im->building);
3549 
3550 	im->chunks.clear();
3551 	im->instance_change_notify(true, false);
3552 }
3553 
immediate_get_aabb(RID p_immediate) const3554 AABB RasterizerStorageGLES2::immediate_get_aabb(RID p_immediate) const {
3555 	Immediate *im = immediate_owner.get(p_immediate);
3556 	ERR_FAIL_COND_V(!im, AABB());
3557 	return im->aabb;
3558 }
3559 
immediate_set_material(RID p_immediate,RID p_material)3560 void RasterizerStorageGLES2::immediate_set_material(RID p_immediate, RID p_material) {
3561 	Immediate *im = immediate_owner.get(p_immediate);
3562 	ERR_FAIL_COND(!im);
3563 
3564 	im->material = p_material;
3565 	im->instance_change_notify(false, true);
3566 }
3567 
immediate_get_material(RID p_immediate) const3568 RID RasterizerStorageGLES2::immediate_get_material(RID p_immediate) const {
3569 	const Immediate *im = immediate_owner.get(p_immediate);
3570 	ERR_FAIL_COND_V(!im, RID());
3571 	return im->material;
3572 }
3573 
3574 /* SKELETON API */
3575 
skeleton_create()3576 RID RasterizerStorageGLES2::skeleton_create() {
3577 
3578 	Skeleton *skeleton = memnew(Skeleton);
3579 
3580 	glGenTextures(1, &skeleton->tex_id);
3581 
3582 	return skeleton_owner.make_rid(skeleton);
3583 }
3584 
skeleton_allocate(RID p_skeleton,int p_bones,bool p_2d_skeleton)3585 void RasterizerStorageGLES2::skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
3586 
3587 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
3588 	ERR_FAIL_COND(!skeleton);
3589 	ERR_FAIL_COND(p_bones < 0);
3590 
3591 	if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) {
3592 		return;
3593 	}
3594 
3595 	skeleton->size = p_bones;
3596 	skeleton->use_2d = p_2d_skeleton;
3597 
3598 	if (!config.use_skeleton_software) {
3599 
3600 		glActiveTexture(GL_TEXTURE0);
3601 		glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
3602 
3603 #ifdef GLES_OVER_GL
3604 		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, p_bones * (skeleton->use_2d ? 2 : 3), 1, 0, GL_RGBA, GL_FLOAT, NULL);
3605 #else
3606 		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_bones * (skeleton->use_2d ? 2 : 3), 1, 0, GL_RGBA, GL_FLOAT, NULL);
3607 #endif
3608 
3609 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3610 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3611 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3612 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3613 
3614 		glBindTexture(GL_TEXTURE_2D, 0);
3615 	}
3616 	if (skeleton->use_2d) {
3617 		skeleton->bone_data.resize(p_bones * 4 * 2);
3618 	} else {
3619 		skeleton->bone_data.resize(p_bones * 4 * 3);
3620 	}
3621 }
3622 
skeleton_get_bone_count(RID p_skeleton) const3623 int RasterizerStorageGLES2::skeleton_get_bone_count(RID p_skeleton) const {
3624 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
3625 	ERR_FAIL_COND_V(!skeleton, 0);
3626 
3627 	return skeleton->size;
3628 }
3629 
skeleton_bone_set_transform(RID p_skeleton,int p_bone,const Transform & p_transform)3630 void RasterizerStorageGLES2::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) {
3631 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
3632 	ERR_FAIL_COND(!skeleton);
3633 
3634 	ERR_FAIL_INDEX(p_bone, skeleton->size);
3635 	ERR_FAIL_COND(skeleton->use_2d);
3636 
3637 	float *bone_data = skeleton->bone_data.ptrw();
3638 
3639 	int base_offset = p_bone * 4 * 3;
3640 
3641 	bone_data[base_offset + 0] = p_transform.basis[0].x;
3642 	bone_data[base_offset + 1] = p_transform.basis[0].y;
3643 	bone_data[base_offset + 2] = p_transform.basis[0].z;
3644 	bone_data[base_offset + 3] = p_transform.origin.x;
3645 
3646 	bone_data[base_offset + 4] = p_transform.basis[1].x;
3647 	bone_data[base_offset + 5] = p_transform.basis[1].y;
3648 	bone_data[base_offset + 6] = p_transform.basis[1].z;
3649 	bone_data[base_offset + 7] = p_transform.origin.y;
3650 
3651 	bone_data[base_offset + 8] = p_transform.basis[2].x;
3652 	bone_data[base_offset + 9] = p_transform.basis[2].y;
3653 	bone_data[base_offset + 10] = p_transform.basis[2].z;
3654 	bone_data[base_offset + 11] = p_transform.origin.z;
3655 
3656 	if (!skeleton->update_list.in_list()) {
3657 		skeleton_update_list.add(&skeleton->update_list);
3658 	}
3659 }
3660 
skeleton_bone_get_transform(RID p_skeleton,int p_bone) const3661 Transform RasterizerStorageGLES2::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
3662 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
3663 	ERR_FAIL_COND_V(!skeleton, Transform());
3664 
3665 	ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform());
3666 	ERR_FAIL_COND_V(skeleton->use_2d, Transform());
3667 
3668 	const float *bone_data = skeleton->bone_data.ptr();
3669 
3670 	Transform ret;
3671 
3672 	int base_offset = p_bone * 4 * 3;
3673 
3674 	ret.basis[0].x = bone_data[base_offset + 0];
3675 	ret.basis[0].y = bone_data[base_offset + 1];
3676 	ret.basis[0].z = bone_data[base_offset + 2];
3677 	ret.origin.x = bone_data[base_offset + 3];
3678 
3679 	ret.basis[1].x = bone_data[base_offset + 4];
3680 	ret.basis[1].y = bone_data[base_offset + 5];
3681 	ret.basis[1].z = bone_data[base_offset + 6];
3682 	ret.origin.y = bone_data[base_offset + 7];
3683 
3684 	ret.basis[2].x = bone_data[base_offset + 8];
3685 	ret.basis[2].y = bone_data[base_offset + 9];
3686 	ret.basis[2].z = bone_data[base_offset + 10];
3687 	ret.origin.z = bone_data[base_offset + 11];
3688 
3689 	return ret;
3690 }
skeleton_bone_set_transform_2d(RID p_skeleton,int p_bone,const Transform2D & p_transform)3691 void RasterizerStorageGLES2::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
3692 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
3693 	ERR_FAIL_COND(!skeleton);
3694 
3695 	ERR_FAIL_INDEX(p_bone, skeleton->size);
3696 	ERR_FAIL_COND(!skeleton->use_2d);
3697 
3698 	float *bone_data = skeleton->bone_data.ptrw();
3699 
3700 	int base_offset = p_bone * 4 * 2;
3701 
3702 	bone_data[base_offset + 0] = p_transform[0][0];
3703 	bone_data[base_offset + 1] = p_transform[1][0];
3704 	bone_data[base_offset + 2] = 0;
3705 	bone_data[base_offset + 3] = p_transform[2][0];
3706 	bone_data[base_offset + 4] = p_transform[0][1];
3707 	bone_data[base_offset + 5] = p_transform[1][1];
3708 	bone_data[base_offset + 6] = 0;
3709 	bone_data[base_offset + 7] = p_transform[2][1];
3710 
3711 	if (!skeleton->update_list.in_list()) {
3712 		skeleton_update_list.add(&skeleton->update_list);
3713 	}
3714 }
3715 
skeleton_bone_get_transform_2d(RID p_skeleton,int p_bone) const3716 Transform2D RasterizerStorageGLES2::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
3717 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
3718 	ERR_FAIL_COND_V(!skeleton, Transform2D());
3719 
3720 	ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
3721 	ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
3722 
3723 	const float *bone_data = skeleton->bone_data.ptr();
3724 
3725 	Transform2D ret;
3726 
3727 	int base_offset = p_bone * 4 * 2;
3728 
3729 	ret[0][0] = bone_data[base_offset + 0];
3730 	ret[1][0] = bone_data[base_offset + 1];
3731 	ret[2][0] = bone_data[base_offset + 3];
3732 	ret[0][1] = bone_data[base_offset + 4];
3733 	ret[1][1] = bone_data[base_offset + 5];
3734 	ret[2][1] = bone_data[base_offset + 7];
3735 
3736 	return ret;
3737 }
3738 
skeleton_set_base_transform_2d(RID p_skeleton,const Transform2D & p_base_transform)3739 void RasterizerStorageGLES2::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
3740 
3741 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
3742 	ERR_FAIL_COND(!skeleton);
3743 
3744 	skeleton->base_transform_2d = p_base_transform;
3745 }
3746 
_update_skeleton_transform_buffer(const PoolVector<float> & p_data,size_t p_size)3747 void RasterizerStorageGLES2::_update_skeleton_transform_buffer(const PoolVector<float> &p_data, size_t p_size) {
3748 
3749 	glBindBuffer(GL_ARRAY_BUFFER, resources.skeleton_transform_buffer);
3750 
3751 	if (p_size > resources.skeleton_transform_buffer_size) {
3752 		// new requested buffer is bigger, so resizing the GPU buffer
3753 
3754 		resources.skeleton_transform_buffer_size = p_size;
3755 
3756 		glBufferData(GL_ARRAY_BUFFER, p_size * sizeof(float), p_data.read().ptr(), GL_DYNAMIC_DRAW);
3757 	} else {
3758 		glBufferSubData(GL_ARRAY_BUFFER, 0, p_size * sizeof(float), p_data.read().ptr());
3759 	}
3760 
3761 	glBindBuffer(GL_ARRAY_BUFFER, 0);
3762 }
3763 
update_dirty_skeletons()3764 void RasterizerStorageGLES2::update_dirty_skeletons() {
3765 
3766 	if (config.use_skeleton_software)
3767 		return;
3768 
3769 	glActiveTexture(GL_TEXTURE0);
3770 
3771 	while (skeleton_update_list.first()) {
3772 		Skeleton *skeleton = skeleton_update_list.first()->self();
3773 
3774 		if (skeleton->size) {
3775 			glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
3776 
3777 			glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, skeleton->size * (skeleton->use_2d ? 2 : 3), 1, GL_RGBA, GL_FLOAT, skeleton->bone_data.ptr());
3778 		}
3779 
3780 		for (Set<RasterizerScene::InstanceBase *>::Element *E = skeleton->instances.front(); E; E = E->next()) {
3781 			E->get()->base_changed(true, false);
3782 		}
3783 
3784 		skeleton_update_list.remove(skeleton_update_list.first());
3785 	}
3786 }
3787 
3788 /* Light API */
3789 
light_create(VS::LightType p_type)3790 RID RasterizerStorageGLES2::light_create(VS::LightType p_type) {
3791 
3792 	Light *light = memnew(Light);
3793 
3794 	light->type = p_type;
3795 
3796 	light->param[VS::LIGHT_PARAM_ENERGY] = 1.0;
3797 	light->param[VS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
3798 	light->param[VS::LIGHT_PARAM_SPECULAR] = 0.5;
3799 	light->param[VS::LIGHT_PARAM_RANGE] = 1.0;
3800 	light->param[VS::LIGHT_PARAM_SPOT_ANGLE] = 45;
3801 	light->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE] = 45;
3802 	light->param[VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
3803 	light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
3804 	light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
3805 	light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
3806 	light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 0.1;
3807 	light->param[VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE] = 0.1;
3808 
3809 	light->color = Color(1, 1, 1, 1);
3810 	light->shadow = false;
3811 	light->negative = false;
3812 	light->cull_mask = 0xFFFFFFFF;
3813 	light->directional_shadow_mode = VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
3814 	light->omni_shadow_mode = VS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
3815 	light->omni_shadow_detail = VS::LIGHT_OMNI_SHADOW_DETAIL_VERTICAL;
3816 	light->directional_blend_splits = false;
3817 	light->directional_range_mode = VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE;
3818 	light->reverse_cull = false;
3819 	light->use_gi = true;
3820 	light->version = 0;
3821 
3822 	return light_owner.make_rid(light);
3823 }
3824 
light_set_color(RID p_light,const Color & p_color)3825 void RasterizerStorageGLES2::light_set_color(RID p_light, const Color &p_color) {
3826 	Light *light = light_owner.getornull(p_light);
3827 	ERR_FAIL_COND(!light);
3828 
3829 	light->color = p_color;
3830 }
3831 
light_set_param(RID p_light,VS::LightParam p_param,float p_value)3832 void RasterizerStorageGLES2::light_set_param(RID p_light, VS::LightParam p_param, float p_value) {
3833 	Light *light = light_owner.getornull(p_light);
3834 	ERR_FAIL_COND(!light);
3835 	ERR_FAIL_INDEX(p_param, VS::LIGHT_PARAM_MAX);
3836 
3837 	switch (p_param) {
3838 		case VS::LIGHT_PARAM_RANGE:
3839 		case VS::LIGHT_PARAM_SPOT_ANGLE:
3840 		case VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
3841 		case VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
3842 		case VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
3843 		case VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
3844 		case VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
3845 		case VS::LIGHT_PARAM_SHADOW_BIAS: {
3846 			light->version++;
3847 			light->instance_change_notify(true, false);
3848 		} break;
3849 		default: {
3850 		}
3851 	}
3852 
3853 	light->param[p_param] = p_value;
3854 }
3855 
light_set_shadow(RID p_light,bool p_enabled)3856 void RasterizerStorageGLES2::light_set_shadow(RID p_light, bool p_enabled) {
3857 	Light *light = light_owner.getornull(p_light);
3858 	ERR_FAIL_COND(!light);
3859 
3860 	light->shadow = p_enabled;
3861 
3862 	light->version++;
3863 	light->instance_change_notify(true, false);
3864 }
3865 
light_set_shadow_color(RID p_light,const Color & p_color)3866 void RasterizerStorageGLES2::light_set_shadow_color(RID p_light, const Color &p_color) {
3867 	Light *light = light_owner.getornull(p_light);
3868 	ERR_FAIL_COND(!light);
3869 
3870 	light->shadow_color = p_color;
3871 }
3872 
light_set_projector(RID p_light,RID p_texture)3873 void RasterizerStorageGLES2::light_set_projector(RID p_light, RID p_texture) {
3874 	Light *light = light_owner.getornull(p_light);
3875 	ERR_FAIL_COND(!light);
3876 
3877 	light->projector = p_texture;
3878 }
3879 
light_set_negative(RID p_light,bool p_enable)3880 void RasterizerStorageGLES2::light_set_negative(RID p_light, bool p_enable) {
3881 	Light *light = light_owner.getornull(p_light);
3882 	ERR_FAIL_COND(!light);
3883 
3884 	light->negative = p_enable;
3885 }
3886 
light_set_cull_mask(RID p_light,uint32_t p_mask)3887 void RasterizerStorageGLES2::light_set_cull_mask(RID p_light, uint32_t p_mask) {
3888 	Light *light = light_owner.getornull(p_light);
3889 	ERR_FAIL_COND(!light);
3890 
3891 	light->cull_mask = p_mask;
3892 
3893 	light->version++;
3894 	light->instance_change_notify(true, false);
3895 }
3896 
light_set_reverse_cull_face_mode(RID p_light,bool p_enabled)3897 void RasterizerStorageGLES2::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
3898 	Light *light = light_owner.getornull(p_light);
3899 	ERR_FAIL_COND(!light);
3900 
3901 	light->reverse_cull = p_enabled;
3902 
3903 	light->version++;
3904 	light->instance_change_notify(true, false);
3905 }
3906 
light_set_use_gi(RID p_light,bool p_enabled)3907 void RasterizerStorageGLES2::light_set_use_gi(RID p_light, bool p_enabled) {
3908 	Light *light = light_owner.getornull(p_light);
3909 	ERR_FAIL_COND(!light);
3910 
3911 	light->use_gi = p_enabled;
3912 
3913 	light->version++;
3914 	light->instance_change_notify(true, false);
3915 }
3916 
light_omni_set_shadow_mode(RID p_light,VS::LightOmniShadowMode p_mode)3917 void RasterizerStorageGLES2::light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) {
3918 	Light *light = light_owner.getornull(p_light);
3919 	ERR_FAIL_COND(!light);
3920 
3921 	light->omni_shadow_mode = p_mode;
3922 
3923 	light->version++;
3924 	light->instance_change_notify(true, false);
3925 }
3926 
light_omni_get_shadow_mode(RID p_light)3927 VS::LightOmniShadowMode RasterizerStorageGLES2::light_omni_get_shadow_mode(RID p_light) {
3928 	Light *light = light_owner.getornull(p_light);
3929 	ERR_FAIL_COND_V(!light, VS::LIGHT_OMNI_SHADOW_CUBE);
3930 
3931 	return light->omni_shadow_mode;
3932 }
3933 
light_omni_set_shadow_detail(RID p_light,VS::LightOmniShadowDetail p_detail)3934 void RasterizerStorageGLES2::light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail) {
3935 	Light *light = light_owner.getornull(p_light);
3936 	ERR_FAIL_COND(!light);
3937 
3938 	light->omni_shadow_detail = p_detail;
3939 
3940 	light->version++;
3941 	light->instance_change_notify(true, false);
3942 }
3943 
light_directional_set_shadow_mode(RID p_light,VS::LightDirectionalShadowMode p_mode)3944 void RasterizerStorageGLES2::light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) {
3945 	Light *light = light_owner.getornull(p_light);
3946 	ERR_FAIL_COND(!light);
3947 
3948 	light->directional_shadow_mode = p_mode;
3949 
3950 	light->version++;
3951 	light->instance_change_notify(true, false);
3952 }
3953 
light_directional_set_blend_splits(RID p_light,bool p_enable)3954 void RasterizerStorageGLES2::light_directional_set_blend_splits(RID p_light, bool p_enable) {
3955 	Light *light = light_owner.getornull(p_light);
3956 	ERR_FAIL_COND(!light);
3957 
3958 	light->directional_blend_splits = p_enable;
3959 
3960 	light->version++;
3961 	light->instance_change_notify(true, false);
3962 }
3963 
light_directional_get_blend_splits(RID p_light) const3964 bool RasterizerStorageGLES2::light_directional_get_blend_splits(RID p_light) const {
3965 	Light *light = light_owner.getornull(p_light);
3966 	ERR_FAIL_COND_V(!light, false);
3967 	return light->directional_blend_splits;
3968 }
3969 
light_directional_get_shadow_mode(RID p_light)3970 VS::LightDirectionalShadowMode RasterizerStorageGLES2::light_directional_get_shadow_mode(RID p_light) {
3971 	Light *light = light_owner.getornull(p_light);
3972 	ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
3973 	return light->directional_shadow_mode;
3974 }
3975 
light_directional_set_shadow_depth_range_mode(RID p_light,VS::LightDirectionalShadowDepthRangeMode p_range_mode)3976 void RasterizerStorageGLES2::light_directional_set_shadow_depth_range_mode(RID p_light, VS::LightDirectionalShadowDepthRangeMode p_range_mode) {
3977 	Light *light = light_owner.getornull(p_light);
3978 	ERR_FAIL_COND(!light);
3979 
3980 	light->directional_range_mode = p_range_mode;
3981 }
3982 
light_directional_get_shadow_depth_range_mode(RID p_light) const3983 VS::LightDirectionalShadowDepthRangeMode RasterizerStorageGLES2::light_directional_get_shadow_depth_range_mode(RID p_light) const {
3984 	Light *light = light_owner.getornull(p_light);
3985 	ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE);
3986 
3987 	return light->directional_range_mode;
3988 }
3989 
light_get_type(RID p_light) const3990 VS::LightType RasterizerStorageGLES2::light_get_type(RID p_light) const {
3991 	Light *light = light_owner.getornull(p_light);
3992 	ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL);
3993 
3994 	return light->type;
3995 }
3996 
light_get_param(RID p_light,VS::LightParam p_param)3997 float RasterizerStorageGLES2::light_get_param(RID p_light, VS::LightParam p_param) {
3998 	Light *light = light_owner.getornull(p_light);
3999 	ERR_FAIL_COND_V(!light, 0.0);
4000 	ERR_FAIL_INDEX_V(p_param, VS::LIGHT_PARAM_MAX, 0.0);
4001 
4002 	return light->param[p_param];
4003 }
4004 
light_get_color(RID p_light)4005 Color RasterizerStorageGLES2::light_get_color(RID p_light) {
4006 	Light *light = light_owner.getornull(p_light);
4007 	ERR_FAIL_COND_V(!light, Color());
4008 
4009 	return light->color;
4010 }
4011 
light_get_use_gi(RID p_light)4012 bool RasterizerStorageGLES2::light_get_use_gi(RID p_light) {
4013 	Light *light = light_owner.getornull(p_light);
4014 	ERR_FAIL_COND_V(!light, false);
4015 
4016 	return light->use_gi;
4017 }
4018 
light_has_shadow(RID p_light) const4019 bool RasterizerStorageGLES2::light_has_shadow(RID p_light) const {
4020 	Light *light = light_owner.getornull(p_light);
4021 	ERR_FAIL_COND_V(!light, false);
4022 
4023 	return light->shadow;
4024 }
4025 
light_get_version(RID p_light) const4026 uint64_t RasterizerStorageGLES2::light_get_version(RID p_light) const {
4027 	Light *light = light_owner.getornull(p_light);
4028 	ERR_FAIL_COND_V(!light, 0);
4029 
4030 	return light->version;
4031 }
4032 
light_get_aabb(RID p_light) const4033 AABB RasterizerStorageGLES2::light_get_aabb(RID p_light) const {
4034 	Light *light = light_owner.getornull(p_light);
4035 	ERR_FAIL_COND_V(!light, AABB());
4036 
4037 	switch (light->type) {
4038 
4039 		case VS::LIGHT_SPOT: {
4040 			float len = light->param[VS::LIGHT_PARAM_RANGE];
4041 			float size = Math::tan(Math::deg2rad(light->param[VS::LIGHT_PARAM_SPOT_ANGLE])) * len;
4042 			return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
4043 		};
4044 
4045 		case VS::LIGHT_OMNI: {
4046 			float r = light->param[VS::LIGHT_PARAM_RANGE];
4047 			return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
4048 		};
4049 
4050 		case VS::LIGHT_DIRECTIONAL: {
4051 			return AABB();
4052 		};
4053 	}
4054 
4055 	ERR_FAIL_V(AABB());
4056 }
4057 
4058 /* PROBE API */
4059 
reflection_probe_create()4060 RID RasterizerStorageGLES2::reflection_probe_create() {
4061 
4062 	ReflectionProbe *reflection_probe = memnew(ReflectionProbe);
4063 
4064 	reflection_probe->intensity = 1.0;
4065 	reflection_probe->interior_ambient = Color();
4066 	reflection_probe->interior_ambient_energy = 1.0;
4067 	reflection_probe->interior_ambient_probe_contrib = 0.0;
4068 	reflection_probe->max_distance = 0;
4069 	reflection_probe->extents = Vector3(1, 1, 1);
4070 	reflection_probe->origin_offset = Vector3(0, 0, 0);
4071 	reflection_probe->interior = false;
4072 	reflection_probe->box_projection = false;
4073 	reflection_probe->enable_shadows = false;
4074 	reflection_probe->cull_mask = (1 << 20) - 1;
4075 	reflection_probe->update_mode = VS::REFLECTION_PROBE_UPDATE_ONCE;
4076 	reflection_probe->resolution = 128;
4077 
4078 	return reflection_probe_owner.make_rid(reflection_probe);
4079 }
4080 
reflection_probe_set_update_mode(RID p_probe,VS::ReflectionProbeUpdateMode p_mode)4081 void RasterizerStorageGLES2::reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode) {
4082 
4083 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4084 	ERR_FAIL_COND(!reflection_probe);
4085 
4086 	reflection_probe->update_mode = p_mode;
4087 	reflection_probe->instance_change_notify(true, false);
4088 }
4089 
reflection_probe_set_intensity(RID p_probe,float p_intensity)4090 void RasterizerStorageGLES2::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
4091 
4092 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4093 	ERR_FAIL_COND(!reflection_probe);
4094 
4095 	reflection_probe->intensity = p_intensity;
4096 }
4097 
reflection_probe_set_interior_ambient(RID p_probe,const Color & p_ambient)4098 void RasterizerStorageGLES2::reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) {
4099 
4100 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4101 	ERR_FAIL_COND(!reflection_probe);
4102 
4103 	reflection_probe->interior_ambient = p_ambient;
4104 }
4105 
reflection_probe_set_interior_ambient_energy(RID p_probe,float p_energy)4106 void RasterizerStorageGLES2::reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) {
4107 
4108 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4109 	ERR_FAIL_COND(!reflection_probe);
4110 
4111 	reflection_probe->interior_ambient_energy = p_energy;
4112 }
4113 
reflection_probe_set_interior_ambient_probe_contribution(RID p_probe,float p_contrib)4114 void RasterizerStorageGLES2::reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) {
4115 
4116 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4117 	ERR_FAIL_COND(!reflection_probe);
4118 
4119 	reflection_probe->interior_ambient_probe_contrib = p_contrib;
4120 }
4121 
reflection_probe_set_max_distance(RID p_probe,float p_distance)4122 void RasterizerStorageGLES2::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
4123 
4124 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4125 	ERR_FAIL_COND(!reflection_probe);
4126 
4127 	reflection_probe->max_distance = p_distance;
4128 	reflection_probe->instance_change_notify(true, false);
4129 }
reflection_probe_set_extents(RID p_probe,const Vector3 & p_extents)4130 void RasterizerStorageGLES2::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
4131 
4132 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4133 	ERR_FAIL_COND(!reflection_probe);
4134 
4135 	reflection_probe->extents = p_extents;
4136 	reflection_probe->instance_change_notify(true, false);
4137 }
reflection_probe_set_origin_offset(RID p_probe,const Vector3 & p_offset)4138 void RasterizerStorageGLES2::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
4139 
4140 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4141 	ERR_FAIL_COND(!reflection_probe);
4142 
4143 	reflection_probe->origin_offset = p_offset;
4144 	reflection_probe->instance_change_notify(true, false);
4145 }
4146 
reflection_probe_set_as_interior(RID p_probe,bool p_enable)4147 void RasterizerStorageGLES2::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
4148 
4149 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4150 	ERR_FAIL_COND(!reflection_probe);
4151 
4152 	reflection_probe->interior = p_enable;
4153 	reflection_probe->instance_change_notify(true, false);
4154 }
reflection_probe_set_enable_box_projection(RID p_probe,bool p_enable)4155 void RasterizerStorageGLES2::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
4156 
4157 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4158 	ERR_FAIL_COND(!reflection_probe);
4159 
4160 	reflection_probe->box_projection = p_enable;
4161 }
4162 
reflection_probe_set_enable_shadows(RID p_probe,bool p_enable)4163 void RasterizerStorageGLES2::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
4164 
4165 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4166 	ERR_FAIL_COND(!reflection_probe);
4167 
4168 	reflection_probe->enable_shadows = p_enable;
4169 	reflection_probe->instance_change_notify(true, false);
4170 }
reflection_probe_set_cull_mask(RID p_probe,uint32_t p_layers)4171 void RasterizerStorageGLES2::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
4172 
4173 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4174 	ERR_FAIL_COND(!reflection_probe);
4175 
4176 	reflection_probe->cull_mask = p_layers;
4177 	reflection_probe->instance_change_notify(true, false);
4178 }
4179 
reflection_probe_set_resolution(RID p_probe,int p_resolution)4180 void RasterizerStorageGLES2::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
4181 
4182 	ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4183 	ERR_FAIL_COND(!reflection_probe);
4184 
4185 	reflection_probe->resolution = p_resolution;
4186 }
4187 
reflection_probe_get_aabb(RID p_probe) const4188 AABB RasterizerStorageGLES2::reflection_probe_get_aabb(RID p_probe) const {
4189 	const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4190 	ERR_FAIL_COND_V(!reflection_probe, AABB());
4191 
4192 	AABB aabb;
4193 	aabb.position = -reflection_probe->extents;
4194 	aabb.size = reflection_probe->extents * 2.0;
4195 
4196 	return aabb;
4197 }
reflection_probe_get_update_mode(RID p_probe) const4198 VS::ReflectionProbeUpdateMode RasterizerStorageGLES2::reflection_probe_get_update_mode(RID p_probe) const {
4199 
4200 	const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4201 	ERR_FAIL_COND_V(!reflection_probe, VS::REFLECTION_PROBE_UPDATE_ALWAYS);
4202 
4203 	return reflection_probe->update_mode;
4204 }
4205 
reflection_probe_get_cull_mask(RID p_probe) const4206 uint32_t RasterizerStorageGLES2::reflection_probe_get_cull_mask(RID p_probe) const {
4207 
4208 	const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4209 	ERR_FAIL_COND_V(!reflection_probe, 0);
4210 
4211 	return reflection_probe->cull_mask;
4212 }
4213 
reflection_probe_get_extents(RID p_probe) const4214 Vector3 RasterizerStorageGLES2::reflection_probe_get_extents(RID p_probe) const {
4215 
4216 	const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4217 	ERR_FAIL_COND_V(!reflection_probe, Vector3());
4218 
4219 	return reflection_probe->extents;
4220 }
reflection_probe_get_origin_offset(RID p_probe) const4221 Vector3 RasterizerStorageGLES2::reflection_probe_get_origin_offset(RID p_probe) const {
4222 
4223 	const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4224 	ERR_FAIL_COND_V(!reflection_probe, Vector3());
4225 
4226 	return reflection_probe->origin_offset;
4227 }
4228 
reflection_probe_renders_shadows(RID p_probe) const4229 bool RasterizerStorageGLES2::reflection_probe_renders_shadows(RID p_probe) const {
4230 
4231 	const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4232 	ERR_FAIL_COND_V(!reflection_probe, false);
4233 
4234 	return reflection_probe->enable_shadows;
4235 }
4236 
reflection_probe_get_origin_max_distance(RID p_probe) const4237 float RasterizerStorageGLES2::reflection_probe_get_origin_max_distance(RID p_probe) const {
4238 
4239 	const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4240 	ERR_FAIL_COND_V(!reflection_probe, 0);
4241 
4242 	return reflection_probe->max_distance;
4243 }
4244 
reflection_probe_get_resolution(RID p_probe) const4245 int RasterizerStorageGLES2::reflection_probe_get_resolution(RID p_probe) const {
4246 
4247 	const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
4248 	ERR_FAIL_COND_V(!reflection_probe, 0);
4249 
4250 	return reflection_probe->resolution;
4251 }
4252 
gi_probe_create()4253 RID RasterizerStorageGLES2::gi_probe_create() {
4254 	return RID();
4255 }
4256 
gi_probe_set_bounds(RID p_probe,const AABB & p_bounds)4257 void RasterizerStorageGLES2::gi_probe_set_bounds(RID p_probe, const AABB &p_bounds) {
4258 }
4259 
gi_probe_get_bounds(RID p_probe) const4260 AABB RasterizerStorageGLES2::gi_probe_get_bounds(RID p_probe) const {
4261 	return AABB();
4262 }
4263 
gi_probe_set_cell_size(RID p_probe,float p_size)4264 void RasterizerStorageGLES2::gi_probe_set_cell_size(RID p_probe, float p_size) {
4265 }
4266 
gi_probe_get_cell_size(RID p_probe) const4267 float RasterizerStorageGLES2::gi_probe_get_cell_size(RID p_probe) const {
4268 	return 0.0;
4269 }
4270 
gi_probe_set_to_cell_xform(RID p_probe,const Transform & p_xform)4271 void RasterizerStorageGLES2::gi_probe_set_to_cell_xform(RID p_probe, const Transform &p_xform) {
4272 }
4273 
gi_probe_get_to_cell_xform(RID p_probe) const4274 Transform RasterizerStorageGLES2::gi_probe_get_to_cell_xform(RID p_probe) const {
4275 	return Transform();
4276 }
4277 
gi_probe_set_dynamic_data(RID p_probe,const PoolVector<int> & p_data)4278 void RasterizerStorageGLES2::gi_probe_set_dynamic_data(RID p_probe, const PoolVector<int> &p_data) {
4279 }
4280 
gi_probe_get_dynamic_data(RID p_probe) const4281 PoolVector<int> RasterizerStorageGLES2::gi_probe_get_dynamic_data(RID p_probe) const {
4282 	return PoolVector<int>();
4283 }
4284 
gi_probe_set_dynamic_range(RID p_probe,int p_range)4285 void RasterizerStorageGLES2::gi_probe_set_dynamic_range(RID p_probe, int p_range) {
4286 }
4287 
gi_probe_get_dynamic_range(RID p_probe) const4288 int RasterizerStorageGLES2::gi_probe_get_dynamic_range(RID p_probe) const {
4289 	return 0;
4290 }
4291 
gi_probe_set_energy(RID p_probe,float p_range)4292 void RasterizerStorageGLES2::gi_probe_set_energy(RID p_probe, float p_range) {
4293 }
4294 
gi_probe_set_bias(RID p_probe,float p_range)4295 void RasterizerStorageGLES2::gi_probe_set_bias(RID p_probe, float p_range) {
4296 }
4297 
gi_probe_set_normal_bias(RID p_probe,float p_range)4298 void RasterizerStorageGLES2::gi_probe_set_normal_bias(RID p_probe, float p_range) {
4299 }
4300 
gi_probe_set_propagation(RID p_probe,float p_range)4301 void RasterizerStorageGLES2::gi_probe_set_propagation(RID p_probe, float p_range) {
4302 }
4303 
gi_probe_set_interior(RID p_probe,bool p_enable)4304 void RasterizerStorageGLES2::gi_probe_set_interior(RID p_probe, bool p_enable) {
4305 }
4306 
gi_probe_is_interior(RID p_probe) const4307 bool RasterizerStorageGLES2::gi_probe_is_interior(RID p_probe) const {
4308 	return false;
4309 }
4310 
gi_probe_set_compress(RID p_probe,bool p_enable)4311 void RasterizerStorageGLES2::gi_probe_set_compress(RID p_probe, bool p_enable) {
4312 }
4313 
gi_probe_is_compressed(RID p_probe) const4314 bool RasterizerStorageGLES2::gi_probe_is_compressed(RID p_probe) const {
4315 	return false;
4316 }
gi_probe_get_energy(RID p_probe) const4317 float RasterizerStorageGLES2::gi_probe_get_energy(RID p_probe) const {
4318 	return 0;
4319 }
4320 
gi_probe_get_bias(RID p_probe) const4321 float RasterizerStorageGLES2::gi_probe_get_bias(RID p_probe) const {
4322 	return 0;
4323 }
4324 
gi_probe_get_normal_bias(RID p_probe) const4325 float RasterizerStorageGLES2::gi_probe_get_normal_bias(RID p_probe) const {
4326 	return 0;
4327 }
4328 
gi_probe_get_propagation(RID p_probe) const4329 float RasterizerStorageGLES2::gi_probe_get_propagation(RID p_probe) const {
4330 	return 0;
4331 }
4332 
gi_probe_get_version(RID p_probe)4333 uint32_t RasterizerStorageGLES2::gi_probe_get_version(RID p_probe) {
4334 	return 0;
4335 }
4336 
gi_probe_get_dynamic_data_get_preferred_compression() const4337 RasterizerStorage::GIProbeCompression RasterizerStorageGLES2::gi_probe_get_dynamic_data_get_preferred_compression() const {
4338 	return GI_PROBE_UNCOMPRESSED;
4339 }
4340 
gi_probe_dynamic_data_create(int p_width,int p_height,int p_depth,GIProbeCompression p_compression)4341 RID RasterizerStorageGLES2::gi_probe_dynamic_data_create(int p_width, int p_height, int p_depth, GIProbeCompression p_compression) {
4342 	return RID();
4343 }
4344 
gi_probe_dynamic_data_update(RID p_gi_probe_data,int p_depth_slice,int p_slice_count,int p_mipmap,const void * p_data)4345 void RasterizerStorageGLES2::gi_probe_dynamic_data_update(RID p_gi_probe_data, int p_depth_slice, int p_slice_count, int p_mipmap, const void *p_data) {
4346 }
4347 
4348 ///////
4349 
lightmap_capture_create()4350 RID RasterizerStorageGLES2::lightmap_capture_create() {
4351 
4352 	LightmapCapture *capture = memnew(LightmapCapture);
4353 	return lightmap_capture_data_owner.make_rid(capture);
4354 }
4355 
lightmap_capture_set_bounds(RID p_capture,const AABB & p_bounds)4356 void RasterizerStorageGLES2::lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds) {
4357 
4358 	LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4359 	ERR_FAIL_COND(!capture);
4360 	capture->bounds = p_bounds;
4361 	capture->instance_change_notify(true, false);
4362 }
lightmap_capture_get_bounds(RID p_capture) const4363 AABB RasterizerStorageGLES2::lightmap_capture_get_bounds(RID p_capture) const {
4364 
4365 	const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4366 	ERR_FAIL_COND_V(!capture, AABB());
4367 	return capture->bounds;
4368 }
lightmap_capture_set_octree(RID p_capture,const PoolVector<uint8_t> & p_octree)4369 void RasterizerStorageGLES2::lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) {
4370 
4371 	LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4372 	ERR_FAIL_COND(!capture);
4373 
4374 	ERR_FAIL_COND(p_octree.size() == 0 || (p_octree.size() % sizeof(LightmapCaptureOctree)) != 0);
4375 
4376 	capture->octree.resize(p_octree.size() / sizeof(LightmapCaptureOctree));
4377 	if (p_octree.size()) {
4378 		PoolVector<LightmapCaptureOctree>::Write w = capture->octree.write();
4379 		PoolVector<uint8_t>::Read r = p_octree.read();
4380 		copymem(w.ptr(), r.ptr(), p_octree.size());
4381 	}
4382 	capture->instance_change_notify(true, false);
4383 }
lightmap_capture_get_octree(RID p_capture) const4384 PoolVector<uint8_t> RasterizerStorageGLES2::lightmap_capture_get_octree(RID p_capture) const {
4385 
4386 	const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4387 	ERR_FAIL_COND_V(!capture, PoolVector<uint8_t>());
4388 
4389 	if (capture->octree.size() == 0)
4390 		return PoolVector<uint8_t>();
4391 
4392 	PoolVector<uint8_t> ret;
4393 	ret.resize(capture->octree.size() * sizeof(LightmapCaptureOctree));
4394 	{
4395 		PoolVector<LightmapCaptureOctree>::Read r = capture->octree.read();
4396 		PoolVector<uint8_t>::Write w = ret.write();
4397 		copymem(w.ptr(), r.ptr(), ret.size());
4398 	}
4399 
4400 	return ret;
4401 }
4402 
lightmap_capture_set_octree_cell_transform(RID p_capture,const Transform & p_xform)4403 void RasterizerStorageGLES2::lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform) {
4404 	LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4405 	ERR_FAIL_COND(!capture);
4406 	capture->cell_xform = p_xform;
4407 }
4408 
lightmap_capture_get_octree_cell_transform(RID p_capture) const4409 Transform RasterizerStorageGLES2::lightmap_capture_get_octree_cell_transform(RID p_capture) const {
4410 	const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4411 	ERR_FAIL_COND_V(!capture, Transform());
4412 	return capture->cell_xform;
4413 }
4414 
lightmap_capture_set_octree_cell_subdiv(RID p_capture,int p_subdiv)4415 void RasterizerStorageGLES2::lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv) {
4416 	LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4417 	ERR_FAIL_COND(!capture);
4418 	capture->cell_subdiv = p_subdiv;
4419 }
4420 
lightmap_capture_get_octree_cell_subdiv(RID p_capture) const4421 int RasterizerStorageGLES2::lightmap_capture_get_octree_cell_subdiv(RID p_capture) const {
4422 	const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4423 	ERR_FAIL_COND_V(!capture, 0);
4424 	return capture->cell_subdiv;
4425 }
4426 
lightmap_capture_set_energy(RID p_capture,float p_energy)4427 void RasterizerStorageGLES2::lightmap_capture_set_energy(RID p_capture, float p_energy) {
4428 
4429 	LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4430 	ERR_FAIL_COND(!capture);
4431 	capture->energy = p_energy;
4432 }
4433 
lightmap_capture_get_energy(RID p_capture) const4434 float RasterizerStorageGLES2::lightmap_capture_get_energy(RID p_capture) const {
4435 
4436 	const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4437 	ERR_FAIL_COND_V(!capture, 0);
4438 	return capture->energy;
4439 }
4440 
lightmap_capture_get_octree_ptr(RID p_capture) const4441 const PoolVector<RasterizerStorage::LightmapCaptureOctree> *RasterizerStorageGLES2::lightmap_capture_get_octree_ptr(RID p_capture) const {
4442 	const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
4443 	ERR_FAIL_COND_V(!capture, NULL);
4444 	return &capture->octree;
4445 }
4446 
4447 ///////
4448 
particles_create()4449 RID RasterizerStorageGLES2::particles_create() {
4450 	return RID();
4451 }
4452 
particles_set_emitting(RID p_particles,bool p_emitting)4453 void RasterizerStorageGLES2::particles_set_emitting(RID p_particles, bool p_emitting) {
4454 }
4455 
particles_get_emitting(RID p_particles)4456 bool RasterizerStorageGLES2::particles_get_emitting(RID p_particles) {
4457 	return false;
4458 }
4459 
particles_set_amount(RID p_particles,int p_amount)4460 void RasterizerStorageGLES2::particles_set_amount(RID p_particles, int p_amount) {
4461 }
4462 
particles_set_lifetime(RID p_particles,float p_lifetime)4463 void RasterizerStorageGLES2::particles_set_lifetime(RID p_particles, float p_lifetime) {
4464 }
4465 
particles_set_one_shot(RID p_particles,bool p_one_shot)4466 void RasterizerStorageGLES2::particles_set_one_shot(RID p_particles, bool p_one_shot) {
4467 }
4468 
particles_set_pre_process_time(RID p_particles,float p_time)4469 void RasterizerStorageGLES2::particles_set_pre_process_time(RID p_particles, float p_time) {
4470 }
4471 
particles_set_explosiveness_ratio(RID p_particles,float p_ratio)4472 void RasterizerStorageGLES2::particles_set_explosiveness_ratio(RID p_particles, float p_ratio) {
4473 }
4474 
particles_set_randomness_ratio(RID p_particles,float p_ratio)4475 void RasterizerStorageGLES2::particles_set_randomness_ratio(RID p_particles, float p_ratio) {
4476 }
4477 
particles_set_custom_aabb(RID p_particles,const AABB & p_aabb)4478 void RasterizerStorageGLES2::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
4479 }
4480 
particles_set_speed_scale(RID p_particles,float p_scale)4481 void RasterizerStorageGLES2::particles_set_speed_scale(RID p_particles, float p_scale) {
4482 }
4483 
particles_set_use_local_coordinates(RID p_particles,bool p_enable)4484 void RasterizerStorageGLES2::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
4485 }
4486 
particles_set_fixed_fps(RID p_particles,int p_fps)4487 void RasterizerStorageGLES2::particles_set_fixed_fps(RID p_particles, int p_fps) {
4488 }
4489 
particles_set_fractional_delta(RID p_particles,bool p_enable)4490 void RasterizerStorageGLES2::particles_set_fractional_delta(RID p_particles, bool p_enable) {
4491 }
4492 
particles_set_process_material(RID p_particles,RID p_material)4493 void RasterizerStorageGLES2::particles_set_process_material(RID p_particles, RID p_material) {
4494 }
4495 
particles_set_draw_order(RID p_particles,VS::ParticlesDrawOrder p_order)4496 void RasterizerStorageGLES2::particles_set_draw_order(RID p_particles, VS::ParticlesDrawOrder p_order) {
4497 }
4498 
particles_set_draw_passes(RID p_particles,int p_passes)4499 void RasterizerStorageGLES2::particles_set_draw_passes(RID p_particles, int p_passes) {
4500 }
4501 
particles_set_draw_pass_mesh(RID p_particles,int p_pass,RID p_mesh)4502 void RasterizerStorageGLES2::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
4503 }
4504 
particles_restart(RID p_particles)4505 void RasterizerStorageGLES2::particles_restart(RID p_particles) {
4506 }
4507 
particles_request_process(RID p_particles)4508 void RasterizerStorageGLES2::particles_request_process(RID p_particles) {
4509 }
4510 
particles_get_current_aabb(RID p_particles)4511 AABB RasterizerStorageGLES2::particles_get_current_aabb(RID p_particles) {
4512 	return AABB();
4513 }
4514 
particles_get_aabb(RID p_particles) const4515 AABB RasterizerStorageGLES2::particles_get_aabb(RID p_particles) const {
4516 	return AABB();
4517 }
4518 
particles_set_emission_transform(RID p_particles,const Transform & p_transform)4519 void RasterizerStorageGLES2::particles_set_emission_transform(RID p_particles, const Transform &p_transform) {
4520 }
4521 
particles_get_draw_passes(RID p_particles) const4522 int RasterizerStorageGLES2::particles_get_draw_passes(RID p_particles) const {
4523 	return 0;
4524 }
4525 
particles_get_draw_pass_mesh(RID p_particles,int p_pass) const4526 RID RasterizerStorageGLES2::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
4527 	return RID();
4528 }
4529 
update_particles()4530 void RasterizerStorageGLES2::update_particles() {
4531 }
4532 
particles_is_inactive(RID p_particles) const4533 bool RasterizerStorageGLES2::particles_is_inactive(RID p_particles) const {
4534 	return true;
4535 }
4536 
4537 ////////
4538 
instance_add_skeleton(RID p_skeleton,RasterizerScene::InstanceBase * p_instance)4539 void RasterizerStorageGLES2::instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
4540 
4541 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
4542 	ERR_FAIL_COND(!skeleton);
4543 
4544 	skeleton->instances.insert(p_instance);
4545 }
4546 
instance_remove_skeleton(RID p_skeleton,RasterizerScene::InstanceBase * p_instance)4547 void RasterizerStorageGLES2::instance_remove_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
4548 
4549 	Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
4550 	ERR_FAIL_COND(!skeleton);
4551 
4552 	skeleton->instances.erase(p_instance);
4553 }
4554 
instance_add_dependency(RID p_base,RasterizerScene::InstanceBase * p_instance)4555 void RasterizerStorageGLES2::instance_add_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
4556 
4557 	Instantiable *inst = NULL;
4558 	switch (p_instance->base_type) {
4559 		case VS::INSTANCE_MESH: {
4560 			inst = mesh_owner.getornull(p_base);
4561 			ERR_FAIL_COND(!inst);
4562 		} break;
4563 		case VS::INSTANCE_MULTIMESH: {
4564 			inst = multimesh_owner.getornull(p_base);
4565 			ERR_FAIL_COND(!inst);
4566 		} break;
4567 		case VS::INSTANCE_IMMEDIATE: {
4568 			inst = immediate_owner.getornull(p_base);
4569 			ERR_FAIL_COND(!inst);
4570 		} break;
4571 		/*case VS::INSTANCE_PARTICLES: {
4572 			inst = particles_owner.getornull(p_base);
4573 			ERR_FAIL_COND(!inst);
4574 		} break;*/
4575 		case VS::INSTANCE_REFLECTION_PROBE: {
4576 			inst = reflection_probe_owner.getornull(p_base);
4577 			ERR_FAIL_COND(!inst);
4578 		} break;
4579 		case VS::INSTANCE_LIGHT: {
4580 			inst = light_owner.getornull(p_base);
4581 			ERR_FAIL_COND(!inst);
4582 		} break;
4583 		/*case VS::INSTANCE_GI_PROBE: {
4584 			inst = gi_probe_owner.getornull(p_base);
4585 			ERR_FAIL_COND(!inst);
4586 		} break;*/
4587 		case VS::INSTANCE_LIGHTMAP_CAPTURE: {
4588 			inst = lightmap_capture_data_owner.getornull(p_base);
4589 			ERR_FAIL_COND(!inst);
4590 		} break;
4591 		default: {
4592 			ERR_FAIL();
4593 		}
4594 	}
4595 
4596 	inst->instance_list.add(&p_instance->dependency_item);
4597 }
4598 
instance_remove_dependency(RID p_base,RasterizerScene::InstanceBase * p_instance)4599 void RasterizerStorageGLES2::instance_remove_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
4600 
4601 	Instantiable *inst = NULL;
4602 
4603 	switch (p_instance->base_type) {
4604 		case VS::INSTANCE_MESH: {
4605 			inst = mesh_owner.getornull(p_base);
4606 			ERR_FAIL_COND(!inst);
4607 		} break;
4608 		case VS::INSTANCE_MULTIMESH: {
4609 			inst = multimesh_owner.getornull(p_base);
4610 			ERR_FAIL_COND(!inst);
4611 		} break;
4612 		case VS::INSTANCE_IMMEDIATE: {
4613 			inst = immediate_owner.getornull(p_base);
4614 			ERR_FAIL_COND(!inst);
4615 		} break;
4616 		/*case VS::INSTANCE_PARTICLES: {
4617 			inst = particles_owner.getornull(p_base);
4618 			ERR_FAIL_COND(!inst);
4619 		} break;*/
4620 		case VS::INSTANCE_REFLECTION_PROBE: {
4621 			inst = reflection_probe_owner.getornull(p_base);
4622 			ERR_FAIL_COND(!inst);
4623 		} break;
4624 		case VS::INSTANCE_LIGHT: {
4625 			inst = light_owner.getornull(p_base);
4626 			ERR_FAIL_COND(!inst);
4627 		} break;
4628 		/*case VS::INSTANCE_GI_PROBE: {
4629 			inst = gi_probe_owner.getornull(p_base);
4630 			ERR_FAIL_COND(!inst);
4631 		} break; */
4632 		case VS::INSTANCE_LIGHTMAP_CAPTURE: {
4633 			inst = lightmap_capture_data_owner.getornull(p_base);
4634 			ERR_FAIL_COND(!inst);
4635 		} break;
4636 		default: {
4637 			ERR_FAIL();
4638 		}
4639 	}
4640 
4641 	inst->instance_list.remove(&p_instance->dependency_item);
4642 }
4643 
4644 /* RENDER TARGET */
4645 
_render_target_allocate(RenderTarget * rt)4646 void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) {
4647 
4648 	// do not allocate a render target with no size
4649 	if (rt->width <= 0 || rt->height <= 0)
4650 		return;
4651 
4652 	// do not allocate a render target that is attached to the screen
4653 	if (rt->flags[RENDER_TARGET_DIRECT_TO_SCREEN]) {
4654 		rt->fbo = RasterizerStorageGLES2::system_fbo;
4655 		return;
4656 	}
4657 
4658 	GLuint color_internal_format;
4659 	GLuint color_format;
4660 	GLuint color_type = GL_UNSIGNED_BYTE;
4661 	Image::Format image_format;
4662 
4663 	if (rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
4664 #ifdef GLES_OVER_GL
4665 		color_internal_format = GL_RGBA8;
4666 #else
4667 		color_internal_format = GL_RGBA;
4668 #endif
4669 		color_format = GL_RGBA;
4670 		image_format = Image::FORMAT_RGBA8;
4671 	} else {
4672 #ifdef GLES_OVER_GL
4673 		color_internal_format = GL_RGB8;
4674 #else
4675 		color_internal_format = GL_RGB;
4676 #endif
4677 		color_format = GL_RGB;
4678 		image_format = Image::FORMAT_RGB8;
4679 	}
4680 
4681 	rt->used_dof_blur_near = false;
4682 	rt->mip_maps_allocated = false;
4683 
4684 	{
4685 
4686 		/* Front FBO */
4687 
4688 		Texture *texture = texture_owner.getornull(rt->texture);
4689 		ERR_FAIL_COND(!texture);
4690 
4691 		// framebuffer
4692 		glGenFramebuffers(1, &rt->fbo);
4693 		glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
4694 
4695 		// color
4696 		glGenTextures(1, &rt->color);
4697 		glBindTexture(GL_TEXTURE_2D, rt->color);
4698 
4699 		glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, rt->width, rt->height, 0, color_format, color_type, NULL);
4700 
4701 		if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
4702 
4703 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
4704 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
4705 		} else {
4706 
4707 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4708 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4709 		}
4710 
4711 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4712 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4713 
4714 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
4715 
4716 		// depth
4717 
4718 		if (config.support_depth_texture) {
4719 
4720 			glGenTextures(1, &rt->depth);
4721 			glBindTexture(GL_TEXTURE_2D, rt->depth);
4722 			glTexImage2D(GL_TEXTURE_2D, 0, config.depth_internalformat, rt->width, rt->height, 0, GL_DEPTH_COMPONENT, config.depth_type, NULL);
4723 
4724 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4725 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4726 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4727 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4728 
4729 			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
4730 		} else {
4731 
4732 			glGenRenderbuffers(1, &rt->depth);
4733 			glBindRenderbuffer(GL_RENDERBUFFER, rt->depth);
4734 
4735 			glRenderbufferStorage(GL_RENDERBUFFER, config.depth_buffer_internalformat, rt->width, rt->height);
4736 
4737 			glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth);
4738 		}
4739 
4740 		GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
4741 
4742 		if (status != GL_FRAMEBUFFER_COMPLETE) {
4743 
4744 			glDeleteFramebuffers(1, &rt->fbo);
4745 			if (config.support_depth_texture) {
4746 
4747 				glDeleteTextures(1, &rt->depth);
4748 			} else {
4749 
4750 				glDeleteRenderbuffers(1, &rt->depth);
4751 			}
4752 
4753 			glDeleteTextures(1, &rt->color);
4754 			rt->fbo = 0;
4755 			rt->width = 0;
4756 			rt->height = 0;
4757 			rt->color = 0;
4758 			rt->depth = 0;
4759 			texture->tex_id = 0;
4760 			texture->active = false;
4761 			WARN_PRINT("Could not create framebuffer!!");
4762 			return;
4763 		}
4764 
4765 		texture->format = image_format;
4766 		texture->gl_format_cache = color_format;
4767 		texture->gl_type_cache = GL_UNSIGNED_BYTE;
4768 		texture->gl_internal_format_cache = color_internal_format;
4769 		texture->tex_id = rt->color;
4770 		texture->width = rt->width;
4771 		texture->alloc_width = rt->width;
4772 		texture->height = rt->height;
4773 		texture->alloc_height = rt->height;
4774 		texture->active = true;
4775 
4776 		texture_set_flags(rt->texture, texture->flags);
4777 	}
4778 
4779 	/* BACK FBO */
4780 	/* For MSAA */
4781 
4782 #ifndef JAVASCRIPT_ENABLED
4783 	if (rt->msaa >= VS::VIEWPORT_MSAA_2X && rt->msaa <= VS::VIEWPORT_MSAA_16X && config.multisample_supported) {
4784 
4785 		rt->multisample_active = true;
4786 
4787 		static const int msaa_value[] = { 0, 2, 4, 8, 16 };
4788 		int msaa = msaa_value[rt->msaa];
4789 
4790 		int max_samples = 0;
4791 		glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
4792 		if (msaa > max_samples) {
4793 			WARN_PRINTS("MSAA must be <= GL_MAX_SAMPLES, falling-back to GL_MAX_SAMPLES = " + itos(max_samples));
4794 			msaa = max_samples;
4795 		}
4796 
4797 		//regular fbo
4798 		glGenFramebuffers(1, &rt->multisample_fbo);
4799 		glBindFramebuffer(GL_FRAMEBUFFER, rt->multisample_fbo);
4800 
4801 		glGenRenderbuffers(1, &rt->multisample_depth);
4802 		glBindRenderbuffer(GL_RENDERBUFFER, rt->multisample_depth);
4803 		glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, config.depth_buffer_internalformat, rt->width, rt->height);
4804 
4805 		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->multisample_depth);
4806 
4807 #if defined(GLES_OVER_GL) || defined(IPHONE_ENABLED)
4808 
4809 		glGenRenderbuffers(1, &rt->multisample_color);
4810 		glBindRenderbuffer(GL_RENDERBUFFER, rt->multisample_color);
4811 		glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, color_internal_format, rt->width, rt->height);
4812 
4813 		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rt->multisample_color);
4814 #elif ANDROID_ENABLED
4815 		// Render to a texture in android
4816 		glGenTextures(1, &rt->multisample_color);
4817 		glBindTexture(GL_TEXTURE_2D, rt->multisample_color);
4818 
4819 		glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, rt->width, rt->height, 0, color_format, color_type, NULL);
4820 
4821 		// multisample buffer is same size as front buffer, so just use nearest
4822 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4823 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4824 
4825 		glFramebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->multisample_color, 0, msaa);
4826 #endif
4827 
4828 		GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
4829 
4830 		if (status != GL_FRAMEBUFFER_COMPLETE) {
4831 			// Delete allocated resources and default to no MSAA
4832 			WARN_PRINT_ONCE("Cannot allocate back framebuffer for MSAA");
4833 			printf("err status: %x\n", status);
4834 			config.multisample_supported = false;
4835 			rt->multisample_active = false;
4836 
4837 			glDeleteFramebuffers(1, &rt->multisample_fbo);
4838 			rt->multisample_fbo = 0;
4839 
4840 			glDeleteRenderbuffers(1, &rt->multisample_depth);
4841 			rt->multisample_depth = 0;
4842 #ifdef ANDROID_ENABLED
4843 			glDeleteTextures(1, &rt->multisample_color);
4844 #else
4845 			glDeleteRenderbuffers(1, &rt->multisample_color);
4846 #endif
4847 			rt->multisample_color = 0;
4848 		}
4849 
4850 		glBindRenderbuffer(GL_RENDERBUFFER, 0);
4851 		glBindFramebuffer(GL_FRAMEBUFFER, 0);
4852 #ifdef ANDROID_ENABLED
4853 		glBindTexture(GL_TEXTURE_2D, 0);
4854 #endif
4855 
4856 	} else
4857 #endif // JAVASCRIPT_ENABLED
4858 	{
4859 		rt->multisample_active = false;
4860 	}
4861 
4862 	glClearColor(0, 0, 0, 0);
4863 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4864 
4865 	// copy texscreen buffers
4866 	if (!(rt->flags[RasterizerStorage::RENDER_TARGET_NO_SAMPLING])) {
4867 
4868 		glGenTextures(1, &rt->copy_screen_effect.color);
4869 		glBindTexture(GL_TEXTURE_2D, rt->copy_screen_effect.color);
4870 
4871 		if (rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
4872 			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rt->width, rt->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
4873 		} else {
4874 			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rt->width, rt->height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
4875 		}
4876 
4877 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
4878 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
4879 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4880 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4881 
4882 		glGenFramebuffers(1, &rt->copy_screen_effect.fbo);
4883 		glBindFramebuffer(GL_FRAMEBUFFER, rt->copy_screen_effect.fbo);
4884 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->copy_screen_effect.color, 0);
4885 
4886 		glClearColor(0, 0, 0, 0);
4887 		glClear(GL_COLOR_BUFFER_BIT);
4888 
4889 		GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
4890 		if (status != GL_FRAMEBUFFER_COMPLETE) {
4891 			_render_target_clear(rt);
4892 			ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
4893 		}
4894 	}
4895 
4896 	// Allocate mipmap chains for post_process effects
4897 	if (!rt->flags[RasterizerStorage::RENDER_TARGET_NO_3D] && rt->width >= 2 && rt->height >= 2) {
4898 
4899 		for (int i = 0; i < 2; i++) {
4900 
4901 			ERR_FAIL_COND(rt->mip_maps[i].sizes.size());
4902 			int w = rt->width;
4903 			int h = rt->height;
4904 
4905 			if (i > 0) {
4906 				w >>= 1;
4907 				h >>= 1;
4908 			}
4909 
4910 			int level = 0;
4911 			int fb_w = w;
4912 			int fb_h = h;
4913 
4914 			while (true) {
4915 
4916 				RenderTarget::MipMaps::Size mm;
4917 				mm.width = w;
4918 				mm.height = h;
4919 				rt->mip_maps[i].sizes.push_back(mm);
4920 
4921 				w >>= 1;
4922 				h >>= 1;
4923 
4924 				if (w < 2 || h < 2)
4925 					break;
4926 
4927 				level++;
4928 			}
4929 
4930 			GLsizei width = fb_w;
4931 			GLsizei height = fb_h;
4932 
4933 			if (config.render_to_mipmap_supported) {
4934 
4935 				glGenTextures(1, &rt->mip_maps[i].color);
4936 				glBindTexture(GL_TEXTURE_2D, rt->mip_maps[i].color);
4937 
4938 				for (int l = 0; l < level + 1; l++) {
4939 					glTexImage2D(GL_TEXTURE_2D, l, color_internal_format, width, height, 0, color_format, color_type, NULL);
4940 					width = MAX(1, (width / 2));
4941 					height = MAX(1, (height / 2));
4942 				}
4943 #ifdef GLES_OVER_GL
4944 				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
4945 				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level);
4946 #endif
4947 			} else {
4948 
4949 				// Can't render to specific levels of a mipmap in ES 2.0 or Webgl so create a texture for each level
4950 				for (int l = 0; l < level + 1; l++) {
4951 					glGenTextures(1, &rt->mip_maps[i].sizes.write[l].color);
4952 					glBindTexture(GL_TEXTURE_2D, rt->mip_maps[i].sizes[l].color);
4953 					glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, width, height, 0, color_format, color_type, NULL);
4954 					width = MAX(1, (width / 2));
4955 					height = MAX(1, (height / 2));
4956 					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
4957 					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
4958 					glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4959 					glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4960 				}
4961 			}
4962 
4963 			glDisable(GL_SCISSOR_TEST);
4964 			glColorMask(1, 1, 1, 1);
4965 			glDepthMask(GL_TRUE);
4966 
4967 			for (int j = 0; j < rt->mip_maps[i].sizes.size(); j++) {
4968 
4969 				RenderTarget::MipMaps::Size &mm = rt->mip_maps[i].sizes.write[j];
4970 
4971 				glGenFramebuffers(1, &mm.fbo);
4972 				glBindFramebuffer(GL_FRAMEBUFFER, mm.fbo);
4973 
4974 				if (config.render_to_mipmap_supported) {
4975 
4976 					glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->mip_maps[i].color, j);
4977 				} else {
4978 
4979 					glBindTexture(GL_TEXTURE_2D, rt->mip_maps[i].sizes[j].color);
4980 					glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->mip_maps[i].sizes[j].color, 0);
4981 				}
4982 
4983 				bool used_depth = false;
4984 				if (j == 0 && i == 0) { //use always
4985 					if (config.support_depth_texture) {
4986 						glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
4987 					} else {
4988 						glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth);
4989 					}
4990 					used_depth = true;
4991 				}
4992 
4993 				GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
4994 				if (status != GL_FRAMEBUFFER_COMPLETE) {
4995 					WARN_PRINT_ONCE("Cannot allocate mipmaps for 3D post processing effects");
4996 					glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
4997 					return;
4998 				}
4999 
5000 				glClearColor(1.0, 0.0, 1.0, 0.0);
5001 				glClear(GL_COLOR_BUFFER_BIT);
5002 				if (used_depth) {
5003 					glClearDepth(1.0);
5004 					glClear(GL_DEPTH_BUFFER_BIT);
5005 				}
5006 			}
5007 
5008 			rt->mip_maps[i].levels = level;
5009 
5010 			if (config.render_to_mipmap_supported) {
5011 				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
5012 				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
5013 				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
5014 				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
5015 			}
5016 		}
5017 		rt->mip_maps_allocated = true;
5018 	}
5019 
5020 	glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
5021 }
5022 
_render_target_clear(RenderTarget * rt)5023 void RasterizerStorageGLES2::_render_target_clear(RenderTarget *rt) {
5024 
5025 	// there is nothing to clear when DIRECT_TO_SCREEN is used
5026 	if (rt->flags[RENDER_TARGET_DIRECT_TO_SCREEN])
5027 		return;
5028 
5029 	if (rt->fbo) {
5030 		glDeleteFramebuffers(1, &rt->fbo);
5031 		glDeleteTextures(1, &rt->color);
5032 		rt->fbo = 0;
5033 	}
5034 
5035 	if (rt->external.fbo != 0) {
5036 		// free this
5037 		glDeleteFramebuffers(1, &rt->external.fbo);
5038 
5039 		// clean up our texture
5040 		Texture *t = texture_owner.get(rt->external.texture);
5041 		t->alloc_height = 0;
5042 		t->alloc_width = 0;
5043 		t->width = 0;
5044 		t->height = 0;
5045 		t->active = false;
5046 		texture_owner.free(rt->external.texture);
5047 		memdelete(t);
5048 
5049 		rt->external.fbo = 0;
5050 	}
5051 
5052 	if (rt->depth) {
5053 		if (config.support_depth_texture) {
5054 			glDeleteTextures(1, &rt->depth);
5055 		} else {
5056 			glDeleteRenderbuffers(1, &rt->depth);
5057 		}
5058 
5059 		rt->depth = 0;
5060 	}
5061 
5062 	Texture *tex = texture_owner.get(rt->texture);
5063 	tex->alloc_height = 0;
5064 	tex->alloc_width = 0;
5065 	tex->width = 0;
5066 	tex->height = 0;
5067 	tex->active = false;
5068 
5069 	if (rt->copy_screen_effect.color) {
5070 		glDeleteFramebuffers(1, &rt->copy_screen_effect.fbo);
5071 		rt->copy_screen_effect.fbo = 0;
5072 
5073 		glDeleteTextures(1, &rt->copy_screen_effect.color);
5074 		rt->copy_screen_effect.color = 0;
5075 	}
5076 
5077 	for (int i = 0; i < 2; i++) {
5078 		if (rt->mip_maps[i].sizes.size()) {
5079 			for (int j = 0; j < rt->mip_maps[i].sizes.size(); j++) {
5080 				glDeleteFramebuffers(1, &rt->mip_maps[i].sizes[j].fbo);
5081 				glDeleteTextures(1, &rt->mip_maps[i].sizes[j].color);
5082 			}
5083 
5084 			glDeleteTextures(1, &rt->mip_maps[i].color);
5085 			rt->mip_maps[i].sizes.clear();
5086 			rt->mip_maps[i].levels = 0;
5087 			rt->mip_maps[i].color = 0;
5088 		}
5089 	}
5090 
5091 	if (rt->multisample_active) {
5092 		glDeleteFramebuffers(1, &rt->multisample_fbo);
5093 		rt->multisample_fbo = 0;
5094 
5095 		glDeleteRenderbuffers(1, &rt->multisample_depth);
5096 		rt->multisample_depth = 0;
5097 #ifdef ANDROID_ENABLED
5098 		glDeleteTextures(1, &rt->multisample_color);
5099 #else
5100 		glDeleteRenderbuffers(1, &rt->multisample_color);
5101 #endif
5102 		rt->multisample_color = 0;
5103 	}
5104 }
5105 
render_target_create()5106 RID RasterizerStorageGLES2::render_target_create() {
5107 
5108 	RenderTarget *rt = memnew(RenderTarget);
5109 
5110 	Texture *t = memnew(Texture);
5111 
5112 	t->type = VS::TEXTURE_TYPE_2D;
5113 	t->flags = 0;
5114 	t->width = 0;
5115 	t->height = 0;
5116 	t->alloc_height = 0;
5117 	t->alloc_width = 0;
5118 	t->format = Image::FORMAT_R8;
5119 	t->target = GL_TEXTURE_2D;
5120 	t->gl_format_cache = 0;
5121 	t->gl_internal_format_cache = 0;
5122 	t->gl_type_cache = 0;
5123 	t->data_size = 0;
5124 	t->total_data_size = 0;
5125 	t->ignore_mipmaps = false;
5126 	t->compressed = false;
5127 	t->mipmaps = 1;
5128 	t->active = true;
5129 	t->tex_id = 0;
5130 	t->render_target = rt;
5131 
5132 	rt->texture = texture_owner.make_rid(t);
5133 
5134 	return render_target_owner.make_rid(rt);
5135 }
5136 
render_target_set_position(RID p_render_target,int p_x,int p_y)5137 void RasterizerStorageGLES2::render_target_set_position(RID p_render_target, int p_x, int p_y) {
5138 
5139 	RenderTarget *rt = render_target_owner.getornull(p_render_target);
5140 	ERR_FAIL_COND(!rt);
5141 
5142 	rt->x = p_x;
5143 	rt->y = p_y;
5144 }
5145 
render_target_set_size(RID p_render_target,int p_width,int p_height)5146 void RasterizerStorageGLES2::render_target_set_size(RID p_render_target, int p_width, int p_height) {
5147 
5148 	RenderTarget *rt = render_target_owner.getornull(p_render_target);
5149 	ERR_FAIL_COND(!rt);
5150 
5151 	if (p_width == rt->width && p_height == rt->height)
5152 		return;
5153 
5154 	_render_target_clear(rt);
5155 
5156 	rt->width = p_width;
5157 	rt->height = p_height;
5158 
5159 	_render_target_allocate(rt);
5160 }
5161 
render_target_get_texture(RID p_render_target) const5162 RID RasterizerStorageGLES2::render_target_get_texture(RID p_render_target) const {
5163 
5164 	RenderTarget *rt = render_target_owner.getornull(p_render_target);
5165 	ERR_FAIL_COND_V(!rt, RID());
5166 
5167 	if (rt->external.fbo == 0) {
5168 		return rt->texture;
5169 	} else {
5170 		return rt->external.texture;
5171 	}
5172 }
5173 
render_target_set_external_texture(RID p_render_target,unsigned int p_texture_id)5174 void RasterizerStorageGLES2::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) {
5175 	RenderTarget *rt = render_target_owner.getornull(p_render_target);
5176 	ERR_FAIL_COND(!rt);
5177 
5178 	if (p_texture_id == 0) {
5179 		if (rt->external.fbo != 0) {
5180 			// free this
5181 			glDeleteFramebuffers(1, &rt->external.fbo);
5182 
5183 			// and this
5184 			if (rt->external.depth != 0) {
5185 				glDeleteRenderbuffers(1, &rt->external.depth);
5186 			}
5187 
5188 			// clean up our texture
5189 			Texture *t = texture_owner.get(rt->external.texture);
5190 			t->alloc_height = 0;
5191 			t->alloc_width = 0;
5192 			t->width = 0;
5193 			t->height = 0;
5194 			t->active = false;
5195 			texture_owner.free(rt->external.texture);
5196 			memdelete(t);
5197 
5198 			rt->external.fbo = 0;
5199 			rt->external.color = 0;
5200 			rt->external.depth = 0;
5201 		}
5202 	} else {
5203 		Texture *t;
5204 
5205 		if (rt->external.fbo == 0) {
5206 			// create our fbo
5207 			glGenFramebuffers(1, &rt->external.fbo);
5208 			glBindFramebuffer(GL_FRAMEBUFFER, rt->external.fbo);
5209 
5210 			// allocate a texture
5211 			t = memnew(Texture);
5212 
5213 			t->type = VS::TEXTURE_TYPE_2D;
5214 			t->flags = 0;
5215 			t->width = 0;
5216 			t->height = 0;
5217 			t->alloc_height = 0;
5218 			t->alloc_width = 0;
5219 			t->format = Image::FORMAT_RGBA8;
5220 			t->target = GL_TEXTURE_2D;
5221 			t->gl_format_cache = 0;
5222 			t->gl_internal_format_cache = 0;
5223 			t->gl_type_cache = 0;
5224 			t->data_size = 0;
5225 			t->compressed = false;
5226 			t->srgb = false;
5227 			t->total_data_size = 0;
5228 			t->ignore_mipmaps = false;
5229 			t->mipmaps = 1;
5230 			t->active = true;
5231 			t->tex_id = 0;
5232 			t->render_target = rt;
5233 
5234 			rt->external.texture = texture_owner.make_rid(t);
5235 
5236 		} else {
5237 			// bind our frame buffer
5238 			glBindFramebuffer(GL_FRAMEBUFFER, rt->external.fbo);
5239 
5240 			// find our texture
5241 			t = texture_owner.get(rt->external.texture);
5242 		}
5243 
5244 		// set our texture
5245 		t->tex_id = p_texture_id;
5246 		rt->external.color = p_texture_id;
5247 
5248 		// size shouldn't be different
5249 		t->width = rt->width;
5250 		t->height = rt->height;
5251 		t->alloc_height = rt->width;
5252 		t->alloc_width = rt->height;
5253 
5254 		// Switch our texture on our frame buffer
5255 #if ANDROID_ENABLED
5256 		if (rt->msaa >= VS::VIEWPORT_MSAA_EXT_2X && rt->msaa <= VS::VIEWPORT_MSAA_EXT_4X) {
5257 			// This code only applies to the Oculus Go and Oculus Quest. Due to the the tiled nature
5258 			// of the GPU we can do a single render pass by rendering directly into our texture chains
5259 			// texture and apply MSAA as we render.
5260 
5261 			// On any other hardware these two modes are ignored and we do not have any MSAA,
5262 			// the normal MSAA modes need to be used to enable our two pass approach
5263 
5264 			static const int msaa_value[] = { 2, 4 };
5265 			int msaa = msaa_value[rt->msaa - VS::VIEWPORT_MSAA_EXT_2X];
5266 
5267 			if (rt->external.depth == 0) {
5268 				// create a multisample depth buffer, we're not reusing Godots because Godot's didn't get created..
5269 				glGenRenderbuffers(1, &rt->external.depth);
5270 				glBindRenderbuffer(GL_RENDERBUFFER, rt->external.depth);
5271 				glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, config.depth_buffer_internalformat, rt->width, rt->height);
5272 				glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->external.depth);
5273 			}
5274 
5275 			// and set our external texture as the texture...
5276 			glFramebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_texture_id, 0, msaa);
5277 
5278 		} else
5279 #endif
5280 		{
5281 			// set our texture as the destination for our framebuffer
5282 			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_texture_id, 0);
5283 
5284 			// seeing we're rendering into this directly, better also use our depth buffer, just use our existing one :)
5285 			if (config.support_depth_texture) {
5286 				glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
5287 			} else {
5288 				glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth);
5289 			}
5290 		}
5291 
5292 		// check status and unbind
5293 		GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
5294 		glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
5295 
5296 		if (status != GL_FRAMEBUFFER_COMPLETE) {
5297 			printf("framebuffer fail, status: %x\n", status);
5298 		}
5299 
5300 		ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
5301 	}
5302 }
5303 
render_target_set_flag(RID p_render_target,RenderTargetFlags p_flag,bool p_value)5304 void RasterizerStorageGLES2::render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) {
5305 	RenderTarget *rt = render_target_owner.getornull(p_render_target);
5306 	ERR_FAIL_COND(!rt);
5307 
5308 	// When setting DIRECT_TO_SCREEN, you need to clear before the value is set, but allocate after as
5309 	// those functions change how they operate depending on the value of DIRECT_TO_SCREEN
5310 	if (p_flag == RENDER_TARGET_DIRECT_TO_SCREEN && p_value != rt->flags[RENDER_TARGET_DIRECT_TO_SCREEN]) {
5311 		_render_target_clear(rt);
5312 		rt->flags[p_flag] = p_value;
5313 		_render_target_allocate(rt);
5314 	}
5315 
5316 	rt->flags[p_flag] = p_value;
5317 
5318 	switch (p_flag) {
5319 		case RENDER_TARGET_TRANSPARENT:
5320 		case RENDER_TARGET_HDR:
5321 		case RENDER_TARGET_NO_3D:
5322 		case RENDER_TARGET_NO_SAMPLING:
5323 		case RENDER_TARGET_NO_3D_EFFECTS: {
5324 			//must reset for these formats
5325 			_render_target_clear(rt);
5326 			_render_target_allocate(rt);
5327 
5328 		} break;
5329 		default: {
5330 		}
5331 	}
5332 }
5333 
render_target_was_used(RID p_render_target)5334 bool RasterizerStorageGLES2::render_target_was_used(RID p_render_target) {
5335 	RenderTarget *rt = render_target_owner.getornull(p_render_target);
5336 	ERR_FAIL_COND_V(!rt, false);
5337 
5338 	return rt->used_in_frame;
5339 }
5340 
render_target_clear_used(RID p_render_target)5341 void RasterizerStorageGLES2::render_target_clear_used(RID p_render_target) {
5342 	RenderTarget *rt = render_target_owner.getornull(p_render_target);
5343 	ERR_FAIL_COND(!rt);
5344 
5345 	rt->used_in_frame = false;
5346 }
5347 
render_target_set_msaa(RID p_render_target,VS::ViewportMSAA p_msaa)5348 void RasterizerStorageGLES2::render_target_set_msaa(RID p_render_target, VS::ViewportMSAA p_msaa) {
5349 	RenderTarget *rt = render_target_owner.getornull(p_render_target);
5350 	ERR_FAIL_COND(!rt);
5351 
5352 	if (rt->msaa == p_msaa)
5353 		return;
5354 
5355 	if (!config.multisample_supported) {
5356 		ERR_PRINT("MSAA not supported on this hardware.");
5357 		return;
5358 	}
5359 
5360 	_render_target_clear(rt);
5361 	rt->msaa = p_msaa;
5362 	_render_target_allocate(rt);
5363 }
5364 
5365 /* CANVAS SHADOW */
5366 
canvas_light_shadow_buffer_create(int p_width)5367 RID RasterizerStorageGLES2::canvas_light_shadow_buffer_create(int p_width) {
5368 
5369 	CanvasLightShadow *cls = memnew(CanvasLightShadow);
5370 
5371 	if (p_width > config.max_texture_size)
5372 		p_width = config.max_texture_size;
5373 
5374 	cls->size = p_width;
5375 	cls->height = 16;
5376 
5377 	glActiveTexture(GL_TEXTURE0);
5378 
5379 	glGenFramebuffers(1, &cls->fbo);
5380 	glBindFramebuffer(GL_FRAMEBUFFER, cls->fbo);
5381 
5382 	glGenRenderbuffers(1, &cls->depth);
5383 	glBindRenderbuffer(GL_RENDERBUFFER, cls->depth);
5384 	glRenderbufferStorage(GL_RENDERBUFFER, config.depth_buffer_internalformat, cls->size, cls->height);
5385 	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, cls->depth);
5386 
5387 	glGenTextures(1, &cls->distance);
5388 	glBindTexture(GL_TEXTURE_2D, cls->distance);
5389 	if (config.use_rgba_2d_shadows) {
5390 		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cls->size, cls->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
5391 	} else {
5392 #ifdef GLES_OVER_GL
5393 		glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, cls->size, cls->height, 0, _RED_OES, GL_FLOAT, NULL);
5394 #else
5395 		glTexImage2D(GL_TEXTURE_2D, 0, GL_FLOAT, cls->size, cls->height, 0, _RED_OES, GL_FLOAT, NULL);
5396 #endif
5397 	}
5398 
5399 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
5400 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
5401 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
5402 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
5403 	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, cls->distance, 0);
5404 
5405 	GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
5406 	//printf("errnum: %x\n",status);
5407 	glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
5408 
5409 	if (status != GL_FRAMEBUFFER_COMPLETE) {
5410 		memdelete(cls);
5411 		ERR_FAIL_COND_V(status != GL_FRAMEBUFFER_COMPLETE, RID());
5412 	}
5413 
5414 	return canvas_light_shadow_owner.make_rid(cls);
5415 }
5416 
5417 /* LIGHT SHADOW MAPPING */
5418 
canvas_light_occluder_create()5419 RID RasterizerStorageGLES2::canvas_light_occluder_create() {
5420 
5421 	CanvasOccluder *co = memnew(CanvasOccluder);
5422 	co->index_id = 0;
5423 	co->vertex_id = 0;
5424 	co->len = 0;
5425 
5426 	return canvas_occluder_owner.make_rid(co);
5427 }
5428 
canvas_light_occluder_set_polylines(RID p_occluder,const PoolVector<Vector2> & p_lines)5429 void RasterizerStorageGLES2::canvas_light_occluder_set_polylines(RID p_occluder, const PoolVector<Vector2> &p_lines) {
5430 
5431 	CanvasOccluder *co = canvas_occluder_owner.get(p_occluder);
5432 	ERR_FAIL_COND(!co);
5433 
5434 	co->lines = p_lines;
5435 
5436 	if (p_lines.size() != co->len) {
5437 
5438 		if (co->index_id)
5439 			glDeleteBuffers(1, &co->index_id);
5440 		if (co->vertex_id)
5441 			glDeleteBuffers(1, &co->vertex_id);
5442 
5443 		co->index_id = 0;
5444 		co->vertex_id = 0;
5445 		co->len = 0;
5446 	}
5447 
5448 	if (p_lines.size()) {
5449 
5450 		PoolVector<float> geometry;
5451 		PoolVector<uint16_t> indices;
5452 		int lc = p_lines.size();
5453 
5454 		geometry.resize(lc * 6);
5455 		indices.resize(lc * 3);
5456 
5457 		PoolVector<float>::Write vw = geometry.write();
5458 		PoolVector<uint16_t>::Write iw = indices.write();
5459 
5460 		PoolVector<Vector2>::Read lr = p_lines.read();
5461 
5462 		const int POLY_HEIGHT = 16384;
5463 
5464 		for (int i = 0; i < lc / 2; i++) {
5465 
5466 			vw[i * 12 + 0] = lr[i * 2 + 0].x;
5467 			vw[i * 12 + 1] = lr[i * 2 + 0].y;
5468 			vw[i * 12 + 2] = POLY_HEIGHT;
5469 
5470 			vw[i * 12 + 3] = lr[i * 2 + 1].x;
5471 			vw[i * 12 + 4] = lr[i * 2 + 1].y;
5472 			vw[i * 12 + 5] = POLY_HEIGHT;
5473 
5474 			vw[i * 12 + 6] = lr[i * 2 + 1].x;
5475 			vw[i * 12 + 7] = lr[i * 2 + 1].y;
5476 			vw[i * 12 + 8] = -POLY_HEIGHT;
5477 
5478 			vw[i * 12 + 9] = lr[i * 2 + 0].x;
5479 			vw[i * 12 + 10] = lr[i * 2 + 0].y;
5480 			vw[i * 12 + 11] = -POLY_HEIGHT;
5481 
5482 			iw[i * 6 + 0] = i * 4 + 0;
5483 			iw[i * 6 + 1] = i * 4 + 1;
5484 			iw[i * 6 + 2] = i * 4 + 2;
5485 
5486 			iw[i * 6 + 3] = i * 4 + 2;
5487 			iw[i * 6 + 4] = i * 4 + 3;
5488 			iw[i * 6 + 5] = i * 4 + 0;
5489 		}
5490 
5491 		//if same buffer len is being set, just use BufferSubData to avoid a pipeline flush
5492 
5493 		if (!co->vertex_id) {
5494 			glGenBuffers(1, &co->vertex_id);
5495 			glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
5496 			glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(real_t), vw.ptr(), GL_STATIC_DRAW);
5497 		} else {
5498 
5499 			glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
5500 			glBufferSubData(GL_ARRAY_BUFFER, 0, lc * 6 * sizeof(real_t), vw.ptr());
5501 		}
5502 
5503 		glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
5504 
5505 		if (!co->index_id) {
5506 
5507 			glGenBuffers(1, &co->index_id);
5508 			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
5509 			glBufferData(GL_ELEMENT_ARRAY_BUFFER, lc * 3 * sizeof(uint16_t), iw.ptr(), GL_DYNAMIC_DRAW);
5510 		} else {
5511 
5512 			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
5513 			glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, lc * 3 * sizeof(uint16_t), iw.ptr());
5514 		}
5515 
5516 		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind
5517 
5518 		co->len = lc;
5519 	}
5520 }
5521 
get_base_type(RID p_rid) const5522 VS::InstanceType RasterizerStorageGLES2::get_base_type(RID p_rid) const {
5523 
5524 	if (mesh_owner.owns(p_rid)) {
5525 		return VS::INSTANCE_MESH;
5526 	} else if (light_owner.owns(p_rid)) {
5527 		return VS::INSTANCE_LIGHT;
5528 	} else if (multimesh_owner.owns(p_rid)) {
5529 		return VS::INSTANCE_MULTIMESH;
5530 	} else if (immediate_owner.owns(p_rid)) {
5531 		return VS::INSTANCE_IMMEDIATE;
5532 	} else if (reflection_probe_owner.owns(p_rid)) {
5533 		return VS::INSTANCE_REFLECTION_PROBE;
5534 	} else if (lightmap_capture_data_owner.owns(p_rid)) {
5535 		return VS::INSTANCE_LIGHTMAP_CAPTURE;
5536 	} else {
5537 		return VS::INSTANCE_NONE;
5538 	}
5539 }
5540 
free(RID p_rid)5541 bool RasterizerStorageGLES2::free(RID p_rid) {
5542 
5543 	if (render_target_owner.owns(p_rid)) {
5544 
5545 		RenderTarget *rt = render_target_owner.getornull(p_rid);
5546 		_render_target_clear(rt);
5547 
5548 		Texture *t = texture_owner.get(rt->texture);
5549 		texture_owner.free(rt->texture);
5550 		memdelete(t);
5551 		render_target_owner.free(p_rid);
5552 		memdelete(rt);
5553 
5554 		return true;
5555 	} else if (texture_owner.owns(p_rid)) {
5556 
5557 		Texture *t = texture_owner.get(p_rid);
5558 		// can't free a render target texture
5559 		ERR_FAIL_COND_V(t->render_target, true);
5560 
5561 		info.texture_mem -= t->total_data_size;
5562 		texture_owner.free(p_rid);
5563 		memdelete(t);
5564 
5565 		return true;
5566 	} else if (sky_owner.owns(p_rid)) {
5567 
5568 		Sky *sky = sky_owner.get(p_rid);
5569 		sky_set_texture(p_rid, RID(), 256);
5570 		sky_owner.free(p_rid);
5571 		memdelete(sky);
5572 
5573 		return true;
5574 	} else if (shader_owner.owns(p_rid)) {
5575 
5576 		Shader *shader = shader_owner.get(p_rid);
5577 
5578 		if (shader->shader && shader->custom_code_id) {
5579 			shader->shader->free_custom_shader(shader->custom_code_id);
5580 		}
5581 
5582 		if (shader->dirty_list.in_list()) {
5583 			_shader_dirty_list.remove(&shader->dirty_list);
5584 		}
5585 
5586 		while (shader->materials.first()) {
5587 			Material *m = shader->materials.first()->self();
5588 
5589 			m->shader = NULL;
5590 			_material_make_dirty(m);
5591 
5592 			shader->materials.remove(shader->materials.first());
5593 		}
5594 
5595 		shader_owner.free(p_rid);
5596 		memdelete(shader);
5597 
5598 		return true;
5599 	} else if (material_owner.owns(p_rid)) {
5600 
5601 		Material *m = material_owner.get(p_rid);
5602 
5603 		if (m->shader) {
5604 			m->shader->materials.remove(&m->list);
5605 		}
5606 
5607 		for (Map<Geometry *, int>::Element *E = m->geometry_owners.front(); E; E = E->next()) {
5608 			Geometry *g = E->key();
5609 			g->material = RID();
5610 		}
5611 
5612 		for (Map<RasterizerScene::InstanceBase *, int>::Element *E = m->instance_owners.front(); E; E = E->next()) {
5613 
5614 			RasterizerScene::InstanceBase *ins = E->key();
5615 
5616 			if (ins->material_override == p_rid) {
5617 				ins->material_override = RID();
5618 			}
5619 
5620 			for (int i = 0; i < ins->materials.size(); i++) {
5621 				if (ins->materials[i] == p_rid) {
5622 					ins->materials.write[i] = RID();
5623 				}
5624 			}
5625 		}
5626 
5627 		material_owner.free(p_rid);
5628 		memdelete(m);
5629 
5630 		return true;
5631 	} else if (skeleton_owner.owns(p_rid)) {
5632 
5633 		Skeleton *s = skeleton_owner.get(p_rid);
5634 
5635 		if (s->update_list.in_list()) {
5636 			skeleton_update_list.remove(&s->update_list);
5637 		}
5638 
5639 		for (Set<RasterizerScene::InstanceBase *>::Element *E = s->instances.front(); E; E = E->next()) {
5640 			E->get()->skeleton = RID();
5641 		}
5642 
5643 		skeleton_allocate(p_rid, 0, false);
5644 
5645 		if (s->tex_id) {
5646 			glDeleteTextures(1, &s->tex_id);
5647 		}
5648 
5649 		skeleton_owner.free(p_rid);
5650 		memdelete(s);
5651 
5652 		return true;
5653 	} else if (mesh_owner.owns(p_rid)) {
5654 
5655 		Mesh *mesh = mesh_owner.get(p_rid);
5656 
5657 		mesh->instance_remove_deps();
5658 		mesh_clear(p_rid);
5659 
5660 		while (mesh->multimeshes.first()) {
5661 			MultiMesh *multimesh = mesh->multimeshes.first()->self();
5662 			multimesh->mesh = RID();
5663 			multimesh->dirty_aabb = true;
5664 
5665 			mesh->multimeshes.remove(mesh->multimeshes.first());
5666 
5667 			if (!multimesh->update_list.in_list()) {
5668 				multimesh_update_list.add(&multimesh->update_list);
5669 			}
5670 		}
5671 
5672 		mesh_owner.free(p_rid);
5673 		memdelete(mesh);
5674 
5675 		return true;
5676 	} else if (multimesh_owner.owns(p_rid)) {
5677 
5678 		MultiMesh *multimesh = multimesh_owner.get(p_rid);
5679 		multimesh->instance_remove_deps();
5680 
5681 		if (multimesh->mesh.is_valid()) {
5682 			Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
5683 			if (mesh) {
5684 				mesh->multimeshes.remove(&multimesh->mesh_list);
5685 			}
5686 		}
5687 
5688 		multimesh_allocate(p_rid, 0, VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_NONE);
5689 
5690 		update_dirty_multimeshes();
5691 
5692 		multimesh_owner.free(p_rid);
5693 		memdelete(multimesh);
5694 
5695 		return true;
5696 	} else if (immediate_owner.owns(p_rid)) {
5697 		Immediate *im = immediate_owner.get(p_rid);
5698 		im->instance_remove_deps();
5699 
5700 		immediate_owner.free(p_rid);
5701 		memdelete(im);
5702 
5703 		return true;
5704 	} else if (light_owner.owns(p_rid)) {
5705 
5706 		Light *light = light_owner.get(p_rid);
5707 		light->instance_remove_deps();
5708 
5709 		light_owner.free(p_rid);
5710 		memdelete(light);
5711 
5712 		return true;
5713 	} else if (reflection_probe_owner.owns(p_rid)) {
5714 
5715 		// delete the texture
5716 		ReflectionProbe *reflection_probe = reflection_probe_owner.get(p_rid);
5717 		reflection_probe->instance_remove_deps();
5718 
5719 		reflection_probe_owner.free(p_rid);
5720 		memdelete(reflection_probe);
5721 
5722 		return true;
5723 	} else if (lightmap_capture_data_owner.owns(p_rid)) {
5724 
5725 		// delete the texture
5726 		LightmapCapture *lightmap_capture = lightmap_capture_data_owner.get(p_rid);
5727 		lightmap_capture->instance_remove_deps();
5728 
5729 		lightmap_capture_data_owner.free(p_rid);
5730 		memdelete(lightmap_capture);
5731 		return true;
5732 
5733 	} else if (canvas_occluder_owner.owns(p_rid)) {
5734 
5735 		CanvasOccluder *co = canvas_occluder_owner.get(p_rid);
5736 		if (co->index_id)
5737 			glDeleteBuffers(1, &co->index_id);
5738 		if (co->vertex_id)
5739 			glDeleteBuffers(1, &co->vertex_id);
5740 
5741 		canvas_occluder_owner.free(p_rid);
5742 		memdelete(co);
5743 
5744 		return true;
5745 
5746 	} else if (canvas_light_shadow_owner.owns(p_rid)) {
5747 
5748 		CanvasLightShadow *cls = canvas_light_shadow_owner.get(p_rid);
5749 		glDeleteFramebuffers(1, &cls->fbo);
5750 		glDeleteRenderbuffers(1, &cls->depth);
5751 		glDeleteTextures(1, &cls->distance);
5752 		canvas_light_shadow_owner.free(p_rid);
5753 		memdelete(cls);
5754 
5755 		return true;
5756 	} else {
5757 		return false;
5758 	}
5759 }
5760 
has_os_feature(const String & p_feature) const5761 bool RasterizerStorageGLES2::has_os_feature(const String &p_feature) const {
5762 
5763 	if (p_feature == "pvrtc")
5764 		return config.pvrtc_supported;
5765 
5766 	if (p_feature == "s3tc")
5767 		return config.s3tc_supported;
5768 
5769 	if (p_feature == "etc")
5770 		return config.etc1_supported;
5771 
5772 	return false;
5773 }
5774 
5775 ////////////////////////////////////////////
5776 
set_debug_generate_wireframes(bool p_generate)5777 void RasterizerStorageGLES2::set_debug_generate_wireframes(bool p_generate) {
5778 }
5779 
render_info_begin_capture()5780 void RasterizerStorageGLES2::render_info_begin_capture() {
5781 
5782 	info.snap = info.render;
5783 }
5784 
render_info_end_capture()5785 void RasterizerStorageGLES2::render_info_end_capture() {
5786 
5787 	info.snap.object_count = info.render.object_count - info.snap.object_count;
5788 	info.snap.draw_call_count = info.render.draw_call_count - info.snap.draw_call_count;
5789 	info.snap.material_switch_count = info.render.material_switch_count - info.snap.material_switch_count;
5790 	info.snap.surface_switch_count = info.render.surface_switch_count - info.snap.surface_switch_count;
5791 	info.snap.shader_rebind_count = info.render.shader_rebind_count - info.snap.shader_rebind_count;
5792 	info.snap.vertices_count = info.render.vertices_count - info.snap.vertices_count;
5793 	info.snap._2d_item_count = info.render._2d_item_count - info.snap._2d_item_count;
5794 	info.snap._2d_draw_call_count = info.render._2d_draw_call_count - info.snap._2d_draw_call_count;
5795 }
5796 
get_captured_render_info(VS::RenderInfo p_info)5797 int RasterizerStorageGLES2::get_captured_render_info(VS::RenderInfo p_info) {
5798 
5799 	switch (p_info) {
5800 		case VS::INFO_OBJECTS_IN_FRAME: {
5801 			return info.snap.object_count;
5802 		} break;
5803 		case VS::INFO_VERTICES_IN_FRAME: {
5804 			return info.snap.vertices_count;
5805 		} break;
5806 		case VS::INFO_MATERIAL_CHANGES_IN_FRAME: {
5807 			return info.snap.material_switch_count;
5808 		} break;
5809 		case VS::INFO_SHADER_CHANGES_IN_FRAME: {
5810 			return info.snap.shader_rebind_count;
5811 		} break;
5812 		case VS::INFO_SURFACE_CHANGES_IN_FRAME: {
5813 			return info.snap.surface_switch_count;
5814 		} break;
5815 		case VS::INFO_DRAW_CALLS_IN_FRAME: {
5816 			return info.snap.draw_call_count;
5817 		} break;
5818 		case VS::INFO_2D_ITEMS_IN_FRAME: {
5819 			return info.snap._2d_item_count;
5820 		} break;
5821 		case VS::INFO_2D_DRAW_CALLS_IN_FRAME: {
5822 			return info.snap._2d_draw_call_count;
5823 		} break;
5824 		default: {
5825 			return get_render_info(p_info);
5826 		}
5827 	}
5828 }
5829 
get_render_info(VS::RenderInfo p_info)5830 int RasterizerStorageGLES2::get_render_info(VS::RenderInfo p_info) {
5831 	switch (p_info) {
5832 		case VS::INFO_OBJECTS_IN_FRAME:
5833 			return info.render_final.object_count;
5834 		case VS::INFO_VERTICES_IN_FRAME:
5835 			return info.render_final.vertices_count;
5836 		case VS::INFO_MATERIAL_CHANGES_IN_FRAME:
5837 			return info.render_final.material_switch_count;
5838 		case VS::INFO_SHADER_CHANGES_IN_FRAME:
5839 			return info.render_final.shader_rebind_count;
5840 		case VS::INFO_SURFACE_CHANGES_IN_FRAME:
5841 			return info.render_final.surface_switch_count;
5842 		case VS::INFO_DRAW_CALLS_IN_FRAME:
5843 			return info.render_final.draw_call_count;
5844 		case VS::INFO_2D_ITEMS_IN_FRAME:
5845 			return info.render_final._2d_item_count;
5846 		case VS::INFO_2D_DRAW_CALLS_IN_FRAME:
5847 			return info.render_final._2d_draw_call_count;
5848 		case VS::INFO_USAGE_VIDEO_MEM_TOTAL:
5849 			return 0; //no idea
5850 		case VS::INFO_VIDEO_MEM_USED:
5851 			return info.vertex_mem + info.texture_mem;
5852 		case VS::INFO_TEXTURE_MEM_USED:
5853 			return info.texture_mem;
5854 		case VS::INFO_VERTEX_MEM_USED:
5855 			return info.vertex_mem;
5856 		default:
5857 			return 0; //no idea either
5858 	}
5859 }
5860 
get_video_adapter_name() const5861 String RasterizerStorageGLES2::get_video_adapter_name() const {
5862 
5863 	return (const char *)glGetString(GL_RENDERER);
5864 }
5865 
get_video_adapter_vendor() const5866 String RasterizerStorageGLES2::get_video_adapter_vendor() const {
5867 
5868 	return (const char *)glGetString(GL_VENDOR);
5869 }
5870 
initialize()5871 void RasterizerStorageGLES2::initialize() {
5872 	RasterizerStorageGLES2::system_fbo = 0;
5873 
5874 	{
5875 
5876 		const GLubyte *extension_string = glGetString(GL_EXTENSIONS);
5877 
5878 		Vector<String> extensions = String((const char *)extension_string).split(" ");
5879 
5880 		for (int i = 0; i < extensions.size(); i++) {
5881 			config.extensions.insert(extensions[i]);
5882 		}
5883 	}
5884 
5885 	config.keep_original_textures = false;
5886 	config.shrink_textures_x2 = false;
5887 	config.depth_internalformat = GL_DEPTH_COMPONENT;
5888 	config.depth_type = GL_UNSIGNED_INT;
5889 
5890 #ifdef GLES_OVER_GL
5891 	config.float_texture_supported = true;
5892 	config.s3tc_supported = true;
5893 	config.pvrtc_supported = false;
5894 	config.etc1_supported = false;
5895 	config.support_npot_repeat_mipmap = true;
5896 	config.depth_buffer_internalformat = GL_DEPTH_COMPONENT24;
5897 #else
5898 	config.float_texture_supported = config.extensions.has("GL_ARB_texture_float") || config.extensions.has("GL_OES_texture_float");
5899 	config.s3tc_supported = config.extensions.has("GL_EXT_texture_compression_s3tc") || config.extensions.has("WEBGL_compressed_texture_s3tc");
5900 	config.etc1_supported = config.extensions.has("GL_OES_compressed_ETC1_RGB8_texture") || config.extensions.has("WEBGL_compressed_texture_etc1");
5901 	config.pvrtc_supported = config.extensions.has("IMG_texture_compression_pvrtc") || config.extensions.has("WEBGL_compressed_texture_pvrtc");
5902 	config.support_npot_repeat_mipmap = config.extensions.has("GL_OES_texture_npot");
5903 
5904 #ifdef JAVASCRIPT_ENABLED
5905 	// RenderBuffer internal format must be 16 bits in WebGL,
5906 	// but depth_texture should default to 32 always
5907 	// if the implementation doesn't support 32, it should just quietly use 16 instead
5908 	// https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/
5909 	config.depth_buffer_internalformat = GL_DEPTH_COMPONENT16;
5910 	config.depth_type = GL_UNSIGNED_INT;
5911 #else
5912 	// on mobile check for 24 bit depth support for RenderBufferStorage
5913 	if (config.extensions.has("GL_OES_depth24")) {
5914 		config.depth_buffer_internalformat = _DEPTH_COMPONENT24_OES;
5915 		config.depth_type = GL_UNSIGNED_INT;
5916 	} else {
5917 		config.depth_buffer_internalformat = GL_DEPTH_COMPONENT16;
5918 		config.depth_type = GL_UNSIGNED_SHORT;
5919 	}
5920 #endif
5921 #endif
5922 
5923 #ifndef GLES_OVER_GL
5924 	//Manually load extensions for android and ios
5925 
5926 #ifdef IPHONE_ENABLED
5927 	// appears that IPhone doesn't need to dlopen TODO: test this rigorously before removing
5928 	//void *gles2_lib = dlopen(NULL, RTLD_LAZY);
5929 	//glRenderbufferStorageMultisampleAPPLE = dlsym(gles2_lib, "glRenderbufferStorageMultisampleAPPLE");
5930 	//glResolveMultisampleFramebufferAPPLE = dlsym(gles2_lib, "glResolveMultisampleFramebufferAPPLE");
5931 #elif ANDROID_ENABLED
5932 
5933 	void *gles2_lib = dlopen("libGLESv2.so", RTLD_LAZY);
5934 	glRenderbufferStorageMultisampleEXT = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLEEXTPROC)dlsym(gles2_lib, "glRenderbufferStorageMultisampleEXT");
5935 	glFramebufferTexture2DMultisampleEXT = (PFNGLFRAMEBUFFERTEXTURE2DMULTISAMPLEEXTPROC)dlsym(gles2_lib, "glFramebufferTexture2DMultisampleEXT");
5936 #endif
5937 #endif
5938 
5939 	// Check for multisample support
5940 	config.multisample_supported = config.extensions.has("GL_EXT_framebuffer_multisample") || config.extensions.has("GL_EXT_multisampled_render_to_texture") || config.extensions.has("GL_APPLE_framebuffer_multisample");
5941 
5942 #ifdef GLES_OVER_GL
5943 	//TODO: causes huge problems with desktop video drivers. Making false for now, needs to be true to render SCREEN_TEXTURE mipmaps
5944 	config.render_to_mipmap_supported = false;
5945 #else
5946 	//check if mipmaps can be used for SCREEN_TEXTURE and Glow on Mobile and web platforms
5947 	config.render_to_mipmap_supported = config.extensions.has("GL_OES_fbo_render_mipmap") && config.extensions.has("GL_EXT_texture_lod");
5948 #endif
5949 
5950 #ifdef GLES_OVER_GL
5951 	config.use_rgba_2d_shadows = false;
5952 	config.support_depth_texture = true;
5953 	config.use_rgba_3d_shadows = false;
5954 	config.support_depth_cubemaps = true;
5955 #else
5956 	config.use_rgba_2d_shadows = !(config.float_texture_supported && config.extensions.has("GL_EXT_texture_rg"));
5957 	config.support_depth_texture = config.extensions.has("GL_OES_depth_texture") || config.extensions.has("WEBGL_depth_texture");
5958 	config.use_rgba_3d_shadows = !config.support_depth_texture;
5959 	config.support_depth_cubemaps = config.extensions.has("GL_OES_depth_texture_cube_map");
5960 #endif
5961 
5962 #ifdef GLES_OVER_GL
5963 	config.support_32_bits_indices = true;
5964 #else
5965 	config.support_32_bits_indices = config.extensions.has("GL_OES_element_index_uint");
5966 #endif
5967 
5968 #ifdef GLES_OVER_GL
5969 	config.support_write_depth = true;
5970 #elif defined(JAVASCRIPT_ENABLED)
5971 	config.support_write_depth = false;
5972 #else
5973 	config.support_write_depth = config.extensions.has("GL_EXT_frag_depth");
5974 #endif
5975 
5976 	config.support_half_float_vertices = true;
5977 //every platform should support this except web, iOS has issues with their support, so add option to disable
5978 #ifdef JAVASCRIPT_ENABLED
5979 	config.support_half_float_vertices = false;
5980 #endif
5981 	bool disable_half_float = GLOBAL_GET("rendering/gles2/compatibility/disable_half_float");
5982 	if (disable_half_float) {
5983 		config.support_half_float_vertices = false;
5984 	}
5985 
5986 	config.rgtc_supported = config.extensions.has("GL_EXT_texture_compression_rgtc") || config.extensions.has("GL_ARB_texture_compression_rgtc") || config.extensions.has("EXT_texture_compression_rgtc");
5987 	config.bptc_supported = config.extensions.has("GL_ARB_texture_compression_bptc") || config.extensions.has("EXT_texture_compression_bptc");
5988 
5989 	//determine formats for depth textures (or renderbuffers)
5990 	if (config.support_depth_texture) {
5991 		// Will use texture for depth
5992 		// have to manually see if we can create a valid framebuffer texture using UNSIGNED_INT,
5993 		// as there is no extension to test for this.
5994 		GLuint fbo;
5995 		glGenFramebuffers(1, &fbo);
5996 		glBindFramebuffer(GL_FRAMEBUFFER, fbo);
5997 		GLuint depth;
5998 		glGenTextures(1, &depth);
5999 		glBindTexture(GL_TEXTURE_2D, depth);
6000 		glTexImage2D(GL_TEXTURE_2D, 0, config.depth_internalformat, 32, 32, 0, GL_DEPTH_COMPONENT, config.depth_type, NULL);
6001 
6002 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
6003 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
6004 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
6005 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
6006 
6007 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);
6008 
6009 		GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
6010 
6011 		glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
6012 		glDeleteFramebuffers(1, &fbo);
6013 		glBindTexture(GL_TEXTURE_2D, 0);
6014 		glDeleteTextures(1, &depth);
6015 
6016 		if (status != GL_FRAMEBUFFER_COMPLETE) {
6017 			// If it fails, test to see if it supports a framebuffer texture using UNSIGNED_SHORT
6018 			// This is needed because many OSX devices don't support either UNSIGNED_INT or UNSIGNED_SHORT
6019 #ifdef GLES_OVER_GL
6020 			config.depth_internalformat = GL_DEPTH_COMPONENT16;
6021 #else
6022 			// OES_depth_texture extension only specifies GL_DEPTH_COMPONENT.
6023 			config.depth_internalformat = GL_DEPTH_COMPONENT;
6024 #endif
6025 			config.depth_type = GL_UNSIGNED_SHORT;
6026 
6027 			glGenFramebuffers(1, &fbo);
6028 			glBindFramebuffer(GL_FRAMEBUFFER, fbo);
6029 
6030 			glGenTextures(1, &depth);
6031 			glBindTexture(GL_TEXTURE_2D, depth);
6032 			glTexImage2D(GL_TEXTURE_2D, 0, config.depth_internalformat, 32, 32, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);
6033 
6034 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
6035 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
6036 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
6037 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
6038 
6039 			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);
6040 
6041 			status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
6042 			if (status != GL_FRAMEBUFFER_COMPLETE) {
6043 				//if it fails again depth textures aren't supported, use rgba shadows and renderbuffer for depth
6044 				config.support_depth_texture = false;
6045 				config.use_rgba_3d_shadows = true;
6046 			}
6047 
6048 			glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
6049 			glDeleteFramebuffers(1, &fbo);
6050 			glBindTexture(GL_TEXTURE_2D, 0);
6051 			glDeleteTextures(1, &depth);
6052 		}
6053 	}
6054 
6055 	//picky requirements for these
6056 	config.support_shadow_cubemaps = config.support_depth_texture && config.support_write_depth && config.support_depth_cubemaps;
6057 
6058 	frame.count = 0;
6059 	frame.delta = 0;
6060 	frame.current_rt = NULL;
6061 	frame.clear_request = false;
6062 
6063 	glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &config.max_vertex_texture_image_units);
6064 	glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &config.max_texture_image_units);
6065 	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &config.max_texture_size);
6066 
6067 	// the use skeleton software path should be used if either float texture is not supported,
6068 	// OR max_vertex_texture_image_units is zero
6069 	config.use_skeleton_software = (config.float_texture_supported == false) || (config.max_vertex_texture_image_units == 0);
6070 
6071 	shaders.copy.init();
6072 	shaders.cubemap_filter.init();
6073 	bool ggx_hq = GLOBAL_GET("rendering/quality/reflections/high_quality_ggx");
6074 	shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::LOW_QUALITY, !ggx_hq);
6075 
6076 	{
6077 		// quad for copying stuff
6078 
6079 		glGenBuffers(1, &resources.quadie);
6080 		glBindBuffer(GL_ARRAY_BUFFER, resources.quadie);
6081 		{
6082 			const float qv[16] = {
6083 				-1,
6084 				-1,
6085 				0,
6086 				0,
6087 				-1,
6088 				1,
6089 				0,
6090 				1,
6091 				1,
6092 				1,
6093 				1,
6094 				1,
6095 				1,
6096 				-1,
6097 				1,
6098 				0,
6099 			};
6100 
6101 			glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
6102 		}
6103 
6104 		glBindBuffer(GL_ARRAY_BUFFER, 0);
6105 	}
6106 
6107 	{
6108 		//default textures
6109 
6110 		glGenTextures(1, &resources.white_tex);
6111 		unsigned char whitetexdata[8 * 8 * 3];
6112 		for (int i = 0; i < 8 * 8 * 3; i++) {
6113 			whitetexdata[i] = 255;
6114 		}
6115 
6116 		glActiveTexture(GL_TEXTURE0);
6117 		glBindTexture(GL_TEXTURE_2D, resources.white_tex);
6118 		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
6119 		glGenerateMipmap(GL_TEXTURE_2D);
6120 		glBindTexture(GL_TEXTURE_2D, 0);
6121 
6122 		glGenTextures(1, &resources.black_tex);
6123 		unsigned char blacktexdata[8 * 8 * 3];
6124 		for (int i = 0; i < 8 * 8 * 3; i++) {
6125 			blacktexdata[i] = 0;
6126 		}
6127 
6128 		glActiveTexture(GL_TEXTURE0);
6129 		glBindTexture(GL_TEXTURE_2D, resources.black_tex);
6130 		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, blacktexdata);
6131 		glGenerateMipmap(GL_TEXTURE_2D);
6132 		glBindTexture(GL_TEXTURE_2D, 0);
6133 
6134 		glGenTextures(1, &resources.normal_tex);
6135 		unsigned char normaltexdata[8 * 8 * 3];
6136 		for (int i = 0; i < 8 * 8 * 3; i += 3) {
6137 			normaltexdata[i + 0] = 128;
6138 			normaltexdata[i + 1] = 128;
6139 			normaltexdata[i + 2] = 255;
6140 		}
6141 
6142 		glActiveTexture(GL_TEXTURE0);
6143 		glBindTexture(GL_TEXTURE_2D, resources.normal_tex);
6144 		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, normaltexdata);
6145 		glGenerateMipmap(GL_TEXTURE_2D);
6146 		glBindTexture(GL_TEXTURE_2D, 0);
6147 
6148 		glGenTextures(1, &resources.aniso_tex);
6149 		unsigned char anisotexdata[8 * 8 * 3];
6150 		for (int i = 0; i < 8 * 8 * 3; i += 3) {
6151 			anisotexdata[i + 0] = 255;
6152 			anisotexdata[i + 1] = 128;
6153 			anisotexdata[i + 2] = 0;
6154 		}
6155 
6156 		glActiveTexture(GL_TEXTURE0);
6157 		glBindTexture(GL_TEXTURE_2D, resources.aniso_tex);
6158 		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, anisotexdata);
6159 		glGenerateMipmap(GL_TEXTURE_2D);
6160 		glBindTexture(GL_TEXTURE_2D, 0);
6161 	}
6162 
6163 	// skeleton buffer
6164 	{
6165 		resources.skeleton_transform_buffer_size = 0;
6166 		glGenBuffers(1, &resources.skeleton_transform_buffer);
6167 	}
6168 
6169 	// radical inverse vdc cache texture
6170 	// used for cubemap filtering
6171 	if (true /*||config.float_texture_supported*/) { //uint8 is similar and works everywhere
6172 		glGenTextures(1, &resources.radical_inverse_vdc_cache_tex);
6173 
6174 		glActiveTexture(GL_TEXTURE0);
6175 		glBindTexture(GL_TEXTURE_2D, resources.radical_inverse_vdc_cache_tex);
6176 
6177 		uint8_t radical_inverse[512];
6178 
6179 		for (uint32_t i = 0; i < 512; i++) {
6180 			uint32_t bits = i;
6181 
6182 			bits = (bits << 16) | (bits >> 16);
6183 			bits = ((bits & 0x55555555) << 1) | ((bits & 0xAAAAAAAA) >> 1);
6184 			bits = ((bits & 0x33333333) << 2) | ((bits & 0xCCCCCCCC) >> 2);
6185 			bits = ((bits & 0x0F0F0F0F) << 4) | ((bits & 0xF0F0F0F0) >> 4);
6186 			bits = ((bits & 0x00FF00FF) << 8) | ((bits & 0xFF00FF00) >> 8);
6187 
6188 			float value = float(bits) * 2.3283064365386963e-10;
6189 			radical_inverse[i] = uint8_t(CLAMP(value * 255.0, 0, 255));
6190 		}
6191 
6192 		glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 512, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, radical_inverse);
6193 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
6194 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
6195 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
6196 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //need this for proper sampling
6197 
6198 		glBindTexture(GL_TEXTURE_2D, 0);
6199 	}
6200 
6201 	{
6202 
6203 		glGenFramebuffers(1, &resources.mipmap_blur_fbo);
6204 		glGenTextures(1, &resources.mipmap_blur_color);
6205 	}
6206 
6207 #ifdef GLES_OVER_GL
6208 	//this needs to be enabled manually in OpenGL 2.1
6209 
6210 	if (config.extensions.has("GL_ARB_seamless_cube_map")) {
6211 		glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS);
6212 	}
6213 	glEnable(GL_POINT_SPRITE);
6214 	glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
6215 #endif
6216 
6217 	config.force_vertex_shading = GLOBAL_GET("rendering/quality/shading/force_vertex_shading");
6218 	config.use_fast_texture_filter = GLOBAL_GET("rendering/quality/filters/use_nearest_mipmap_filter");
6219 }
6220 
finalize()6221 void RasterizerStorageGLES2::finalize() {
6222 }
6223 
_copy_screen()6224 void RasterizerStorageGLES2::_copy_screen() {
6225 	bind_quad_array();
6226 	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
6227 }
6228 
update_dirty_resources()6229 void RasterizerStorageGLES2::update_dirty_resources() {
6230 	update_dirty_shaders();
6231 	update_dirty_materials();
6232 	update_dirty_skeletons();
6233 	update_dirty_multimeshes();
6234 }
6235 
RasterizerStorageGLES2()6236 RasterizerStorageGLES2::RasterizerStorageGLES2() {
6237 	RasterizerStorageGLES2::system_fbo = 0;
6238 }
6239