| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/lru.h" |
| 34 |
#include "servers/rendering/multi_uma_buffer.h" |
| 35 |
#include "servers/rendering/renderer_canvas_render.h" |
| 36 |
#include "servers/rendering/renderer_rd/pipeline_hash_map_rd.h" |
| 37 |
#include "servers/rendering/renderer_rd/shaders/canvas.glsl.gen.h" |
| 38 |
#include "servers/rendering/renderer_rd/shaders/canvas_occlusion.glsl.gen.h" |
| 39 |
#include "servers/rendering/renderer_rd/storage_rd/material_storage.h" |
| 40 |
#include "servers/rendering/rendering_device.h" |
| 41 |
#include "servers/rendering/shader_compiler.h" |
| 42 |
|
| 43 |
class RendererCanvasRenderRD : public RendererCanvasRender { |
| 44 |
enum { |
| 45 |
BASE_UNIFORM_SET = 0, |
| 46 |
MATERIAL_UNIFORM_SET = 1, |
| 47 |
TRANSFORMS_UNIFORM_SET = 2, |
| 48 |
BATCH_UNIFORM_SET = 3, |
| 49 |
}; |
| 50 |
|
| 51 |
const int SAMPLERS_BINDING_FIRST_INDEX = 10; |
| 52 |
|
| 53 |
static const uint32_t BATCH_DATA_BUFFER_COUNT = 3; |
| 54 |
|
| 55 |
enum ShaderVariant { |
| 56 |
SHADER_VARIANT_QUAD, |
| 57 |
SHADER_VARIANT_NINEPATCH, |
| 58 |
SHADER_VARIANT_PRIMITIVE, |
| 59 |
SHADER_VARIANT_PRIMITIVE_POINTS, |
| 60 |
SHADER_VARIANT_ATTRIBUTES, |
| 61 |
SHADER_VARIANT_ATTRIBUTES_POINTS, |
| 62 |
SHADER_VARIANT_MAX |
| 63 |
}; |
| 64 |
|
| 65 |
enum { |
| 66 |
INSTANCE_FLAGS_LIGHT_COUNT_SHIFT = 0, |
| 67 |
|
| 68 |
INSTANCE_FLAGS_CLIP_RECT_UV = (1 << 4), |
| 69 |
INSTANCE_FLAGS_TRANSPOSE_RECT = (1 << 5), |
| 70 |
|
| 71 |
INSTANCE_FLAGS_NINEPACH_DRAW_CENTER = (1 << 8), |
| 72 |
INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT = 9, |
| 73 |
INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT = 11, |
| 74 |
|
| 75 |
INSTANCE_FLAGS_SHADOW_MASKED_SHIFT = 13, |
| 76 |
}; |
| 77 |
|
| 78 |
enum { |
| 79 |
BATCH_FLAGS_INSTANCING_MASK = 0x7F, |
| 80 |
BATCH_FLAGS_INSTANCING_HAS_COLORS = (1 << 7), |
| 81 |
BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8), |
| 82 |
|
| 83 |
BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 9), |
| 84 |
BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 10), |
| 85 |
}; |
| 86 |
|
| 87 |
enum { |
| 88 |
CANVAS_FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR = (1 << 0), |
| 89 |
}; |
| 90 |
|
| 91 |
enum { |
| 92 |
LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF, |
| 93 |
LIGHT_FLAGS_BLEND_SHIFT = 16, |
| 94 |
LIGHT_FLAGS_BLEND_MASK = (3 << 16), |
| 95 |
LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16), |
| 96 |
LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16), |
| 97 |
LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16), |
| 98 |
LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16), |
| 99 |
LIGHT_FLAGS_HAS_SHADOW = (1 << 20), |
| 100 |
LIGHT_FLAGS_FILTER_SHIFT = 22 |
| 101 |
|
| 102 |
}; |
| 103 |
|
| 104 |
enum { |
| 105 |
MAX_RENDER_ITEMS = 256 * 1024, |
| 106 |
MAX_LIGHT_TEXTURES = 1024, |
| 107 |
MAX_LIGHTS_PER_ITEM = 16, |
| 108 |
MAX_LIGHTS_PER_RENDER = 256, |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
struct ShaderSpecialization { |
| 116 |
union { |
| 117 |
uint32_t packed_0; |
| 118 |
|
| 119 |
struct { |
| 120 |
uint32_t use_lighting : 1; |
| 121 |
uint32_t use_msdf : 1; |
| 122 |
uint32_t use_lcd : 1; |
| 123 |
}; |
| 124 |
}; |
| 125 |
}; |
| 126 |
|
| 127 |
struct PipelineKey { |
| 128 |
ShaderVariant variant = SHADER_VARIANT_MAX; |
| 129 |
RD::FramebufferFormatID framebuffer_format_id = RD::INVALID_FORMAT_ID; |
| 130 |
RD::VertexFormatID vertex_format_id = RD::INVALID_ID; |
| 131 |
RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_MAX; |
| 132 |
ShaderSpecialization shader_specialization = {}; |
| 133 |
uint32_t lcd_blend = 0; |
| 134 |
uint32_t ubershader = 0; |
| 135 |
|
| 136 |
uint32_t hash() const { |
| 137 |
uint32_t h = hash_murmur3_one_32(variant); |
| 138 |
h = hash_murmur3_one_32(framebuffer_format_id, h); |
| 139 |
h = hash_murmur3_one_64((uint64_t)vertex_format_id, h); |
| 140 |
h = hash_murmur3_one_32(render_primitive, h); |
| 141 |
h = hash_murmur3_one_32(shader_specialization.packed_0, h); |
| 142 |
h = hash_murmur3_one_32(lcd_blend, h); |
| 143 |
h = hash_murmur3_one_32(ubershader, h); |
| 144 |
return hash_fmix32(h); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
struct CanvasShaderData : public RendererRD::MaterialStorage::ShaderData { |
| 149 |
Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms; |
| 150 |
int blend_mode = 0; |
| 151 |
|
| 152 |
Vector<uint32_t> ubo_offsets; |
| 153 |
uint32_t ubo_size = 0; |
| 154 |
|
| 155 |
String code; |
| 156 |
RID version; |
| 157 |
PipelineHashMapRD<PipelineKey, CanvasShaderData, void (CanvasShaderData::*)(PipelineKey)> pipeline_hash_map; |
| 158 |
|
| 159 |
static const uint32_t VERTEX_INPUT_MASKS_SIZE = SHADER_VARIANT_MAX * 2; |
| 160 |
std::atomic<uint64_t> vertex_input_masks[VERTEX_INPUT_MASKS_SIZE] = {}; |
| 161 |
|
| 162 |
bool uses_screen_texture = false; |
| 163 |
bool uses_screen_texture_mipmaps = false; |
| 164 |
bool uses_sdf = false; |
| 165 |
bool uses_time = false; |
| 166 |
|
| 167 |
void _clear_vertex_input_mask_cache(); |
| 168 |
void _create_pipeline(PipelineKey p_pipeline_key); |
| 169 |
virtual void set_code(const String &p_Code); |
| 170 |
virtual bool is_animated() const; |
| 171 |
virtual bool casts_shadows() const; |
| 172 |
virtual RS::ShaderNativeSourceCode get_native_source_code() const; |
| 173 |
virtual Pair<ShaderRD *, RID> get_native_shader_and_version() const; |
| 174 |
RID get_shader(ShaderVariant p_shader_variant, bool p_ubershader) const; |
| 175 |
uint64_t get_vertex_input_mask(ShaderVariant p_shader_variant, bool p_ubershader); |
| 176 |
bool is_valid() const; |
| 177 |
|
| 178 |
CanvasShaderData(); |
| 179 |
virtual ~CanvasShaderData(); |
| 180 |
}; |
| 181 |
|
| 182 |
struct { |
| 183 |
|
| 184 |
CanvasShaderData *default_version_data = nullptr; |
| 185 |
CanvasShaderRD canvas_shader; |
| 186 |
RID default_version_rd_shader; |
| 187 |
RID quad_index_buffer; |
| 188 |
RID quad_index_array; |
| 189 |
RD::VertexFormatID quad_vertex_format_id; |
| 190 |
RD::VertexFormatID primitive_vertex_format_id; |
| 191 |
ShaderCompiler compiler; |
| 192 |
uint32_t pipeline_compilations[RS::PIPELINE_SOURCE_MAX] = {}; |
| 193 |
Mutex mutex; |
| 194 |
} shader; |
| 195 |
|
| 196 |
RendererRD::MaterialStorage::ShaderData *_create_shader_func(); |
| 197 |
static RendererRD::MaterialStorage::ShaderData *_create_shader_funcs() { |
| 198 |
return static_cast<RendererCanvasRenderRD *>(singleton)->_create_shader_func(); |
| 199 |
} |
| 200 |
|
| 201 |
struct CanvasMaterialData : public RendererRD::MaterialStorage::MaterialData { |
| 202 |
CanvasShaderData *shader_data = nullptr; |
| 203 |
RID uniform_set; |
| 204 |
RID uniform_set_srgb; |
| 205 |
|
| 206 |
virtual void set_render_priority(int p_priority) {} |
| 207 |
virtual void set_next_pass(RID p_pass) {} |
| 208 |
virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty); |
| 209 |
virtual ~CanvasMaterialData(); |
| 210 |
}; |
| 211 |
|
| 212 |
RendererRD::MaterialStorage::MaterialData *_create_material_func(CanvasShaderData *p_shader); |
| 213 |
static RendererRD::MaterialStorage::MaterialData *_create_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) { |
| 214 |
return static_cast<RendererCanvasRenderRD *>(singleton)->_create_material_func(static_cast<CanvasShaderData *>(p_shader)); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
struct { |
| 222 |
RS::CanvasItemTextureFilter default_filter; |
| 223 |
RS::CanvasItemTextureRepeat default_repeat; |
| 224 |
} default_samplers; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
struct PolygonBuffers { |
| 231 |
RD::VertexFormatID vertex_format_id; |
| 232 |
RID vertex_buffer; |
| 233 |
RID vertex_array; |
| 234 |
RID index_buffer; |
| 235 |
RID indices; |
| 236 |
uint32_t primitive_count = 0; |
| 237 |
}; |
| 238 |
|
| 239 |
struct { |
| 240 |
HashMap<PolygonID, PolygonBuffers> polygons; |
| 241 |
PolygonID last_id; |
| 242 |
} polygon_buffers; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
struct { |
| 249 |
RID index_array[4]; |
| 250 |
} primitive_arrays; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
struct CanvasLight { |
| 261 |
RID texture; |
| 262 |
struct { |
| 263 |
bool enabled = false; |
| 264 |
float z_far; |
| 265 |
float y_offset; |
| 266 |
Transform2D directional_xform; |
| 267 |
} shadow; |
| 268 |
}; |
| 269 |
|
| 270 |
RID_Owner<CanvasLight> canvas_light_owner; |
| 271 |
|
| 272 |
struct PositionalShadowRenderPushConstant { |
| 273 |
float modelview[8]; |
| 274 |
float rotation[4]; |
| 275 |
float direction[2]; |
| 276 |
float z_far; |
| 277 |
uint32_t pad; |
| 278 |
float z_near; |
| 279 |
uint32_t cull_mode; |
| 280 |
float pad2[2]; |
| 281 |
}; |
| 282 |
|
| 283 |
struct ShadowRenderPushConstant { |
| 284 |
float projection[16]; |
| 285 |
float modelview[8]; |
| 286 |
float direction[2]; |
| 287 |
float z_far; |
| 288 |
uint32_t cull_mode; |
| 289 |
}; |
| 290 |
|
| 291 |
struct OccluderPolygon { |
| 292 |
RS::CanvasOccluderPolygonCullMode cull_mode; |
| 293 |
int line_point_count; |
| 294 |
RID vertex_buffer; |
| 295 |
RID vertex_array; |
| 296 |
RID index_buffer; |
| 297 |
RID index_array; |
| 298 |
|
| 299 |
int sdf_point_count; |
| 300 |
int sdf_index_count; |
| 301 |
RID sdf_vertex_buffer; |
| 302 |
RID sdf_vertex_array; |
| 303 |
RID sdf_index_buffer; |
| 304 |
RID sdf_index_array; |
| 305 |
bool sdf_is_lines; |
| 306 |
}; |
| 307 |
|
| 308 |
struct LightUniform { |
| 309 |
float matrix[8]; |
| 310 |
float shadow_matrix[8]; |
| 311 |
float color[4]; |
| 312 |
|
| 313 |
uint8_t shadow_color[4]; |
| 314 |
uint32_t flags; |
| 315 |
float shadow_pixel_size; |
| 316 |
float height; |
| 317 |
|
| 318 |
float position[2]; |
| 319 |
float shadow_z_far_inv; |
| 320 |
float shadow_y_ofs; |
| 321 |
|
| 322 |
float atlas_rect[4]; |
| 323 |
}; |
| 324 |
|
| 325 |
RID_Owner<OccluderPolygon> occluder_polygon_owner; |
| 326 |
|
| 327 |
enum ShadowRenderMode { |
| 328 |
SHADOW_RENDER_MODE_DIRECTIONAL_SHADOW, |
| 329 |
SHADOW_RENDER_MODE_POSITIONAL_SHADOW, |
| 330 |
SHADOW_RENDER_MODE_SDF, |
| 331 |
}; |
| 332 |
|
| 333 |
enum { |
| 334 |
SHADOW_RENDER_SDF_TRIANGLES, |
| 335 |
SHADOW_RENDER_SDF_LINES, |
| 336 |
}; |
| 337 |
|
| 338 |
struct { |
| 339 |
CanvasOcclusionShaderRD shader; |
| 340 |
RID shader_version; |
| 341 |
RID render_pipelines[2]; |
| 342 |
RID sdf_render_pipelines[2]; |
| 343 |
RD::VertexFormatID vertex_format; |
| 344 |
RD::VertexFormatID sdf_vertex_format; |
| 345 |
RD::FramebufferFormatID framebuffer_format; |
| 346 |
RD::FramebufferFormatID sdf_framebuffer_format; |
| 347 |
} shadow_render; |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
struct InstanceData { |
| 356 |
float world[6]; |
| 357 |
float ninepatch_pixel_size[2]; |
| 358 |
union { |
| 359 |
|
| 360 |
struct { |
| 361 |
float modulation[4]; |
| 362 |
float ninepatch_margins[4]; |
| 363 |
float dst_rect[4]; |
| 364 |
float src_rect[4]; |
| 365 |
float pad[2]; |
| 366 |
}; |
| 367 |
|
| 368 |
struct { |
| 369 |
float points[6]; |
| 370 |
float uvs[6]; |
| 371 |
uint32_t colors[6]; |
| 372 |
}; |
| 373 |
}; |
| 374 |
uint32_t flags; |
| 375 |
uint32_t instance_uniforms_ofs; |
| 376 |
uint32_t lights[4]; |
| 377 |
}; |
| 378 |
|
| 379 |
static_assert(sizeof(InstanceData) == 128, "2D instance data struct size must be 128 bytes"); |
| 380 |
|
| 381 |
struct PushConstant { |
| 382 |
ShaderSpecialization shader_specialization; |
| 383 |
uint32_t specular_shininess; |
| 384 |
uint32_t batch_flags; |
| 385 |
uint32_t pad0; |
| 386 |
|
| 387 |
float msdf[2]; |
| 388 |
float color_texture_pixel_size[2]; |
| 389 |
}; |
| 390 |
|
| 391 |
struct PushConstantAttributes { |
| 392 |
PushConstant base; |
| 393 |
|
| 394 |
float world[6]; |
| 395 |
uint32_t flags; |
| 396 |
uint32_t instance_uniforms_ofs; |
| 397 |
float modulation[4]; |
| 398 |
uint32_t lights[4]; |
| 399 |
|
| 400 |
operator PushConstant &() { |
| 401 |
return base; |
| 402 |
} |
| 403 |
}; |
| 404 |
|
| 405 |
|
| 406 |
struct TextureState { |
| 407 |
static const uint32_t FILTER_SHIFT = 0; |
| 408 |
static const uint32_t FILTER_BITS = 3; |
| 409 |
static const uint32_t FILTER_MASK = (1 << FILTER_BITS) - 1; |
| 410 |
static const uint32_t REPEAT_SHIFT = FILTER_BITS; |
| 411 |
static const uint32_t REPEAT_BITS = 2; |
| 412 |
static const uint32_t REPEAT_MASK = (1 << REPEAT_BITS) - 1; |
| 413 |
static const uint32_t TEXTURE_IS_DATA_SHIFT = REPEAT_SHIFT + REPEAT_BITS; |
| 414 |
static const uint32_t TEXTURE_IS_DATA_BITS = 1; |
| 415 |
static const uint32_t TEXTURE_IS_DATA_MASK = (1 << TEXTURE_IS_DATA_BITS) - 1; |
| 416 |
static const uint32_t LINEAR_COLORS_SHIFT = TEXTURE_IS_DATA_SHIFT + TEXTURE_IS_DATA_BITS; |
| 417 |
static const uint32_t LINEAR_COLORS_BITS = 1; |
| 418 |
static const uint32_t LINEAR_COLORS_MASK = (1 << LINEAR_COLORS_BITS) - 1; |
| 419 |
|
| 420 |
RID texture; |
| 421 |
uint32_t other = 0; |
| 422 |
|
| 423 |
TextureState() {} |
| 424 |
|
| 425 |
TextureState(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, bool p_texture_is_data, bool p_use_linear_colors) { |
| 426 |
texture = p_texture; |
| 427 |
other = (((uint32_t)p_base_filter & FILTER_MASK) << FILTER_SHIFT) | |
| 428 |
(((uint32_t)p_base_repeat & REPEAT_MASK) << REPEAT_SHIFT) | |
| 429 |
(((uint32_t)p_texture_is_data & TEXTURE_IS_DATA_MASK) << TEXTURE_IS_DATA_SHIFT) | |
| 430 |
(((uint32_t)p_use_linear_colors & LINEAR_COLORS_MASK) << LINEAR_COLORS_SHIFT); |
| 431 |
} |
| 432 |
|
| 433 |
_ALWAYS_INLINE_ RS::CanvasItemTextureFilter texture_filter() const { |
| 434 |
return (RS::CanvasItemTextureFilter)((other >> FILTER_SHIFT) & FILTER_MASK); |
| 435 |
} |
| 436 |
|
| 437 |
_ALWAYS_INLINE_ RS::CanvasItemTextureRepeat texture_repeat() const { |
| 438 |
return (RS::CanvasItemTextureRepeat)((other >> REPEAT_SHIFT) & REPEAT_MASK); |
| 439 |
} |
| 440 |
|
| 441 |
_ALWAYS_INLINE_ bool linear_colors() const { |
| 442 |
return (other >> LINEAR_COLORS_SHIFT) & LINEAR_COLORS_MASK; |
| 443 |
} |
| 444 |
|
| 445 |
_ALWAYS_INLINE_ bool texture_is_data() const { |
| 446 |
return (other >> TEXTURE_IS_DATA_SHIFT) & TEXTURE_IS_DATA_MASK; |
| 447 |
} |
| 448 |
|
| 449 |
_ALWAYS_INLINE_ bool operator==(const TextureState &p_val) const { |
| 450 |
return (texture == p_val.texture) && (other == p_val.other); |
| 451 |
} |
| 452 |
|
| 453 |
_ALWAYS_INLINE_ bool operator!=(const TextureState &p_val) const { |
| 454 |
return (texture != p_val.texture) || (other != p_val.other); |
| 455 |
} |
| 456 |
|
| 457 |
_ALWAYS_INLINE_ bool is_valid() const { return texture.is_valid(); } |
| 458 |
_ALWAYS_INLINE_ bool is_null() const { return texture.is_null(); } |
| 459 |
|
| 460 |
uint32_t hash() const { |
| 461 |
uint32_t hash = hash_murmur3_one_64(texture.get_id()); |
| 462 |
return hash_murmur3_one_32(other, hash); |
| 463 |
} |
| 464 |
}; |
| 465 |
|
| 466 |
struct TextureInfo { |
| 467 |
TextureState state; |
| 468 |
RID diffuse; |
| 469 |
RID normal; |
| 470 |
RID specular; |
| 471 |
RID sampler; |
| 472 |
Vector2 texpixel_size; |
| 473 |
uint32_t specular_shininess = 0; |
| 474 |
uint32_t flags = 0; |
| 475 |
}; |
| 476 |
|
| 477 |
|
| 478 |
struct RIDSetKey { |
| 479 |
TextureState state; |
| 480 |
|
| 481 |
RIDSetKey() { |
| 482 |
} |
| 483 |
|
| 484 |
RIDSetKey(TextureState p_state) : |
| 485 |
state(p_state) { |
| 486 |
} |
| 487 |
|
| 488 |
_ALWAYS_INLINE_ bool operator==(const RIDSetKey &p_val) const { |
| 489 |
return state == p_val.state; |
| 490 |
} |
| 491 |
|
| 492 |
_ALWAYS_INLINE_ bool operator!=(const RIDSetKey &p_val) const { |
| 493 |
return !(*this == p_val); |
| 494 |
} |
| 495 |
|
| 496 |
_ALWAYS_INLINE_ uint32_t hash() const { |
| 497 |
return state.hash(); |
| 498 |
} |
| 499 |
}; |
| 500 |
|
| 501 |
static void _before_evict(RendererCanvasRenderRD::RIDSetKey &p_key, RID &p_rid); |
| 502 |
static void _uniform_set_invalidation_callback(void *p_userdata); |
| 503 |
static void _canvas_texture_invalidation_callback(bool p_deleted, void *p_userdata); |
| 504 |
|
| 505 |
typedef LRUCache<RIDSetKey, RID, HashMapHasherDefault, HashMapComparatorDefault<RIDSetKey>, _before_evict> RIDCache; |
| 506 |
RIDCache rid_set_to_uniform_set; |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
HashMap<RID, TightLocalVector<RID>> canvas_texture_to_uniform_set; |
| 511 |
|
| 512 |
static constexpr uint32_t PUSH_DATA_INSTANCE_COUNT = 0x8000'0000; |
| 513 |
static constexpr uint32_t INSTANCE_COUNT_MASK = 0x7fff'ffff; |
| 514 |
|
| 515 |
struct Batch { |
| 516 |
|
| 517 |
uint32_t start = 0; |
| 518 |
|
| 519 |
uint32_t instance_count = 0; |
| 520 |
|
| 521 |
RID instance_buffer; |
| 522 |
|
| 523 |
InstanceData push_data = {}; |
| 524 |
|
| 525 |
TextureInfo *tex_info; |
| 526 |
|
| 527 |
Color modulate = Color(1.0, 1.0, 1.0, 1.0); |
| 528 |
float msdf_pix_range = 0.0; |
| 529 |
float msdf_outline = 0.0; |
| 530 |
|
| 531 |
Item *clip = nullptr; |
| 532 |
|
| 533 |
RID material; |
| 534 |
CanvasMaterialData *material_data = nullptr; |
| 535 |
|
| 536 |
const Item::Command *command = nullptr; |
| 537 |
Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; |
| 538 |
ShaderVariant shader_variant = SHADER_VARIANT_QUAD; |
| 539 |
RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES; |
| 540 |
bool use_lighting = false; |
| 541 |
bool use_msdf = false; |
| 542 |
bool use_lcd = false; |
| 543 |
bool has_blend = false; |
| 544 |
|
| 545 |
|
| 546 |
union { |
| 547 |
|
| 548 |
uint32_t primitive_points = 0; |
| 549 |
|
| 550 |
uint32_t mesh_instance_count; |
| 551 |
}; |
| 552 |
uint32_t flags = 0; |
| 553 |
|
| 554 |
_FORCE_INLINE_ PushConstant push_constant() const { |
| 555 |
PushConstant pc; |
| 556 |
pc.specular_shininess = tex_info->specular_shininess; |
| 557 |
pc.batch_flags = tex_info->flags | flags; |
| 558 |
pc.pad0 = 0; |
| 559 |
|
| 560 |
pc.msdf[0] = msdf_pix_range; |
| 561 |
pc.msdf[1] = msdf_outline; |
| 562 |
pc.color_texture_pixel_size[0] = tex_info->texpixel_size.x; |
| 563 |
pc.color_texture_pixel_size[1] = tex_info->texpixel_size.y; |
| 564 |
return pc; |
| 565 |
} |
| 566 |
|
| 567 |
_FORCE_INLINE_ PushConstantAttributes push_constant_attributes() const { |
| 568 |
PushConstantAttributes pc; |
| 569 |
pc.base = push_constant(); |
| 570 |
memcpy(pc.world, push_data.world, sizeof(pc.world)); |
| 571 |
memcpy(pc.modulation, push_data.modulation, sizeof(pc.modulation)); |
| 572 |
memcpy(pc.lights, push_data.lights, sizeof(pc.lights)); |
| 573 |
pc.flags = push_data.flags; |
| 574 |
pc.instance_uniforms_ofs = push_data.instance_uniforms_ofs; |
| 575 |
return pc; |
| 576 |
} |
| 577 |
}; |
| 578 |
|
| 579 |
HashMap<TextureState, TextureInfo, HashMapHasherDefault, HashMapComparatorDefault<TextureState>, PagedAllocator<HashMapElement<TextureState, TextureInfo>>> texture_info_map; |
| 580 |
|
| 581 |
struct State { |
| 582 |
|
| 583 |
struct Buffer { |
| 584 |
float canvas_transform[16]; |
| 585 |
float screen_transform[16]; |
| 586 |
float canvas_normal_transform[16]; |
| 587 |
float canvas_modulate[4]; |
| 588 |
|
| 589 |
float screen_pixel_size[2]; |
| 590 |
float time; |
| 591 |
uint32_t use_pixel_snap; |
| 592 |
|
| 593 |
float sdf_to_tex[4]; |
| 594 |
float sdf_to_screen[2]; |
| 595 |
float screen_to_sdf[2]; |
| 596 |
|
| 597 |
uint32_t directional_light_count; |
| 598 |
float tex_to_sdf; |
| 599 |
float shadow_pixel_size; |
| 600 |
uint32_t flags; |
| 601 |
}; |
| 602 |
|
| 603 |
LocalVector<Batch> canvas_instance_batches; |
| 604 |
uint32_t current_batch_index = 0; |
| 605 |
|
| 606 |
static_assert(std::is_trivially_destructible_v<InstanceData>); |
| 607 |
static_assert(std::is_trivially_constructible_v<InstanceData>); |
| 608 |
|
| 609 |
MultiUmaBuffer<1u> instance_buffers = MultiUmaBuffer<1u>("CANVAS_INSTANCE_DATA"); |
| 610 |
|
| 611 |
InstanceData *instance_data = nullptr; |
| 612 |
|
| 613 |
uint32_t instance_data_index = 0; |
| 614 |
|
| 615 |
InstanceData *prev_instance_data = nullptr; |
| 616 |
uint32_t prev_instance_data_index = 0; |
| 617 |
|
| 618 |
InstanceData intermediary_instance_data; |
| 619 |
|
| 620 |
uint32_t max_instances_per_buffer = 16384; |
| 621 |
uint32_t max_instance_buffer_size = 16384 * sizeof(InstanceData); |
| 622 |
|
| 623 |
Vector<RD::Uniform> batch_texture_uniforms; |
| 624 |
RID current_batch_uniform_set; |
| 625 |
|
| 626 |
LightUniform *light_uniforms = nullptr; |
| 627 |
|
| 628 |
RID lights_storage_buffer; |
| 629 |
RID canvas_state_buffer; |
| 630 |
RID shadow_sampler; |
| 631 |
RID shadow_texture; |
| 632 |
RID shadow_depth_texture; |
| 633 |
RID shadow_fb; |
| 634 |
int shadow_texture_size = 2048; |
| 635 |
|
| 636 |
RID shadow_occluder_buffer; |
| 637 |
uint32_t shadow_occluder_buffer_size; |
| 638 |
RID shadow_ocluder_uniform_set; |
| 639 |
|
| 640 |
RID default_transforms_uniform_set; |
| 641 |
|
| 642 |
double time; |
| 643 |
|
| 644 |
} state; |
| 645 |
|
| 646 |
Item *items[MAX_RENDER_ITEMS]; |
| 647 |
|
| 648 |
TextureInfo default_texture_info; |
| 649 |
|
| 650 |
bool using_directional_lights = false; |
| 651 |
RID default_canvas_texture; |
| 652 |
|
| 653 |
RID default_canvas_group_shader; |
| 654 |
RID default_canvas_group_material; |
| 655 |
RID default_clip_children_material; |
| 656 |
RID default_clip_children_shader; |
| 657 |
|
| 658 |
RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR; |
| 659 |
RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED; |
| 660 |
|
| 661 |
RID _create_base_uniform_set(RID p_to_render_target, bool p_backbuffer); |
| 662 |
|
| 663 |
bool debug_redraw = false; |
| 664 |
Color debug_redraw_color; |
| 665 |
double debug_redraw_time = 1.0; |
| 666 |
|
| 667 |
|
| 668 |
struct RenderTarget { |
| 669 |
|
| 670 |
RID render_target; |
| 671 |
bool use_linear_colors = false; |
| 672 |
}; |
| 673 |
|
| 674 |
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); |
| 675 |
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); |
| 676 |
void _record_item_commands(const Item *p_item, RenderTarget p_render_target, const Transform2D &p_base_transform, Item *&r_current_clip, Light *p_lights, bool &r_batch_broken, bool &r_sdf_used, Batch *&r_current_batch); |
| 677 |
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); |
| 678 |
void _prepare_batch_texture_info(RID p_texture, TextureState &p_state, TextureInfo *p_info); |
| 679 |
|
| 680 |
|
| 681 |
InstanceData *new_instance_data(Batch &p_current_batch, const InstanceData &template_instance, bool p_use_push_data = false); |
| 682 |
[[nodiscard]] Batch *_new_batch(bool &r_batch_broken); |
| 683 |
void _add_to_batch(bool &r_batch_broken, Batch *&r_current_batch); |
| 684 |
void _allocate_instance_buffer(); |
| 685 |
|
| 686 |
_FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4); |
| 687 |
_FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3); |
| 688 |
|
| 689 |
_FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4); |
| 690 |
_FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4); |
| 691 |
|
| 692 |
void _update_shadow_atlas(); |
| 693 |
void _update_occluder_buffer(uint32_t p_size); |
| 694 |
|
| 695 |
public: |
| 696 |
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; |
| 697 |
void free_polygon(PolygonID p_polygon) override; |
| 698 |
|
| 699 |
RID light_create() override; |
| 700 |
void light_set_texture(RID p_rid, RID p_texture) override; |
| 701 |
void light_set_use_shadow(RID p_rid, bool p_enable) override; |
| 702 |
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; |
| 703 |
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; |
| 704 |
|
| 705 |
virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override; |
| 706 |
|
| 707 |
RID occluder_polygon_create() override; |
| 708 |
void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override; |
| 709 |
void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override; |
| 710 |
|
| 711 |
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; |
| 712 |
|
| 713 |
virtual void set_shadow_texture_size(int p_size) override; |
| 714 |
|
| 715 |
void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) override; |
| 716 |
uint32_t get_pipeline_compilations(RS::PipelineSource p_source) override; |
| 717 |
|
| 718 |
void set_time(double p_time); |
| 719 |
void update() override; |
| 720 |
bool free(RID p_rid) override; |
| 721 |
RendererCanvasRenderRD(); |
| 722 |
~RendererCanvasRenderRD(); |
| 723 |
}; |