| 1 | /**************************************************************************/ |
| 2 | /* renderer_canvas_render_rd.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "renderer_canvas_render_rd.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/math/geometry_2d.h" |
| 35 | #include "core/math/math_defs.h" |
| 36 | #include "core/math/math_funcs.h" |
| 37 | #include "core/math/transform_interpolator.h" |
| 38 | #include "core/templates/fixed_vector.h" |
| 39 | #include "servers/rendering/renderer_rd/storage_rd/material_storage.h" |
| 40 | #include "servers/rendering/renderer_rd/storage_rd/mesh_storage.h" |
| 41 | #include "servers/rendering/renderer_rd/storage_rd/particles_storage.h" |
| 42 | #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h" |
| 43 | #include "servers/rendering/rendering_server_default.h" |
| 44 | |
| 45 | void RendererCanvasRenderRD::_update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4) { |
| 46 | p_mat4[0] = p_transform.columns[0][0]; |
| 47 | p_mat4[1] = p_transform.columns[0][1]; |
| 48 | p_mat4[2] = 0; |
| 49 | p_mat4[3] = 0; |
| 50 | p_mat4[4] = p_transform.columns[1][0]; |
| 51 | p_mat4[5] = p_transform.columns[1][1]; |
| 52 | p_mat4[6] = 0; |
| 53 | p_mat4[7] = 0; |
| 54 | p_mat4[8] = 0; |
| 55 | p_mat4[9] = 0; |
| 56 | p_mat4[10] = 1; |
| 57 | p_mat4[11] = 0; |
| 58 | p_mat4[12] = p_transform.columns[2][0]; |
| 59 | p_mat4[13] = p_transform.columns[2][1]; |
| 60 | p_mat4[14] = 0; |
| 61 | p_mat4[15] = 1; |
| 62 | } |
| 63 | |
| 64 | void RendererCanvasRenderRD::_update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4) { |
| 65 | p_mat2x4[0] = p_transform.columns[0][0]; |
| 66 | p_mat2x4[1] = p_transform.columns[1][0]; |
| 67 | p_mat2x4[2] = 0; |
| 68 | p_mat2x4[3] = p_transform.columns[2][0]; |
| 69 | |
| 70 | p_mat2x4[4] = p_transform.columns[0][1]; |
| 71 | p_mat2x4[5] = p_transform.columns[1][1]; |
| 72 | p_mat2x4[6] = 0; |
| 73 | p_mat2x4[7] = p_transform.columns[2][1]; |
| 74 | } |
| 75 | |
| 76 | void RendererCanvasRenderRD::_update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3) { |
| 77 | p_mat2x3[0] = p_transform.columns[0][0]; |
| 78 | p_mat2x3[1] = p_transform.columns[0][1]; |
| 79 | p_mat2x3[2] = p_transform.columns[1][0]; |
| 80 | p_mat2x3[3] = p_transform.columns[1][1]; |
| 81 | p_mat2x3[4] = p_transform.columns[2][0]; |
| 82 | p_mat2x3[5] = p_transform.columns[2][1]; |
| 83 | } |
| 84 | |
| 85 | void RendererCanvasRenderRD::_update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4) { |
| 86 | p_mat4[0] = p_transform.basis.rows[0][0]; |
| 87 | p_mat4[1] = p_transform.basis.rows[1][0]; |
| 88 | p_mat4[2] = p_transform.basis.rows[2][0]; |
| 89 | p_mat4[3] = 0; |
| 90 | p_mat4[4] = p_transform.basis.rows[0][1]; |
| 91 | p_mat4[5] = p_transform.basis.rows[1][1]; |
| 92 | p_mat4[6] = p_transform.basis.rows[2][1]; |
| 93 | p_mat4[7] = 0; |
| 94 | p_mat4[8] = p_transform.basis.rows[0][2]; |
| 95 | p_mat4[9] = p_transform.basis.rows[1][2]; |
| 96 | p_mat4[10] = p_transform.basis.rows[2][2]; |
| 97 | p_mat4[11] = 0; |
| 98 | p_mat4[12] = p_transform.origin.x; |
| 99 | p_mat4[13] = p_transform.origin.y; |
| 100 | p_mat4[14] = p_transform.origin.z; |
| 101 | p_mat4[15] = 1; |
| 102 | } |
| 103 | |
| 104 | RendererCanvasRender::PolygonID RendererCanvasRenderRD::request_polygon(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, const Vector<int> &p_bones, const Vector<float> &p_weights, int p_count) { |
| 105 | // Care must be taken to generate array formats |
| 106 | // in ways where they could be reused, so we will |
| 107 | // put single-occurring elements first, and repeated |
| 108 | // elements later. This way the generated formats are |
| 109 | // the same no matter the length of the arrays. |
| 110 | // This dramatically reduces the amount of pipeline objects |
| 111 | // that need to be created for these formats. |
| 112 | |
| 113 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 114 | |
| 115 | uint32_t vertex_count = p_points.size(); |
| 116 | uint32_t stride = 2; //vertices always repeat |
| 117 | if ((uint32_t)p_colors.size() == vertex_count || p_colors.size() == 1) { |
| 118 | stride += 4; |
| 119 | } |
| 120 | if ((uint32_t)p_uvs.size() == vertex_count) { |
| 121 | stride += 2; |
| 122 | } |
| 123 | if ((uint32_t)p_bones.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) { |
| 124 | stride += 4; |
| 125 | } |
| 126 | |
| 127 | uint32_t buffer_size = stride * p_points.size(); |
| 128 | |
| 129 | Vector<uint8_t> polygon_buffer; |
| 130 | polygon_buffer.resize(buffer_size * sizeof(float)); |
| 131 | Vector<RD::VertexAttribute> descriptions; |
| 132 | descriptions.resize(5); |
| 133 | Vector<RID> buffers; |
| 134 | buffers.resize(5); |
| 135 | |
| 136 | { |
| 137 | uint8_t *r = polygon_buffer.ptrw(); |
| 138 | float *fptr = reinterpret_cast<float *>(r); |
| 139 | uint32_t *uptr = reinterpret_cast<uint32_t *>(r); |
| 140 | uint32_t base_offset = 0; |
| 141 | { //vertices |
| 142 | RD::VertexAttribute vd; |
| 143 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 144 | vd.offset = base_offset * sizeof(float); |
| 145 | vd.location = RS::ARRAY_VERTEX; |
| 146 | vd.stride = stride * sizeof(float); |
| 147 | |
| 148 | descriptions.write[0] = vd; |
| 149 | |
| 150 | const Vector2 *points_ptr = p_points.ptr(); |
| 151 | |
| 152 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 153 | fptr[base_offset + i * stride + 0] = points_ptr[i].x; |
| 154 | fptr[base_offset + i * stride + 1] = points_ptr[i].y; |
| 155 | } |
| 156 | |
| 157 | base_offset += 2; |
| 158 | } |
| 159 | |
| 160 | //colors |
| 161 | if ((uint32_t)p_colors.size() == vertex_count || p_colors.size() == 1) { |
| 162 | RD::VertexAttribute vd; |
| 163 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 164 | vd.offset = base_offset * sizeof(float); |
| 165 | vd.location = RS::ARRAY_COLOR; |
| 166 | vd.stride = stride * sizeof(float); |
| 167 | |
| 168 | descriptions.write[1] = vd; |
| 169 | |
| 170 | if (p_colors.size() == 1) { |
| 171 | Color color = p_colors[0]; |
| 172 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 173 | fptr[base_offset + i * stride + 0] = color.r; |
| 174 | fptr[base_offset + i * stride + 1] = color.g; |
| 175 | fptr[base_offset + i * stride + 2] = color.b; |
| 176 | fptr[base_offset + i * stride + 3] = color.a; |
| 177 | } |
| 178 | } else { |
| 179 | const Color *color_ptr = p_colors.ptr(); |
| 180 | |
| 181 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 182 | fptr[base_offset + i * stride + 0] = color_ptr[i].r; |
| 183 | fptr[base_offset + i * stride + 1] = color_ptr[i].g; |
| 184 | fptr[base_offset + i * stride + 2] = color_ptr[i].b; |
| 185 | fptr[base_offset + i * stride + 3] = color_ptr[i].a; |
| 186 | } |
| 187 | } |
| 188 | base_offset += 4; |
| 189 | } else { |
| 190 | RD::VertexAttribute vd; |
| 191 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 192 | vd.offset = 0; |
| 193 | vd.location = RS::ARRAY_COLOR; |
| 194 | vd.stride = 0; |
| 195 | |
| 196 | descriptions.write[1] = vd; |
| 197 | buffers.write[1] = mesh_storage->mesh_get_default_rd_buffer(RendererRD::MeshStorage::DEFAULT_RD_BUFFER_COLOR); |
| 198 | } |
| 199 | |
| 200 | //uvs |
| 201 | if ((uint32_t)p_uvs.size() == vertex_count) { |
| 202 | RD::VertexAttribute vd; |
| 203 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 204 | vd.offset = base_offset * sizeof(float); |
| 205 | vd.location = RS::ARRAY_TEX_UV; |
| 206 | vd.stride = stride * sizeof(float); |
| 207 | |
| 208 | descriptions.write[2] = vd; |
| 209 | |
| 210 | const Vector2 *uv_ptr = p_uvs.ptr(); |
| 211 | |
| 212 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 213 | fptr[base_offset + i * stride + 0] = uv_ptr[i].x; |
| 214 | fptr[base_offset + i * stride + 1] = uv_ptr[i].y; |
| 215 | } |
| 216 | base_offset += 2; |
| 217 | } else { |
| 218 | RD::VertexAttribute vd; |
| 219 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 220 | vd.offset = 0; |
| 221 | vd.location = RS::ARRAY_TEX_UV; |
| 222 | vd.stride = 0; |
| 223 | |
| 224 | descriptions.write[2] = vd; |
| 225 | buffers.write[2] = mesh_storage->mesh_get_default_rd_buffer(RendererRD::MeshStorage::DEFAULT_RD_BUFFER_TEX_UV); |
| 226 | } |
| 227 | |
| 228 | //bones |
| 229 | if ((uint32_t)p_indices.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) { |
| 230 | RD::VertexAttribute vd; |
| 231 | vd.format = RD::DATA_FORMAT_R16G16B16A16_UINT; |
| 232 | vd.offset = base_offset * sizeof(float); |
| 233 | vd.location = RS::ARRAY_BONES; |
| 234 | vd.stride = stride * sizeof(float); |
| 235 | |
| 236 | descriptions.write[3] = vd; |
| 237 | |
| 238 | const int *bone_ptr = p_bones.ptr(); |
| 239 | |
| 240 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 241 | uint16_t *bone16w = (uint16_t *)&uptr[base_offset + i * stride]; |
| 242 | |
| 243 | bone16w[0] = bone_ptr[i * 4 + 0]; |
| 244 | bone16w[1] = bone_ptr[i * 4 + 1]; |
| 245 | bone16w[2] = bone_ptr[i * 4 + 2]; |
| 246 | bone16w[3] = bone_ptr[i * 4 + 3]; |
| 247 | } |
| 248 | |
| 249 | base_offset += 2; |
| 250 | } else { |
| 251 | RD::VertexAttribute vd; |
| 252 | vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT; |
| 253 | vd.offset = 0; |
| 254 | vd.location = RS::ARRAY_BONES; |
| 255 | vd.stride = 0; |
| 256 | |
| 257 | descriptions.write[3] = vd; |
| 258 | buffers.write[3] = mesh_storage->mesh_get_default_rd_buffer(RendererRD::MeshStorage::DEFAULT_RD_BUFFER_BONES); |
| 259 | } |
| 260 | |
| 261 | //weights |
| 262 | if ((uint32_t)p_weights.size() == vertex_count * 4) { |
| 263 | RD::VertexAttribute vd; |
| 264 | vd.format = RD::DATA_FORMAT_R16G16B16A16_UNORM; |
| 265 | vd.offset = base_offset * sizeof(float); |
| 266 | vd.location = RS::ARRAY_WEIGHTS; |
| 267 | vd.stride = stride * sizeof(float); |
| 268 | |
| 269 | descriptions.write[4] = vd; |
| 270 | |
| 271 | const float *weight_ptr = p_weights.ptr(); |
| 272 | |
| 273 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 274 | uint16_t *weight16w = (uint16_t *)&uptr[base_offset + i * stride]; |
| 275 | |
| 276 | weight16w[0] = CLAMP(weight_ptr[i * 4 + 0] * 65535, 0, 65535); |
| 277 | weight16w[1] = CLAMP(weight_ptr[i * 4 + 1] * 65535, 0, 65535); |
| 278 | weight16w[2] = CLAMP(weight_ptr[i * 4 + 2] * 65535, 0, 65535); |
| 279 | weight16w[3] = CLAMP(weight_ptr[i * 4 + 3] * 65535, 0, 65535); |
| 280 | } |
| 281 | |
| 282 | base_offset += 2; |
| 283 | } else { |
| 284 | RD::VertexAttribute vd; |
| 285 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 286 | vd.offset = 0; |
| 287 | vd.location = RS::ARRAY_WEIGHTS; |
| 288 | vd.stride = 0; |
| 289 | |
| 290 | descriptions.write[4] = vd; |
| 291 | buffers.write[4] = mesh_storage->mesh_get_default_rd_buffer(RendererRD::MeshStorage::DEFAULT_RD_BUFFER_WEIGHTS); |
| 292 | } |
| 293 | |
| 294 | //check that everything is as it should be |
| 295 | ERR_FAIL_COND_V(base_offset != stride, 0); //bug |
| 296 | } |
| 297 | |
| 298 | RD::VertexFormatID vertex_id = RD::get_singleton()->vertex_format_create(descriptions); |
| 299 | ERR_FAIL_COND_V(vertex_id == RD::INVALID_ID, 0); |
| 300 | |
| 301 | PolygonBuffers pb; |
| 302 | pb.vertex_buffer = RD::get_singleton()->vertex_buffer_create(polygon_buffer.size(), polygon_buffer); |
| 303 | for (int i = 0; i < descriptions.size(); i++) { |
| 304 | if (buffers[i] == RID()) { //if put in vertex, use as vertex |
| 305 | buffers.write[i] = pb.vertex_buffer; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | pb.vertex_array = RD::get_singleton()->vertex_array_create(p_points.size(), vertex_id, buffers); |
| 310 | pb.primitive_count = vertex_count; |
| 311 | |
| 312 | if (p_indices.size()) { |
| 313 | //create indices, as indices were requested |
| 314 | Vector<uint8_t> index_buffer; |
| 315 | index_buffer.resize(p_count * sizeof(int32_t)); |
| 316 | { |
| 317 | uint8_t *w = index_buffer.ptrw(); |
| 318 | memcpy(w, p_indices.ptr(), sizeof(int32_t) * p_indices.size()); |
| 319 | } |
| 320 | pb.index_buffer = RD::get_singleton()->index_buffer_create(p_count, RD::INDEX_BUFFER_FORMAT_UINT32, index_buffer); |
| 321 | pb.indices = RD::get_singleton()->index_array_create(pb.index_buffer, 0, p_count); |
| 322 | pb.primitive_count = p_count; |
| 323 | } |
| 324 | |
| 325 | pb.vertex_format_id = vertex_id; |
| 326 | |
| 327 | PolygonID id = polygon_buffers.last_id++; |
| 328 | |
| 329 | polygon_buffers.polygons[id] = pb; |
| 330 | |
| 331 | return id; |
| 332 | } |
| 333 | |
| 334 | void RendererCanvasRenderRD::free_polygon(PolygonID p_polygon) { |
| 335 | PolygonBuffers *pb_ptr = polygon_buffers.polygons.getptr(p_polygon); |
| 336 | ERR_FAIL_NULL(pb_ptr); |
| 337 | |
| 338 | PolygonBuffers &pb = *pb_ptr; |
| 339 | |
| 340 | if (pb.indices.is_valid()) { |
| 341 | RD::get_singleton()->free_rid(pb.indices); |
| 342 | } |
| 343 | if (pb.index_buffer.is_valid()) { |
| 344 | RD::get_singleton()->free_rid(pb.index_buffer); |
| 345 | } |
| 346 | |
| 347 | RD::get_singleton()->free_rid(pb.vertex_array); |
| 348 | RD::get_singleton()->free_rid(pb.vertex_buffer); |
| 349 | |
| 350 | polygon_buffers.polygons.erase(p_polygon); |
| 351 | } |
| 352 | |
| 353 | //////////////////// |
| 354 | |
| 355 | static RD::RenderPrimitive _primitive_type_to_render_primitive(RS::PrimitiveType p_primitive) { |
| 356 | switch (p_primitive) { |
| 357 | case RS::PRIMITIVE_POINTS: |
| 358 | return RD::RENDER_PRIMITIVE_POINTS; |
| 359 | case RS::PRIMITIVE_LINES: |
| 360 | return RD::RENDER_PRIMITIVE_LINES; |
| 361 | case RS::PRIMITIVE_LINE_STRIP: |
| 362 | return RD::RENDER_PRIMITIVE_LINESTRIPS; |
| 363 | case RS::PRIMITIVE_TRIANGLES: |
| 364 | return RD::RENDER_PRIMITIVE_TRIANGLES; |
| 365 | case RS::PRIMITIVE_TRIANGLE_STRIP: |
| 366 | return RD::RENDER_PRIMITIVE_TRIANGLE_STRIPS; |
| 367 | default: |
| 368 | return RD::RENDER_PRIMITIVE_MAX; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | _FORCE_INLINE_ static uint32_t _indices_to_primitives(RS::PrimitiveType p_primitive, uint32_t p_indices) { |
| 373 | static const uint32_t divisor[RS::PRIMITIVE_MAX] = { 1, 2, 1, 3, 1 }; |
| 374 | static const uint32_t subtractor[RS::PRIMITIVE_MAX] = { 0, 0, 1, 0, 2 }; |
| 375 | return (p_indices - subtractor[p_primitive]) / divisor[p_primitive]; |
| 376 | } |
| 377 | |
| 378 | RID RendererCanvasRenderRD::_create_base_uniform_set(RID p_to_render_target, bool p_backbuffer) { |
| 379 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 380 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 381 | |
| 382 | //re create canvas state |
| 383 | thread_local LocalVector<RD::Uniform> uniforms; |
| 384 | uniforms.clear(); |
| 385 | |
| 386 | { |
| 387 | RD::Uniform u; |
| 388 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 389 | u.binding = 1; |
| 390 | u.append_id(state.canvas_state_buffer); |
| 391 | uniforms.push_back(u); |
| 392 | } |
| 393 | |
| 394 | { |
| 395 | RD::Uniform u; |
| 396 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 397 | u.binding = 2; |
| 398 | u.append_id(state.lights_storage_buffer); |
| 399 | uniforms.push_back(u); |
| 400 | } |
| 401 | |
| 402 | { |
| 403 | RD::Uniform u; |
| 404 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 405 | u.binding = 3; |
| 406 | u.append_id(RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture()); |
| 407 | uniforms.push_back(u); |
| 408 | } |
| 409 | |
| 410 | { |
| 411 | RD::Uniform u; |
| 412 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 413 | u.binding = 4; |
| 414 | u.append_id(state.shadow_texture); |
| 415 | uniforms.push_back(u); |
| 416 | } |
| 417 | |
| 418 | { |
| 419 | RD::Uniform u; |
| 420 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 421 | u.binding = 5; |
| 422 | u.append_id(state.shadow_sampler); |
| 423 | uniforms.push_back(u); |
| 424 | } |
| 425 | |
| 426 | { |
| 427 | RD::Uniform u; |
| 428 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 429 | u.binding = 6; |
| 430 | RID screen; |
| 431 | if (p_backbuffer) { |
| 432 | screen = texture_storage->render_target_get_rd_texture(p_to_render_target); |
| 433 | } else { |
| 434 | screen = texture_storage->render_target_get_rd_backbuffer(p_to_render_target); |
| 435 | if (screen.is_null()) { //unallocated backbuffer |
| 436 | screen = RendererRD::TextureStorage::get_singleton()->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE); |
| 437 | } |
| 438 | } |
| 439 | u.append_id(screen); |
| 440 | uniforms.push_back(u); |
| 441 | } |
| 442 | |
| 443 | { |
| 444 | RD::Uniform u; |
| 445 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 446 | u.binding = 7; |
| 447 | RID sdf = texture_storage->render_target_get_sdf_texture(p_to_render_target); |
| 448 | u.append_id(sdf); |
| 449 | uniforms.push_back(u); |
| 450 | } |
| 451 | |
| 452 | { |
| 453 | RD::Uniform u; |
| 454 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 455 | u.binding = 9; |
| 456 | u.append_id(RendererRD::MaterialStorage::get_singleton()->global_shader_uniforms_get_storage_buffer()); |
| 457 | uniforms.push_back(u); |
| 458 | } |
| 459 | |
| 460 | material_storage->samplers_rd_get_default().append_uniforms(uniforms, SAMPLERS_BINDING_FIRST_INDEX); |
| 461 | |
| 462 | RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader.default_version_rd_shader, BASE_UNIFORM_SET); |
| 463 | if (p_backbuffer) { |
| 464 | texture_storage->render_target_set_backbuffer_uniform_set(p_to_render_target, uniform_set); |
| 465 | } else { |
| 466 | texture_storage->render_target_set_framebuffer_uniform_set(p_to_render_target, uniform_set); |
| 467 | } |
| 468 | |
| 469 | return uniform_set; |
| 470 | } |
| 471 | |
| 472 | RID RendererCanvasRenderRD::_get_pipeline_specialization_or_ubershader(CanvasShaderData *p_shader_data, PipelineKey &r_pipeline_key, PushConstant &r_push_constant, RID p_mesh_instance, void *p_surface, uint32_t p_surface_index, RID *r_vertex_array) { |
| 473 | r_pipeline_key.ubershader = 0; |
| 474 | |
| 475 | const uint32_t ubershader_iterations = 1; |
| 476 | while (r_pipeline_key.ubershader < ubershader_iterations) { |
| 477 | if (r_vertex_array != nullptr) { |
| 478 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 479 | uint64_t input_mask = p_shader_data->get_vertex_input_mask(r_pipeline_key.variant, r_pipeline_key.ubershader); |
| 480 | if (p_mesh_instance.is_valid()) { |
| 481 | mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(p_mesh_instance, p_surface_index, input_mask, false, false, *r_vertex_array, r_pipeline_key.vertex_format_id); |
| 482 | } else { |
| 483 | mesh_storage->mesh_surface_get_vertex_arrays_and_format(p_surface, input_mask, false, false, *r_vertex_array, r_pipeline_key.vertex_format_id); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (r_pipeline_key.ubershader) { |
| 488 | r_push_constant.shader_specialization = r_pipeline_key.shader_specialization; |
| 489 | r_pipeline_key.shader_specialization = {}; |
| 490 | } else { |
| 491 | r_push_constant.shader_specialization = {}; |
| 492 | } |
| 493 | |
| 494 | bool wait_for_compilation = r_pipeline_key.ubershader || ubershader_iterations == 1; |
| 495 | RS::PipelineSource source = RS::PIPELINE_SOURCE_CANVAS; |
| 496 | RID pipeline = p_shader_data->pipeline_hash_map.get_pipeline(r_pipeline_key, r_pipeline_key.hash(), wait_for_compilation, source); |
| 497 | if (pipeline.is_valid()) { |
| 498 | return pipeline; |
| 499 | } |
| 500 | |
| 501 | r_pipeline_key.ubershader++; |
| 502 | } |
| 503 | |
| 504 | // This case should never be reached unless the shader wasn't available. |
| 505 | return RID(); |
| 506 | } |
| 507 | |
| 508 | void RendererCanvasRenderRD::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, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, bool &r_sdf_used, RenderingMethod::RenderInfo *r_render_info) { |
| 509 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 510 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 511 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 512 | |
| 513 | r_sdf_used = false; |
| 514 | int item_count = 0; |
| 515 | |
| 516 | //setup canvas state uniforms if needed |
| 517 | |
| 518 | Transform2D canvas_transform_inverse = p_canvas_transform.affine_inverse(); |
| 519 | |
| 520 | //setup directional lights if exist |
| 521 | |
| 522 | uint32_t light_count = 0; |
| 523 | uint32_t directional_light_count = 0; |
| 524 | { |
| 525 | Light *l = p_directional_light_list; |
| 526 | uint32_t index = 0; |
| 527 | |
| 528 | while (l) { |
| 529 | if (index == MAX_LIGHTS_PER_RENDER) { |
| 530 | l->render_index_cache = -1; |
| 531 | l = l->next_ptr; |
| 532 | continue; |
| 533 | } |
| 534 | |
| 535 | CanvasLight *clight = canvas_light_owner.get_or_null(l->light_internal); |
| 536 | if (!clight) { //unused or invalid texture |
| 537 | l->render_index_cache = -1; |
| 538 | l = l->next_ptr; |
| 539 | ERR_CONTINUE(!clight); |
| 540 | } |
| 541 | |
| 542 | Vector2 canvas_light_dir = l->xform_cache.columns[1].normalized(); |
| 543 | |
| 544 | state.light_uniforms[index].position[0] = -canvas_light_dir.x; |
| 545 | state.light_uniforms[index].position[1] = -canvas_light_dir.y; |
| 546 | |
| 547 | _update_transform_2d_to_mat2x4(clight->shadow.directional_xform, state.light_uniforms[index].shadow_matrix); |
| 548 | |
| 549 | state.light_uniforms[index].height = l->height; //0..1 here |
| 550 | |
| 551 | for (int i = 0; i < 4; i++) { |
| 552 | state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255)); |
| 553 | state.light_uniforms[index].color[i] = l->color[i]; |
| 554 | } |
| 555 | |
| 556 | state.light_uniforms[index].color[3] *= l->energy; //use alpha for energy, so base color can go separate |
| 557 | |
| 558 | if (state.shadow_fb.is_valid()) { |
| 559 | state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth); |
| 560 | state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far; |
| 561 | state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset; |
| 562 | } else { |
| 563 | state.light_uniforms[index].shadow_pixel_size = 1.0; |
| 564 | state.light_uniforms[index].shadow_z_far_inv = 1.0; |
| 565 | state.light_uniforms[index].shadow_y_ofs = 0; |
| 566 | } |
| 567 | |
| 568 | state.light_uniforms[index].flags = l->blend_mode << LIGHT_FLAGS_BLEND_SHIFT; |
| 569 | state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT; |
| 570 | if (clight->shadow.enabled) { |
| 571 | state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW; |
| 572 | } |
| 573 | |
| 574 | l->render_index_cache = index; |
| 575 | |
| 576 | index++; |
| 577 | l = l->next_ptr; |
| 578 | } |
| 579 | |
| 580 | light_count = index; |
| 581 | directional_light_count = light_count; |
| 582 | using_directional_lights = directional_light_count > 0; |
| 583 | } |
| 584 | |
| 585 | //setup lights if exist |
| 586 | |
| 587 | { |
| 588 | Light *l = p_light_list; |
| 589 | uint32_t index = light_count; |
| 590 | |
| 591 | while (l) { |
| 592 | if (index == MAX_LIGHTS_PER_RENDER) { |
| 593 | l->render_index_cache = -1; |
| 594 | l = l->next_ptr; |
| 595 | continue; |
| 596 | } |
| 597 | |
| 598 | CanvasLight *clight = canvas_light_owner.get_or_null(l->light_internal); |
| 599 | if (!clight) { //unused or invalid texture |
| 600 | l->render_index_cache = -1; |
| 601 | l = l->next_ptr; |
| 602 | ERR_CONTINUE(!clight); |
| 603 | } |
| 604 | |
| 605 | Transform2D final_xform; |
| 606 | if (!RSG::canvas->_interpolation_data.interpolation_enabled || !l->interpolated || !l->on_interpolate_transform_list) { |
| 607 | final_xform = l->xform_curr; |
| 608 | } else { |
| 609 | real_t f = Engine::get_singleton()->get_physics_interpolation_fraction(); |
| 610 | TransformInterpolator::interpolate_transform_2d(l->xform_prev, l->xform_curr, final_xform, f); |
| 611 | } |
| 612 | // Convert light position to canvas coordinates, as all computation is done in canvas coordinates to avoid precision loss. |
| 613 | Vector2 canvas_light_pos = p_canvas_transform.xform(final_xform.get_origin()); |
| 614 | state.light_uniforms[index].position[0] = canvas_light_pos.x; |
| 615 | state.light_uniforms[index].position[1] = canvas_light_pos.y; |
| 616 | |
| 617 | _update_transform_2d_to_mat2x4(l->light_shader_xform.affine_inverse(), state.light_uniforms[index].matrix); |
| 618 | _update_transform_2d_to_mat2x4(l->xform_cache.affine_inverse(), state.light_uniforms[index].shadow_matrix); |
| 619 | |
| 620 | state.light_uniforms[index].height = l->height * (p_canvas_transform.columns[0].length() + p_canvas_transform.columns[1].length()) * 0.5; //approximate height conversion to the canvas size, since all calculations are done in canvas coords to avoid precision loss |
| 621 | for (int i = 0; i < 4; i++) { |
| 622 | state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255)); |
| 623 | state.light_uniforms[index].color[i] = l->color[i]; |
| 624 | } |
| 625 | |
| 626 | state.light_uniforms[index].color[3] *= l->energy; //use alpha for energy, so base color can go separate |
| 627 | |
| 628 | if (state.shadow_fb.is_valid()) { |
| 629 | state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth); |
| 630 | state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far; |
| 631 | state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset; |
| 632 | } else { |
| 633 | state.light_uniforms[index].shadow_pixel_size = 1.0; |
| 634 | state.light_uniforms[index].shadow_z_far_inv = 1.0; |
| 635 | state.light_uniforms[index].shadow_y_ofs = 0; |
| 636 | } |
| 637 | |
| 638 | state.light_uniforms[index].flags = l->blend_mode << LIGHT_FLAGS_BLEND_SHIFT; |
| 639 | state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT; |
| 640 | if (clight->shadow.enabled) { |
| 641 | state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW; |
| 642 | } |
| 643 | |
| 644 | if (clight->texture.is_valid()) { |
| 645 | Rect2 atlas_rect = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture_rect(clight->texture); |
| 646 | state.light_uniforms[index].atlas_rect[0] = atlas_rect.position.x; |
| 647 | state.light_uniforms[index].atlas_rect[1] = atlas_rect.position.y; |
| 648 | state.light_uniforms[index].atlas_rect[2] = atlas_rect.size.width; |
| 649 | state.light_uniforms[index].atlas_rect[3] = atlas_rect.size.height; |
| 650 | |
| 651 | } else { |
| 652 | state.light_uniforms[index].atlas_rect[0] = 0; |
| 653 | state.light_uniforms[index].atlas_rect[1] = 0; |
| 654 | state.light_uniforms[index].atlas_rect[2] = 0; |
| 655 | state.light_uniforms[index].atlas_rect[3] = 0; |
| 656 | } |
| 657 | |
| 658 | l->render_index_cache = index; |
| 659 | |
| 660 | index++; |
| 661 | l = l->next_ptr; |
| 662 | } |
| 663 | |
| 664 | light_count = index; |
| 665 | } |
| 666 | |
| 667 | if (light_count > 0) { |
| 668 | RD::get_singleton()->buffer_update(state.lights_storage_buffer, 0, sizeof(LightUniform) * light_count, &state.light_uniforms[0]); |
| 669 | } |
| 670 | |
| 671 | bool use_linear_colors = texture_storage->render_target_is_using_hdr(p_to_render_target); |
| 672 | |
| 673 | { |
| 674 | //update canvas state uniform buffer |
| 675 | State::Buffer state_buffer; |
| 676 | |
| 677 | Size2i ssize = texture_storage->render_target_get_size(p_to_render_target); |
| 678 | |
| 679 | Transform3D screen_transform; |
| 680 | screen_transform.translate_local(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f); |
| 681 | screen_transform.scale(Vector3(2.0f / ssize.width, 2.0f / ssize.height, 1.0f)); |
| 682 | _update_transform_to_mat4(screen_transform, state_buffer.screen_transform); |
| 683 | _update_transform_2d_to_mat4(p_canvas_transform, state_buffer.canvas_transform); |
| 684 | |
| 685 | Transform2D normal_transform = p_canvas_transform; |
| 686 | normal_transform.columns[0].normalize(); |
| 687 | normal_transform.columns[1].normalize(); |
| 688 | normal_transform.columns[2] = Vector2(); |
| 689 | _update_transform_2d_to_mat4(normal_transform, state_buffer.canvas_normal_transform); |
| 690 | |
| 691 | Color modulate = p_modulate; |
| 692 | if (use_linear_colors) { |
| 693 | modulate = p_modulate.srgb_to_linear(); |
| 694 | } |
| 695 | state_buffer.canvas_modulate[0] = modulate.r; |
| 696 | state_buffer.canvas_modulate[1] = modulate.g; |
| 697 | state_buffer.canvas_modulate[2] = modulate.b; |
| 698 | state_buffer.canvas_modulate[3] = modulate.a; |
| 699 | |
| 700 | Size2 render_target_size = texture_storage->render_target_get_size(p_to_render_target); |
| 701 | state_buffer.screen_pixel_size[0] = 1.0 / render_target_size.x; |
| 702 | state_buffer.screen_pixel_size[1] = 1.0 / render_target_size.y; |
| 703 | |
| 704 | state_buffer.time = state.time; |
| 705 | state_buffer.use_pixel_snap = p_snap_2d_vertices_to_pixel; |
| 706 | |
| 707 | state_buffer.directional_light_count = directional_light_count; |
| 708 | |
| 709 | Vector2 canvas_scale = p_canvas_transform.get_scale(); |
| 710 | |
| 711 | state_buffer.sdf_to_screen[0] = render_target_size.width / canvas_scale.x; |
| 712 | state_buffer.sdf_to_screen[1] = render_target_size.height / canvas_scale.y; |
| 713 | |
| 714 | state_buffer.screen_to_sdf[0] = 1.0 / state_buffer.sdf_to_screen[0]; |
| 715 | state_buffer.screen_to_sdf[1] = 1.0 / state_buffer.sdf_to_screen[1]; |
| 716 | |
| 717 | Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_to_render_target); |
| 718 | Rect2 sdf_tex_rect(sdf_rect.position / canvas_scale, sdf_rect.size / canvas_scale); |
| 719 | |
| 720 | state_buffer.sdf_to_tex[0] = 1.0 / sdf_tex_rect.size.width; |
| 721 | state_buffer.sdf_to_tex[1] = 1.0 / sdf_tex_rect.size.height; |
| 722 | state_buffer.sdf_to_tex[2] = -sdf_tex_rect.position.x / sdf_tex_rect.size.width; |
| 723 | state_buffer.sdf_to_tex[3] = -sdf_tex_rect.position.y / sdf_tex_rect.size.height; |
| 724 | |
| 725 | //print_line("w: " + itos(ssize.width) + " s: " + rtos(canvas_scale)); |
| 726 | state_buffer.tex_to_sdf = 1.0 / ((canvas_scale.x + canvas_scale.y) * 0.5); |
| 727 | state_buffer.shadow_pixel_size = 1.0f / (float)(state.shadow_texture_size); |
| 728 | |
| 729 | state_buffer.flags = use_linear_colors ? CANVAS_FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR : 0; |
| 730 | |
| 731 | RD::get_singleton()->buffer_update(state.canvas_state_buffer, 0, sizeof(State::Buffer), &state_buffer); |
| 732 | } |
| 733 | |
| 734 | { //default filter/repeat |
| 735 | default_filter = p_default_filter; |
| 736 | default_repeat = p_default_repeat; |
| 737 | } |
| 738 | |
| 739 | Item *ci = p_item_list; |
| 740 | |
| 741 | //fill the list until rendering is possible. |
| 742 | bool material_screen_texture_cached = false; |
| 743 | bool material_screen_texture_mipmaps_cached = false; |
| 744 | |
| 745 | Rect2 back_buffer_rect; |
| 746 | bool backbuffer_copy = false; |
| 747 | bool backbuffer_gen_mipmaps = false; |
| 748 | |
| 749 | Item *canvas_group_owner = nullptr; |
| 750 | bool skip_item = false; |
| 751 | |
| 752 | bool update_skeletons = false; |
| 753 | bool time_used = false; |
| 754 | |
| 755 | bool backbuffer_cleared = false; |
| 756 | |
| 757 | RenderTarget to_render_target; |
| 758 | to_render_target.render_target = p_to_render_target; |
| 759 | to_render_target.use_linear_colors = use_linear_colors; |
| 760 | |
| 761 | while (ci) { |
| 762 | if (ci->copy_back_buffer && canvas_group_owner == nullptr) { |
| 763 | backbuffer_copy = true; |
| 764 | |
| 765 | if (ci->copy_back_buffer->full) { |
| 766 | back_buffer_rect = Rect2(); |
| 767 | } else { |
| 768 | back_buffer_rect = ci->copy_back_buffer->rect; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material; |
| 773 | |
| 774 | if (material.is_valid()) { |
| 775 | CanvasMaterialData *md = static_cast<CanvasMaterialData *>(material_storage->material_get_data(material, RendererRD::MaterialStorage::SHADER_TYPE_2D)); |
| 776 | if (md && md->shader_data->is_valid()) { |
| 777 | if (md->shader_data->uses_screen_texture && canvas_group_owner == nullptr) { |
| 778 | if (!material_screen_texture_cached) { |
| 779 | backbuffer_copy = true; |
| 780 | back_buffer_rect = Rect2(); |
| 781 | backbuffer_gen_mipmaps = md->shader_data->uses_screen_texture_mipmaps; |
| 782 | } else if (!material_screen_texture_mipmaps_cached) { |
| 783 | backbuffer_gen_mipmaps = md->shader_data->uses_screen_texture_mipmaps; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (md->shader_data->uses_sdf) { |
| 788 | r_sdf_used = true; |
| 789 | } |
| 790 | if (md->shader_data->uses_time) { |
| 791 | time_used = true; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | if (ci->skeleton.is_valid()) { |
| 797 | const Item::Command *c = ci->commands; |
| 798 | |
| 799 | while (c) { |
| 800 | if (c->type == Item::Command::TYPE_MESH) { |
| 801 | const Item::CommandMesh *cm = static_cast<const Item::CommandMesh *>(c); |
| 802 | if (cm->mesh_instance.is_valid()) { |
| 803 | mesh_storage->mesh_instance_check_for_update(cm->mesh_instance); |
| 804 | mesh_storage->mesh_instance_set_canvas_item_transform(cm->mesh_instance, canvas_transform_inverse * ci->final_transform); |
| 805 | update_skeletons = true; |
| 806 | } |
| 807 | } |
| 808 | c = c->next; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | if (ci->canvas_group_owner != nullptr) { |
| 813 | if (canvas_group_owner == nullptr) { |
| 814 | // Canvas group begins here, render until before this item |
| 815 | if (update_skeletons) { |
| 816 | mesh_storage->update_mesh_instances(); |
| 817 | update_skeletons = false; |
| 818 | } |
| 819 | _render_batch_items(to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, false, r_render_info); |
| 820 | item_count = 0; |
| 821 | |
| 822 | if (ci->canvas_group_owner->canvas_group->mode != RS::CANVAS_GROUP_MODE_TRANSPARENT) { |
| 823 | Rect2i group_rect = ci->canvas_group_owner->global_rect_cache; |
| 824 | texture_storage->render_target_copy_to_back_buffer(p_to_render_target, group_rect, false); |
| 825 | if (ci->canvas_group_owner->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) { |
| 826 | ci->canvas_group_owner->use_canvas_group = false; |
| 827 | items[item_count++] = ci->canvas_group_owner; |
| 828 | } |
| 829 | } else if (!backbuffer_cleared) { |
| 830 | texture_storage->render_target_clear_back_buffer(p_to_render_target, Rect2i(), Color(0, 0, 0, 0)); |
| 831 | backbuffer_cleared = true; |
| 832 | } |
| 833 | |
| 834 | backbuffer_copy = false; |
| 835 | canvas_group_owner = ci->canvas_group_owner; //continue until owner found |
| 836 | } |
| 837 | |
| 838 | ci->canvas_group_owner = nullptr; //must be cleared |
| 839 | } |
| 840 | |
| 841 | if (canvas_group_owner == nullptr && ci->canvas_group != nullptr && ci->canvas_group->mode != RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) { |
| 842 | skip_item = true; |
| 843 | } |
| 844 | |
| 845 | if (ci == canvas_group_owner) { |
| 846 | if (update_skeletons) { |
| 847 | mesh_storage->update_mesh_instances(); |
| 848 | update_skeletons = false; |
| 849 | } |
| 850 | |
| 851 | _render_batch_items(to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, true, r_render_info); |
| 852 | item_count = 0; |
| 853 | |
| 854 | if (ci->canvas_group->blur_mipmaps) { |
| 855 | texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, ci->global_rect_cache); |
| 856 | } |
| 857 | |
| 858 | canvas_group_owner = nullptr; |
| 859 | // Backbuffer is dirty now and needs to be re-cleared if another CanvasGroup needs it. |
| 860 | backbuffer_cleared = false; |
| 861 | |
| 862 | // Tell the renderer to paint this as a canvas group |
| 863 | ci->use_canvas_group = true; |
| 864 | } else { |
| 865 | ci->use_canvas_group = false; |
| 866 | } |
| 867 | |
| 868 | if (backbuffer_copy) { |
| 869 | //render anything pending, including clearing if no items |
| 870 | if (update_skeletons) { |
| 871 | mesh_storage->update_mesh_instances(); |
| 872 | update_skeletons = false; |
| 873 | } |
| 874 | |
| 875 | _render_batch_items(to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, false, r_render_info); |
| 876 | item_count = 0; |
| 877 | |
| 878 | texture_storage->render_target_copy_to_back_buffer(p_to_render_target, back_buffer_rect, backbuffer_gen_mipmaps); |
| 879 | |
| 880 | backbuffer_copy = false; |
| 881 | material_screen_texture_cached = true; // After a backbuffer copy, screen texture makes no further copies. |
| 882 | material_screen_texture_mipmaps_cached = backbuffer_gen_mipmaps; |
| 883 | backbuffer_gen_mipmaps = false; |
| 884 | } |
| 885 | |
| 886 | if (backbuffer_gen_mipmaps) { |
| 887 | texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, back_buffer_rect); |
| 888 | |
| 889 | backbuffer_gen_mipmaps = false; |
| 890 | material_screen_texture_mipmaps_cached = true; |
| 891 | } |
| 892 | |
| 893 | if (skip_item) { |
| 894 | skip_item = false; |
| 895 | } else { |
| 896 | items[item_count++] = ci; |
| 897 | } |
| 898 | |
| 899 | if (!ci->next || item_count == MAX_RENDER_ITEMS - 1) { |
| 900 | if (update_skeletons) { |
| 901 | mesh_storage->update_mesh_instances(); |
| 902 | update_skeletons = false; |
| 903 | } |
| 904 | |
| 905 | _render_batch_items(to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, canvas_group_owner != nullptr, r_render_info); |
| 906 | //then reset |
| 907 | item_count = 0; |
| 908 | } |
| 909 | |
| 910 | ci = ci->next; |
| 911 | } |
| 912 | |
| 913 | if (time_used) { |
| 914 | RenderingServerDefault::redraw_request(); |
| 915 | } |
| 916 | |
| 917 | texture_info_map.clear(); |
| 918 | |
| 919 | // Save the previous instance data pointer in case more items are rendered in the same frame. |
| 920 | state.prev_instance_data = state.instance_data; |
| 921 | state.prev_instance_data_index = state.instance_data_index; |
| 922 | |
| 923 | state.instance_data = nullptr; |
| 924 | if (state.instance_data_index > 0) { |
| 925 | // If there was any remaining instance data, it must be flushed. |
| 926 | RID buf = state.instance_buffers._get(0); |
| 927 | RD::get_singleton()->buffer_flush(buf); |
| 928 | state.instance_data_index = 0; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | RID RendererCanvasRenderRD::light_create() { |
| 933 | CanvasLight canvas_light; |
| 934 | return canvas_light_owner.make_rid(canvas_light); |
| 935 | } |
| 936 | |
| 937 | void RendererCanvasRenderRD::light_set_texture(RID p_rid, RID p_texture) { |
| 938 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 939 | |
| 940 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 941 | ERR_FAIL_NULL(cl); |
| 942 | if (cl->texture == p_texture) { |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | ERR_FAIL_COND(p_texture.is_valid() && !texture_storage->owns_texture(p_texture)); |
| 947 | |
| 948 | if (cl->texture.is_valid()) { |
| 949 | texture_storage->texture_remove_from_decal_atlas(cl->texture); |
| 950 | } |
| 951 | cl->texture = p_texture; |
| 952 | |
| 953 | if (cl->texture.is_valid()) { |
| 954 | texture_storage->texture_add_to_decal_atlas(cl->texture); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | void RendererCanvasRenderRD::light_set_use_shadow(RID p_rid, bool p_enable) { |
| 959 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 960 | ERR_FAIL_NULL(cl); |
| 961 | |
| 962 | cl->shadow.enabled = p_enable; |
| 963 | } |
| 964 | |
| 965 | void RendererCanvasRenderRD::_update_shadow_atlas() { |
| 966 | if (state.shadow_fb == RID()) { |
| 967 | //ah, we lack the shadow texture.. |
| 968 | RD::get_singleton()->free_rid(state.shadow_texture); //erase placeholder |
| 969 | |
| 970 | Vector<RID> fb_textures; |
| 971 | |
| 972 | { //texture |
| 973 | RD::TextureFormat tf; |
| 974 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 975 | tf.width = state.shadow_texture_size; |
| 976 | tf.height = MAX_LIGHTS_PER_RENDER * 2; |
| 977 | tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT; |
| 978 | tf.format = RD::DATA_FORMAT_R32_SFLOAT; |
| 979 | |
| 980 | state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 981 | fb_textures.push_back(state.shadow_texture); |
| 982 | } |
| 983 | { |
| 984 | RD::TextureFormat tf; |
| 985 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 986 | tf.width = state.shadow_texture_size; |
| 987 | tf.height = MAX_LIGHTS_PER_RENDER * 2; |
| 988 | tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 989 | tf.format = RD::DATA_FORMAT_D32_SFLOAT; |
| 990 | tf.is_discardable = true; |
| 991 | //chunks to write |
| 992 | state.shadow_depth_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 993 | fb_textures.push_back(state.shadow_depth_texture); |
| 994 | } |
| 995 | |
| 996 | state.shadow_fb = RD::get_singleton()->framebuffer_create(fb_textures); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | void RendererCanvasRenderRD::_update_occluder_buffer(uint32_t p_size) { |
| 1001 | bool needs_update = state.shadow_occluder_buffer.is_null(); |
| 1002 | |
| 1003 | if (p_size > state.shadow_occluder_buffer_size) { |
| 1004 | needs_update = true; |
| 1005 | state.shadow_occluder_buffer_size = next_power_of_2(p_size); |
| 1006 | if (state.shadow_occluder_buffer.is_valid()) { |
| 1007 | RD::get_singleton()->free_rid(state.shadow_occluder_buffer); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | if (needs_update) { |
| 1012 | state.shadow_occluder_buffer = RD::get_singleton()->storage_buffer_create(state.shadow_occluder_buffer_size); |
| 1013 | |
| 1014 | Vector<RD::Uniform> uniforms; |
| 1015 | |
| 1016 | { |
| 1017 | RD::Uniform u; |
| 1018 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1019 | u.binding = 0; |
| 1020 | u.append_id(state.shadow_occluder_buffer); |
| 1021 | uniforms.push_back(u); |
| 1022 | } |
| 1023 | state.shadow_ocluder_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shadow_render.shader.version_get_shader(shadow_render.shader_version, SHADOW_RENDER_MODE_POSITIONAL_SHADOW), 0); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | void RendererCanvasRenderRD::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) { |
| 1028 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 1029 | ERR_FAIL_COND(!cl->shadow.enabled); |
| 1030 | |
| 1031 | _update_shadow_atlas(); |
| 1032 | |
| 1033 | cl->shadow.z_far = p_far; |
| 1034 | cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(MAX_LIGHTS_PER_RENDER * 2); |
| 1035 | Color cc = Color(p_far, p_far, p_far, 1.0); |
| 1036 | |
| 1037 | // First, do a culling pass and record what occluders need to be drawn for this light. |
| 1038 | static thread_local LocalVector<OccluderPolygon *> occluders; |
| 1039 | static thread_local LocalVector<uint32_t> occluder_indices; |
| 1040 | occluders.clear(); |
| 1041 | occluder_indices.clear(); |
| 1042 | |
| 1043 | uint32_t occluder_count = 0; |
| 1044 | |
| 1045 | LightOccluderInstance *instance = p_occluders; |
| 1046 | while (instance) { |
| 1047 | OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder); |
| 1048 | |
| 1049 | occluder_count++; |
| 1050 | |
| 1051 | if (!co || co->index_array.is_null()) { |
| 1052 | instance = instance->next; |
| 1053 | continue; |
| 1054 | } |
| 1055 | |
| 1056 | if (!(p_light_mask & instance->light_mask) || !p_light_rect.intersects_transformed(instance->xform_cache, instance->aabb_cache)) { |
| 1057 | instance = instance->next; |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | occluders.push_back(co); |
| 1062 | occluder_indices.push_back(occluder_count - 1); |
| 1063 | |
| 1064 | instance = instance->next; |
| 1065 | } |
| 1066 | |
| 1067 | // Then, upload all the occluder transforms to a shared buffer. |
| 1068 | // We only do this for the first light so we can avoid uploading the same |
| 1069 | // Transforms over and over again. |
| 1070 | if (p_shadow_index == 0 && occluder_count > 0) { |
| 1071 | static thread_local LocalVector<float> transforms; |
| 1072 | transforms.clear(); |
| 1073 | transforms.resize(occluder_count * 8); |
| 1074 | |
| 1075 | instance = p_occluders; |
| 1076 | uint32_t index = 0; |
| 1077 | while (instance) { |
| 1078 | _update_transform_2d_to_mat2x4(instance->xform_cache, &transforms[index * 8]); |
| 1079 | index++; |
| 1080 | instance = instance->next; |
| 1081 | } |
| 1082 | |
| 1083 | _update_occluder_buffer(occluder_count * 8 * sizeof(float)); |
| 1084 | RD::get_singleton()->buffer_update(state.shadow_occluder_buffer, 0, transforms.size() * sizeof(float), transforms.ptr()); |
| 1085 | } |
| 1086 | |
| 1087 | Rect2i rect(0, p_shadow_index * 2, state.shadow_texture_size, 2); |
| 1088 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(state.shadow_fb, RD::DRAW_CLEAR_ALL, VectorView(&cc, 1), 1.0f, 0, rect); |
| 1089 | |
| 1090 | if (state.shadow_occluder_buffer.is_valid()) { |
| 1091 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, shadow_render.render_pipelines[SHADOW_RENDER_MODE_POSITIONAL_SHADOW]); |
| 1092 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, state.shadow_ocluder_uniform_set, 0); |
| 1093 | |
| 1094 | for (int i = 0; i < 4; i++) { |
| 1095 | Rect2i sub_rect((state.shadow_texture_size / 4) * i, p_shadow_index * 2, (state.shadow_texture_size / 4), 2); |
| 1096 | RD::get_singleton()->draw_list_set_viewport(draw_list, sub_rect); |
| 1097 | |
| 1098 | static const Vector2 directions[4] = { Vector2(1, 0), Vector2(0, 1), Vector2(-1, 0), Vector2(0, -1) }; |
| 1099 | static const Vector4 rotations[4] = { Vector4(0, -1, 1, 0), Vector4(-1, 0, 0, -1), Vector4(0, 1, -1, 0), Vector4(1, 0, 0, 1) }; |
| 1100 | |
| 1101 | PositionalShadowRenderPushConstant push_constant; |
| 1102 | _update_transform_2d_to_mat2x4(p_light_xform, push_constant.modelview); |
| 1103 | push_constant.direction[0] = directions[i].x; |
| 1104 | push_constant.direction[1] = directions[i].y; |
| 1105 | push_constant.rotation[0] = rotations[i].x; |
| 1106 | push_constant.rotation[1] = rotations[i].y; |
| 1107 | push_constant.rotation[2] = rotations[i].z; |
| 1108 | push_constant.rotation[3] = rotations[i].w; |
| 1109 | push_constant.z_far = p_far; |
| 1110 | push_constant.z_near = p_near; |
| 1111 | |
| 1112 | for (uint32_t j = 0; j < occluders.size(); j++) { |
| 1113 | OccluderPolygon *co = occluders[j]; |
| 1114 | |
| 1115 | push_constant.pad = occluder_indices[j]; |
| 1116 | push_constant.cull_mode = uint32_t(co->cull_mode); |
| 1117 | |
| 1118 | // The slowest part about this whole function is that we have to draw the occluders one by one, 4 times. |
| 1119 | // We can optimize this so that all occluders draw at once if we store vertices and indices in a giant |
| 1120 | // SSBO and just save an index into that SSBO for each occluder. |
| 1121 | RD::get_singleton()->draw_list_bind_vertex_array(draw_list, co->vertex_array); |
| 1122 | RD::get_singleton()->draw_list_bind_index_array(draw_list, co->index_array); |
| 1123 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(PositionalShadowRenderPushConstant)); |
| 1124 | |
| 1125 | RD::get_singleton()->draw_list_draw(draw_list, true); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | RD::get_singleton()->draw_list_end(); |
| 1130 | } |
| 1131 | |
| 1132 | void RendererCanvasRenderRD::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) { |
| 1133 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 1134 | ERR_FAIL_COND(!cl->shadow.enabled); |
| 1135 | |
| 1136 | _update_shadow_atlas(); |
| 1137 | |
| 1138 | Vector2 light_dir = p_light_xform.columns[1].normalized(); |
| 1139 | |
| 1140 | Vector2 center = p_clip_rect.get_center(); |
| 1141 | |
| 1142 | float to_edge_distance = Math::abs(light_dir.dot(p_clip_rect.get_support(-light_dir)) - light_dir.dot(center)); |
| 1143 | |
| 1144 | Vector2 from_pos = center - light_dir * (to_edge_distance + p_cull_distance); |
| 1145 | float distance = to_edge_distance * 2.0 + p_cull_distance; |
| 1146 | float half_size = p_clip_rect.size.length() * 0.5; //shadow length, must keep this no matter the angle |
| 1147 | |
| 1148 | cl->shadow.z_far = distance; |
| 1149 | cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(MAX_LIGHTS_PER_RENDER * 2); |
| 1150 | |
| 1151 | Transform2D to_light_xform; |
| 1152 | |
| 1153 | to_light_xform[2] = from_pos; |
| 1154 | to_light_xform[1] = light_dir; |
| 1155 | to_light_xform[0] = -light_dir.orthogonal(); |
| 1156 | |
| 1157 | to_light_xform.invert(); |
| 1158 | |
| 1159 | Vector<Color> cc; |
| 1160 | cc.push_back(Color(1, 1, 1, 1)); |
| 1161 | |
| 1162 | Rect2i rect(0, p_shadow_index * 2, state.shadow_texture_size, 2); |
| 1163 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(state.shadow_fb, RD::DRAW_CLEAR_ALL, cc, 1.0f, 0, rect); |
| 1164 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, shadow_render.render_pipelines[SHADOW_RENDER_MODE_DIRECTIONAL_SHADOW]); |
| 1165 | |
| 1166 | Projection projection; |
| 1167 | projection.set_orthogonal(-half_size, half_size, -0.5, 0.5, 0.0, distance); |
| 1168 | projection = projection * Projection(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, -1)).affine_inverse()); |
| 1169 | |
| 1170 | ShadowRenderPushConstant push_constant; |
| 1171 | for (int y = 0; y < 4; y++) { |
| 1172 | for (int x = 0; x < 4; x++) { |
| 1173 | push_constant.projection[y * 4 + x] = projection.columns[y][x]; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | push_constant.direction[0] = 0.0; |
| 1178 | push_constant.direction[1] = 1.0; |
| 1179 | push_constant.z_far = distance; |
| 1180 | |
| 1181 | LightOccluderInstance *instance = p_occluders; |
| 1182 | |
| 1183 | while (instance) { |
| 1184 | OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder); |
| 1185 | |
| 1186 | if (!co || co->index_array.is_null() || !(p_light_mask & instance->light_mask)) { |
| 1187 | instance = instance->next; |
| 1188 | continue; |
| 1189 | } |
| 1190 | |
| 1191 | _update_transform_2d_to_mat2x4(to_light_xform * instance->xform_cache, push_constant.modelview); |
| 1192 | push_constant.cull_mode = uint32_t(co->cull_mode); |
| 1193 | |
| 1194 | RD::get_singleton()->draw_list_bind_vertex_array(draw_list, co->vertex_array); |
| 1195 | RD::get_singleton()->draw_list_bind_index_array(draw_list, co->index_array); |
| 1196 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowRenderPushConstant)); |
| 1197 | |
| 1198 | RD::get_singleton()->draw_list_draw(draw_list, true); |
| 1199 | |
| 1200 | instance = instance->next; |
| 1201 | } |
| 1202 | |
| 1203 | RD::get_singleton()->draw_list_end(); |
| 1204 | |
| 1205 | Transform2D to_shadow; |
| 1206 | to_shadow.columns[0].x = 1.0 / -(half_size * 2.0); |
| 1207 | to_shadow.columns[2].x = 0.5; |
| 1208 | |
| 1209 | cl->shadow.directional_xform = to_shadow * to_light_xform; |
| 1210 | } |
| 1211 | |
| 1212 | void RendererCanvasRenderRD::render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) { |
| 1213 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 1214 | |
| 1215 | RID fb = texture_storage->render_target_get_sdf_framebuffer(p_render_target); |
| 1216 | Rect2i rect = texture_storage->render_target_get_sdf_rect(p_render_target); |
| 1217 | |
| 1218 | Transform2D to_sdf; |
| 1219 | to_sdf.columns[0] *= rect.size.width; |
| 1220 | to_sdf.columns[1] *= rect.size.height; |
| 1221 | to_sdf.columns[2] = rect.position; |
| 1222 | |
| 1223 | Transform2D to_clip; |
| 1224 | to_clip.columns[0] *= 2.0; |
| 1225 | to_clip.columns[1] *= 2.0; |
| 1226 | to_clip.columns[2] = -Vector2(1.0, 1.0); |
| 1227 | |
| 1228 | to_clip = to_clip * to_sdf.affine_inverse(); |
| 1229 | |
| 1230 | Vector<Color> cc; |
| 1231 | cc.push_back(Color(0, 0, 0, 0)); |
| 1232 | |
| 1233 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(fb, RD::DRAW_CLEAR_ALL, cc); |
| 1234 | |
| 1235 | Projection projection; |
| 1236 | |
| 1237 | ShadowRenderPushConstant push_constant; |
| 1238 | for (int y = 0; y < 4; y++) { |
| 1239 | for (int x = 0; x < 4; x++) { |
| 1240 | push_constant.projection[y * 4 + x] = projection.columns[y][x]; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | push_constant.direction[0] = 0.0; |
| 1245 | push_constant.direction[1] = 0.0; |
| 1246 | push_constant.z_far = 0; |
| 1247 | push_constant.cull_mode = 0; |
| 1248 | |
| 1249 | LightOccluderInstance *instance = p_occluders; |
| 1250 | |
| 1251 | while (instance) { |
| 1252 | OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder); |
| 1253 | |
| 1254 | if (!co || co->sdf_index_array.is_null() || !instance->sdf_collision) { |
| 1255 | instance = instance->next; |
| 1256 | continue; |
| 1257 | } |
| 1258 | |
| 1259 | _update_transform_2d_to_mat2x4(to_clip * instance->xform_cache, push_constant.modelview); |
| 1260 | |
| 1261 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, shadow_render.sdf_render_pipelines[co->sdf_is_lines ? SHADOW_RENDER_SDF_LINES : SHADOW_RENDER_SDF_TRIANGLES]); |
| 1262 | RD::get_singleton()->draw_list_bind_vertex_array(draw_list, co->sdf_vertex_array); |
| 1263 | RD::get_singleton()->draw_list_bind_index_array(draw_list, co->sdf_index_array); |
| 1264 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowRenderPushConstant)); |
| 1265 | |
| 1266 | RD::get_singleton()->draw_list_draw(draw_list, true); |
| 1267 | |
| 1268 | instance = instance->next; |
| 1269 | } |
| 1270 | |
| 1271 | RD::get_singleton()->draw_list_end(); |
| 1272 | |
| 1273 | texture_storage->render_target_sdf_process(p_render_target); //done rendering, process it |
| 1274 | } |
| 1275 | |
| 1276 | RID RendererCanvasRenderRD::occluder_polygon_create() { |
| 1277 | OccluderPolygon occluder; |
| 1278 | occluder.line_point_count = 0; |
| 1279 | occluder.sdf_point_count = 0; |
| 1280 | occluder.sdf_index_count = 0; |
| 1281 | occluder.cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED; |
| 1282 | return occluder_polygon_owner.make_rid(occluder); |
| 1283 | } |
| 1284 | |
| 1285 | void RendererCanvasRenderRD::occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) { |
| 1286 | OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder); |
| 1287 | ERR_FAIL_NULL(oc); |
| 1288 | |
| 1289 | Vector<Vector2> lines; |
| 1290 | |
| 1291 | if (p_points.size()) { |
| 1292 | int lc = p_points.size() * 2; |
| 1293 | |
| 1294 | lines.resize(lc - (p_closed ? 0 : 2)); |
| 1295 | { |
| 1296 | Vector2 *w = lines.ptrw(); |
| 1297 | const Vector2 *r = p_points.ptr(); |
| 1298 | |
| 1299 | int max = lc / 2; |
| 1300 | if (!p_closed) { |
| 1301 | max--; |
| 1302 | } |
| 1303 | for (int i = 0; i < max; i++) { |
| 1304 | Vector2 a = r[i]; |
| 1305 | Vector2 b = r[(i + 1) % (lc / 2)]; |
| 1306 | w[i * 2 + 0] = a; |
| 1307 | w[i * 2 + 1] = b; |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | if ((oc->line_point_count != lines.size() || lines.is_empty()) && oc->vertex_array.is_valid()) { |
| 1313 | RD::get_singleton()->free_rid(oc->vertex_array); |
| 1314 | RD::get_singleton()->free_rid(oc->vertex_buffer); |
| 1315 | RD::get_singleton()->free_rid(oc->index_array); |
| 1316 | RD::get_singleton()->free_rid(oc->index_buffer); |
| 1317 | |
| 1318 | oc->vertex_array = RID(); |
| 1319 | oc->vertex_buffer = RID(); |
| 1320 | oc->index_array = RID(); |
| 1321 | oc->index_buffer = RID(); |
| 1322 | |
| 1323 | oc->line_point_count = lines.size(); |
| 1324 | } |
| 1325 | |
| 1326 | if (lines.size()) { |
| 1327 | oc->line_point_count = lines.size(); |
| 1328 | Vector<uint8_t> geometry; |
| 1329 | Vector<uint8_t> indices; |
| 1330 | int lc = lines.size(); |
| 1331 | |
| 1332 | geometry.resize(lc * 6 * sizeof(float)); |
| 1333 | indices.resize(lc * 3 * sizeof(uint16_t)); |
| 1334 | |
| 1335 | { |
| 1336 | uint8_t *vw = geometry.ptrw(); |
| 1337 | float *vwptr = reinterpret_cast<float *>(vw); |
| 1338 | uint8_t *iw = indices.ptrw(); |
| 1339 | uint16_t *iwptr = (uint16_t *)iw; |
| 1340 | |
| 1341 | const Vector2 *lr = lines.ptr(); |
| 1342 | |
| 1343 | const int POLY_HEIGHT = 16384; |
| 1344 | |
| 1345 | for (int i = 0; i < lc / 2; i++) { |
| 1346 | vwptr[i * 12 + 0] = lr[i * 2 + 0].x; |
| 1347 | vwptr[i * 12 + 1] = lr[i * 2 + 0].y; |
| 1348 | vwptr[i * 12 + 2] = POLY_HEIGHT; |
| 1349 | |
| 1350 | vwptr[i * 12 + 3] = lr[i * 2 + 1].x; |
| 1351 | vwptr[i * 12 + 4] = lr[i * 2 + 1].y; |
| 1352 | vwptr[i * 12 + 5] = POLY_HEIGHT; |
| 1353 | |
| 1354 | vwptr[i * 12 + 6] = lr[i * 2 + 1].x; |
| 1355 | vwptr[i * 12 + 7] = lr[i * 2 + 1].y; |
| 1356 | vwptr[i * 12 + 8] = -POLY_HEIGHT; |
| 1357 | |
| 1358 | vwptr[i * 12 + 9] = lr[i * 2 + 0].x; |
| 1359 | vwptr[i * 12 + 10] = lr[i * 2 + 0].y; |
| 1360 | vwptr[i * 12 + 11] = -POLY_HEIGHT; |
| 1361 | |
| 1362 | iwptr[i * 6 + 0] = i * 4 + 0; |
| 1363 | iwptr[i * 6 + 1] = i * 4 + 1; |
| 1364 | iwptr[i * 6 + 2] = i * 4 + 2; |
| 1365 | |
| 1366 | iwptr[i * 6 + 3] = i * 4 + 2; |
| 1367 | iwptr[i * 6 + 4] = i * 4 + 3; |
| 1368 | iwptr[i * 6 + 5] = i * 4 + 0; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | //if same buffer len is being set, just use buffer_update to avoid a pipeline flush |
| 1373 | |
| 1374 | if (oc->vertex_array.is_null()) { |
| 1375 | //create from scratch |
| 1376 | //vertices |
| 1377 | oc->vertex_buffer = RD::get_singleton()->vertex_buffer_create(lc * 6 * sizeof(float), geometry); |
| 1378 | |
| 1379 | Vector<RID> buffer; |
| 1380 | buffer.push_back(oc->vertex_buffer); |
| 1381 | oc->vertex_array = RD::get_singleton()->vertex_array_create(4 * lc / 2, shadow_render.vertex_format, buffer); |
| 1382 | //indices |
| 1383 | |
| 1384 | oc->index_buffer = RD::get_singleton()->index_buffer_create(3 * lc, RD::INDEX_BUFFER_FORMAT_UINT16, indices); |
| 1385 | oc->index_array = RD::get_singleton()->index_array_create(oc->index_buffer, 0, 3 * lc); |
| 1386 | |
| 1387 | } else { |
| 1388 | //update existing |
| 1389 | const uint8_t *vr = geometry.ptr(); |
| 1390 | RD::get_singleton()->buffer_update(oc->vertex_buffer, 0, geometry.size(), vr); |
| 1391 | const uint8_t *ir = indices.ptr(); |
| 1392 | RD::get_singleton()->buffer_update(oc->index_buffer, 0, indices.size(), ir); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | // sdf |
| 1397 | |
| 1398 | Vector<int> sdf_indices; |
| 1399 | |
| 1400 | if (p_points.size()) { |
| 1401 | if (p_closed) { |
| 1402 | sdf_indices = Geometry2D::triangulate_polygon(p_points); |
| 1403 | oc->sdf_is_lines = false; |
| 1404 | } else { |
| 1405 | int max = p_points.size(); |
| 1406 | sdf_indices.resize(max * 2); |
| 1407 | |
| 1408 | int *iw = sdf_indices.ptrw(); |
| 1409 | for (int i = 0; i < max; i++) { |
| 1410 | iw[i * 2 + 0] = i; |
| 1411 | iw[i * 2 + 1] = (i + 1) % max; |
| 1412 | } |
| 1413 | oc->sdf_is_lines = true; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | if (((oc->sdf_index_count != sdf_indices.size() && oc->sdf_point_count != p_points.size()) || p_points.is_empty()) && oc->sdf_vertex_array.is_valid()) { |
| 1418 | RD::get_singleton()->free_rid(oc->sdf_vertex_array); |
| 1419 | RD::get_singleton()->free_rid(oc->sdf_vertex_buffer); |
| 1420 | RD::get_singleton()->free_rid(oc->sdf_index_array); |
| 1421 | RD::get_singleton()->free_rid(oc->sdf_index_buffer); |
| 1422 | |
| 1423 | oc->sdf_vertex_array = RID(); |
| 1424 | oc->sdf_vertex_buffer = RID(); |
| 1425 | oc->sdf_index_array = RID(); |
| 1426 | oc->sdf_index_buffer = RID(); |
| 1427 | |
| 1428 | oc->sdf_index_count = sdf_indices.size(); |
| 1429 | oc->sdf_point_count = p_points.size(); |
| 1430 | |
| 1431 | oc->sdf_is_lines = false; |
| 1432 | } |
| 1433 | |
| 1434 | if (sdf_indices.size()) { |
| 1435 | if (oc->sdf_vertex_array.is_null()) { |
| 1436 | //create from scratch |
| 1437 | //vertices |
| 1438 | #ifdef REAL_T_IS_DOUBLE |
| 1439 | PackedFloat32Array float_points; |
| 1440 | float_points.resize(p_points.size() * 2); |
| 1441 | float *float_points_ptr = (float *)float_points.ptrw(); |
| 1442 | for (int i = 0; i < p_points.size(); i++) { |
| 1443 | float_points_ptr[i * 2] = p_points[i].x; |
| 1444 | float_points_ptr[i * 2 + 1] = p_points[i].y; |
| 1445 | } |
| 1446 | oc->sdf_vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_points.size() * 2 * sizeof(float), float_points.span().reinterpret<uint8_t>()); |
| 1447 | #else |
| 1448 | oc->sdf_vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_points.size() * 2 * sizeof(float), p_points.span().reinterpret<uint8_t>()); |
| 1449 | #endif |
| 1450 | oc->sdf_index_buffer = RD::get_singleton()->index_buffer_create(sdf_indices.size(), RD::INDEX_BUFFER_FORMAT_UINT32, sdf_indices.span().reinterpret<uint8_t>()); |
| 1451 | oc->sdf_index_array = RD::get_singleton()->index_array_create(oc->sdf_index_buffer, 0, sdf_indices.size()); |
| 1452 | |
| 1453 | Vector<RID> buffer; |
| 1454 | buffer.push_back(oc->sdf_vertex_buffer); |
| 1455 | oc->sdf_vertex_array = RD::get_singleton()->vertex_array_create(p_points.size(), shadow_render.sdf_vertex_format, buffer); |
| 1456 | //indices |
| 1457 | |
| 1458 | } else { |
| 1459 | //update existing |
| 1460 | #ifdef REAL_T_IS_DOUBLE |
| 1461 | PackedFloat32Array float_points; |
| 1462 | float_points.resize(p_points.size() * 2); |
| 1463 | float *float_points_ptr = (float *)float_points.ptrw(); |
| 1464 | for (int i = 0; i < p_points.size(); i++) { |
| 1465 | float_points_ptr[i * 2] = p_points[i].x; |
| 1466 | float_points_ptr[i * 2 + 1] = p_points[i].y; |
| 1467 | } |
| 1468 | RD::get_singleton()->buffer_update(oc->sdf_vertex_buffer, 0, sizeof(float) * 2 * p_points.size(), float_points.ptr()); |
| 1469 | #else |
| 1470 | RD::get_singleton()->buffer_update(oc->sdf_vertex_buffer, 0, sizeof(float) * 2 * p_points.size(), p_points.ptr()); |
| 1471 | #endif |
| 1472 | RD::get_singleton()->buffer_update(oc->sdf_index_buffer, 0, sdf_indices.size() * sizeof(int32_t), sdf_indices.ptr()); |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | void RendererCanvasRenderRD::occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) { |
| 1478 | OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder); |
| 1479 | ERR_FAIL_NULL(oc); |
| 1480 | oc->cull_mode = p_mode; |
| 1481 | } |
| 1482 | |
| 1483 | void RendererCanvasRenderRD::CanvasShaderData::_clear_vertex_input_mask_cache() { |
| 1484 | for (uint32_t i = 0; i < VERTEX_INPUT_MASKS_SIZE; i++) { |
| 1485 | vertex_input_masks[i].store(0); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | void RendererCanvasRenderRD::CanvasShaderData::_create_pipeline(PipelineKey p_pipeline_key) { |
| 1490 | #if PRINT_PIPELINE_COMPILATION_KEYS |
| 1491 | print_line( |
| 1492 | "HASH:", p_pipeline_key.hash(), |
| 1493 | "VERSION:", version, |
| 1494 | "VARIANT:", p_pipeline_key.variant, |
| 1495 | "FRAMEBUFFER:", p_pipeline_key.framebuffer_format_id, |
| 1496 | "VERTEX:", p_pipeline_key.vertex_format_id, |
| 1497 | "PRIMITIVE:", p_pipeline_key.render_primitive, |
| 1498 | "SPEC PACKED #0:", p_pipeline_key.shader_specialization.packed_0, |
| 1499 | "LCD:", p_pipeline_key.lcd_blend); |
| 1500 | #endif |
| 1501 | |
| 1502 | RendererRD::MaterialStorage::ShaderData::BlendMode blend_mode_rd = RendererRD::MaterialStorage::ShaderData::BlendMode(blend_mode); |
| 1503 | RD::PipelineColorBlendState blend_state; |
| 1504 | RD::PipelineColorBlendState::Attachment attachment; |
| 1505 | uint32_t dynamic_state_flags = 0; |
| 1506 | if (p_pipeline_key.lcd_blend) { |
| 1507 | attachment.enable_blend = true; |
| 1508 | attachment.alpha_blend_op = RD::BLEND_OP_ADD; |
| 1509 | attachment.color_blend_op = RD::BLEND_OP_ADD; |
| 1510 | attachment.src_color_blend_factor = RD::BLEND_FACTOR_CONSTANT_COLOR; |
| 1511 | attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_COLOR; |
| 1512 | attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
| 1513 | attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 1514 | dynamic_state_flags = RD::DYNAMIC_STATE_BLEND_CONSTANTS; |
| 1515 | } else { |
| 1516 | attachment = RendererRD::MaterialStorage::ShaderData::blend_mode_to_blend_attachment(blend_mode_rd); |
| 1517 | } |
| 1518 | |
| 1519 | blend_state.attachments.push_back(attachment); |
| 1520 | |
| 1521 | RD::PipelineMultisampleState multisample_state; |
| 1522 | multisample_state.sample_count = RD::get_singleton()->framebuffer_format_get_texture_samples(p_pipeline_key.framebuffer_format_id, 0); |
| 1523 | |
| 1524 | // Convert the specialization from the key to pipeline specialization constants. |
| 1525 | Vector<RD::PipelineSpecializationConstant> specialization_constants; |
| 1526 | RD::PipelineSpecializationConstant sc; |
| 1527 | sc.constant_id = 0; |
| 1528 | sc.int_value = p_pipeline_key.shader_specialization.packed_0; |
| 1529 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT; |
| 1530 | specialization_constants.push_back(sc); |
| 1531 | |
| 1532 | RID shader_rid = get_shader(p_pipeline_key.variant, p_pipeline_key.ubershader); |
| 1533 | ERR_FAIL_COND(shader_rid.is_null()); |
| 1534 | |
| 1535 | RID pipeline = RD::get_singleton()->render_pipeline_create(shader_rid, p_pipeline_key.framebuffer_format_id, p_pipeline_key.vertex_format_id, p_pipeline_key.render_primitive, RD::PipelineRasterizationState(), multisample_state, RD::PipelineDepthStencilState(), blend_state, dynamic_state_flags, 0, specialization_constants); |
| 1536 | ERR_FAIL_COND(pipeline.is_null()); |
| 1537 | |
| 1538 | pipeline_hash_map.add_compiled_pipeline(p_pipeline_key.hash(), pipeline); |
| 1539 | } |
| 1540 | |
| 1541 | void RendererCanvasRenderRD::CanvasShaderData::set_code(const String &p_code) { |
| 1542 | //compile |
| 1543 | |
| 1544 | code = p_code; |
| 1545 | ubo_size = 0; |
| 1546 | uniforms.clear(); |
| 1547 | uses_screen_texture = false; |
| 1548 | uses_screen_texture_mipmaps = false; |
| 1549 | uses_sdf = false; |
| 1550 | uses_time = false; |
| 1551 | _clear_vertex_input_mask_cache(); |
| 1552 | |
| 1553 | if (code.is_empty()) { |
| 1554 | return; //just invalid, but no error |
| 1555 | } |
| 1556 | |
| 1557 | ShaderCompiler::GeneratedCode gen_code; |
| 1558 | |
| 1559 | blend_mode = BLEND_MODE_MIX; |
| 1560 | |
| 1561 | ShaderCompiler::IdentifierActions actions; |
| 1562 | actions.entry_point_stages["vertex"] = ShaderCompiler::STAGE_VERTEX; |
| 1563 | actions.entry_point_stages["fragment"] = ShaderCompiler::STAGE_FRAGMENT; |
| 1564 | actions.entry_point_stages["light"] = ShaderCompiler::STAGE_FRAGMENT; |
| 1565 | |
| 1566 | actions.render_mode_values["blend_add"] = Pair<int *, int>(&blend_mode, BLEND_MODE_ADD); |
| 1567 | actions.render_mode_values["blend_mix"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MIX); |
| 1568 | actions.render_mode_values["blend_sub"] = Pair<int *, int>(&blend_mode, BLEND_MODE_SUB); |
| 1569 | actions.render_mode_values["blend_mul"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MUL); |
| 1570 | actions.render_mode_values["blend_premul_alpha"] = Pair<int *, int>(&blend_mode, BLEND_MODE_PREMULTIPLIED_ALPHA); |
| 1571 | actions.render_mode_values["blend_disabled"] = Pair<int *, int>(&blend_mode, BLEND_MODE_DISABLED); |
| 1572 | |
| 1573 | actions.usage_flag_pointers["texture_sdf"] = &uses_sdf; |
| 1574 | actions.usage_flag_pointers["TIME"] = &uses_time; |
| 1575 | |
| 1576 | actions.uniforms = &uniforms; |
| 1577 | |
| 1578 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 1579 | MutexLock lock(canvas_singleton->shader.mutex); |
| 1580 | |
| 1581 | Error err = canvas_singleton->shader.compiler.compile(RS::SHADER_CANVAS_ITEM, code, &actions, path, gen_code); |
| 1582 | if (err != OK) { |
| 1583 | if (version.is_valid()) { |
| 1584 | canvas_singleton->shader.canvas_shader.version_free(version); |
| 1585 | version = RID(); |
| 1586 | } |
| 1587 | ERR_FAIL_MSG("Shader compilation failed."); |
| 1588 | } |
| 1589 | |
| 1590 | uses_screen_texture_mipmaps = gen_code.uses_screen_texture_mipmaps; |
| 1591 | uses_screen_texture = gen_code.uses_screen_texture; |
| 1592 | |
| 1593 | pipeline_hash_map.clear_pipelines(); |
| 1594 | |
| 1595 | if (version.is_null()) { |
| 1596 | version = canvas_singleton->shader.canvas_shader.version_create(false); |
| 1597 | } |
| 1598 | |
| 1599 | #if 0 |
| 1600 | print_line("**compiling shader:"); |
| 1601 | print_line("**defines:\n"); |
| 1602 | for (int i = 0; i < gen_code.defines.size(); i++) { |
| 1603 | print_line(gen_code.defines[i]); |
| 1604 | } |
| 1605 | |
| 1606 | HashMap<String, String>::Iterator el = gen_code.code.begin(); |
| 1607 | while (el) { |
| 1608 | print_line("\n**code " + el->key + ":\n" + el->value); |
| 1609 | ++el; |
| 1610 | } |
| 1611 | |
| 1612 | print_line("\n**uniforms:\n" + gen_code.uniforms); |
| 1613 | print_line("\n**vertex_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX]); |
| 1614 | print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]); |
| 1615 | #endif |
| 1616 | canvas_singleton->shader.canvas_shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines); |
| 1617 | |
| 1618 | ubo_size = gen_code.uniform_total_size; |
| 1619 | ubo_offsets = gen_code.uniform_offsets; |
| 1620 | texture_uniforms = gen_code.texture_uniforms; |
| 1621 | } |
| 1622 | |
| 1623 | bool RendererCanvasRenderRD::CanvasShaderData::is_animated() const { |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
| 1627 | bool RendererCanvasRenderRD::CanvasShaderData::casts_shadows() const { |
| 1628 | return false; |
| 1629 | } |
| 1630 | |
| 1631 | RS::ShaderNativeSourceCode RendererCanvasRenderRD::CanvasShaderData::get_native_source_code() const { |
| 1632 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 1633 | MutexLock lock(canvas_singleton->shader.mutex); |
| 1634 | return canvas_singleton->shader.canvas_shader.version_get_native_source_code(version); |
| 1635 | } |
| 1636 | |
| 1637 | Pair<ShaderRD *, RID> RendererCanvasRenderRD::CanvasShaderData::get_native_shader_and_version() const { |
| 1638 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 1639 | return { &canvas_singleton->shader.canvas_shader, version }; |
| 1640 | } |
| 1641 | |
| 1642 | RID RendererCanvasRenderRD::CanvasShaderData::get_shader(ShaderVariant p_shader_variant, bool p_ubershader) const { |
| 1643 | if (version.is_valid()) { |
| 1644 | uint32_t variant_index = p_shader_variant + (p_ubershader ? SHADER_VARIANT_MAX : 0); |
| 1645 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 1646 | MutexLock lock(canvas_singleton->shader.mutex); |
| 1647 | return canvas_singleton->shader.canvas_shader.version_get_shader(version, variant_index); |
| 1648 | } else { |
| 1649 | return RID(); |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | uint64_t RendererCanvasRenderRD::CanvasShaderData::get_vertex_input_mask(ShaderVariant p_shader_variant, bool p_ubershader) { |
| 1654 | // Vertex input masks require knowledge of the shader. Since querying the shader can be expensive due to high contention and the necessary mutex, we cache the result instead. |
| 1655 | uint32_t input_mask_index = p_shader_variant + (p_ubershader ? SHADER_VARIANT_MAX : 0); |
| 1656 | uint64_t input_mask = vertex_input_masks[input_mask_index].load(std::memory_order_relaxed); |
| 1657 | if (input_mask == 0) { |
| 1658 | RID shader_rid = get_shader(p_shader_variant, p_ubershader); |
| 1659 | ERR_FAIL_COND_V(shader_rid.is_null(), 0); |
| 1660 | |
| 1661 | input_mask = RD::get_singleton()->shader_get_vertex_input_attribute_mask(shader_rid); |
| 1662 | vertex_input_masks[input_mask_index].store(input_mask, std::memory_order_relaxed); |
| 1663 | } |
| 1664 | |
| 1665 | return input_mask; |
| 1666 | } |
| 1667 | |
| 1668 | bool RendererCanvasRenderRD::CanvasShaderData::is_valid() const { |
| 1669 | if (version.is_valid()) { |
| 1670 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 1671 | MutexLock lock(canvas_singleton->shader.mutex); |
| 1672 | return canvas_singleton->shader.canvas_shader.version_is_valid(version); |
| 1673 | } else { |
| 1674 | return false; |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | RendererCanvasRenderRD::CanvasShaderData::CanvasShaderData() { |
| 1679 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 1680 | pipeline_hash_map.set_creation_object_and_function(this, &CanvasShaderData::_create_pipeline); |
| 1681 | pipeline_hash_map.set_compilations(&canvas_singleton->shader.pipeline_compilations[0], &canvas_singleton->shader.mutex); |
| 1682 | } |
| 1683 | |
| 1684 | RendererCanvasRenderRD::CanvasShaderData::~CanvasShaderData() { |
| 1685 | pipeline_hash_map.clear_pipelines(); |
| 1686 | |
| 1687 | if (version.is_valid()) { |
| 1688 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 1689 | MutexLock lock(canvas_singleton->shader.mutex); |
| 1690 | canvas_singleton->shader.canvas_shader.version_free(version); |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | RendererRD::MaterialStorage::ShaderData *RendererCanvasRenderRD::_create_shader_func() { |
| 1695 | CanvasShaderData *shader_data = memnew(CanvasShaderData); |
| 1696 | return shader_data; |
| 1697 | } |
| 1698 | |
| 1699 | bool RendererCanvasRenderRD::CanvasMaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) { |
| 1700 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 1701 | MutexLock lock(canvas_singleton->shader.mutex); |
| 1702 | RID shader_to_update = canvas_singleton->shader.canvas_shader.version_get_shader(shader_data->version, 0); |
| 1703 | bool uniform_set_changed = update_parameters_uniform_set(p_parameters, p_uniform_dirty, p_textures_dirty, shader_data->uniforms, shader_data->ubo_offsets.ptr(), shader_data->texture_uniforms, shader_data->default_texture_params, shader_data->ubo_size, uniform_set, shader_to_update, MATERIAL_UNIFORM_SET, true, false); |
| 1704 | bool uniform_set_srgb_changed = update_parameters_uniform_set(p_parameters, p_uniform_dirty, p_textures_dirty, shader_data->uniforms, shader_data->ubo_offsets.ptr(), shader_data->texture_uniforms, shader_data->default_texture_params, shader_data->ubo_size, uniform_set_srgb, shader_to_update, MATERIAL_UNIFORM_SET, false, false); |
| 1705 | return uniform_set_changed || uniform_set_srgb_changed; |
| 1706 | } |
| 1707 | |
| 1708 | RendererCanvasRenderRD::CanvasMaterialData::~CanvasMaterialData() { |
| 1709 | free_parameters_uniform_set(uniform_set); |
| 1710 | free_parameters_uniform_set(uniform_set_srgb); |
| 1711 | } |
| 1712 | |
| 1713 | RendererRD::MaterialStorage::MaterialData *RendererCanvasRenderRD::_create_material_func(CanvasShaderData *p_shader) { |
| 1714 | CanvasMaterialData *material_data = memnew(CanvasMaterialData); |
| 1715 | material_data->shader_data = p_shader; |
| 1716 | //update will happen later anyway so do nothing. |
| 1717 | return material_data; |
| 1718 | } |
| 1719 | |
| 1720 | void RendererCanvasRenderRD::set_time(double p_time) { |
| 1721 | state.time = p_time; |
| 1722 | } |
| 1723 | |
| 1724 | void RendererCanvasRenderRD::update() { |
| 1725 | } |
| 1726 | |
| 1727 | RendererCanvasRenderRD::RendererCanvasRenderRD() { |
| 1728 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 1729 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 1730 | |
| 1731 | { //create default samplers |
| 1732 | |
| 1733 | default_samplers.default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR; |
| 1734 | default_samplers.default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED; |
| 1735 | } |
| 1736 | |
| 1737 | // preallocate slots for uniform set 3 |
| 1738 | state.batch_texture_uniforms.resize(4); |
| 1739 | |
| 1740 | { //shader variants |
| 1741 | |
| 1742 | String global_defines; |
| 1743 | global_defines += "#define MAX_LIGHTS " + itos(MAX_LIGHTS_PER_RENDER) + "\n"; |
| 1744 | global_defines += "\n#define SAMPLERS_BINDING_FIRST_INDEX " + itos(SAMPLERS_BINDING_FIRST_INDEX) + "\n"; |
| 1745 | |
| 1746 | state.light_uniforms = memnew_arr(LightUniform, MAX_LIGHTS_PER_RENDER); |
| 1747 | Vector<String> variants; |
| 1748 | const uint32_t ubershader_iterations = 1; |
| 1749 | for (uint32_t ubershader = 0; ubershader < ubershader_iterations; ubershader++) { |
| 1750 | const String base_define = ubershader ? "\n#define UBERSHADER\n" : ""; |
| 1751 | variants.push_back(base_define + ""); // SHADER_VARIANT_QUAD |
| 1752 | variants.push_back(base_define + "#define USE_NINEPATCH\n"); // SHADER_VARIANT_NINEPATCH |
| 1753 | variants.push_back(base_define + "#define USE_PRIMITIVE\n"); // SHADER_VARIANT_PRIMITIVE |
| 1754 | variants.push_back(base_define + "#define USE_PRIMITIVE\n#define USE_POINT_SIZE\n"); // SHADER_VARIANT_PRIMITIVE_POINTS |
| 1755 | variants.push_back(base_define + "#define USE_ATTRIBUTES\n"); // SHADER_VARIANT_ATTRIBUTES |
| 1756 | variants.push_back(base_define + "#define USE_ATTRIBUTES\n#define USE_POINT_SIZE\n"); // SHADER_VARIANT_ATTRIBUTES_POINTS |
| 1757 | } |
| 1758 | |
| 1759 | shader.canvas_shader.initialize(variants, global_defines, {}, {}); |
| 1760 | |
| 1761 | shader.default_version_data = memnew(CanvasShaderData); |
| 1762 | shader.default_version_data->version = shader.canvas_shader.version_create(); |
| 1763 | shader.default_version_data->blend_mode = RendererRD::MaterialStorage::ShaderData::BLEND_MODE_MIX; |
| 1764 | shader.default_version_rd_shader = shader.default_version_data->get_shader(SHADER_VARIANT_QUAD, false); |
| 1765 | } |
| 1766 | |
| 1767 | { |
| 1768 | //shader compiler |
| 1769 | ShaderCompiler::DefaultIdentifierActions actions; |
| 1770 | |
| 1771 | actions.renames["VERTEX"] = "vertex"; |
| 1772 | actions.renames["LIGHT_VERTEX"] = "light_vertex"; |
| 1773 | actions.renames["SHADOW_VERTEX"] = "shadow_vertex"; |
| 1774 | actions.renames["UV"] = "uv"; |
| 1775 | actions.renames["POINT_SIZE"] = "point_size"; |
| 1776 | |
| 1777 | actions.renames["MODEL_MATRIX"] = "model_matrix"; |
| 1778 | actions.renames["CANVAS_MATRIX"] = "canvas_data.canvas_transform"; |
| 1779 | actions.renames["SCREEN_MATRIX"] = "canvas_data.screen_transform"; |
| 1780 | actions.renames["TIME"] = "canvas_data.time"; |
| 1781 | actions.renames["PI"] = String::num(Math::PI); |
| 1782 | actions.renames["TAU"] = String::num(Math::TAU); |
| 1783 | actions.renames["E"] = String::num(Math::E); |
| 1784 | actions.renames["AT_LIGHT_PASS"] = "false"; |
| 1785 | actions.renames["INSTANCE_CUSTOM"] = "instance_custom"; |
| 1786 | |
| 1787 | actions.renames["COLOR"] = "color"; |
| 1788 | actions.renames["NORMAL"] = "normal"; |
| 1789 | actions.renames["NORMAL_MAP"] = "normal_map"; |
| 1790 | actions.renames["NORMAL_MAP_DEPTH"] = "normal_map_depth"; |
| 1791 | actions.renames["TEXTURE"] = "color_texture"; |
| 1792 | actions.renames["TEXTURE_PIXEL_SIZE"] = "read_draw_data_color_texture_pixel_size"; |
| 1793 | actions.renames["NORMAL_TEXTURE"] = "normal_texture"; |
| 1794 | actions.renames["SPECULAR_SHININESS_TEXTURE"] = "specular_texture"; |
| 1795 | actions.renames["SPECULAR_SHININESS"] = "specular_shininess"; |
| 1796 | actions.renames["SCREEN_UV"] = "screen_uv"; |
| 1797 | actions.renames["REGION_RECT"] = "region_rect"; |
| 1798 | actions.renames["SCREEN_PIXEL_SIZE"] = "canvas_data.screen_pixel_size"; |
| 1799 | actions.renames["FRAGCOORD"] = "gl_FragCoord"; |
| 1800 | actions.renames["POINT_COORD"] = "gl_PointCoord"; |
| 1801 | actions.renames["INSTANCE_ID"] = "gl_InstanceIndex"; |
| 1802 | actions.renames["VERTEX_ID"] = "gl_VertexIndex"; |
| 1803 | |
| 1804 | actions.renames["CUSTOM0"] = "custom0"; |
| 1805 | actions.renames["CUSTOM1"] = "custom1"; |
| 1806 | |
| 1807 | actions.renames["LIGHT_POSITION"] = "light_position"; |
| 1808 | actions.renames["LIGHT_DIRECTION"] = "light_direction"; |
| 1809 | actions.renames["LIGHT_IS_DIRECTIONAL"] = "is_directional"; |
| 1810 | actions.renames["LIGHT_COLOR"] = "light_color"; |
| 1811 | actions.renames["LIGHT_ENERGY"] = "light_energy"; |
| 1812 | actions.renames["LIGHT"] = "light"; |
| 1813 | actions.renames["SHADOW_MODULATE"] = "shadow_modulate"; |
| 1814 | |
| 1815 | actions.renames["texture_sdf"] = "texture_sdf"; |
| 1816 | actions.renames["texture_sdf_normal"] = "texture_sdf_normal"; |
| 1817 | actions.renames["sdf_to_screen_uv"] = "sdf_to_screen_uv"; |
| 1818 | actions.renames["screen_uv_to_sdf"] = "screen_uv_to_sdf"; |
| 1819 | |
| 1820 | actions.usage_defines["COLOR"] = "#define COLOR_USED\n"; |
| 1821 | actions.usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n"; |
| 1822 | actions.usage_defines["SCREEN_PIXEL_SIZE"] = "@SCREEN_UV"; |
| 1823 | actions.usage_defines["NORMAL"] = "#define NORMAL_USED\n"; |
| 1824 | actions.usage_defines["NORMAL_MAP"] = "#define NORMAL_MAP_USED\n"; |
| 1825 | actions.usage_defines["SPECULAR_SHININESS"] = "#define SPECULAR_SHININESS_USED\n"; |
| 1826 | actions.usage_defines["POINT_SIZE"] = "#define USE_POINT_SIZE\n"; |
| 1827 | actions.usage_defines["CUSTOM0"] = "#define CUSTOM0_USED\n"; |
| 1828 | actions.usage_defines["CUSTOM1"] = "#define CUSTOM1_USED\n"; |
| 1829 | |
| 1830 | actions.render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n"; |
| 1831 | actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n"; |
| 1832 | actions.render_mode_defines["light_only"] = "#define MODE_LIGHT_ONLY\n"; |
| 1833 | actions.render_mode_defines["world_vertex_coords"] = "#define USE_WORLD_VERTEX_COORDS\n"; |
| 1834 | |
| 1835 | actions.custom_samplers["TEXTURE"] = "texture_sampler"; |
| 1836 | actions.custom_samplers["NORMAL_TEXTURE"] = "texture_sampler"; |
| 1837 | actions.custom_samplers["SPECULAR_SHININESS_TEXTURE"] = "texture_sampler"; |
| 1838 | actions.base_texture_binding_index = 1; |
| 1839 | actions.texture_layout_set = MATERIAL_UNIFORM_SET; |
| 1840 | actions.base_uniform_string = "material."; |
| 1841 | actions.default_filter = ShaderLanguage::FILTER_LINEAR; |
| 1842 | actions.default_repeat = ShaderLanguage::REPEAT_DISABLE; |
| 1843 | actions.base_varying_index = 9; |
| 1844 | |
| 1845 | actions.global_buffer_array_variable = "global_shader_uniforms.data"; |
| 1846 | actions.instance_uniform_index_variable = "read_draw_data_instance_offset"; |
| 1847 | |
| 1848 | shader.compiler.initialize(actions); |
| 1849 | } |
| 1850 | |
| 1851 | { //shadow rendering |
| 1852 | Vector<String> versions; |
| 1853 | versions.push_back("\n#define MODE_SHADOW\n"); // Shadow. |
| 1854 | versions.push_back("\n#define MODE_SHADOW\n#define POSITIONAL_SHADOW\n"); // Positional shadow. |
| 1855 | versions.push_back("\n#define MODE_SDF\n"); // SDF. |
| 1856 | shadow_render.shader.initialize(versions); |
| 1857 | |
| 1858 | { |
| 1859 | Vector<RD::AttachmentFormat> attachments; |
| 1860 | |
| 1861 | RD::AttachmentFormat af_color; |
| 1862 | af_color.format = RD::DATA_FORMAT_R32_SFLOAT; |
| 1863 | af_color.usage_flags = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; |
| 1864 | |
| 1865 | attachments.push_back(af_color); |
| 1866 | |
| 1867 | RD::AttachmentFormat af_depth; |
| 1868 | af_depth.format = RD::DATA_FORMAT_D32_SFLOAT; |
| 1869 | af_depth.usage_flags = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 1870 | |
| 1871 | attachments.push_back(af_depth); |
| 1872 | |
| 1873 | shadow_render.framebuffer_format = RD::get_singleton()->framebuffer_format_create(attachments); |
| 1874 | } |
| 1875 | |
| 1876 | { |
| 1877 | Vector<RD::AttachmentFormat> attachments; |
| 1878 | |
| 1879 | RD::AttachmentFormat af_color; |
| 1880 | af_color.format = RD::DATA_FORMAT_R8_UNORM; |
| 1881 | af_color.usage_flags = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; |
| 1882 | |
| 1883 | attachments.push_back(af_color); |
| 1884 | |
| 1885 | shadow_render.sdf_framebuffer_format = RD::get_singleton()->framebuffer_format_create(attachments); |
| 1886 | } |
| 1887 | |
| 1888 | //pipelines |
| 1889 | Vector<RD::VertexAttribute> vf; |
| 1890 | RD::VertexAttribute vd; |
| 1891 | vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT; |
| 1892 | vd.stride = sizeof(float) * 3; |
| 1893 | vd.location = 0; |
| 1894 | vd.offset = 0; |
| 1895 | vf.push_back(vd); |
| 1896 | shadow_render.vertex_format = RD::get_singleton()->vertex_format_create(vf); |
| 1897 | |
| 1898 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 1899 | vd.stride = sizeof(float) * 2; |
| 1900 | |
| 1901 | vf.write[0] = vd; |
| 1902 | shadow_render.sdf_vertex_format = RD::get_singleton()->vertex_format_create(vf); |
| 1903 | |
| 1904 | shadow_render.shader_version = shadow_render.shader.version_create(); |
| 1905 | |
| 1906 | for (int i = 0; i < 2; i++) { |
| 1907 | RD::PipelineRasterizationState rs; |
| 1908 | RD::PipelineDepthStencilState ds; |
| 1909 | ds.enable_depth_write = true; |
| 1910 | ds.enable_depth_test = true; |
| 1911 | ds.depth_compare_operator = RD::COMPARE_OP_LESS; |
| 1912 | shadow_render.render_pipelines[i] = RD::get_singleton()->render_pipeline_create(shadow_render.shader.version_get_shader(shadow_render.shader_version, ShadowRenderMode(i)), shadow_render.framebuffer_format, shadow_render.vertex_format, RD::RENDER_PRIMITIVE_TRIANGLES, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0); |
| 1913 | } |
| 1914 | |
| 1915 | for (int i = 0; i < 2; i++) { |
| 1916 | shadow_render.sdf_render_pipelines[i] = RD::get_singleton()->render_pipeline_create(shadow_render.shader.version_get_shader(shadow_render.shader_version, SHADOW_RENDER_MODE_SDF), shadow_render.sdf_framebuffer_format, shadow_render.sdf_vertex_format, i == 0 ? RD::RENDER_PRIMITIVE_TRIANGLES : RD::RENDER_PRIMITIVE_LINES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0); |
| 1917 | } |
| 1918 | |
| 1919 | // Unload shader modules to save memory. |
| 1920 | RD::get_singleton()->shader_destroy_modules(shadow_render.shader.version_get_shader(shadow_render.shader_version, SHADOW_RENDER_MODE_DIRECTIONAL_SHADOW)); |
| 1921 | RD::get_singleton()->shader_destroy_modules(shadow_render.shader.version_get_shader(shadow_render.shader_version, SHADOW_RENDER_MODE_POSITIONAL_SHADOW)); |
| 1922 | RD::get_singleton()->shader_destroy_modules(shadow_render.shader.version_get_shader(shadow_render.shader_version, SHADOW_RENDER_MODE_SDF)); |
| 1923 | } |
| 1924 | |
| 1925 | { //bindings |
| 1926 | |
| 1927 | state.canvas_state_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(State::Buffer)); |
| 1928 | state.lights_storage_buffer = RD::get_singleton()->storage_buffer_create(sizeof(LightUniform) * MAX_LIGHTS_PER_RENDER); |
| 1929 | |
| 1930 | RD::SamplerState shadow_sampler_state; |
| 1931 | shadow_sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST; |
| 1932 | shadow_sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST; |
| 1933 | shadow_sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT; //shadow wrap around |
| 1934 | state.shadow_sampler = RD::get_singleton()->sampler_create(shadow_sampler_state); |
| 1935 | } |
| 1936 | |
| 1937 | { |
| 1938 | //polygon buffers |
| 1939 | polygon_buffers.last_id = 1; |
| 1940 | } |
| 1941 | |
| 1942 | { // default index buffer |
| 1943 | |
| 1944 | Vector<uint8_t> pv; |
| 1945 | pv.resize(6 * 2); |
| 1946 | { |
| 1947 | uint8_t *w = pv.ptrw(); |
| 1948 | uint16_t *p16 = (uint16_t *)w; |
| 1949 | p16[0] = 0; |
| 1950 | p16[1] = 1; |
| 1951 | p16[2] = 2; |
| 1952 | p16[3] = 0; |
| 1953 | p16[4] = 2; |
| 1954 | p16[5] = 3; |
| 1955 | } |
| 1956 | shader.quad_index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT16, pv); |
| 1957 | shader.quad_index_array = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 6); |
| 1958 | } |
| 1959 | |
| 1960 | { |
| 1961 | Vector<RD::VertexAttribute> vf; |
| 1962 | uint32_t offset = 0; |
| 1963 | RD::VertexAttribute vd; |
| 1964 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 1965 | vd.stride = sizeof(InstanceData); |
| 1966 | vd.frequency = RD::VERTEX_FREQUENCY_INSTANCE; |
| 1967 | vd.location = 8; |
| 1968 | vd.binding = 0; // Explicitly assign binding 0 for instance data. |
| 1969 | vd.offset = offset; |
| 1970 | offset += sizeof(float) * 4; |
| 1971 | vf.push_back(vd); // attrib_A |
| 1972 | |
| 1973 | vd.location = 9; |
| 1974 | vd.offset = offset; |
| 1975 | offset += sizeof(float) * 4; |
| 1976 | vf.push_back(vd); // attrib_B |
| 1977 | |
| 1978 | vd.location = 10; |
| 1979 | vd.offset = offset; |
| 1980 | offset += sizeof(float) * 4; |
| 1981 | vf.push_back(vd); // attrib_C |
| 1982 | |
| 1983 | vd.location = 11; |
| 1984 | vd.offset = offset; |
| 1985 | offset += sizeof(float) * 4; |
| 1986 | vf.push_back(vd); // attrib_D |
| 1987 | |
| 1988 | vd.location = 12; |
| 1989 | vd.offset = offset; |
| 1990 | offset += sizeof(float) * 4; |
| 1991 | vf.push_back(vd); // attrib_E |
| 1992 | |
| 1993 | uint32_t attrib_F_index = vf.size(); |
| 1994 | vd.location = 13; |
| 1995 | vd.offset = offset; |
| 1996 | offset += sizeof(float) * 4; |
| 1997 | vf.push_back(vd); // attrib_F (RECT, NINEPATCH) |
| 1998 | |
| 1999 | vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT; |
| 2000 | vd.location = 14; |
| 2001 | vd.offset = offset; |
| 2002 | offset += sizeof(uint32_t) * 4; |
| 2003 | vf.push_back(vd); // attrib_G |
| 2004 | |
| 2005 | vd.location = 15; |
| 2006 | vd.offset = offset; |
| 2007 | offset += sizeof(uint32_t) * 4; |
| 2008 | vf.push_back(vd); // attrib_H |
| 2009 | |
| 2010 | // RECT, NINEPATCH |
| 2011 | shader.quad_vertex_format_id = RD::get_singleton()->vertex_format_create(vf); |
| 2012 | |
| 2013 | // PRIMITIVE |
| 2014 | vf.write[attrib_F_index].format = RD::DATA_FORMAT_R32G32B32A32_UINT; |
| 2015 | shader.primitive_vertex_format_id = RD::get_singleton()->vertex_format_create(vf); |
| 2016 | } |
| 2017 | |
| 2018 | { //primitive |
| 2019 | primitive_arrays.index_array[0] = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 1); |
| 2020 | primitive_arrays.index_array[1] = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 2); |
| 2021 | primitive_arrays.index_array[2] = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 3); |
| 2022 | primitive_arrays.index_array[3] = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 6); |
| 2023 | } |
| 2024 | |
| 2025 | { |
| 2026 | //default shadow texture to keep uniform set happy |
| 2027 | RD::TextureFormat tf; |
| 2028 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 2029 | tf.width = 4; |
| 2030 | tf.height = 4; |
| 2031 | tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT; |
| 2032 | tf.format = RD::DATA_FORMAT_R32_SFLOAT; |
| 2033 | |
| 2034 | state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 2035 | } |
| 2036 | |
| 2037 | { |
| 2038 | Vector<RD::Uniform> uniforms; |
| 2039 | |
| 2040 | { |
| 2041 | RD::Uniform u; |
| 2042 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 2043 | u.binding = 0; |
| 2044 | u.append_id(RendererRD::MeshStorage::get_singleton()->get_default_rd_storage_buffer()); |
| 2045 | uniforms.push_back(u); |
| 2046 | } |
| 2047 | |
| 2048 | state.default_transforms_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader.default_version_rd_shader, TRANSFORMS_UNIFORM_SET); |
| 2049 | } |
| 2050 | |
| 2051 | default_canvas_texture = texture_storage->canvas_texture_allocate(); |
| 2052 | texture_storage->canvas_texture_initialize(default_canvas_texture); |
| 2053 | |
| 2054 | RendererRD::TextureStorage::CanvasTextureInfo info = RendererRD::TextureStorage::get_singleton()->canvas_texture_get_info(default_canvas_texture, default_filter, default_repeat, false, false); |
| 2055 | default_texture_info.diffuse = info.diffuse; |
| 2056 | default_texture_info.normal = info.normal; |
| 2057 | default_texture_info.specular = info.specular; |
| 2058 | default_texture_info.sampler = info.sampler; |
| 2059 | |
| 2060 | state.shadow_texture_size = GLOBAL_GET("rendering/2d/shadow_atlas/size"); |
| 2061 | |
| 2062 | //create functions for shader and material |
| 2063 | material_storage->shader_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_2D, _create_shader_funcs); |
| 2064 | material_storage->material_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_2D, _create_material_funcs); |
| 2065 | |
| 2066 | state.time = 0; |
| 2067 | |
| 2068 | { |
| 2069 | default_canvas_group_shader = material_storage->shader_allocate(); |
| 2070 | material_storage->shader_initialize(default_canvas_group_shader); |
| 2071 | |
| 2072 | material_storage->shader_set_code(default_canvas_group_shader, R"( |
| 2073 | // Default CanvasGroup shader. |
| 2074 | |
| 2075 | shader_type canvas_item; |
| 2076 | render_mode unshaded; |
| 2077 | |
| 2078 | uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest; |
| 2079 | |
| 2080 | void fragment() { |
| 2081 | vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0); |
| 2082 | |
| 2083 | if (c.a > 0.0001) { |
| 2084 | c.rgb /= c.a; |
| 2085 | } |
| 2086 | |
| 2087 | COLOR *= c; |
| 2088 | } |
| 2089 | )"); |
| 2090 | default_canvas_group_material = material_storage->material_allocate(); |
| 2091 | material_storage->material_initialize(default_canvas_group_material); |
| 2092 | |
| 2093 | material_storage->material_set_shader(default_canvas_group_material, default_canvas_group_shader); |
| 2094 | } |
| 2095 | |
| 2096 | { |
| 2097 | default_clip_children_shader = material_storage->shader_allocate(); |
| 2098 | material_storage->shader_initialize(default_clip_children_shader); |
| 2099 | |
| 2100 | material_storage->shader_set_code(default_clip_children_shader, R"( |
| 2101 | // Default clip children shader. |
| 2102 | |
| 2103 | shader_type canvas_item; |
| 2104 | render_mode unshaded; |
| 2105 | |
| 2106 | uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest; |
| 2107 | |
| 2108 | void fragment() { |
| 2109 | vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0); |
| 2110 | COLOR.rgb = c.rgb; |
| 2111 | } |
| 2112 | )"); |
| 2113 | default_clip_children_material = material_storage->material_allocate(); |
| 2114 | material_storage->material_initialize(default_clip_children_material); |
| 2115 | |
| 2116 | material_storage->material_set_shader(default_clip_children_material, default_clip_children_shader); |
| 2117 | } |
| 2118 | |
| 2119 | { |
| 2120 | uint32_t cache_size = uint32_t(GLOBAL_GET("rendering/2d/batching/uniform_set_cache_size")); |
| 2121 | rid_set_to_uniform_set.set_capacity(cache_size); |
| 2122 | } |
| 2123 | |
| 2124 | { |
| 2125 | state.max_instances_per_buffer = uint32_t(GLOBAL_GET("rendering/2d/batching/item_buffer_size")); |
| 2126 | state.max_instance_buffer_size = state.max_instances_per_buffer * sizeof(InstanceData); |
| 2127 | state.canvas_instance_batches.reserve(200); |
| 2128 | state.instance_buffers.set_vertex_size(0, state.max_instance_buffer_size); |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | bool RendererCanvasRenderRD::free(RID p_rid) { |
| 2133 | if (canvas_light_owner.owns(p_rid)) { |
| 2134 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 2135 | ERR_FAIL_NULL_V(cl, false); |
| 2136 | light_set_use_shadow(p_rid, false); |
| 2137 | canvas_light_owner.free(p_rid); |
| 2138 | } else if (occluder_polygon_owner.owns(p_rid)) { |
| 2139 | occluder_polygon_set_shape(p_rid, Vector<Vector2>(), false); |
| 2140 | occluder_polygon_owner.free(p_rid); |
| 2141 | } else { |
| 2142 | return false; |
| 2143 | } |
| 2144 | |
| 2145 | return true; |
| 2146 | } |
| 2147 | |
| 2148 | void RendererCanvasRenderRD::set_shadow_texture_size(int p_size) { |
| 2149 | p_size = MAX(1, nearest_power_of_2_templated(p_size)); |
| 2150 | if (p_size == state.shadow_texture_size) { |
| 2151 | return; |
| 2152 | } |
| 2153 | state.shadow_texture_size = p_size; |
| 2154 | if (state.shadow_fb.is_valid()) { |
| 2155 | RD::get_singleton()->free_rid(state.shadow_texture); |
| 2156 | RD::get_singleton()->free_rid(state.shadow_depth_texture); |
| 2157 | state.shadow_fb = RID(); |
| 2158 | |
| 2159 | { |
| 2160 | //create a default shadow texture to keep uniform set happy (and that it gets erased when a new one is created) |
| 2161 | RD::TextureFormat tf; |
| 2162 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 2163 | tf.width = 4; |
| 2164 | tf.height = 4; |
| 2165 | tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT; |
| 2166 | tf.format = RD::DATA_FORMAT_R32_SFLOAT; |
| 2167 | |
| 2168 | state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | void RendererCanvasRenderRD::set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) { |
| 2174 | debug_redraw = p_enabled; |
| 2175 | debug_redraw_time = p_time; |
| 2176 | debug_redraw_color = p_color; |
| 2177 | } |
| 2178 | |
| 2179 | uint32_t RendererCanvasRenderRD::get_pipeline_compilations(RS::PipelineSource p_source) { |
| 2180 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 2181 | MutexLock lock(canvas_singleton->shader.mutex); |
| 2182 | return shader.pipeline_compilations[p_source]; |
| 2183 | } |
| 2184 | |
| 2185 | void RendererCanvasRenderRD::_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, RenderingMethod::RenderInfo *r_render_info) { |
| 2186 | // Record batches |
| 2187 | { |
| 2188 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 2189 | Item *current_clip = nullptr; |
| 2190 | |
| 2191 | // Record Batches. |
| 2192 | // First item always forms its own batch. |
| 2193 | bool batch_broken = false; |
| 2194 | Batch *current_batch = _new_batch(batch_broken); |
| 2195 | |
| 2196 | for (int i = 0; i < p_item_count; i++) { |
| 2197 | Item *ci = items[i]; |
| 2198 | |
| 2199 | if (ci->final_clip_owner != current_batch->clip) { |
| 2200 | current_batch = _new_batch(batch_broken); |
| 2201 | current_batch->clip = ci->final_clip_owner; |
| 2202 | current_clip = ci->final_clip_owner; |
| 2203 | } |
| 2204 | |
| 2205 | RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material; |
| 2206 | |
| 2207 | if (ci->use_canvas_group) { |
| 2208 | if (ci->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) { |
| 2209 | material = default_clip_children_material; |
| 2210 | } else { |
| 2211 | if (material.is_null()) { |
| 2212 | if (ci->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_ONLY) { |
| 2213 | material = default_clip_children_material; |
| 2214 | } else { |
| 2215 | material = default_canvas_group_material; |
| 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | if (material != current_batch->material) { |
| 2222 | current_batch = _new_batch(batch_broken); |
| 2223 | |
| 2224 | CanvasMaterialData *material_data = nullptr; |
| 2225 | if (material.is_valid()) { |
| 2226 | material_data = static_cast<CanvasMaterialData *>(material_storage->material_get_data(material, RendererRD::MaterialStorage::SHADER_TYPE_2D)); |
| 2227 | } |
| 2228 | |
| 2229 | current_batch->material = material; |
| 2230 | current_batch->material_data = material_data; |
| 2231 | } |
| 2232 | |
| 2233 | if (ci->repeat_source_item == nullptr || ci->repeat_size == Vector2()) { |
| 2234 | Transform2D base_transform = p_canvas_transform_inverse * ci->final_transform; |
| 2235 | _record_item_commands(ci, p_to_render_target, base_transform, current_clip, p_lights, batch_broken, r_sdf_used, current_batch); |
| 2236 | } else { |
| 2237 | Point2 start_pos = ci->repeat_size * -(ci->repeat_times / 2); |
| 2238 | Point2 offset; |
| 2239 | int repeat_times_x = ci->repeat_size.x ? ci->repeat_times : 0; |
| 2240 | int repeat_times_y = ci->repeat_size.y ? ci->repeat_times : 0; |
| 2241 | for (int ry = 0; ry <= repeat_times_y; ry++) { |
| 2242 | offset.y = start_pos.y + ry * ci->repeat_size.y; |
| 2243 | for (int rx = 0; rx <= repeat_times_x; rx++) { |
| 2244 | offset.x = start_pos.x + rx * ci->repeat_size.x; |
| 2245 | Transform2D base_transform = ci->final_transform; |
| 2246 | base_transform.columns[2] += ci->repeat_source_item->final_transform.basis_xform(offset); |
| 2247 | base_transform = p_canvas_transform_inverse * base_transform; |
| 2248 | _record_item_commands(ci, p_to_render_target, base_transform, current_clip, p_lights, batch_broken, r_sdf_used, current_batch); |
| 2249 | } |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | if (state.canvas_instance_batches.is_empty()) { |
| 2256 | // Nothing to render, just return. |
| 2257 | return; |
| 2258 | } |
| 2259 | |
| 2260 | // Render batches |
| 2261 | |
| 2262 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 2263 | |
| 2264 | RID framebuffer; |
| 2265 | RID fb_uniform_set; |
| 2266 | bool clear = false; |
| 2267 | Color clear_color; |
| 2268 | |
| 2269 | if (p_to_backbuffer) { |
| 2270 | framebuffer = texture_storage->render_target_get_rd_backbuffer_framebuffer(p_to_render_target.render_target); |
| 2271 | fb_uniform_set = texture_storage->render_target_get_backbuffer_uniform_set(p_to_render_target.render_target); |
| 2272 | } else { |
| 2273 | framebuffer = texture_storage->render_target_get_rd_framebuffer(p_to_render_target.render_target); |
| 2274 | texture_storage->render_target_set_msaa_needs_resolve(p_to_render_target.render_target, false); // If MSAA is enabled, our framebuffer will be resolved! |
| 2275 | |
| 2276 | if (texture_storage->render_target_is_clear_requested(p_to_render_target.render_target)) { |
| 2277 | clear = true; |
| 2278 | clear_color = texture_storage->render_target_get_clear_request_color(p_to_render_target.render_target); |
| 2279 | if (texture_storage->render_target_is_using_hdr(p_to_render_target.render_target)) { |
| 2280 | clear_color = clear_color.srgb_to_linear(); |
| 2281 | } |
| 2282 | texture_storage->render_target_disable_clear_request(p_to_render_target.render_target); |
| 2283 | } |
| 2284 | // TODO: Obtain from framebuffer format eventually when this is implemented. |
| 2285 | fb_uniform_set = texture_storage->render_target_get_framebuffer_uniform_set(p_to_render_target.render_target); |
| 2286 | } |
| 2287 | |
| 2288 | if (fb_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(fb_uniform_set)) { |
| 2289 | fb_uniform_set = _create_base_uniform_set(p_to_render_target.render_target, p_to_backbuffer); |
| 2290 | } |
| 2291 | |
| 2292 | RD::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(framebuffer); |
| 2293 | |
| 2294 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, clear ? RD::DRAW_CLEAR_COLOR_0 : RD::DRAW_DEFAULT_ALL, clear_color, 1.0f, 0, Rect2(), RDD::BreadcrumbMarker::UI_PASS); |
| 2295 | |
| 2296 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, fb_uniform_set, BASE_UNIFORM_SET); |
| 2297 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, state.default_transforms_uniform_set, TRANSFORMS_UNIFORM_SET); |
| 2298 | |
| 2299 | Item *current_clip = nullptr; |
| 2300 | state.current_batch_uniform_set = RID(); |
| 2301 | |
| 2302 | for (uint32_t i = 0; i <= state.current_batch_index; i++) { |
| 2303 | Batch *current_batch = &state.canvas_instance_batches[i]; |
| 2304 | // Skipping when there is no instances. |
| 2305 | if (current_batch->instance_count == 0) { |
| 2306 | continue; |
| 2307 | } |
| 2308 | |
| 2309 | //setup clip |
| 2310 | if (current_clip != current_batch->clip) { |
| 2311 | current_clip = current_batch->clip; |
| 2312 | if (current_clip) { |
| 2313 | RD::get_singleton()->draw_list_enable_scissor(draw_list, current_clip->final_clip_rect); |
| 2314 | } else { |
| 2315 | RD::get_singleton()->draw_list_disable_scissor(draw_list); |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | CanvasShaderData *shader_data = shader.default_version_data; |
| 2320 | CanvasMaterialData *material_data = current_batch->material_data; |
| 2321 | if (material_data) { |
| 2322 | if (material_data->shader_data->version.is_valid() && material_data->shader_data->is_valid()) { |
| 2323 | shader_data = material_data->shader_data; |
| 2324 | // Update uniform set. |
| 2325 | RID uniform_set = texture_storage->render_target_is_using_hdr(p_to_render_target.render_target) ? material_data->uniform_set : material_data->uniform_set_srgb; |
| 2326 | if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) { // Material may not have a uniform set. |
| 2327 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set, MATERIAL_UNIFORM_SET); |
| 2328 | material_data->set_as_used(); |
| 2329 | } |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | _render_batch(draw_list, shader_data, fb_format, p_lights, current_batch, r_render_info); |
| 2334 | } |
| 2335 | |
| 2336 | RD::get_singleton()->draw_list_end(); |
| 2337 | |
| 2338 | state.current_batch_index = 0; |
| 2339 | state.canvas_instance_batches.clear(); |
| 2340 | } |
| 2341 | |
| 2342 | void RendererCanvasRenderRD::_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) { |
| 2343 | const RenderingServer::CanvasItemTextureFilter texture_filter = p_item->texture_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? default_filter : p_item->texture_filter; |
| 2344 | const RenderingServer::CanvasItemTextureRepeat texture_repeat = p_item->texture_repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? default_repeat : p_item->texture_repeat; |
| 2345 | |
| 2346 | Transform2D base_transform = p_base_transform; |
| 2347 | |
| 2348 | InstanceData template_instance; |
| 2349 | memset(&template_instance, 0, sizeof(InstanceData)); |
| 2350 | |
| 2351 | Transform2D draw_transform; // Used by transform command |
| 2352 | _update_transform_2d_to_mat2x3(base_transform, template_instance.world); |
| 2353 | |
| 2354 | Color base_color = p_item->final_modulate; |
| 2355 | bool use_linear_colors = p_render_target.use_linear_colors; |
| 2356 | template_instance.instance_uniforms_ofs = static_cast<uint32_t>(p_item->instance_allocated_shader_uniforms_offset); |
| 2357 | |
| 2358 | bool reclip = false; |
| 2359 | |
| 2360 | bool skipping = false; |
| 2361 | |
| 2362 | uint16_t light_count = 0; |
| 2363 | uint16_t shadow_mask = 0; |
| 2364 | |
| 2365 | { |
| 2366 | Light *light = p_lights; |
| 2367 | |
| 2368 | while (light) { |
| 2369 | if (light->render_index_cache >= 0 && p_item->light_mask & light->item_mask && p_item->z_final >= light->z_min && p_item->z_final <= light->z_max && p_item->global_rect_cache.intersects(light->rect_cache)) { |
| 2370 | uint32_t light_index = light->render_index_cache; |
| 2371 | // TODO: consider making lights a per-batch property and then baking light operations in the shader for better performance. |
| 2372 | template_instance.lights[light_count >> 2] |= light_index << ((light_count & 3) * 8); |
| 2373 | |
| 2374 | if (p_item->light_mask & light->item_shadow_mask) { |
| 2375 | shadow_mask |= 1 << light_count; |
| 2376 | } |
| 2377 | |
| 2378 | light_count++; |
| 2379 | |
| 2380 | if (light_count == MAX_LIGHTS_PER_ITEM - 1) { |
| 2381 | break; |
| 2382 | } |
| 2383 | } |
| 2384 | light = light->next_ptr; |
| 2385 | } |
| 2386 | |
| 2387 | template_instance.flags |= light_count << INSTANCE_FLAGS_LIGHT_COUNT_SHIFT; |
| 2388 | template_instance.flags |= shadow_mask << INSTANCE_FLAGS_SHADOW_MASKED_SHIFT; |
| 2389 | } |
| 2390 | |
| 2391 | bool use_lighting = (light_count > 0 || using_directional_lights); |
| 2392 | |
| 2393 | if (use_lighting != r_current_batch->use_lighting) { |
| 2394 | r_current_batch = _new_batch(r_batch_broken); |
| 2395 | r_current_batch->use_lighting = use_lighting; |
| 2396 | } |
| 2397 | |
| 2398 | const Item::Command *c = p_item->commands; |
| 2399 | while (c) { |
| 2400 | if (skipping && c->type != Item::Command::TYPE_ANIMATION_SLICE) { |
| 2401 | c = c->next; |
| 2402 | continue; |
| 2403 | } |
| 2404 | |
| 2405 | switch (c->type) { |
| 2406 | case Item::Command::TYPE_RECT: { |
| 2407 | const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c); |
| 2408 | |
| 2409 | // 1: If commands are different, start a new batch. |
| 2410 | if (r_current_batch->command_type != Item::Command::TYPE_RECT) { |
| 2411 | r_current_batch = _new_batch(r_batch_broken); |
| 2412 | r_current_batch->command_type = Item::Command::TYPE_RECT; |
| 2413 | r_current_batch->command = c; |
| 2414 | // default variant |
| 2415 | r_current_batch->shader_variant = SHADER_VARIANT_QUAD; |
| 2416 | r_current_batch->render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES; |
| 2417 | r_current_batch->flags = 0; |
| 2418 | } |
| 2419 | |
| 2420 | RenderingServer::CanvasItemTextureRepeat rect_repeat = texture_repeat; |
| 2421 | if (bool(rect->flags & CANVAS_RECT_TILE)) { |
| 2422 | rect_repeat = RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED; |
| 2423 | } |
| 2424 | |
| 2425 | Color modulated = rect->modulate * base_color; |
| 2426 | if (use_linear_colors) { |
| 2427 | modulated = modulated.srgb_to_linear(); |
| 2428 | } |
| 2429 | |
| 2430 | bool has_blend = bool(rect->flags & CANVAS_RECT_LCD); |
| 2431 | // Start a new batch if the blend mode has changed, |
| 2432 | // or blend mode is enabled and the modulation has changed. |
| 2433 | if (has_blend != r_current_batch->has_blend || (has_blend && modulated != r_current_batch->modulate)) { |
| 2434 | r_current_batch = _new_batch(r_batch_broken); |
| 2435 | r_current_batch->has_blend = has_blend; |
| 2436 | r_current_batch->modulate = modulated; |
| 2437 | r_current_batch->shader_variant = SHADER_VARIANT_QUAD; |
| 2438 | r_current_batch->render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES; |
| 2439 | } |
| 2440 | |
| 2441 | bool has_msdf = bool(rect->flags & CANVAS_RECT_MSDF); |
| 2442 | TextureState tex_state(rect->texture, texture_filter, rect_repeat, has_msdf, use_linear_colors); |
| 2443 | TextureInfo *tex_info = texture_info_map.getptr(tex_state); |
| 2444 | if (!tex_info) { |
| 2445 | tex_info = &texture_info_map.insert(tex_state, TextureInfo())->value; |
| 2446 | _prepare_batch_texture_info(rect->texture, tex_state, tex_info); |
| 2447 | } |
| 2448 | |
| 2449 | if (has_msdf != r_current_batch->use_msdf || rect->px_range != r_current_batch->msdf_pix_range || rect->outline != r_current_batch->msdf_outline) { |
| 2450 | r_current_batch = _new_batch(r_batch_broken); |
| 2451 | r_current_batch->use_msdf = has_msdf; |
| 2452 | r_current_batch->msdf_pix_range = rect->px_range; |
| 2453 | r_current_batch->msdf_outline = rect->outline; |
| 2454 | } |
| 2455 | |
| 2456 | bool has_lcd = bool(rect->flags & CANVAS_RECT_LCD); |
| 2457 | if (has_lcd != r_current_batch->use_lcd) { |
| 2458 | r_current_batch = _new_batch(r_batch_broken); |
| 2459 | r_current_batch->use_lcd = has_lcd; |
| 2460 | } |
| 2461 | |
| 2462 | if (r_current_batch->tex_info != tex_info) { |
| 2463 | r_current_batch = _new_batch(r_batch_broken); |
| 2464 | r_current_batch->tex_info = tex_info; |
| 2465 | } |
| 2466 | |
| 2467 | InstanceData *instance_data = new_instance_data(*r_current_batch, template_instance); |
| 2468 | Rect2 src_rect; |
| 2469 | Rect2 dst_rect; |
| 2470 | |
| 2471 | if (rect->texture.is_valid()) { |
| 2472 | src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * tex_info->texpixel_size, rect->source.size * tex_info->texpixel_size) : Rect2(0, 0, 1, 1); |
| 2473 | dst_rect = Rect2(rect->rect.position, rect->rect.size); |
| 2474 | |
| 2475 | if (dst_rect.size.width < 0) { |
| 2476 | dst_rect.position.x += dst_rect.size.width; |
| 2477 | dst_rect.size.width *= -1; |
| 2478 | } |
| 2479 | if (dst_rect.size.height < 0) { |
| 2480 | dst_rect.position.y += dst_rect.size.height; |
| 2481 | dst_rect.size.height *= -1; |
| 2482 | } |
| 2483 | |
| 2484 | if (rect->flags & CANVAS_RECT_FLIP_H) { |
| 2485 | src_rect.size.x *= -1; |
| 2486 | } |
| 2487 | |
| 2488 | if (rect->flags & CANVAS_RECT_FLIP_V) { |
| 2489 | src_rect.size.y *= -1; |
| 2490 | } |
| 2491 | |
| 2492 | if (rect->flags & CANVAS_RECT_TRANSPOSE) { |
| 2493 | instance_data->flags |= INSTANCE_FLAGS_TRANSPOSE_RECT; |
| 2494 | } |
| 2495 | |
| 2496 | if (rect->flags & CANVAS_RECT_CLIP_UV) { |
| 2497 | instance_data->flags |= INSTANCE_FLAGS_CLIP_RECT_UV; |
| 2498 | } |
| 2499 | |
| 2500 | } else { |
| 2501 | dst_rect = Rect2(rect->rect.position, rect->rect.size); |
| 2502 | |
| 2503 | if (dst_rect.size.width < 0) { |
| 2504 | dst_rect.position.x += dst_rect.size.width; |
| 2505 | dst_rect.size.width *= -1; |
| 2506 | } |
| 2507 | if (dst_rect.size.height < 0) { |
| 2508 | dst_rect.position.y += dst_rect.size.height; |
| 2509 | dst_rect.size.height *= -1; |
| 2510 | } |
| 2511 | |
| 2512 | src_rect = Rect2(0, 0, 1, 1); |
| 2513 | } |
| 2514 | |
| 2515 | instance_data->modulation[0] = modulated.r; |
| 2516 | instance_data->modulation[1] = modulated.g; |
| 2517 | instance_data->modulation[2] = modulated.b; |
| 2518 | instance_data->modulation[3] = modulated.a; |
| 2519 | |
| 2520 | instance_data->src_rect[0] = src_rect.position.x; |
| 2521 | instance_data->src_rect[1] = src_rect.position.y; |
| 2522 | instance_data->src_rect[2] = src_rect.size.width; |
| 2523 | instance_data->src_rect[3] = src_rect.size.height; |
| 2524 | |
| 2525 | instance_data->dst_rect[0] = dst_rect.position.x; |
| 2526 | instance_data->dst_rect[1] = dst_rect.position.y; |
| 2527 | instance_data->dst_rect[2] = dst_rect.size.width; |
| 2528 | instance_data->dst_rect[3] = dst_rect.size.height; |
| 2529 | |
| 2530 | _add_to_batch(r_batch_broken, r_current_batch); |
| 2531 | } break; |
| 2532 | |
| 2533 | case Item::Command::TYPE_NINEPATCH: { |
| 2534 | const Item::CommandNinePatch *np = static_cast<const Item::CommandNinePatch *>(c); |
| 2535 | |
| 2536 | if (r_current_batch->command_type != Item::Command::TYPE_NINEPATCH) { |
| 2537 | r_current_batch = _new_batch(r_batch_broken); |
| 2538 | r_current_batch->command_type = Item::Command::TYPE_NINEPATCH; |
| 2539 | r_current_batch->command = c; |
| 2540 | r_current_batch->has_blend = false; |
| 2541 | r_current_batch->shader_variant = SHADER_VARIANT_NINEPATCH; |
| 2542 | r_current_batch->render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES; |
| 2543 | r_current_batch->flags = 0; |
| 2544 | r_current_batch->use_msdf = false; |
| 2545 | r_current_batch->use_lcd = false; |
| 2546 | } |
| 2547 | |
| 2548 | TextureState tex_state(np->texture, texture_filter, texture_repeat, false, use_linear_colors); |
| 2549 | TextureInfo *tex_info = texture_info_map.getptr(tex_state); |
| 2550 | if (!tex_info) { |
| 2551 | tex_info = &texture_info_map.insert(tex_state, TextureInfo())->value; |
| 2552 | _prepare_batch_texture_info(np->texture, tex_state, tex_info); |
| 2553 | } |
| 2554 | |
| 2555 | if (r_current_batch->tex_info != tex_info) { |
| 2556 | r_current_batch = _new_batch(r_batch_broken); |
| 2557 | r_current_batch->tex_info = tex_info; |
| 2558 | } |
| 2559 | |
| 2560 | InstanceData *instance_data = new_instance_data(*r_current_batch, template_instance); |
| 2561 | |
| 2562 | Rect2 src_rect; |
| 2563 | Rect2 dst_rect(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y); |
| 2564 | |
| 2565 | if (np->texture.is_valid() && np->source != Rect2()) { |
| 2566 | src_rect = Rect2(np->source.position.x * tex_info->texpixel_size.width, np->source.position.y * tex_info->texpixel_size.height, np->source.size.x * tex_info->texpixel_size.width, np->source.size.y * tex_info->texpixel_size.height); |
| 2567 | instance_data->ninepatch_pixel_size[0] = 1.0 / np->source.size.width; |
| 2568 | instance_data->ninepatch_pixel_size[1] = 1.0 / np->source.size.height; |
| 2569 | } else { |
| 2570 | src_rect = Rect2(0, 0, 1, 1); |
| 2571 | // Set the default ninepatch pixel size to the full texture size. |
| 2572 | instance_data->ninepatch_pixel_size[0] = tex_info->texpixel_size.width; |
| 2573 | instance_data->ninepatch_pixel_size[1] = tex_info->texpixel_size.height; |
| 2574 | } |
| 2575 | |
| 2576 | Color modulated = np->color * base_color; |
| 2577 | if (use_linear_colors) { |
| 2578 | modulated = modulated.srgb_to_linear(); |
| 2579 | } |
| 2580 | |
| 2581 | instance_data->modulation[0] = modulated.r; |
| 2582 | instance_data->modulation[1] = modulated.g; |
| 2583 | instance_data->modulation[2] = modulated.b; |
| 2584 | instance_data->modulation[3] = modulated.a; |
| 2585 | |
| 2586 | instance_data->src_rect[0] = src_rect.position.x; |
| 2587 | instance_data->src_rect[1] = src_rect.position.y; |
| 2588 | instance_data->src_rect[2] = src_rect.size.width; |
| 2589 | instance_data->src_rect[3] = src_rect.size.height; |
| 2590 | |
| 2591 | instance_data->dst_rect[0] = dst_rect.position.x; |
| 2592 | instance_data->dst_rect[1] = dst_rect.position.y; |
| 2593 | instance_data->dst_rect[2] = dst_rect.size.width; |
| 2594 | instance_data->dst_rect[3] = dst_rect.size.height; |
| 2595 | |
| 2596 | instance_data->flags |= int(np->axis_x) << INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT; |
| 2597 | instance_data->flags |= int(np->axis_y) << INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT; |
| 2598 | |
| 2599 | if (np->draw_center) { |
| 2600 | instance_data->flags |= INSTANCE_FLAGS_NINEPACH_DRAW_CENTER; |
| 2601 | } |
| 2602 | |
| 2603 | instance_data->ninepatch_margins[0] = np->margin[SIDE_LEFT]; |
| 2604 | instance_data->ninepatch_margins[1] = np->margin[SIDE_TOP]; |
| 2605 | instance_data->ninepatch_margins[2] = np->margin[SIDE_RIGHT]; |
| 2606 | instance_data->ninepatch_margins[3] = np->margin[SIDE_BOTTOM]; |
| 2607 | |
| 2608 | _add_to_batch(r_batch_broken, r_current_batch); |
| 2609 | } break; |
| 2610 | |
| 2611 | case Item::Command::TYPE_POLYGON: { |
| 2612 | const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c); |
| 2613 | |
| 2614 | // Polygon's can't be batched, so always create a new batch |
| 2615 | r_current_batch = _new_batch(r_batch_broken); |
| 2616 | |
| 2617 | r_current_batch->command_type = Item::Command::TYPE_POLYGON; |
| 2618 | r_current_batch->has_blend = false; |
| 2619 | r_current_batch->command = c; |
| 2620 | r_current_batch->flags = 0; |
| 2621 | r_current_batch->use_msdf = false; |
| 2622 | r_current_batch->use_lcd = false; |
| 2623 | |
| 2624 | TextureState tex_state(polygon->texture, texture_filter, texture_repeat, false, use_linear_colors); |
| 2625 | TextureInfo *tex_info = texture_info_map.getptr(tex_state); |
| 2626 | if (!tex_info) { |
| 2627 | tex_info = &texture_info_map.insert(tex_state, TextureInfo())->value; |
| 2628 | _prepare_batch_texture_info(polygon->texture, tex_state, tex_info); |
| 2629 | } |
| 2630 | |
| 2631 | if (r_current_batch->tex_info != tex_info) { |
| 2632 | r_current_batch = _new_batch(r_batch_broken); |
| 2633 | r_current_batch->tex_info = tex_info; |
| 2634 | } |
| 2635 | |
| 2636 | // pipeline variant |
| 2637 | { |
| 2638 | ERR_CONTINUE(polygon->primitive < 0 || polygon->primitive >= RS::PRIMITIVE_MAX); |
| 2639 | r_current_batch->shader_variant = polygon->primitive == RS::PRIMITIVE_POINTS ? SHADER_VARIANT_ATTRIBUTES_POINTS : SHADER_VARIANT_ATTRIBUTES; |
| 2640 | r_current_batch->render_primitive = _primitive_type_to_render_primitive(polygon->primitive); |
| 2641 | } |
| 2642 | |
| 2643 | InstanceData *instance_data = new_instance_data(*r_current_batch, template_instance, true); |
| 2644 | |
| 2645 | Color color = base_color; |
| 2646 | if (use_linear_colors) { |
| 2647 | color = color.srgb_to_linear(); |
| 2648 | } |
| 2649 | |
| 2650 | instance_data->modulation[0] = color.r; |
| 2651 | instance_data->modulation[1] = color.g; |
| 2652 | instance_data->modulation[2] = color.b; |
| 2653 | instance_data->modulation[3] = color.a; |
| 2654 | } break; |
| 2655 | |
| 2656 | case Item::Command::TYPE_PRIMITIVE: { |
| 2657 | const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c); |
| 2658 | |
| 2659 | if (primitive->point_count != r_current_batch->primitive_points || r_current_batch->command_type != Item::Command::TYPE_PRIMITIVE) { |
| 2660 | r_current_batch = _new_batch(r_batch_broken); |
| 2661 | r_current_batch->command_type = Item::Command::TYPE_PRIMITIVE; |
| 2662 | r_current_batch->has_blend = false; |
| 2663 | r_current_batch->command = c; |
| 2664 | r_current_batch->primitive_points = primitive->point_count; |
| 2665 | r_current_batch->flags = 0; |
| 2666 | |
| 2667 | ERR_CONTINUE(primitive->point_count == 0 || primitive->point_count > 4); |
| 2668 | |
| 2669 | switch (primitive->point_count) { |
| 2670 | case 1: |
| 2671 | r_current_batch->shader_variant = SHADER_VARIANT_PRIMITIVE_POINTS; |
| 2672 | r_current_batch->render_primitive = RD::RENDER_PRIMITIVE_POINTS; |
| 2673 | break; |
| 2674 | case 2: |
| 2675 | r_current_batch->shader_variant = SHADER_VARIANT_PRIMITIVE; |
| 2676 | r_current_batch->render_primitive = RD::RENDER_PRIMITIVE_LINES; |
| 2677 | break; |
| 2678 | case 3: |
| 2679 | case 4: |
| 2680 | r_current_batch->shader_variant = SHADER_VARIANT_PRIMITIVE; |
| 2681 | r_current_batch->render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES; |
| 2682 | break; |
| 2683 | default: |
| 2684 | // Unknown point count. |
| 2685 | break; |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | TextureState tex_state(primitive->texture, texture_filter, texture_repeat, false, use_linear_colors); |
| 2690 | TextureInfo *tex_info = texture_info_map.getptr(tex_state); |
| 2691 | if (!tex_info) { |
| 2692 | tex_info = &texture_info_map.insert(tex_state, TextureInfo())->value; |
| 2693 | _prepare_batch_texture_info(primitive->texture, tex_state, tex_info); |
| 2694 | } |
| 2695 | |
| 2696 | if (r_current_batch->tex_info != tex_info) { |
| 2697 | r_current_batch = _new_batch(r_batch_broken); |
| 2698 | r_current_batch->tex_info = tex_info; |
| 2699 | } |
| 2700 | |
| 2701 | InstanceData *instance_data = new_instance_data(*r_current_batch, template_instance); |
| 2702 | |
| 2703 | for (uint32_t j = 0; j < MIN(3u, primitive->point_count); j++) { |
| 2704 | instance_data->points[j * 2 + 0] = primitive->points[j].x; |
| 2705 | instance_data->points[j * 2 + 1] = primitive->points[j].y; |
| 2706 | instance_data->uvs[j * 2 + 0] = primitive->uvs[j].x; |
| 2707 | instance_data->uvs[j * 2 + 1] = primitive->uvs[j].y; |
| 2708 | Color col = primitive->colors[j] * base_color; |
| 2709 | if (use_linear_colors) { |
| 2710 | col = col.srgb_to_linear(); |
| 2711 | } |
| 2712 | instance_data->colors[j * 2 + 0] = (uint32_t(Math::make_half_float(col.g)) << 16) | Math::make_half_float(col.r); |
| 2713 | instance_data->colors[j * 2 + 1] = (uint32_t(Math::make_half_float(col.a)) << 16) | Math::make_half_float(col.b); |
| 2714 | } |
| 2715 | |
| 2716 | _add_to_batch(r_batch_broken, r_current_batch); |
| 2717 | |
| 2718 | if (primitive->point_count == 4) { |
| 2719 | instance_data = new_instance_data(*r_current_batch, template_instance); |
| 2720 | |
| 2721 | for (uint32_t j = 0; j < 3; j++) { |
| 2722 | int offset = j == 0 ? 0 : 1; |
| 2723 | // Second triangle in the quad. Uses vertices 0, 2, 3. |
| 2724 | instance_data->points[j * 2 + 0] = primitive->points[j + offset].x; |
| 2725 | instance_data->points[j * 2 + 1] = primitive->points[j + offset].y; |
| 2726 | instance_data->uvs[j * 2 + 0] = primitive->uvs[j + offset].x; |
| 2727 | instance_data->uvs[j * 2 + 1] = primitive->uvs[j + offset].y; |
| 2728 | Color col = primitive->colors[j + offset] * base_color; |
| 2729 | if (use_linear_colors) { |
| 2730 | col = col.srgb_to_linear(); |
| 2731 | } |
| 2732 | instance_data->colors[j * 2 + 0] = (uint32_t(Math::make_half_float(col.g)) << 16) | Math::make_half_float(col.r); |
| 2733 | instance_data->colors[j * 2 + 1] = (uint32_t(Math::make_half_float(col.a)) << 16) | Math::make_half_float(col.b); |
| 2734 | } |
| 2735 | |
| 2736 | _add_to_batch(r_batch_broken, r_current_batch); |
| 2737 | } |
| 2738 | } break; |
| 2739 | |
| 2740 | case Item::Command::TYPE_MESH: |
| 2741 | case Item::Command::TYPE_MULTIMESH: |
| 2742 | case Item::Command::TYPE_PARTICLES: { |
| 2743 | // Mesh's can't be batched, so always create a new batch |
| 2744 | r_current_batch = _new_batch(r_batch_broken); |
| 2745 | r_current_batch->command = c; |
| 2746 | r_current_batch->command_type = c->type; |
| 2747 | r_current_batch->has_blend = false; |
| 2748 | r_current_batch->flags = 0; |
| 2749 | r_current_batch->use_msdf = false; |
| 2750 | r_current_batch->use_lcd = false; |
| 2751 | |
| 2752 | InstanceData *instance_data = nullptr; |
| 2753 | |
| 2754 | Color modulate(1, 1, 1, 1); |
| 2755 | if (c->type == Item::Command::TYPE_MESH) { |
| 2756 | const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(c); |
| 2757 | TextureState tex_state(m->texture, texture_filter, texture_repeat, false, use_linear_colors); |
| 2758 | TextureInfo *tex_info = texture_info_map.getptr(tex_state); |
| 2759 | if (!tex_info) { |
| 2760 | tex_info = &texture_info_map.insert(tex_state, TextureInfo())->value; |
| 2761 | _prepare_batch_texture_info(m->texture, tex_state, tex_info); |
| 2762 | } |
| 2763 | r_current_batch->tex_info = tex_info; |
| 2764 | instance_data = new_instance_data(*r_current_batch, template_instance, true); |
| 2765 | |
| 2766 | r_current_batch->mesh_instance_count = 1; |
| 2767 | _update_transform_2d_to_mat2x3(base_transform * draw_transform * m->transform, instance_data->world); |
| 2768 | modulate = m->modulate; |
| 2769 | } else if (c->type == Item::Command::TYPE_MULTIMESH) { |
| 2770 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 2771 | |
| 2772 | const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(c); |
| 2773 | RID multimesh = mm->multimesh; |
| 2774 | |
| 2775 | if (mesh_storage->multimesh_get_transform_format(multimesh) != RS::MULTIMESH_TRANSFORM_2D) { |
| 2776 | break; |
| 2777 | } |
| 2778 | |
| 2779 | r_current_batch->mesh_instance_count = mesh_storage->multimesh_get_instances_to_draw(multimesh); |
| 2780 | if (r_current_batch->mesh_instance_count == 0) { |
| 2781 | break; |
| 2782 | } |
| 2783 | |
| 2784 | TextureState tex_state(mm->texture, texture_filter, texture_repeat, false, use_linear_colors); |
| 2785 | TextureInfo *tex_info = texture_info_map.getptr(tex_state); |
| 2786 | if (!tex_info) { |
| 2787 | tex_info = &texture_info_map.insert(tex_state, TextureInfo())->value; |
| 2788 | _prepare_batch_texture_info(mm->texture, tex_state, tex_info); |
| 2789 | } |
| 2790 | r_current_batch->tex_info = tex_info; |
| 2791 | instance_data = new_instance_data(*r_current_batch, template_instance, true); |
| 2792 | |
| 2793 | r_current_batch->flags |= 1; // multimesh, trails disabled |
| 2794 | |
| 2795 | if (mesh_storage->multimesh_uses_colors(mm->multimesh)) { |
| 2796 | r_current_batch->flags |= BATCH_FLAGS_INSTANCING_HAS_COLORS; |
| 2797 | } |
| 2798 | if (mesh_storage->multimesh_uses_custom_data(mm->multimesh)) { |
| 2799 | r_current_batch->flags |= BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA; |
| 2800 | } |
| 2801 | } else if (c->type == Item::Command::TYPE_PARTICLES) { |
| 2802 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 2803 | RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton(); |
| 2804 | |
| 2805 | const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(c); |
| 2806 | TextureState tex_state(pt->texture, texture_filter, texture_repeat, false, use_linear_colors); |
| 2807 | TextureInfo *tex_info = texture_info_map.getptr(tex_state); |
| 2808 | if (!tex_info) { |
| 2809 | tex_info = &texture_info_map.insert(tex_state, TextureInfo())->value; |
| 2810 | _prepare_batch_texture_info(pt->texture, tex_state, tex_info); |
| 2811 | } |
| 2812 | r_current_batch->tex_info = tex_info; |
| 2813 | instance_data = new_instance_data(*r_current_batch, template_instance, true); |
| 2814 | |
| 2815 | uint32_t divisor = 1; |
| 2816 | r_current_batch->mesh_instance_count = particles_storage->particles_get_amount(pt->particles, divisor); |
| 2817 | r_current_batch->flags |= (divisor & BATCH_FLAGS_INSTANCING_MASK); |
| 2818 | r_current_batch->mesh_instance_count /= divisor; |
| 2819 | |
| 2820 | RID particles = pt->particles; |
| 2821 | |
| 2822 | r_current_batch->flags |= BATCH_FLAGS_INSTANCING_HAS_COLORS; |
| 2823 | r_current_batch->flags |= BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA; |
| 2824 | |
| 2825 | if (particles_storage->particles_has_collision(particles) && texture_storage->render_target_is_sdf_enabled(p_render_target.render_target)) { |
| 2826 | // Pass collision information. |
| 2827 | Transform2D xform = p_item->final_transform; |
| 2828 | |
| 2829 | RID sdf_texture = texture_storage->render_target_get_sdf_texture(p_render_target.render_target); |
| 2830 | |
| 2831 | Rect2 to_screen; |
| 2832 | { |
| 2833 | Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_render_target.render_target); |
| 2834 | |
| 2835 | to_screen.size = Vector2(1.0 / sdf_rect.size.width, 1.0 / sdf_rect.size.height); |
| 2836 | to_screen.position = -sdf_rect.position * to_screen.size; |
| 2837 | } |
| 2838 | |
| 2839 | particles_storage->particles_set_canvas_sdf_collision(pt->particles, true, xform, to_screen, sdf_texture); |
| 2840 | } else { |
| 2841 | particles_storage->particles_set_canvas_sdf_collision(pt->particles, false, Transform2D(), Rect2(), RID()); |
| 2842 | } |
| 2843 | r_sdf_used |= particles_storage->particles_has_collision(particles); |
| 2844 | } |
| 2845 | |
| 2846 | Color modulated = modulate * base_color; |
| 2847 | if (use_linear_colors) { |
| 2848 | modulated = modulated.srgb_to_linear(); |
| 2849 | } |
| 2850 | |
| 2851 | instance_data->modulation[0] = modulated.r; |
| 2852 | instance_data->modulation[1] = modulated.g; |
| 2853 | instance_data->modulation[2] = modulated.b; |
| 2854 | instance_data->modulation[3] = modulated.a; |
| 2855 | } break; |
| 2856 | |
| 2857 | case Item::Command::TYPE_TRANSFORM: { |
| 2858 | const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c); |
| 2859 | draw_transform = transform->xform; |
| 2860 | _update_transform_2d_to_mat2x3(base_transform * transform->xform, template_instance.world); |
| 2861 | } break; |
| 2862 | |
| 2863 | case Item::Command::TYPE_CLIP_IGNORE: { |
| 2864 | const Item::CommandClipIgnore *ci = static_cast<const Item::CommandClipIgnore *>(c); |
| 2865 | if (r_current_clip) { |
| 2866 | if (ci->ignore != reclip) { |
| 2867 | r_current_batch = _new_batch(r_batch_broken); |
| 2868 | if (ci->ignore) { |
| 2869 | r_current_batch->clip = nullptr; |
| 2870 | reclip = true; |
| 2871 | } else { |
| 2872 | r_current_batch->clip = r_current_clip; |
| 2873 | reclip = false; |
| 2874 | } |
| 2875 | } |
| 2876 | } |
| 2877 | } break; |
| 2878 | |
| 2879 | case Item::Command::TYPE_ANIMATION_SLICE: { |
| 2880 | const Item::CommandAnimationSlice *as = static_cast<const Item::CommandAnimationSlice *>(c); |
| 2881 | double current_time = RSG::rasterizer->get_total_time(); |
| 2882 | double local_time = Math::fposmod(current_time - as->offset, as->animation_length); |
| 2883 | skipping = !(local_time >= as->slice_begin && local_time < as->slice_end); |
| 2884 | |
| 2885 | RenderingServerDefault::redraw_request(); // animation visible means redraw request |
| 2886 | } break; |
| 2887 | } |
| 2888 | |
| 2889 | c = c->next; |
| 2890 | r_batch_broken = false; |
| 2891 | } |
| 2892 | |
| 2893 | #ifdef DEBUG_ENABLED |
| 2894 | if (debug_redraw && p_item->debug_redraw_time > 0.0) { |
| 2895 | Color dc = debug_redraw_color; |
| 2896 | dc.a *= p_item->debug_redraw_time / debug_redraw_time; |
| 2897 | |
| 2898 | // 1: If commands are different, start a new batch. |
| 2899 | if (r_current_batch->command_type != Item::Command::TYPE_RECT) { |
| 2900 | r_current_batch = _new_batch(r_batch_broken); |
| 2901 | r_current_batch->command_type = Item::Command::TYPE_RECT; |
| 2902 | // it is ok to be null for a TYPE_RECT |
| 2903 | r_current_batch->command = nullptr; |
| 2904 | // default variant |
| 2905 | r_current_batch->shader_variant = SHADER_VARIANT_QUAD; |
| 2906 | r_current_batch->render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES; |
| 2907 | r_current_batch->flags = 0; |
| 2908 | } |
| 2909 | |
| 2910 | // 2: If the current batch has lighting, start a new batch. |
| 2911 | if (r_current_batch->use_lighting) { |
| 2912 | r_current_batch = _new_batch(r_batch_broken); |
| 2913 | r_current_batch->use_lighting = false; |
| 2914 | } |
| 2915 | |
| 2916 | // 3: If the current batch has blend, start a new batch. |
| 2917 | if (r_current_batch->has_blend) { |
| 2918 | r_current_batch = _new_batch(r_batch_broken); |
| 2919 | r_current_batch->has_blend = false; |
| 2920 | } |
| 2921 | |
| 2922 | TextureState tex_state(default_canvas_texture, texture_filter, texture_repeat, false, use_linear_colors); |
| 2923 | TextureInfo *tex_info = texture_info_map.getptr(tex_state); |
| 2924 | if (!tex_info) { |
| 2925 | tex_info = &texture_info_map.insert(tex_state, TextureInfo())->value; |
| 2926 | _prepare_batch_texture_info(default_canvas_texture, tex_state, tex_info); |
| 2927 | } |
| 2928 | |
| 2929 | if (r_current_batch->tex_info != tex_info) { |
| 2930 | r_current_batch = _new_batch(r_batch_broken); |
| 2931 | r_current_batch->tex_info = tex_info; |
| 2932 | } |
| 2933 | |
| 2934 | _update_transform_2d_to_mat2x3(base_transform, template_instance.world); |
| 2935 | InstanceData *instance_data = new_instance_data(*r_current_batch, template_instance); |
| 2936 | |
| 2937 | Rect2 src_rect; |
| 2938 | Rect2 dst_rect; |
| 2939 | |
| 2940 | dst_rect = p_item->rect; |
| 2941 | if (dst_rect.size.width < 0) { |
| 2942 | dst_rect.position.x += dst_rect.size.width; |
| 2943 | dst_rect.size.width *= -1; |
| 2944 | } |
| 2945 | if (dst_rect.size.height < 0) { |
| 2946 | dst_rect.position.y += dst_rect.size.height; |
| 2947 | dst_rect.size.height *= -1; |
| 2948 | } |
| 2949 | |
| 2950 | src_rect = Rect2(0, 0, 1, 1); |
| 2951 | |
| 2952 | instance_data->modulation[0] = dc.r; |
| 2953 | instance_data->modulation[1] = dc.g; |
| 2954 | instance_data->modulation[2] = dc.b; |
| 2955 | instance_data->modulation[3] = dc.a; |
| 2956 | |
| 2957 | instance_data->src_rect[0] = src_rect.position.x; |
| 2958 | instance_data->src_rect[1] = src_rect.position.y; |
| 2959 | instance_data->src_rect[2] = src_rect.size.width; |
| 2960 | instance_data->src_rect[3] = src_rect.size.height; |
| 2961 | |
| 2962 | instance_data->dst_rect[0] = dst_rect.position.x; |
| 2963 | instance_data->dst_rect[1] = dst_rect.position.y; |
| 2964 | instance_data->dst_rect[2] = dst_rect.size.width; |
| 2965 | instance_data->dst_rect[3] = dst_rect.size.height; |
| 2966 | |
| 2967 | _add_to_batch(r_batch_broken, r_current_batch); |
| 2968 | |
| 2969 | p_item->debug_redraw_time -= RSG::rasterizer->get_frame_delta_time(); |
| 2970 | |
| 2971 | RenderingServerDefault::redraw_request(); |
| 2972 | |
| 2973 | r_batch_broken = false; |
| 2974 | } |
| 2975 | #endif |
| 2976 | |
| 2977 | if (r_current_clip && reclip) { |
| 2978 | // will make it re-enable clipping if needed afterwards |
| 2979 | r_current_clip = nullptr; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | void RendererCanvasRenderRD::_before_evict(RendererCanvasRenderRD::RIDSetKey &p_key, RID &p_rid) { |
| 2984 | RD::get_singleton()->uniform_set_set_invalidation_callback(p_rid, nullptr, nullptr); |
| 2985 | RD::get_singleton()->free_rid(p_rid); |
| 2986 | } |
| 2987 | |
| 2988 | void RendererCanvasRenderRD::_uniform_set_invalidation_callback(void *p_userdata) { |
| 2989 | const RIDSetKey *key = static_cast<RIDSetKey *>(p_userdata); |
| 2990 | static_cast<RendererCanvasRenderRD *>(singleton)->rid_set_to_uniform_set.erase(*key); |
| 2991 | } |
| 2992 | |
| 2993 | void RendererCanvasRenderRD::_canvas_texture_invalidation_callback(bool p_deleted, void *p_userdata) { |
| 2994 | KeyValue<RID, TightLocalVector<RID>> *kv = static_cast<KeyValue<RID, TightLocalVector<RID>> *>(p_userdata); |
| 2995 | RD *rd = RD::get_singleton(); |
| 2996 | for (RID rid : kv->value) { |
| 2997 | // The invalidation callback will also take care of clearing rid_set_to_uniform_set cache. |
| 2998 | rd->free_rid(rid); |
| 2999 | } |
| 3000 | kv->value.clear(); |
| 3001 | if (p_deleted) { |
| 3002 | static_cast<RendererCanvasRenderRD *>(singleton)->canvas_texture_to_uniform_set.erase(kv->key); |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | void RendererCanvasRenderRD::_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) { |
| 3007 | { |
| 3008 | RendererRD::TextureStorage *ts = RendererRD::TextureStorage::get_singleton(); |
| 3009 | |
| 3010 | RIDSetKey key(p_batch->tex_info->state); |
| 3011 | |
| 3012 | const RID *uniform_set = rid_set_to_uniform_set.getptr(key); |
| 3013 | if (uniform_set == nullptr) { |
| 3014 | RD::Uniform *uniform_ptrw = state.batch_texture_uniforms.ptrw(); |
| 3015 | uniform_ptrw[0] = RD::Uniform(RD::UNIFORM_TYPE_TEXTURE, 0, p_batch->tex_info->diffuse); |
| 3016 | uniform_ptrw[1] = RD::Uniform(RD::UNIFORM_TYPE_TEXTURE, 1, p_batch->tex_info->normal); |
| 3017 | uniform_ptrw[2] = RD::Uniform(RD::UNIFORM_TYPE_TEXTURE, 2, p_batch->tex_info->specular); |
| 3018 | uniform_ptrw[3] = RD::Uniform(RD::UNIFORM_TYPE_SAMPLER, 3, p_batch->tex_info->sampler); |
| 3019 | |
| 3020 | RID rid = RD::get_singleton()->uniform_set_create(state.batch_texture_uniforms, shader.default_version_rd_shader, BATCH_UNIFORM_SET); |
| 3021 | ERR_FAIL_COND_MSG(rid.is_null(), "Failed to create uniform set for batch."); |
| 3022 | |
| 3023 | const RIDCache::Pair *iter = rid_set_to_uniform_set.insert(key, rid); |
| 3024 | uniform_set = &iter->data; |
| 3025 | RD::get_singleton()->uniform_set_set_invalidation_callback(rid, RendererCanvasRenderRD::_uniform_set_invalidation_callback, (void *)&iter->key); |
| 3026 | |
| 3027 | // If this is a CanvasTexture, it must be tracked so that any changes to the diffuse, normal, |
| 3028 | // or specular channels invalidate all associated uniform sets. |
| 3029 | if (ts->owns_canvas_texture(p_batch->tex_info->state.texture)) { |
| 3030 | KeyValue<RID, TightLocalVector<RID>> *kv = nullptr; |
| 3031 | if (HashMap<RID, TightLocalVector<RID>>::Iterator i = canvas_texture_to_uniform_set.find(p_batch->tex_info->state.texture); i == canvas_texture_to_uniform_set.end()) { |
| 3032 | kv = &*canvas_texture_to_uniform_set.insert(p_batch->tex_info->state.texture, { *uniform_set }); |
| 3033 | } else { |
| 3034 | i->value.push_back(rid); |
| 3035 | kv = &*i; |
| 3036 | } |
| 3037 | ts->canvas_texture_set_invalidation_callback(p_batch->tex_info->state.texture, RendererCanvasRenderRD::_canvas_texture_invalidation_callback, kv); |
| 3038 | } |
| 3039 | } |
| 3040 | |
| 3041 | if (state.current_batch_uniform_set != *uniform_set) { |
| 3042 | state.current_batch_uniform_set = *uniform_set; |
| 3043 | RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, *uniform_set, BATCH_UNIFORM_SET); |
| 3044 | } |
| 3045 | } |
| 3046 | |
| 3047 | RID pipeline; |
| 3048 | PipelineKey pipeline_key; |
| 3049 | pipeline_key.framebuffer_format_id = p_framebuffer_format; |
| 3050 | pipeline_key.variant = p_batch->shader_variant; |
| 3051 | pipeline_key.render_primitive = p_batch->render_primitive; |
| 3052 | pipeline_key.shader_specialization.use_lighting = p_batch->use_lighting; |
| 3053 | pipeline_key.shader_specialization.use_msdf = p_batch->use_msdf; |
| 3054 | pipeline_key.shader_specialization.use_lcd = p_batch->use_lcd; |
| 3055 | pipeline_key.lcd_blend = p_batch->has_blend; |
| 3056 | |
| 3057 | switch (p_batch->command_type) { |
| 3058 | case Item::Command::TYPE_RECT: |
| 3059 | case Item::Command::TYPE_NINEPATCH: { |
| 3060 | PushConstant push_constant = p_batch->push_constant(); |
| 3061 | |
| 3062 | pipeline_key.vertex_format_id = shader.quad_vertex_format_id; |
| 3063 | pipeline = _get_pipeline_specialization_or_ubershader(p_shader_data, pipeline_key, push_constant); |
| 3064 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 3065 | if (p_batch->has_blend) { |
| 3066 | RD::get_singleton()->draw_list_set_blend_constants(p_draw_list, p_batch->modulate); |
| 3067 | } |
| 3068 | |
| 3069 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(push_constant)); |
| 3070 | FixedVector<RID, 1> vb = { p_batch->instance_buffer }; |
| 3071 | FixedVector<uint64_t, 1> vo = { uint64_t(p_batch->start) * sizeof(InstanceData) }; |
| 3072 | RD::get_singleton()->draw_list_bind_vertex_buffers_format(p_draw_list, shader.quad_vertex_format_id, 1, vb, vo); |
| 3073 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, shader.quad_index_array); |
| 3074 | RD::get_singleton()->draw_list_draw(p_draw_list, true, p_batch->instance_count); |
| 3075 | |
| 3076 | if (r_render_info) { |
| 3077 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] += p_batch->instance_count; |
| 3078 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += 2 * p_batch->instance_count; |
| 3079 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME]++; |
| 3080 | } |
| 3081 | } break; |
| 3082 | |
| 3083 | case Item::Command::TYPE_POLYGON: { |
| 3084 | ERR_FAIL_NULL(p_batch->command); |
| 3085 | PushConstantAttributes push_constant = p_batch->push_constant_attributes(); |
| 3086 | |
| 3087 | const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(p_batch->command); |
| 3088 | |
| 3089 | PolygonBuffers *pb = polygon_buffers.polygons.getptr(polygon->polygon.polygon_id); |
| 3090 | ERR_FAIL_NULL(pb); |
| 3091 | |
| 3092 | pipeline_key.vertex_format_id = pb->vertex_format_id; |
| 3093 | pipeline = _get_pipeline_specialization_or_ubershader(p_shader_data, pipeline_key, push_constant); |
| 3094 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 3095 | |
| 3096 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(push_constant)); |
| 3097 | RD::get_singleton()->draw_list_bind_vertex_array(p_draw_list, pb->vertex_array); |
| 3098 | if (pb->indices.is_valid()) { |
| 3099 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, pb->indices); |
| 3100 | } |
| 3101 | |
| 3102 | RD::get_singleton()->draw_list_draw(p_draw_list, pb->indices.is_valid()); |
| 3103 | if (r_render_info) { |
| 3104 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME]++; |
| 3105 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += _indices_to_primitives(polygon->primitive, pb->primitive_count); |
| 3106 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME]++; |
| 3107 | } |
| 3108 | } break; |
| 3109 | |
| 3110 | case Item::Command::TYPE_PRIMITIVE: { |
| 3111 | ERR_FAIL_NULL(p_batch->command); |
| 3112 | |
| 3113 | const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(p_batch->command); |
| 3114 | |
| 3115 | PushConstant push_constant = p_batch->push_constant(); |
| 3116 | pipeline_key.vertex_format_id = shader.primitive_vertex_format_id; |
| 3117 | pipeline = _get_pipeline_specialization_or_ubershader(p_shader_data, pipeline_key, push_constant); |
| 3118 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 3119 | |
| 3120 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(push_constant)); |
| 3121 | FixedVector<RID, 1> vb = { p_batch->instance_buffer }; |
| 3122 | FixedVector<uint64_t, 1> vo = { uint64_t(p_batch->start) * sizeof(InstanceData) }; |
| 3123 | RD::get_singleton()->draw_list_bind_vertex_buffers_format(p_draw_list, shader.primitive_vertex_format_id, 1, vb, vo); |
| 3124 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, primitive_arrays.index_array[MIN(3u, primitive->point_count) - 1]); |
| 3125 | uint32_t instance_count = p_batch->instance_count; |
| 3126 | RD::get_singleton()->draw_list_draw(p_draw_list, true, instance_count); |
| 3127 | |
| 3128 | if (r_render_info) { |
| 3129 | const RenderingServer::PrimitiveType rs_primitive[5] = { RS::PRIMITIVE_POINTS, RS::PRIMITIVE_POINTS, RS::PRIMITIVE_LINES, RS::PRIMITIVE_TRIANGLES, RS::PRIMITIVE_TRIANGLES }; |
| 3130 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] += instance_count; |
| 3131 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += _indices_to_primitives(rs_primitive[p_batch->primitive_points], p_batch->primitive_points) * instance_count; |
| 3132 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME]++; |
| 3133 | } |
| 3134 | } break; |
| 3135 | |
| 3136 | case Item::Command::TYPE_MESH: |
| 3137 | case Item::Command::TYPE_MULTIMESH: |
| 3138 | case Item::Command::TYPE_PARTICLES: { |
| 3139 | ERR_FAIL_NULL(p_batch->command); |
| 3140 | |
| 3141 | PushConstantAttributes push_constant = p_batch->push_constant_attributes(); |
| 3142 | |
| 3143 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 3144 | RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton(); |
| 3145 | |
| 3146 | RID mesh; |
| 3147 | RID mesh_instance; |
| 3148 | |
| 3149 | if (p_batch->command_type == Item::Command::TYPE_MESH) { |
| 3150 | const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(p_batch->command); |
| 3151 | mesh = m->mesh; |
| 3152 | mesh_instance = m->mesh_instance; |
| 3153 | } else if (p_batch->command_type == Item::Command::TYPE_MULTIMESH) { |
| 3154 | const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(p_batch->command); |
| 3155 | RID multimesh = mm->multimesh; |
| 3156 | mesh = mesh_storage->multimesh_get_mesh(multimesh); |
| 3157 | |
| 3158 | RID uniform_set = mesh_storage->multimesh_get_2d_uniform_set(multimesh, shader.default_version_rd_shader, TRANSFORMS_UNIFORM_SET); |
| 3159 | RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set, TRANSFORMS_UNIFORM_SET); |
| 3160 | } else if (p_batch->command_type == Item::Command::TYPE_PARTICLES) { |
| 3161 | const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(p_batch->command); |
| 3162 | RID particles = pt->particles; |
| 3163 | mesh = particles_storage->particles_get_draw_pass_mesh(particles, 0); |
| 3164 | |
| 3165 | ERR_BREAK(particles_storage->particles_get_mode(particles) != RS::PARTICLES_MODE_2D); |
| 3166 | particles_storage->particles_request_process(particles); |
| 3167 | |
| 3168 | if (particles_storage->particles_is_inactive(particles)) { |
| 3169 | break; |
| 3170 | } |
| 3171 | |
| 3172 | RenderingServerDefault::redraw_request(); // Active particles means redraw request. |
| 3173 | |
| 3174 | int dpc = particles_storage->particles_get_draw_passes(particles); |
| 3175 | if (dpc == 0) { |
| 3176 | break; // Nothing to draw. |
| 3177 | } |
| 3178 | |
| 3179 | RID uniform_set = particles_storage->particles_get_instance_buffer_uniform_set(pt->particles, shader.default_version_rd_shader, TRANSFORMS_UNIFORM_SET); |
| 3180 | RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set, TRANSFORMS_UNIFORM_SET); |
| 3181 | } |
| 3182 | |
| 3183 | if (mesh.is_null()) { |
| 3184 | break; |
| 3185 | } |
| 3186 | |
| 3187 | uint32_t surf_count = mesh_storage->mesh_get_surface_count(mesh); |
| 3188 | |
| 3189 | for (uint32_t j = 0; j < surf_count; j++) { |
| 3190 | void *surface = mesh_storage->mesh_get_surface(mesh, j); |
| 3191 | |
| 3192 | RS::PrimitiveType primitive = mesh_storage->mesh_surface_get_primitive(surface); |
| 3193 | ERR_CONTINUE(primitive < 0 || primitive >= RS::PRIMITIVE_MAX); |
| 3194 | |
| 3195 | RID vertex_array; |
| 3196 | pipeline_key.variant = primitive == RS::PRIMITIVE_POINTS ? SHADER_VARIANT_ATTRIBUTES_POINTS : SHADER_VARIANT_ATTRIBUTES; |
| 3197 | pipeline_key.render_primitive = _primitive_type_to_render_primitive(primitive); |
| 3198 | pipeline_key.vertex_format_id = RD::INVALID_FORMAT_ID; |
| 3199 | |
| 3200 | pipeline = _get_pipeline_specialization_or_ubershader(p_shader_data, pipeline_key, push_constant, mesh_instance, surface, j, &vertex_array); |
| 3201 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 3202 | |
| 3203 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(push_constant)); |
| 3204 | |
| 3205 | RID index_array = mesh_storage->mesh_surface_get_index_array(surface, 0); |
| 3206 | |
| 3207 | if (index_array.is_valid()) { |
| 3208 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, index_array); |
| 3209 | } |
| 3210 | |
| 3211 | RD::get_singleton()->draw_list_bind_vertex_array(p_draw_list, vertex_array); |
| 3212 | RD::get_singleton()->draw_list_draw(p_draw_list, index_array.is_valid(), p_batch->mesh_instance_count); |
| 3213 | |
| 3214 | if (r_render_info) { |
| 3215 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME]++; |
| 3216 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += _indices_to_primitives(primitive, mesh_storage->mesh_surface_get_vertices_drawn_count(surface)) * p_batch->mesh_instance_count; |
| 3217 | r_render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_CANVAS][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME]++; |
| 3218 | } |
| 3219 | } |
| 3220 | } break; |
| 3221 | case Item::Command::TYPE_TRANSFORM: |
| 3222 | case Item::Command::TYPE_CLIP_IGNORE: |
| 3223 | case Item::Command::TYPE_ANIMATION_SLICE: { |
| 3224 | // Can ignore these as they only impact batch creation. |
| 3225 | } break; |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | RendererCanvasRenderRD::InstanceData *RendererCanvasRenderRD::new_instance_data(Batch &p_current_batch, const InstanceData &template_instance, bool p_use_push_data) { |
| 3230 | InstanceData *instance_data = nullptr; |
| 3231 | |
| 3232 | if (unlikely(p_use_push_data)) { |
| 3233 | instance_data = &p_current_batch.push_data; |
| 3234 | // instance_count must be > 0 to indicate the batch has been used when calling _new_batch, so we set a flag. |
| 3235 | p_current_batch.instance_count = PUSH_DATA_INSTANCE_COUNT; |
| 3236 | } else { |
| 3237 | // Return the intermediary instance data to prevent the caller from accidentally reading write-combined memory pages, which has huge performance implications. |
| 3238 | instance_data = &state.intermediary_instance_data; |
| 3239 | } |
| 3240 | |
| 3241 | memcpy(instance_data, &template_instance, sizeof(InstanceData)); |
| 3242 | return instance_data; |
| 3243 | } |
| 3244 | |
| 3245 | RendererCanvasRenderRD::Batch *RendererCanvasRenderRD::_new_batch(bool &r_batch_broken) { |
| 3246 | if (state.canvas_instance_batches.is_empty()) { |
| 3247 | Batch new_batch; |
| 3248 | // First try to reuse previous instance buffer if possible. |
| 3249 | if (state.prev_instance_data && state.prev_instance_data_index < state.max_instances_per_buffer) { |
| 3250 | bool must_remap = state.instance_buffers.prepare_for_map(true); |
| 3251 | // must_remap will be false if we're preparing to map the buffer for the same frame and can reuse the existing UMA buffer. |
| 3252 | if (!must_remap) { |
| 3253 | state.instance_data = state.prev_instance_data; |
| 3254 | state.instance_data_index = state.prev_instance_data_index; |
| 3255 | } |
| 3256 | state.prev_instance_data = nullptr; |
| 3257 | state.prev_instance_data_index = 0; |
| 3258 | } |
| 3259 | // This will still be a valid point when multiple calls to _render_batch_items |
| 3260 | // are made in the same draw call. |
| 3261 | if (state.instance_data == nullptr) { |
| 3262 | // If there is no existing instance buffer, we must allocate a new one. |
| 3263 | _allocate_instance_buffer(); |
| 3264 | } else { |
| 3265 | // Otherwise, just use the existing one from where it last left off. |
| 3266 | new_batch.start = state.instance_data_index; |
| 3267 | } |
| 3268 | new_batch.instance_buffer = state.instance_buffers._get(0); |
| 3269 | state.canvas_instance_batches.push_back(new_batch); |
| 3270 | return state.canvas_instance_batches.ptr(); |
| 3271 | } |
| 3272 | |
| 3273 | if (r_batch_broken || state.canvas_instance_batches[state.current_batch_index].instance_count == 0) { |
| 3274 | return &state.canvas_instance_batches[state.current_batch_index]; |
| 3275 | } |
| 3276 | |
| 3277 | r_batch_broken = true; |
| 3278 | |
| 3279 | // Copy the properties of the current batch, we will manually update the things that changed. |
| 3280 | Batch new_batch = state.canvas_instance_batches[state.current_batch_index]; |
| 3281 | new_batch.instance_count = 0; |
| 3282 | new_batch.start = state.instance_data_index; |
| 3283 | memset(&new_batch.push_data, 0, sizeof(new_batch.push_data)); |
| 3284 | state.current_batch_index++; |
| 3285 | state.canvas_instance_batches.push_back(new_batch); |
| 3286 | return &state.canvas_instance_batches[state.current_batch_index]; |
| 3287 | } |
| 3288 | |
| 3289 | void RendererCanvasRenderRD::_add_to_batch(bool &r_batch_broken, Batch *&r_current_batch) { |
| 3290 | DEV_ASSERT(r_current_batch->command_type == Item::Command::TYPE_RECT || |
| 3291 | r_current_batch->command_type == Item::Command::TYPE_NINEPATCH || |
| 3292 | r_current_batch->command_type == Item::Command::TYPE_PRIMITIVE); |
| 3293 | r_current_batch->instance_count++; |
| 3294 | memcpy(&state.instance_data[state.instance_data_index], &state.intermediary_instance_data, sizeof(InstanceData)); |
| 3295 | state.instance_data_index++; |
| 3296 | if (state.instance_data_index >= state.max_instances_per_buffer) { |
| 3297 | RD::get_singleton()->buffer_flush(r_current_batch->instance_buffer); |
| 3298 | state.instance_data = nullptr; |
| 3299 | _allocate_instance_buffer(); |
| 3300 | state.instance_data_index = 0; |
| 3301 | r_batch_broken = false; // Force a new batch to be created |
| 3302 | r_current_batch = _new_batch(r_batch_broken); |
| 3303 | r_current_batch->instance_buffer = state.instance_buffers._get(0); |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | void RendererCanvasRenderRD::_allocate_instance_buffer() { |
| 3308 | state.instance_buffers.prepare_for_upload(); |
| 3309 | state.instance_data = reinterpret_cast<InstanceData *>(state.instance_buffers.map_raw_for_upload(0)); |
| 3310 | } |
| 3311 | |
| 3312 | void RendererCanvasRenderRD::_prepare_batch_texture_info(RID p_texture, TextureState &p_state, TextureInfo *p_info) { |
| 3313 | if (p_texture.is_null()) { |
| 3314 | p_texture = default_canvas_texture; |
| 3315 | } |
| 3316 | |
| 3317 | RendererRD::TextureStorage::CanvasTextureInfo info = |
| 3318 | RendererRD::TextureStorage::get_singleton()->canvas_texture_get_info( |
| 3319 | p_texture, |
| 3320 | p_state.texture_filter(), |
| 3321 | p_state.texture_repeat(), |
| 3322 | p_state.linear_colors(), |
| 3323 | p_state.texture_is_data()); |
| 3324 | // something odd happened |
| 3325 | if (info.is_null()) { |
| 3326 | _prepare_batch_texture_info(default_canvas_texture, p_state, p_info); |
| 3327 | return; |
| 3328 | } |
| 3329 | |
| 3330 | p_info->state = p_state; |
| 3331 | p_info->diffuse = info.diffuse; |
| 3332 | p_info->normal = info.normal; |
| 3333 | p_info->specular = info.specular; |
| 3334 | p_info->sampler = info.sampler; |
| 3335 | |
| 3336 | // cache values to be copied to instance data |
| 3337 | if (info.specular_color.a < 0.999) { |
| 3338 | p_info->flags |= BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED; |
| 3339 | } |
| 3340 | |
| 3341 | if (info.use_normal) { |
| 3342 | p_info->flags |= BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED; |
| 3343 | } |
| 3344 | |
| 3345 | uint8_t a = uint8_t(CLAMP(info.specular_color.a * 255.0, 0.0, 255.0)); |
| 3346 | uint8_t b = uint8_t(CLAMP(info.specular_color.b * 255.0, 0.0, 255.0)); |
| 3347 | uint8_t g = uint8_t(CLAMP(info.specular_color.g * 255.0, 0.0, 255.0)); |
| 3348 | uint8_t r = uint8_t(CLAMP(info.specular_color.r * 255.0, 0.0, 255.0)); |
| 3349 | p_info->specular_shininess = uint32_t(a) << 24 | uint32_t(b) << 16 | uint32_t(g) << 8 | uint32_t(r); |
| 3350 | |
| 3351 | p_info->texpixel_size = Vector2(1.0 / float(info.size.width), 1.0 / float(info.size.height)); |
| 3352 | } |
| 3353 | |
| 3354 | RendererCanvasRenderRD::~RendererCanvasRenderRD() { |
| 3355 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 3356 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 3357 | |
| 3358 | //canvas state |
| 3359 | |
| 3360 | material_storage->material_free(default_canvas_group_material); |
| 3361 | material_storage->shader_free(default_canvas_group_shader); |
| 3362 | |
| 3363 | material_storage->material_free(default_clip_children_material); |
| 3364 | material_storage->shader_free(default_clip_children_shader); |
| 3365 | |
| 3366 | { |
| 3367 | if (state.canvas_state_buffer.is_valid()) { |
| 3368 | RD::get_singleton()->free_rid(state.canvas_state_buffer); |
| 3369 | } |
| 3370 | |
| 3371 | memdelete_arr(state.light_uniforms); |
| 3372 | RD::get_singleton()->free_rid(state.lights_storage_buffer); |
| 3373 | } |
| 3374 | |
| 3375 | //shadow rendering |
| 3376 | { |
| 3377 | shadow_render.shader.version_free(shadow_render.shader_version); |
| 3378 | //this will also automatically clear all pipelines |
| 3379 | RD::get_singleton()->free_rid(state.shadow_sampler); |
| 3380 | } |
| 3381 | |
| 3382 | //buffers |
| 3383 | { |
| 3384 | RD::get_singleton()->free_rid(shader.quad_index_array); |
| 3385 | RD::get_singleton()->free_rid(shader.quad_index_buffer); |
| 3386 | //primitives are erase by dependency |
| 3387 | } |
| 3388 | |
| 3389 | if (state.shadow_fb.is_valid()) { |
| 3390 | RD::get_singleton()->free_rid(state.shadow_depth_texture); |
| 3391 | } |
| 3392 | RD::get_singleton()->free_rid(state.shadow_texture); |
| 3393 | |
| 3394 | if (state.shadow_occluder_buffer.is_valid()) { |
| 3395 | RD::get_singleton()->free_rid(state.shadow_occluder_buffer); |
| 3396 | } |
| 3397 | |
| 3398 | state.instance_buffers.uninit(); |
| 3399 | |
| 3400 | // Disable the callback, as we're tearing everything down |
| 3401 | texture_storage->canvas_texture_set_invalidation_callback(default_canvas_texture, nullptr, nullptr); |
| 3402 | texture_storage->canvas_texture_free(default_canvas_texture); |
| 3403 | //pipelines don't need freeing, they are all gone after shaders are gone |
| 3404 | |
| 3405 | memdelete(shader.default_version_data); |
| 3406 | } |