| 
									
										
										
										
											2019-06-22 19:34:26 +03:00
										 |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-12-04 15:26:24 -03:00
										 |  |  | /*  renderer_canvas_render_rd.h                                           */ | 
					
						
							| 
									
										
										
										
											2019-06-22 19:34:26 +03:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-02-11 14:01:43 +01:00
										 |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							| 
									
										
										
										
											2019-06-22 19:34:26 +03:00
										 |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 23:41:51 +02:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | #include "core/templates/lru.h"
 | 
					
						
							| 
									
										
										
										
											2020-12-04 15:26:24 -03:00
										 |  |  | #include "servers/rendering/renderer_canvas_render.h"
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | #include "servers/rendering/renderer_rd/pipeline_hash_map_rd.h"
 | 
					
						
							| 
									
										
										
										
											2020-12-04 15:26:24 -03:00
										 |  |  | #include "servers/rendering/renderer_rd/shaders/canvas.glsl.gen.h"
 | 
					
						
							|  |  |  | #include "servers/rendering/renderer_rd/shaders/canvas_occlusion.glsl.gen.h"
 | 
					
						
							| 
									
										
										
										
											2022-06-21 10:08:33 +10:00
										 |  |  | #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | #include "servers/rendering/rendering_device.h"
 | 
					
						
							| 
									
										
										
										
											2021-11-16 07:25:42 -08:00
										 |  |  | #include "servers/rendering/shader_compiler.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 15:26:24 -03:00
										 |  |  | class RendererCanvasRenderRD : public RendererCanvasRender { | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 	enum { | 
					
						
							|  |  |  | 		BASE_UNIFORM_SET = 0, | 
					
						
							|  |  |  | 		MATERIAL_UNIFORM_SET = 1, | 
					
						
							|  |  |  | 		TRANSFORMS_UNIFORM_SET = 2, | 
					
						
							| 
									
										
										
										
											2024-09-27 11:05:46 -07:00
										 |  |  | 		BATCH_UNIFORM_SET = 3, | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 00:35:16 +03:00
										 |  |  | 	const int SAMPLERS_BINDING_FIRST_INDEX = 10; | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 	// The size of the ring buffer to store GPU buffers. Triple-buffering the max expected frames in flight.
 | 
					
						
							|  |  |  | 	static const uint32_t BATCH_DATA_BUFFER_COUNT = 3; | 
					
						
							| 
									
										
										
										
											2023-05-27 00:35:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	enum ShaderVariant { | 
					
						
							|  |  |  | 		SHADER_VARIANT_QUAD, | 
					
						
							|  |  |  | 		SHADER_VARIANT_NINEPATCH, | 
					
						
							| 
									
										
										
										
											2019-06-24 16:13:06 -03:00
										 |  |  | 		SHADER_VARIANT_PRIMITIVE, | 
					
						
							|  |  |  | 		SHADER_VARIANT_PRIMITIVE_POINTS, | 
					
						
							|  |  |  | 		SHADER_VARIANT_ATTRIBUTES, | 
					
						
							|  |  |  | 		SHADER_VARIANT_ATTRIBUTES_POINTS, | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 		SHADER_VARIANT_MAX | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	enum { | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 		INSTANCE_FLAGS_LIGHT_COUNT_SHIFT = 0, // 4 bits for light count.
 | 
					
						
							| 
									
										
										
										
											2021-05-10 13:12:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 		INSTANCE_FLAGS_CLIP_RECT_UV = (1 << 4), | 
					
						
							|  |  |  | 		INSTANCE_FLAGS_TRANSPOSE_RECT = (1 << 5), | 
					
						
							|  |  |  | 		INSTANCE_FLAGS_USE_MSDF = (1 << 6), | 
					
						
							|  |  |  | 		INSTANCE_FLAGS_USE_LCD = (1 << 7), | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 		INSTANCE_FLAGS_NINEPACH_DRAW_CENTER = (1 << 8), | 
					
						
							|  |  |  | 		INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT = 9, | 
					
						
							|  |  |  | 		INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT = 11, | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 		INSTANCE_FLAGS_SHADOW_MASKED_SHIFT = 13, // 16 bits.
 | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 	enum { | 
					
						
							|  |  |  | 		BATCH_FLAGS_INSTANCING_MASK = 0x7F, | 
					
						
							|  |  |  | 		BATCH_FLAGS_INSTANCING_HAS_COLORS = (1 << 7), | 
					
						
							|  |  |  | 		BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8), | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 		BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 9), | 
					
						
							|  |  |  | 		BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 10), | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2023-01-14 11:50:42 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 	enum { | 
					
						
							|  |  |  | 		CANVAS_FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR = (1 << 0), | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	enum { | 
					
						
							|  |  |  | 		LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF, | 
					
						
							|  |  |  | 		LIGHT_FLAGS_BLEND_SHIFT = 16, | 
					
						
							|  |  |  | 		LIGHT_FLAGS_BLEND_MASK = (3 << 16), | 
					
						
							|  |  |  | 		LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16), | 
					
						
							|  |  |  | 		LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16), | 
					
						
							|  |  |  | 		LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16), | 
					
						
							| 
									
										
										
										
											2019-07-07 01:49:40 -03:00
										 |  |  | 		LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16), | 
					
						
							|  |  |  | 		LIGHT_FLAGS_HAS_SHADOW = (1 << 20), | 
					
						
							|  |  |  | 		LIGHT_FLAGS_FILTER_SHIFT = 22 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	enum { | 
					
						
							|  |  |  | 		MAX_RENDER_ITEMS = 256 * 1024, | 
					
						
							|  |  |  | 		MAX_LIGHT_TEXTURES = 1024, | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 		MAX_LIGHTS_PER_ITEM = 16, | 
					
						
							| 
									
										
										
										
											2019-07-10 17:44:55 -03:00
										 |  |  | 		DEFAULT_MAX_LIGHTS_PER_RENDER = 256 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/****************/ | 
					
						
							|  |  |  | 	/**** SHADER ****/ | 
					
						
							|  |  |  | 	/****************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 	struct ShaderSpecialization { | 
					
						
							|  |  |  | 		union { | 
					
						
							| 
									
										
										
										
											2025-01-09 10:39:13 -03:00
										 |  |  | 			uint32_t packed_0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 			struct { | 
					
						
							|  |  |  | 				uint32_t use_lighting : 1; | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 	struct PipelineKey { | 
					
						
							|  |  |  | 		ShaderVariant variant = SHADER_VARIANT_MAX; | 
					
						
							|  |  |  | 		RD::FramebufferFormatID framebuffer_format_id = RD::INVALID_FORMAT_ID; | 
					
						
							|  |  |  | 		RD::VertexFormatID vertex_format_id = RD::INVALID_ID; | 
					
						
							|  |  |  | 		RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_MAX; | 
					
						
							|  |  |  | 		ShaderSpecialization shader_specialization = {}; | 
					
						
							|  |  |  | 		uint32_t lcd_blend = 0; | 
					
						
							|  |  |  | 		uint32_t ubershader = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		uint32_t hash() const { | 
					
						
							|  |  |  | 			uint32_t h = hash_murmur3_one_32(variant); | 
					
						
							|  |  |  | 			h = hash_murmur3_one_32(framebuffer_format_id, h); | 
					
						
							| 
									
										
										
										
											2024-12-13 19:03:37 +01:00
										 |  |  | 			h = hash_murmur3_one_64((uint64_t)vertex_format_id, h); | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 			h = hash_murmur3_one_32(render_primitive, h); | 
					
						
							|  |  |  | 			h = hash_murmur3_one_32(shader_specialization.packed_0, h); | 
					
						
							|  |  |  | 			h = hash_murmur3_one_32(lcd_blend, h); | 
					
						
							|  |  |  | 			h = hash_murmur3_one_32(ubershader, h); | 
					
						
							|  |  |  | 			return hash_fmix32(h); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-01 11:59:14 +10:00
										 |  |  | 	struct CanvasShaderData : public RendererRD::MaterialStorage::ShaderData { | 
					
						
							| 
									
										
										
										
											2021-11-16 07:25:42 -08:00
										 |  |  | 		Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms; | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 		int blend_mode = 0; | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Vector<uint32_t> ubo_offsets; | 
					
						
							| 
									
										
										
										
											2022-05-02 16:28:25 +02:00
										 |  |  | 		uint32_t ubo_size = 0; | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		String code; | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 		RID version; | 
					
						
							|  |  |  | 		PipelineHashMapRD<PipelineKey, CanvasShaderData, void (CanvasShaderData::*)(PipelineKey)> pipeline_hash_map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		static const uint32_t VERTEX_INPUT_MASKS_SIZE = SHADER_VARIANT_MAX * 2; | 
					
						
							|  |  |  | 		std::atomic<uint64_t> vertex_input_masks[VERTEX_INPUT_MASKS_SIZE] = {}; | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 		bool uses_screen_texture = false; | 
					
						
							| 
									
										
										
										
											2022-08-09 12:29:49 -04:00
										 |  |  | 		bool uses_screen_texture_mipmaps = false; | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 		bool uses_sdf = false; | 
					
						
							| 
									
										
										
										
											2021-05-20 11:25:06 -03:00
										 |  |  | 		bool uses_time = false; | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 		void _clear_vertex_input_mask_cache(); | 
					
						
							|  |  |  | 		void _create_pipeline(PipelineKey p_pipeline_key); | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 		virtual void set_code(const String &p_Code); | 
					
						
							|  |  |  | 		virtual bool is_animated() const; | 
					
						
							|  |  |  | 		virtual bool casts_shadows() const; | 
					
						
							| 
									
										
										
										
											2021-01-05 20:01:50 -03:00
										 |  |  | 		virtual RS::ShaderNativeSourceCode get_native_source_code() const; | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 		RID get_shader(ShaderVariant p_shader_variant, bool p_ubershader) const; | 
					
						
							|  |  |  | 		uint64_t get_vertex_input_mask(ShaderVariant p_shader_variant, bool p_ubershader); | 
					
						
							|  |  |  | 		bool is_valid() const; | 
					
						
							| 
									
										
										
										
											2021-01-05 20:01:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 		CanvasShaderData(); | 
					
						
							| 
									
										
										
										
											2022-03-21 22:25:25 +11:00
										 |  |  | 		virtual ~CanvasShaderData(); | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 	struct { | 
					
						
							|  |  |  | 		// Data must be guaranteed to be erased before the rest on the destructor.
 | 
					
						
							|  |  |  | 		CanvasShaderData *default_version_data = nullptr; | 
					
						
							|  |  |  | 		CanvasShaderRD canvas_shader; | 
					
						
							|  |  |  | 		RID default_version_rd_shader; | 
					
						
							|  |  |  | 		RID quad_index_buffer; | 
					
						
							|  |  |  | 		RID quad_index_array; | 
					
						
							|  |  |  | 		ShaderCompiler compiler; | 
					
						
							|  |  |  | 		uint32_t pipeline_compilations[RS::PIPELINE_SOURCE_MAX] = {}; | 
					
						
							|  |  |  | 		Mutex mutex; | 
					
						
							|  |  |  | 	} shader; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-01 11:59:14 +10:00
										 |  |  | 	RendererRD::MaterialStorage::ShaderData *_create_shader_func(); | 
					
						
							|  |  |  | 	static RendererRD::MaterialStorage::ShaderData *_create_shader_funcs() { | 
					
						
							| 
									
										
										
										
											2020-12-04 15:26:24 -03:00
										 |  |  | 		return static_cast<RendererCanvasRenderRD *>(singleton)->_create_shader_func(); | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-01 11:59:14 +10:00
										 |  |  | 	struct CanvasMaterialData : public RendererRD::MaterialStorage::MaterialData { | 
					
						
							| 
									
										
										
										
											2022-04-04 15:06:57 +02:00
										 |  |  | 		CanvasShaderData *shader_data = nullptr; | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 		RID uniform_set; | 
					
						
							| 
									
										
										
										
											2023-08-03 14:10:01 +02:00
										 |  |  | 		RID uniform_set_srgb; | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		virtual void set_render_priority(int p_priority) {} | 
					
						
							|  |  |  | 		virtual void set_next_pass(RID p_pass) {} | 
					
						
							| 
									
										
										
										
											2022-05-13 15:04:37 +02:00
										 |  |  | 		virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty); | 
					
						
							| 
									
										
										
										
											2022-03-21 22:25:25 +11:00
										 |  |  | 		virtual ~CanvasMaterialData(); | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-01 11:59:14 +10:00
										 |  |  | 	RendererRD::MaterialStorage::MaterialData *_create_material_func(CanvasShaderData *p_shader); | 
					
						
							|  |  |  | 	static RendererRD::MaterialStorage::MaterialData *_create_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) { | 
					
						
							| 
									
										
										
										
											2022-03-21 22:25:25 +11:00
										 |  |  | 		return static_cast<RendererCanvasRenderRD *>(singleton)->_create_material_func(static_cast<CanvasShaderData *>(p_shader)); | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	/**************************/ | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 	/**** CANVAS TEXTURES *****/ | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	/**************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		RS::CanvasItemTextureFilter default_filter; | 
					
						
							|  |  |  | 		RS::CanvasItemTextureRepeat default_repeat; | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	} default_samplers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 16:13:06 -03:00
										 |  |  | 	/******************/ | 
					
						
							|  |  |  | 	/**** POLYGONS ****/ | 
					
						
							|  |  |  | 	/******************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct PolygonBuffers { | 
					
						
							|  |  |  | 		RD::VertexFormatID vertex_format_id; | 
					
						
							|  |  |  | 		RID vertex_buffer; | 
					
						
							|  |  |  | 		RID vertex_array; | 
					
						
							|  |  |  | 		RID index_buffer; | 
					
						
							|  |  |  | 		RID indices; | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 		uint32_t primitive_count = 0; | 
					
						
							| 
									
										
										
										
											2019-06-24 16:13:06 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct { | 
					
						
							|  |  |  | 		HashMap<PolygonID, PolygonBuffers> polygons; | 
					
						
							|  |  |  | 		PolygonID last_id; | 
					
						
							|  |  |  | 	} polygon_buffers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/********************/ | 
					
						
							|  |  |  | 	/**** PRIMITIVES ****/ | 
					
						
							|  |  |  | 	/********************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct { | 
					
						
							|  |  |  | 		RID index_array[4]; | 
					
						
							|  |  |  | 	} primitive_arrays; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	/*******************/ | 
					
						
							|  |  |  | 	/**** MATERIALS ****/ | 
					
						
							|  |  |  | 	/*******************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 15:11:52 -03:00
										 |  |  | 	/******************/ | 
					
						
							|  |  |  | 	/**** LIGHTING ****/ | 
					
						
							|  |  |  | 	/******************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	struct CanvasLight { | 
					
						
							|  |  |  | 		RID texture; | 
					
						
							|  |  |  | 		struct { | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 			bool enabled = false; | 
					
						
							|  |  |  | 			float z_far; | 
					
						
							|  |  |  | 			float y_offset; | 
					
						
							| 
									
										
										
										
											2020-11-03 16:51:53 -03:00
										 |  |  | 			Transform2D directional_xform; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 		} shadow; | 
					
						
							| 
									
										
										
										
											2019-06-26 15:11:52 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	RID_Owner<CanvasLight> canvas_light_owner; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 23:32:19 -08:00
										 |  |  | 	struct PositionalShadowRenderPushConstant { | 
					
						
							|  |  |  | 		float modelview[8]; | 
					
						
							|  |  |  | 		float rotation[4]; | 
					
						
							|  |  |  | 		float direction[2]; | 
					
						
							|  |  |  | 		float z_far; | 
					
						
							|  |  |  | 		uint32_t pad; | 
					
						
							|  |  |  | 		float z_near; | 
					
						
							|  |  |  | 		uint32_t cull_mode; | 
					
						
							|  |  |  | 		float pad2[2]; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	struct ShadowRenderPushConstant { | 
					
						
							|  |  |  | 		float projection[16]; | 
					
						
							| 
									
										
										
										
											2019-07-07 01:49:40 -03:00
										 |  |  | 		float modelview[8]; | 
					
						
							|  |  |  | 		float direction[2]; | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 		float z_far; | 
					
						
							| 
									
										
										
										
											2024-12-11 23:32:19 -08:00
										 |  |  | 		uint32_t cull_mode; | 
					
						
							| 
									
										
										
										
											2019-07-07 01:49:40 -03:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	struct OccluderPolygon { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		RS::CanvasOccluderPolygonCullMode cull_mode; | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 		int line_point_count; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 		RID vertex_buffer; | 
					
						
							|  |  |  | 		RID vertex_array; | 
					
						
							|  |  |  | 		RID index_buffer; | 
					
						
							|  |  |  | 		RID index_array; | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		int sdf_point_count; | 
					
						
							|  |  |  | 		int sdf_index_count; | 
					
						
							|  |  |  | 		RID sdf_vertex_buffer; | 
					
						
							|  |  |  | 		RID sdf_vertex_array; | 
					
						
							|  |  |  | 		RID sdf_index_buffer; | 
					
						
							|  |  |  | 		RID sdf_index_array; | 
					
						
							|  |  |  | 		bool sdf_is_lines; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct LightUniform { | 
					
						
							|  |  |  | 		float matrix[8]; //light to texture coordinate matrix
 | 
					
						
							| 
									
										
										
										
											2019-07-07 01:49:40 -03:00
										 |  |  | 		float shadow_matrix[8]; //light to shadow coordinate matrix
 | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 		float color[4]; | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		uint8_t shadow_color[4]; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 		uint32_t flags; //index to light texture
 | 
					
						
							|  |  |  | 		float shadow_pixel_size; | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 		float height; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		float position[2]; | 
					
						
							|  |  |  | 		float shadow_z_far_inv; | 
					
						
							|  |  |  | 		float shadow_y_ofs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		float atlas_rect[4]; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	RID_Owner<OccluderPolygon> occluder_polygon_owner; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 	enum ShadowRenderMode { | 
					
						
							| 
									
										
										
										
											2024-12-11 23:32:19 -08:00
										 |  |  | 		SHADOW_RENDER_MODE_DIRECTIONAL_SHADOW, | 
					
						
							|  |  |  | 		SHADOW_RENDER_MODE_POSITIONAL_SHADOW, | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 		SHADOW_RENDER_MODE_SDF, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	enum { | 
					
						
							|  |  |  | 		SHADOW_RENDER_SDF_TRIANGLES, | 
					
						
							|  |  |  | 		SHADOW_RENDER_SDF_LINES, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 15:11:52 -03:00
										 |  |  | 	struct { | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 		CanvasOcclusionShaderRD shader; | 
					
						
							|  |  |  | 		RID shader_version; | 
					
						
							| 
									
										
										
										
											2024-12-11 23:32:19 -08:00
										 |  |  | 		RID render_pipelines[2]; | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 		RID sdf_render_pipelines[2]; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 		RD::VertexFormatID vertex_format; | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 		RD::VertexFormatID sdf_vertex_format; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 		RD::FramebufferFormatID framebuffer_format; | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 		RD::FramebufferFormatID sdf_framebuffer_format; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	} shadow_render; | 
					
						
							| 
									
										
										
										
											2019-06-26 15:11:52 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	/***************/ | 
					
						
							|  |  |  | 	/**** STATE ****/ | 
					
						
							|  |  |  | 	/***************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//state that does not vary across rendering all items
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	struct InstanceData { | 
					
						
							|  |  |  | 		float world[6]; | 
					
						
							|  |  |  | 		uint32_t flags; | 
					
						
							| 
									
										
										
										
											2023-05-29 00:23:00 +02:00
										 |  |  | 		uint32_t instance_uniforms_ofs; | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 		union { | 
					
						
							|  |  |  | 			//rect
 | 
					
						
							|  |  |  | 			struct { | 
					
						
							|  |  |  | 				float modulation[4]; | 
					
						
							|  |  |  | 				union { | 
					
						
							|  |  |  | 					float msdf[4]; | 
					
						
							|  |  |  | 					float ninepatch_margins[4]; | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 				float dst_rect[4]; | 
					
						
							|  |  |  | 				float src_rect[4]; | 
					
						
							|  |  |  | 				float pad[2]; | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			//primitive
 | 
					
						
							|  |  |  | 			struct { | 
					
						
							|  |  |  | 				float points[6]; // vec2 points[3]
 | 
					
						
							|  |  |  | 				float uvs[6]; // vec2 points[3]
 | 
					
						
							|  |  |  | 				uint32_t colors[6]; // colors encoded as half
 | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		float color_texture_pixel_size[2]; | 
					
						
							|  |  |  | 		uint32_t lights[4]; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct PushConstant { | 
					
						
							|  |  |  | 		uint32_t base_instance_index; | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 		ShaderSpecialization shader_specialization; | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		uint32_t specular_shininess; | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 		uint32_t batch_flags; | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// TextureState is used to determine when a new batch is required due to a change of texture state.
 | 
					
						
							|  |  |  | 	struct TextureState { | 
					
						
							|  |  |  | 		static const uint32_t FILTER_SHIFT = 0; | 
					
						
							|  |  |  | 		static const uint32_t FILTER_BITS = 3; | 
					
						
							|  |  |  | 		static const uint32_t FILTER_MASK = (1 << FILTER_BITS) - 1; | 
					
						
							|  |  |  | 		static const uint32_t REPEAT_SHIFT = FILTER_BITS; | 
					
						
							|  |  |  | 		static const uint32_t REPEAT_BITS = 2; | 
					
						
							|  |  |  | 		static const uint32_t REPEAT_MASK = (1 << REPEAT_BITS) - 1; | 
					
						
							|  |  |  | 		static const uint32_t TEXTURE_IS_DATA_SHIFT = REPEAT_SHIFT + REPEAT_BITS; | 
					
						
							|  |  |  | 		static const uint32_t TEXTURE_IS_DATA_BITS = 1; | 
					
						
							|  |  |  | 		static const uint32_t TEXTURE_IS_DATA_MASK = (1 << TEXTURE_IS_DATA_BITS) - 1; | 
					
						
							|  |  |  | 		static const uint32_t LINEAR_COLORS_SHIFT = TEXTURE_IS_DATA_SHIFT + TEXTURE_IS_DATA_BITS; | 
					
						
							|  |  |  | 		static const uint32_t LINEAR_COLORS_BITS = 1; | 
					
						
							|  |  |  | 		static const uint32_t LINEAR_COLORS_MASK = (1 << LINEAR_COLORS_BITS) - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		RID texture; | 
					
						
							|  |  |  | 		uint32_t other = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		TextureState() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		TextureState(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, bool p_texture_is_data, bool p_use_linear_colors) { | 
					
						
							|  |  |  | 			texture = p_texture; | 
					
						
							|  |  |  | 			other = (((uint32_t)p_base_filter & FILTER_MASK) << FILTER_SHIFT) | | 
					
						
							|  |  |  | 					(((uint32_t)p_base_repeat & REPEAT_MASK) << REPEAT_SHIFT) | | 
					
						
							|  |  |  | 					(((uint32_t)p_texture_is_data & TEXTURE_IS_DATA_MASK) << TEXTURE_IS_DATA_SHIFT) | | 
					
						
							|  |  |  | 					(((uint32_t)p_use_linear_colors & LINEAR_COLORS_MASK) << LINEAR_COLORS_SHIFT); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		_ALWAYS_INLINE_ RS::CanvasItemTextureFilter texture_filter() const { | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 			return (RS::CanvasItemTextureFilter)((other >> FILTER_SHIFT) & FILTER_MASK); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		_ALWAYS_INLINE_ RS::CanvasItemTextureRepeat texture_repeat() const { | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 			return (RS::CanvasItemTextureRepeat)((other >> REPEAT_SHIFT) & REPEAT_MASK); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		_ALWAYS_INLINE_ bool linear_colors() const { | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 			return (other >> LINEAR_COLORS_SHIFT) & LINEAR_COLORS_MASK; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		_ALWAYS_INLINE_ bool texture_is_data() const { | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 			return (other >> TEXTURE_IS_DATA_SHIFT) & TEXTURE_IS_DATA_MASK; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		_ALWAYS_INLINE_ bool operator==(const TextureState &p_val) const { | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 			return (texture == p_val.texture) && (other == p_val.other); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		_ALWAYS_INLINE_ bool operator!=(const TextureState &p_val) const { | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 			return (texture != p_val.texture) || (other != p_val.other); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		_ALWAYS_INLINE_ bool is_valid() const { return texture.is_valid(); } | 
					
						
							|  |  |  | 		_ALWAYS_INLINE_ bool is_null() const { return texture.is_null(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		uint32_t hash() const { | 
					
						
							|  |  |  | 			uint32_t hash = hash_murmur3_one_64(texture.get_id()); | 
					
						
							|  |  |  | 			return hash_murmur3_one_32(other, hash); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-27 11:05:46 -07:00
										 |  |  | 	struct TextureInfo { | 
					
						
							|  |  |  | 		TextureState state; | 
					
						
							|  |  |  | 		RID diffuse; | 
					
						
							|  |  |  | 		RID normal; | 
					
						
							|  |  |  | 		RID specular; | 
					
						
							|  |  |  | 		RID sampler; | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		Vector2 texpixel_size; | 
					
						
							|  |  |  | 		uint32_t specular_shininess = 0; | 
					
						
							|  |  |  | 		uint32_t flags = 0; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/// A key used to uniquely identify a distinct BATCH_UNIFORM_SET
 | 
					
						
							|  |  |  | 	struct RIDSetKey { | 
					
						
							|  |  |  | 		TextureState state; | 
					
						
							|  |  |  | 		RID instance_data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		RIDSetKey() { | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		RIDSetKey(TextureState p_state, RID p_instance_data) : | 
					
						
							|  |  |  | 				state(p_state), | 
					
						
							|  |  |  | 				instance_data(p_instance_data) { | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_ALWAYS_INLINE_ bool operator==(const RIDSetKey &p_val) const { | 
					
						
							|  |  |  | 			return state == p_val.state && instance_data == p_val.instance_data; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_ALWAYS_INLINE_ bool operator!=(const RIDSetKey &p_val) const { | 
					
						
							|  |  |  | 			return !(*this == p_val); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_ALWAYS_INLINE_ uint32_t hash() const { | 
					
						
							|  |  |  | 			uint32_t h = state.hash(); | 
					
						
							|  |  |  | 			h = hash_murmur3_one_64(instance_data.get_id(), h); | 
					
						
							|  |  |  | 			return hash_fmix32(h); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-09-27 11:05:46 -07:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 	static void _before_evict(RendererCanvasRenderRD::RIDSetKey &p_key, RID &p_rid); | 
					
						
							|  |  |  | 	static void _uniform_set_invalidation_callback(void *p_userdata); | 
					
						
							| 
									
										
										
										
											2025-01-23 06:38:41 +11:00
										 |  |  | 	static void _canvas_texture_invalidation_callback(bool p_deleted, void *p_userdata); | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	typedef LRUCache<RIDSetKey, RID, HashableHasher<RIDSetKey>, HashMapComparatorDefault<RIDSetKey>, _before_evict> RIDCache; | 
					
						
							|  |  |  | 	RIDCache rid_set_to_uniform_set; | 
					
						
							| 
									
										
										
										
											2025-01-23 06:38:41 +11:00
										 |  |  | 	/// Maps a CanvasTexture to its associated uniform sets, which must
 | 
					
						
							|  |  |  | 	/// be invalidated when the CanvasTexture is updated, such as changing the
 | 
					
						
							|  |  |  | 	/// diffuse texture.
 | 
					
						
							|  |  |  | 	HashMap<RID, TightLocalVector<RID>> canvas_texture_to_uniform_set; | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	struct Batch { | 
					
						
							|  |  |  | 		// Position in the UBO measured in bytes
 | 
					
						
							|  |  |  | 		uint32_t start = 0; | 
					
						
							|  |  |  | 		uint32_t instance_count = 0; | 
					
						
							|  |  |  | 		uint32_t instance_buffer_index = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		TextureInfo *tex_info; | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Color modulate = Color(1.0, 1.0, 1.0, 1.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Item *clip = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		RID material; | 
					
						
							|  |  |  | 		CanvasMaterialData *material_data = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const Item::Command *command = nullptr; | 
					
						
							|  |  |  | 		Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch.
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 		ShaderVariant shader_variant = SHADER_VARIANT_QUAD; | 
					
						
							|  |  |  | 		RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES; | 
					
						
							|  |  |  | 		bool use_lighting = false; | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// batch-specific data
 | 
					
						
							|  |  |  | 		union { | 
					
						
							|  |  |  | 			// TYPE_PRIMITIVE
 | 
					
						
							|  |  |  | 			uint32_t primitive_points = 0; | 
					
						
							|  |  |  | 			// TYPE_PARTICLES
 | 
					
						
							|  |  |  | 			uint32_t mesh_instance_count; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		bool has_blend = false; | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 		uint32_t flags = 0; | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-09 22:23:00 -07:00
										 |  |  | 	HashMap<TextureState, TextureInfo, HashableHasher<TextureState>, HashMapComparatorDefault<TextureState>, PagedAllocator<HashMapElement<TextureState, TextureInfo>>> texture_info_map; | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// per-frame buffers
 | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	struct DataBuffer { | 
					
						
							|  |  |  | 		LocalVector<RID> instance_buffers; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	struct State { | 
					
						
							|  |  |  | 		//state buffer
 | 
					
						
							|  |  |  | 		struct Buffer { | 
					
						
							|  |  |  | 			float canvas_transform[16]; | 
					
						
							|  |  |  | 			float screen_transform[16]; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 			float canvas_normal_transform[16]; | 
					
						
							|  |  |  | 			float canvas_modulate[4]; | 
					
						
							| 
									
										
										
										
											2020-11-03 16:51:53 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 10:23:24 -03:00
										 |  |  | 			float screen_pixel_size[2]; | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 			float time; | 
					
						
							| 
									
										
										
										
											2020-10-29 18:09:16 -03:00
										 |  |  | 			uint32_t use_pixel_snap; | 
					
						
							| 
									
										
										
										
											2019-07-27 10:23:24 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 			float sdf_to_tex[4]; | 
					
						
							|  |  |  | 			float sdf_to_screen[2]; | 
					
						
							|  |  |  | 			float screen_to_sdf[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 16:51:53 -03:00
										 |  |  | 			uint32_t directional_light_count; | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 			float tex_to_sdf; | 
					
						
							| 
									
										
										
										
											2025-05-14 11:15:28 -03:00
										 |  |  | 			float shadow_pixel_size; | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 			uint32_t flags; | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		DataBuffer canvas_instance_data_buffers[BATCH_DATA_BUFFER_COUNT]; | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 		LocalVector<Batch> canvas_instance_batches; | 
					
						
							|  |  |  | 		uint32_t current_data_buffer_index = 0; | 
					
						
							|  |  |  | 		uint32_t current_instance_buffer_index = 0; | 
					
						
							|  |  |  | 		uint32_t current_batch_index = 0; | 
					
						
							|  |  |  | 		uint32_t last_instance_index = 0; | 
					
						
							|  |  |  | 		InstanceData *instance_data_array = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		uint32_t max_instances_per_buffer = 16384; | 
					
						
							|  |  |  | 		uint32_t max_instance_buffer_size = 16384 * sizeof(InstanceData); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 		Vector<RD::Uniform> batch_texture_uniforms; | 
					
						
							| 
									
										
										
										
											2024-09-27 11:05:46 -07:00
										 |  |  | 		RID current_batch_uniform_set; | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-04 15:06:57 +02:00
										 |  |  | 		LightUniform *light_uniforms = nullptr; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		RID lights_uniform_buffer; | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 		RID canvas_state_buffer; | 
					
						
							| 
									
										
										
										
											2019-07-08 09:53:40 -03:00
										 |  |  | 		RID shadow_sampler; | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 		RID shadow_texture; | 
					
						
							|  |  |  | 		RID shadow_depth_texture; | 
					
						
							|  |  |  | 		RID shadow_fb; | 
					
						
							|  |  |  | 		int shadow_texture_size = 2048; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 23:32:19 -08:00
										 |  |  | 		RID shadow_occluder_buffer; | 
					
						
							|  |  |  | 		uint32_t shadow_occluder_buffer_size; | 
					
						
							|  |  |  | 		RID shadow_ocluder_uniform_set; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 		RID default_transforms_uniform_set; | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-10 17:44:55 -03:00
										 |  |  | 		uint32_t max_lights_per_render; | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		double time; | 
					
						
							| 
									
										
										
										
											2020-11-03 16:51:53 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 	} state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Item *items[MAX_RENDER_ITEMS]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-27 11:05:46 -07:00
										 |  |  | 	TextureInfo default_texture_info; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 16:51:53 -03:00
										 |  |  | 	bool using_directional_lights = false; | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 	RID default_canvas_texture; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-28 15:34:27 -03:00
										 |  |  | 	RID default_canvas_group_shader; | 
					
						
							|  |  |  | 	RID default_canvas_group_material; | 
					
						
							| 
									
										
										
										
											2022-10-06 15:27:11 -07:00
										 |  |  | 	RID default_clip_children_material; | 
					
						
							|  |  |  | 	RID default_clip_children_shader; | 
					
						
							| 
									
										
										
										
											2020-10-28 15:34:27 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 	RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR; | 
					
						
							|  |  |  | 	RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	RID _create_base_uniform_set(RID p_to_render_target, bool p_backbuffer); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 19:35:54 +02:00
										 |  |  | 	bool debug_redraw = false; | 
					
						
							|  |  |  | 	Color debug_redraw_color; | 
					
						
							|  |  |  | 	double debug_redraw_time = 1.0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	// A structure to store cached render target information
 | 
					
						
							|  |  |  | 	struct RenderTarget { | 
					
						
							|  |  |  | 		// Current render target for the canvas.
 | 
					
						
							|  |  |  | 		RID render_target; | 
					
						
							| 
									
										
										
										
											2024-11-04 21:21:38 -08:00
										 |  |  | 		bool use_linear_colors = false; | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 	inline RID _get_pipeline_specialization_or_ubershader(CanvasShaderData *p_shader_data, PipelineKey &r_pipeline_key, PushConstant &r_push_constant, RID p_mesh_instance = RID(), void *p_surface = nullptr, uint32_t p_surface_index = 0, RID *r_vertex_array = nullptr); | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	void _render_batch_items(RenderTarget p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights, bool &r_sdf_used, bool p_to_backbuffer = false, RenderingMethod::RenderInfo *r_render_info = nullptr); | 
					
						
							| 
									
										
										
										
											2024-09-14 06:05:06 +10:00
										 |  |  | 	void _record_item_commands(const Item *p_item, RenderTarget p_render_target, const Transform2D &p_base_transform, Item *&r_current_clip, Light *p_lights, uint32_t &r_index, bool &r_batch_broken, bool &r_sdf_used, Batch *&r_current_batch); | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 	void _render_batch(RD::DrawListID p_draw_list, CanvasShaderData *p_shader_data, RenderingDevice::FramebufferFormatID p_framebuffer_format, Light *p_lights, Batch const *p_batch, RenderingMethod::RenderInfo *r_render_info = nullptr); | 
					
						
							| 
									
										
										
										
											2024-10-30 08:27:16 +11:00
										 |  |  | 	void _prepare_batch_texture_info(RID p_texture, TextureState &p_state, TextureInfo *p_info); | 
					
						
							| 
									
										
										
										
											2023-05-29 00:23:00 +02:00
										 |  |  | 	InstanceData *new_instance_data(float *p_world, uint32_t *p_lights, uint32_t p_base_flags, uint32_t p_index, uint32_t p_uniforms_ofs, TextureInfo *p_info); | 
					
						
							| 
									
										
										
										
											2024-07-30 07:23:03 +10:00
										 |  |  | 	[[nodiscard]] Batch *_new_batch(bool &r_batch_broken); | 
					
						
							|  |  |  | 	void _add_to_batch(uint32_t &r_index, bool &r_batch_broken, Batch *&r_current_batch); | 
					
						
							|  |  |  | 	void _allocate_instance_buffer(); | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	_FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4); | 
					
						
							|  |  |  | 	_FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3); | 
					
						
							| 
									
										
										
										
											2019-06-15 23:45:24 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	_FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4); | 
					
						
							| 
									
										
										
										
											2020-10-17 01:08:21 -04:00
										 |  |  | 	_FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4); | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 16:51:53 -03:00
										 |  |  | 	void _update_shadow_atlas(); | 
					
						
							| 
									
										
										
										
											2024-12-11 23:32:19 -08:00
										 |  |  | 	void _update_occluder_buffer(uint32_t p_size); | 
					
						
							| 
									
										
										
										
											2020-11-03 16:51:53 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2025-05-14 22:42:18 +09:00
										 |  |  | 	PolygonID request_polygon(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>(), int p_count = -1) override; | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	void free_polygon(PolygonID p_polygon) override; | 
					
						
							| 
									
										
										
										
											2019-06-24 16:13:06 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	RID light_create() override; | 
					
						
							|  |  |  | 	void light_set_texture(RID p_rid, RID p_texture) override; | 
					
						
							|  |  |  | 	void light_set_use_shadow(RID p_rid, bool p_enable) override; | 
					
						
							| 
									
										
										
										
											2024-12-11 23:32:19 -08:00
										 |  |  | 	void light_update_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, const Rect2 &p_light_rect) override; | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	void light_update_directional_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_cull_distance, const Rect2 &p_clip_rect, LightOccluderInstance *p_occluders) override; | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override; | 
					
						
							| 
									
										
										
										
											2020-11-26 09:50:21 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	RID occluder_polygon_create() override; | 
					
						
							|  |  |  | 	void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override; | 
					
						
							|  |  |  | 	void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override; | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_light_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, bool &r_sdf_used, RenderingMethod::RenderInfo *r_render_info = nullptr) override; | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	virtual void set_shadow_texture_size(int p_size) override; | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) override; | 
					
						
							| 
									
										
										
										
											2024-03-15 14:13:31 -03:00
										 |  |  | 	uint32_t get_pipeline_compilations(RS::PipelineSource p_source) override; | 
					
						
							| 
									
										
										
										
											2023-07-07 19:35:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-21 11:31:30 -03:00
										 |  |  | 	void set_time(double p_time); | 
					
						
							| 
									
										
										
										
											2023-12-05 16:48:01 -07:00
										 |  |  | 	void update() override; | 
					
						
							|  |  |  | 	bool free(RID p_rid) override; | 
					
						
							| 
									
										
										
										
											2022-06-21 10:08:33 +10:00
										 |  |  | 	RendererCanvasRenderRD(); | 
					
						
							| 
									
										
										
										
											2020-12-04 15:26:24 -03:00
										 |  |  | 	~RendererCanvasRenderRD(); | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | }; |