| 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 "renderer_compositor_rd.h" |
| 38 | #include "servers/rendering/renderer_rd/storage_rd/material_storage.h" |
| 39 | #include "servers/rendering/renderer_rd/storage_rd/particles_storage.h" |
| 40 | #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h" |
| 41 | #include "servers/rendering/rendering_server_default.h" |
| 42 | |
| 43 | void RendererCanvasRenderRD::_update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4) { |
| 44 | p_mat4[0] = p_transform.columns[0][0]; |
| 45 | p_mat4[1] = p_transform.columns[0][1]; |
| 46 | p_mat4[2] = 0; |
| 47 | p_mat4[3] = 0; |
| 48 | p_mat4[4] = p_transform.columns[1][0]; |
| 49 | p_mat4[5] = p_transform.columns[1][1]; |
| 50 | p_mat4[6] = 0; |
| 51 | p_mat4[7] = 0; |
| 52 | p_mat4[8] = 0; |
| 53 | p_mat4[9] = 0; |
| 54 | p_mat4[10] = 1; |
| 55 | p_mat4[11] = 0; |
| 56 | p_mat4[12] = p_transform.columns[2][0]; |
| 57 | p_mat4[13] = p_transform.columns[2][1]; |
| 58 | p_mat4[14] = 0; |
| 59 | p_mat4[15] = 1; |
| 60 | } |
| 61 | |
| 62 | void RendererCanvasRenderRD::_update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4) { |
| 63 | p_mat2x4[0] = p_transform.columns[0][0]; |
| 64 | p_mat2x4[1] = p_transform.columns[1][0]; |
| 65 | p_mat2x4[2] = 0; |
| 66 | p_mat2x4[3] = p_transform.columns[2][0]; |
| 67 | |
| 68 | p_mat2x4[4] = p_transform.columns[0][1]; |
| 69 | p_mat2x4[5] = p_transform.columns[1][1]; |
| 70 | p_mat2x4[6] = 0; |
| 71 | p_mat2x4[7] = p_transform.columns[2][1]; |
| 72 | } |
| 73 | |
| 74 | void RendererCanvasRenderRD::_update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3) { |
| 75 | p_mat2x3[0] = p_transform.columns[0][0]; |
| 76 | p_mat2x3[1] = p_transform.columns[0][1]; |
| 77 | p_mat2x3[2] = p_transform.columns[1][0]; |
| 78 | p_mat2x3[3] = p_transform.columns[1][1]; |
| 79 | p_mat2x3[4] = p_transform.columns[2][0]; |
| 80 | p_mat2x3[5] = p_transform.columns[2][1]; |
| 81 | } |
| 82 | |
| 83 | void RendererCanvasRenderRD::_update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4) { |
| 84 | p_mat4[0] = p_transform.basis.rows[0][0]; |
| 85 | p_mat4[1] = p_transform.basis.rows[1][0]; |
| 86 | p_mat4[2] = p_transform.basis.rows[2][0]; |
| 87 | p_mat4[3] = 0; |
| 88 | p_mat4[4] = p_transform.basis.rows[0][1]; |
| 89 | p_mat4[5] = p_transform.basis.rows[1][1]; |
| 90 | p_mat4[6] = p_transform.basis.rows[2][1]; |
| 91 | p_mat4[7] = 0; |
| 92 | p_mat4[8] = p_transform.basis.rows[0][2]; |
| 93 | p_mat4[9] = p_transform.basis.rows[1][2]; |
| 94 | p_mat4[10] = p_transform.basis.rows[2][2]; |
| 95 | p_mat4[11] = 0; |
| 96 | p_mat4[12] = p_transform.origin.x; |
| 97 | p_mat4[13] = p_transform.origin.y; |
| 98 | p_mat4[14] = p_transform.origin.z; |
| 99 | p_mat4[15] = 1; |
| 100 | } |
| 101 | |
| 102 | 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) { |
| 103 | // Care must be taken to generate array formats |
| 104 | // in ways where they could be reused, so we will |
| 105 | // put single-occuring elements first, and repeated |
| 106 | // elements later. This way the generated formats are |
| 107 | // the same no matter the length of the arrays. |
| 108 | // This dramatically reduces the amount of pipeline objects |
| 109 | // that need to be created for these formats. |
| 110 | |
| 111 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 112 | |
| 113 | uint32_t vertex_count = p_points.size(); |
| 114 | uint32_t stride = 2; //vertices always repeat |
| 115 | if ((uint32_t)p_colors.size() == vertex_count || p_colors.size() == 1) { |
| 116 | stride += 4; |
| 117 | } |
| 118 | if ((uint32_t)p_uvs.size() == vertex_count) { |
| 119 | stride += 2; |
| 120 | } |
| 121 | if ((uint32_t)p_bones.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) { |
| 122 | stride += 4; |
| 123 | } |
| 124 | |
| 125 | uint32_t buffer_size = stride * p_points.size(); |
| 126 | |
| 127 | Vector<uint8_t> polygon_buffer; |
| 128 | polygon_buffer.resize(buffer_size * sizeof(float)); |
| 129 | Vector<RD::VertexAttribute> descriptions; |
| 130 | descriptions.resize(5); |
| 131 | Vector<RID> buffers; |
| 132 | buffers.resize(5); |
| 133 | |
| 134 | { |
| 135 | uint8_t *r = polygon_buffer.ptrw(); |
| 136 | float *fptr = reinterpret_cast<float *>(r); |
| 137 | uint32_t *uptr = reinterpret_cast<uint32_t *>(r); |
| 138 | uint32_t base_offset = 0; |
| 139 | { //vertices |
| 140 | RD::VertexAttribute vd; |
| 141 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 142 | vd.offset = base_offset * sizeof(float); |
| 143 | vd.location = RS::ARRAY_VERTEX; |
| 144 | vd.stride = stride * sizeof(float); |
| 145 | |
| 146 | descriptions.write[0] = vd; |
| 147 | |
| 148 | const Vector2 *points_ptr = p_points.ptr(); |
| 149 | |
| 150 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 151 | fptr[base_offset + i * stride + 0] = points_ptr[i].x; |
| 152 | fptr[base_offset + i * stride + 1] = points_ptr[i].y; |
| 153 | } |
| 154 | |
| 155 | base_offset += 2; |
| 156 | } |
| 157 | |
| 158 | //colors |
| 159 | if ((uint32_t)p_colors.size() == vertex_count || p_colors.size() == 1) { |
| 160 | RD::VertexAttribute vd; |
| 161 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 162 | vd.offset = base_offset * sizeof(float); |
| 163 | vd.location = RS::ARRAY_COLOR; |
| 164 | vd.stride = stride * sizeof(float); |
| 165 | |
| 166 | descriptions.write[1] = vd; |
| 167 | |
| 168 | if (p_colors.size() == 1) { |
| 169 | Color color = p_colors[0]; |
| 170 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 171 | fptr[base_offset + i * stride + 0] = color.r; |
| 172 | fptr[base_offset + i * stride + 1] = color.g; |
| 173 | fptr[base_offset + i * stride + 2] = color.b; |
| 174 | fptr[base_offset + i * stride + 3] = color.a; |
| 175 | } |
| 176 | } else { |
| 177 | const Color *color_ptr = p_colors.ptr(); |
| 178 | |
| 179 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 180 | fptr[base_offset + i * stride + 0] = color_ptr[i].r; |
| 181 | fptr[base_offset + i * stride + 1] = color_ptr[i].g; |
| 182 | fptr[base_offset + i * stride + 2] = color_ptr[i].b; |
| 183 | fptr[base_offset + i * stride + 3] = color_ptr[i].a; |
| 184 | } |
| 185 | } |
| 186 | base_offset += 4; |
| 187 | } else { |
| 188 | RD::VertexAttribute vd; |
| 189 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 190 | vd.offset = 0; |
| 191 | vd.location = RS::ARRAY_COLOR; |
| 192 | vd.stride = 0; |
| 193 | |
| 194 | descriptions.write[1] = vd; |
| 195 | buffers.write[1] = mesh_storage->mesh_get_default_rd_buffer(RendererRD::MeshStorage::DEFAULT_RD_BUFFER_COLOR); |
| 196 | } |
| 197 | |
| 198 | //uvs |
| 199 | if ((uint32_t)p_uvs.size() == vertex_count) { |
| 200 | RD::VertexAttribute vd; |
| 201 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 202 | vd.offset = base_offset * sizeof(float); |
| 203 | vd.location = RS::ARRAY_TEX_UV; |
| 204 | vd.stride = stride * sizeof(float); |
| 205 | |
| 206 | descriptions.write[2] = vd; |
| 207 | |
| 208 | const Vector2 *uv_ptr = p_uvs.ptr(); |
| 209 | |
| 210 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 211 | fptr[base_offset + i * stride + 0] = uv_ptr[i].x; |
| 212 | fptr[base_offset + i * stride + 1] = uv_ptr[i].y; |
| 213 | } |
| 214 | base_offset += 2; |
| 215 | } else { |
| 216 | RD::VertexAttribute vd; |
| 217 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 218 | vd.offset = 0; |
| 219 | vd.location = RS::ARRAY_TEX_UV; |
| 220 | vd.stride = 0; |
| 221 | |
| 222 | descriptions.write[2] = vd; |
| 223 | buffers.write[2] = mesh_storage->mesh_get_default_rd_buffer(RendererRD::MeshStorage::DEFAULT_RD_BUFFER_TEX_UV); |
| 224 | } |
| 225 | |
| 226 | //bones |
| 227 | if ((uint32_t)p_indices.size() == vertex_count * 4 && (uint32_t)p_weights.size() == vertex_count * 4) { |
| 228 | RD::VertexAttribute vd; |
| 229 | vd.format = RD::DATA_FORMAT_R16G16B16A16_UINT; |
| 230 | vd.offset = base_offset * sizeof(float); |
| 231 | vd.location = RS::ARRAY_BONES; |
| 232 | vd.stride = stride * sizeof(float); |
| 233 | |
| 234 | descriptions.write[3] = vd; |
| 235 | |
| 236 | const int *bone_ptr = p_bones.ptr(); |
| 237 | |
| 238 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 239 | uint16_t *bone16w = (uint16_t *)&uptr[base_offset + i * stride]; |
| 240 | |
| 241 | bone16w[0] = bone_ptr[i * 4 + 0]; |
| 242 | bone16w[1] = bone_ptr[i * 4 + 1]; |
| 243 | bone16w[2] = bone_ptr[i * 4 + 2]; |
| 244 | bone16w[3] = bone_ptr[i * 4 + 3]; |
| 245 | } |
| 246 | |
| 247 | base_offset += 2; |
| 248 | } else { |
| 249 | RD::VertexAttribute vd; |
| 250 | vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT; |
| 251 | vd.offset = 0; |
| 252 | vd.location = RS::ARRAY_BONES; |
| 253 | vd.stride = 0; |
| 254 | |
| 255 | descriptions.write[3] = vd; |
| 256 | buffers.write[3] = mesh_storage->mesh_get_default_rd_buffer(RendererRD::MeshStorage::DEFAULT_RD_BUFFER_BONES); |
| 257 | } |
| 258 | |
| 259 | //weights |
| 260 | if ((uint32_t)p_weights.size() == vertex_count * 4) { |
| 261 | RD::VertexAttribute vd; |
| 262 | vd.format = RD::DATA_FORMAT_R16G16B16A16_UNORM; |
| 263 | vd.offset = base_offset * sizeof(float); |
| 264 | vd.location = RS::ARRAY_WEIGHTS; |
| 265 | vd.stride = stride * sizeof(float); |
| 266 | |
| 267 | descriptions.write[4] = vd; |
| 268 | |
| 269 | const float *weight_ptr = p_weights.ptr(); |
| 270 | |
| 271 | for (uint32_t i = 0; i < vertex_count; i++) { |
| 272 | uint16_t *weight16w = (uint16_t *)&uptr[base_offset + i * stride]; |
| 273 | |
| 274 | weight16w[0] = CLAMP(weight_ptr[i * 4 + 0] * 65535, 0, 65535); |
| 275 | weight16w[1] = CLAMP(weight_ptr[i * 4 + 1] * 65535, 0, 65535); |
| 276 | weight16w[2] = CLAMP(weight_ptr[i * 4 + 2] * 65535, 0, 65535); |
| 277 | weight16w[3] = CLAMP(weight_ptr[i * 4 + 3] * 65535, 0, 65535); |
| 278 | } |
| 279 | |
| 280 | base_offset += 2; |
| 281 | } else { |
| 282 | RD::VertexAttribute vd; |
| 283 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 284 | vd.offset = 0; |
| 285 | vd.location = RS::ARRAY_WEIGHTS; |
| 286 | vd.stride = 0; |
| 287 | |
| 288 | descriptions.write[4] = vd; |
| 289 | buffers.write[4] = mesh_storage->mesh_get_default_rd_buffer(RendererRD::MeshStorage::DEFAULT_RD_BUFFER_WEIGHTS); |
| 290 | } |
| 291 | |
| 292 | //check that everything is as it should be |
| 293 | ERR_FAIL_COND_V(base_offset != stride, 0); //bug |
| 294 | } |
| 295 | |
| 296 | RD::VertexFormatID vertex_id = RD::get_singleton()->vertex_format_create(descriptions); |
| 297 | ERR_FAIL_COND_V(vertex_id == RD::INVALID_ID, 0); |
| 298 | |
| 299 | PolygonBuffers pb; |
| 300 | pb.vertex_buffer = RD::get_singleton()->vertex_buffer_create(polygon_buffer.size(), polygon_buffer); |
| 301 | for (int i = 0; i < descriptions.size(); i++) { |
| 302 | if (buffers[i] == RID()) { //if put in vertex, use as vertex |
| 303 | buffers.write[i] = pb.vertex_buffer; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | pb.vertex_array = RD::get_singleton()->vertex_array_create(p_points.size(), vertex_id, buffers); |
| 308 | |
| 309 | if (p_indices.size()) { |
| 310 | //create indices, as indices were requested |
| 311 | Vector<uint8_t> index_buffer; |
| 312 | index_buffer.resize(p_indices.size() * sizeof(int32_t)); |
| 313 | { |
| 314 | uint8_t *w = index_buffer.ptrw(); |
| 315 | memcpy(w, p_indices.ptr(), sizeof(int32_t) * p_indices.size()); |
| 316 | } |
| 317 | pb.index_buffer = RD::get_singleton()->index_buffer_create(p_indices.size(), RD::INDEX_BUFFER_FORMAT_UINT32, index_buffer); |
| 318 | pb.indices = RD::get_singleton()->index_array_create(pb.index_buffer, 0, p_indices.size()); |
| 319 | } |
| 320 | |
| 321 | pb.vertex_format_id = vertex_id; |
| 322 | |
| 323 | PolygonID id = polygon_buffers.last_id++; |
| 324 | |
| 325 | polygon_buffers.polygons[id] = pb; |
| 326 | |
| 327 | return id; |
| 328 | } |
| 329 | |
| 330 | void RendererCanvasRenderRD::free_polygon(PolygonID p_polygon) { |
| 331 | PolygonBuffers *pb_ptr = polygon_buffers.polygons.getptr(p_polygon); |
| 332 | ERR_FAIL_COND(!pb_ptr); |
| 333 | |
| 334 | PolygonBuffers &pb = *pb_ptr; |
| 335 | |
| 336 | if (pb.indices.is_valid()) { |
| 337 | RD::get_singleton()->free(pb.indices); |
| 338 | } |
| 339 | if (pb.index_buffer.is_valid()) { |
| 340 | RD::get_singleton()->free(pb.index_buffer); |
| 341 | } |
| 342 | |
| 343 | RD::get_singleton()->free(pb.vertex_array); |
| 344 | RD::get_singleton()->free(pb.vertex_buffer); |
| 345 | |
| 346 | polygon_buffers.polygons.erase(p_polygon); |
| 347 | } |
| 348 | |
| 349 | //////////////////// |
| 350 | |
| 351 | void RendererCanvasRenderRD::_bind_canvas_texture(RD::DrawListID p_draw_list, RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, RID &r_last_texture, PushConstant &push_constant, Size2 &r_texpixel_size, bool p_texture_is_data) { |
| 352 | if (p_texture == RID()) { |
| 353 | p_texture = default_canvas_texture; |
| 354 | } |
| 355 | |
| 356 | if (r_last_texture == p_texture) { |
| 357 | return; //nothing to do, its the same |
| 358 | } |
| 359 | |
| 360 | RID uniform_set; |
| 361 | Color specular_shininess; |
| 362 | Size2i size; |
| 363 | bool use_normal; |
| 364 | bool use_specular; |
| 365 | |
| 366 | bool success = RendererRD::TextureStorage::get_singleton()->canvas_texture_get_uniform_set(p_texture, p_base_filter, p_base_repeat, shader.default_version_rd_shader, CANVAS_TEXTURE_UNIFORM_SET, bool(push_constant.flags & FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR), uniform_set, size, specular_shininess, use_normal, use_specular, p_texture_is_data); |
| 367 | //something odd happened |
| 368 | if (!success) { |
| 369 | _bind_canvas_texture(p_draw_list, default_canvas_texture, p_base_filter, p_base_repeat, r_last_texture, push_constant, r_texpixel_size); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set, CANVAS_TEXTURE_UNIFORM_SET); |
| 374 | |
| 375 | if (specular_shininess.a < 0.999) { |
| 376 | push_constant.flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED; |
| 377 | } else { |
| 378 | push_constant.flags &= ~FLAGS_DEFAULT_SPECULAR_MAP_USED; |
| 379 | } |
| 380 | |
| 381 | if (use_normal) { |
| 382 | push_constant.flags |= FLAGS_DEFAULT_NORMAL_MAP_USED; |
| 383 | } else { |
| 384 | push_constant.flags &= ~FLAGS_DEFAULT_NORMAL_MAP_USED; |
| 385 | } |
| 386 | |
| 387 | push_constant.specular_shininess = uint32_t(CLAMP(specular_shininess.a * 255.0, 0, 255)) << 24; |
| 388 | push_constant.specular_shininess |= uint32_t(CLAMP(specular_shininess.b * 255.0, 0, 255)) << 16; |
| 389 | push_constant.specular_shininess |= uint32_t(CLAMP(specular_shininess.g * 255.0, 0, 255)) << 8; |
| 390 | push_constant.specular_shininess |= uint32_t(CLAMP(specular_shininess.r * 255.0, 0, 255)); |
| 391 | |
| 392 | r_texpixel_size.x = 1.0 / float(size.x); |
| 393 | r_texpixel_size.y = 1.0 / float(size.y); |
| 394 | |
| 395 | push_constant.color_texture_pixel_size[0] = r_texpixel_size.x; |
| 396 | push_constant.color_texture_pixel_size[1] = r_texpixel_size.y; |
| 397 | |
| 398 | r_last_texture = p_texture; |
| 399 | } |
| 400 | |
| 401 | void RendererCanvasRenderRD::_render_item(RD::DrawListID p_draw_list, RID p_render_target, const Item *p_item, RD::FramebufferFormatID p_framebuffer_format, const Transform2D &p_canvas_transform_inverse, Item *¤t_clip, Light *p_lights, PipelineVariants *p_pipeline_variants, bool &r_sdf_used) { |
| 402 | //create an empty push constant |
| 403 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 404 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 405 | RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton(); |
| 406 | |
| 407 | RS::CanvasItemTextureFilter current_filter = default_filter; |
| 408 | RS::CanvasItemTextureRepeat current_repeat = default_repeat; |
| 409 | |
| 410 | if (p_item->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT) { |
| 411 | current_filter = p_item->texture_filter; |
| 412 | } |
| 413 | |
| 414 | if (p_item->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT) { |
| 415 | current_repeat = p_item->texture_repeat; |
| 416 | } |
| 417 | |
| 418 | PushConstant push_constant; |
| 419 | Transform2D base_transform = p_canvas_transform_inverse * p_item->final_transform; |
| 420 | Transform2D draw_transform; |
| 421 | _update_transform_2d_to_mat2x3(base_transform, push_constant.world); |
| 422 | |
| 423 | Color base_color = p_item->final_modulate; |
| 424 | bool use_linear_colors = texture_storage->render_target_is_using_hdr(p_render_target); |
| 425 | |
| 426 | for (int i = 0; i < 4; i++) { |
| 427 | push_constant.modulation[i] = 0; |
| 428 | push_constant.ninepatch_margins[i] = 0; |
| 429 | push_constant.src_rect[i] = 0; |
| 430 | push_constant.dst_rect[i] = 0; |
| 431 | } |
| 432 | push_constant.flags = 0; |
| 433 | push_constant.color_texture_pixel_size[0] = 0; |
| 434 | push_constant.color_texture_pixel_size[1] = 0; |
| 435 | |
| 436 | push_constant.pad[0] = 0; |
| 437 | push_constant.pad[1] = 0; |
| 438 | |
| 439 | push_constant.lights[0] = 0; |
| 440 | push_constant.lights[1] = 0; |
| 441 | push_constant.lights[2] = 0; |
| 442 | push_constant.lights[3] = 0; |
| 443 | |
| 444 | uint32_t base_flags = 0; |
| 445 | base_flags |= use_linear_colors ? FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR : 0; |
| 446 | |
| 447 | uint16_t light_count = 0; |
| 448 | PipelineLightMode light_mode; |
| 449 | |
| 450 | { |
| 451 | Light *light = p_lights; |
| 452 | |
| 453 | while (light) { |
| 454 | 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_transformed(light->xform_cache, light->rect_cache)) { |
| 455 | uint32_t light_index = light->render_index_cache; |
| 456 | push_constant.lights[light_count >> 2] |= light_index << ((light_count & 3) * 8); |
| 457 | |
| 458 | light_count++; |
| 459 | |
| 460 | if (light_count == MAX_LIGHTS_PER_ITEM - 1) { |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | light = light->next_ptr; |
| 465 | } |
| 466 | |
| 467 | base_flags |= light_count << FLAGS_LIGHT_COUNT_SHIFT; |
| 468 | } |
| 469 | |
| 470 | light_mode = (light_count > 0 || using_directional_lights) ? PIPELINE_LIGHT_MODE_ENABLED : PIPELINE_LIGHT_MODE_DISABLED; |
| 471 | |
| 472 | PipelineVariants *pipeline_variants = p_pipeline_variants; |
| 473 | |
| 474 | bool reclip = false; |
| 475 | |
| 476 | RID last_texture; |
| 477 | Size2 texpixel_size; |
| 478 | |
| 479 | bool skipping = false; |
| 480 | |
| 481 | const Item::Command *c = p_item->commands; |
| 482 | while (c) { |
| 483 | if (skipping && c->type != Item::Command::TYPE_ANIMATION_SLICE) { |
| 484 | c = c->next; |
| 485 | continue; |
| 486 | } |
| 487 | |
| 488 | push_constant.flags = base_flags | (push_constant.flags & (FLAGS_DEFAULT_NORMAL_MAP_USED | FLAGS_DEFAULT_SPECULAR_MAP_USED)); //reset on each command for sanity, keep canvastexture binding config |
| 489 | |
| 490 | switch (c->type) { |
| 491 | case Item::Command::TYPE_RECT: { |
| 492 | const Item::CommandRect *rect = static_cast<const Item::CommandRect *>(c); |
| 493 | |
| 494 | if (rect->flags & CANVAS_RECT_TILE) { |
| 495 | current_repeat = RenderingServer::CanvasItemTextureRepeat::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED; |
| 496 | } |
| 497 | |
| 498 | //bind pipeline |
| 499 | if (rect->flags & CANVAS_RECT_LCD) { |
| 500 | RID pipeline = pipeline_variants->variants[light_mode][PIPELINE_VARIANT_QUAD_LCD_BLEND].get_render_pipeline(RD::INVALID_ID, p_framebuffer_format); |
| 501 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 502 | RD::get_singleton()->draw_list_set_blend_constants(p_draw_list, rect->modulate); |
| 503 | } else { |
| 504 | RID pipeline = pipeline_variants->variants[light_mode][PIPELINE_VARIANT_QUAD].get_render_pipeline(RD::INVALID_ID, p_framebuffer_format); |
| 505 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 506 | } |
| 507 | |
| 508 | //bind textures |
| 509 | |
| 510 | _bind_canvas_texture(p_draw_list, rect->texture, current_filter, current_repeat, last_texture, push_constant, texpixel_size, bool(rect->flags & CANVAS_RECT_MSDF)); |
| 511 | |
| 512 | Rect2 src_rect; |
| 513 | Rect2 dst_rect; |
| 514 | |
| 515 | if (rect->texture != RID()) { |
| 516 | src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1); |
| 517 | dst_rect = Rect2(rect->rect.position, rect->rect.size); |
| 518 | |
| 519 | if (dst_rect.size.width < 0) { |
| 520 | dst_rect.position.x += dst_rect.size.width; |
| 521 | dst_rect.size.width *= -1; |
| 522 | } |
| 523 | if (dst_rect.size.height < 0) { |
| 524 | dst_rect.position.y += dst_rect.size.height; |
| 525 | dst_rect.size.height *= -1; |
| 526 | } |
| 527 | |
| 528 | if (rect->flags & CANVAS_RECT_FLIP_H) { |
| 529 | src_rect.size.x *= -1; |
| 530 | push_constant.flags |= FLAGS_FLIP_H; |
| 531 | } |
| 532 | |
| 533 | if (rect->flags & CANVAS_RECT_FLIP_V) { |
| 534 | src_rect.size.y *= -1; |
| 535 | push_constant.flags |= FLAGS_FLIP_V; |
| 536 | } |
| 537 | |
| 538 | if (rect->flags & CANVAS_RECT_TRANSPOSE) { |
| 539 | push_constant.flags |= FLAGS_TRANSPOSE_RECT; |
| 540 | } |
| 541 | |
| 542 | if (rect->flags & CANVAS_RECT_CLIP_UV) { |
| 543 | push_constant.flags |= FLAGS_CLIP_RECT_UV; |
| 544 | } |
| 545 | |
| 546 | } else { |
| 547 | dst_rect = Rect2(rect->rect.position, rect->rect.size); |
| 548 | |
| 549 | if (dst_rect.size.width < 0) { |
| 550 | dst_rect.position.x += dst_rect.size.width; |
| 551 | dst_rect.size.width *= -1; |
| 552 | } |
| 553 | if (dst_rect.size.height < 0) { |
| 554 | dst_rect.position.y += dst_rect.size.height; |
| 555 | dst_rect.size.height *= -1; |
| 556 | } |
| 557 | |
| 558 | src_rect = Rect2(0, 0, 1, 1); |
| 559 | } |
| 560 | |
| 561 | if (rect->flags & CANVAS_RECT_MSDF) { |
| 562 | push_constant.flags |= FLAGS_USE_MSDF; |
| 563 | push_constant.msdf[0] = rect->px_range; // Pixel range. |
| 564 | push_constant.msdf[1] = rect->outline; // Outline size. |
| 565 | push_constant.msdf[2] = 0.f; // Reserved. |
| 566 | push_constant.msdf[3] = 0.f; // Reserved. |
| 567 | } else if (rect->flags & CANVAS_RECT_LCD) { |
| 568 | push_constant.flags |= FLAGS_USE_LCD; |
| 569 | } |
| 570 | |
| 571 | Color modulated = rect->modulate * base_color; |
| 572 | if (use_linear_colors) { |
| 573 | modulated = modulated.srgb_to_linear(); |
| 574 | } |
| 575 | |
| 576 | push_constant.modulation[0] = modulated.r; |
| 577 | push_constant.modulation[1] = modulated.g; |
| 578 | push_constant.modulation[2] = modulated.b; |
| 579 | push_constant.modulation[3] = modulated.a; |
| 580 | |
| 581 | push_constant.src_rect[0] = src_rect.position.x; |
| 582 | push_constant.src_rect[1] = src_rect.position.y; |
| 583 | push_constant.src_rect[2] = src_rect.size.width; |
| 584 | push_constant.src_rect[3] = src_rect.size.height; |
| 585 | |
| 586 | push_constant.dst_rect[0] = dst_rect.position.x; |
| 587 | push_constant.dst_rect[1] = dst_rect.position.y; |
| 588 | push_constant.dst_rect[2] = dst_rect.size.width; |
| 589 | push_constant.dst_rect[3] = dst_rect.size.height; |
| 590 | |
| 591 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant)); |
| 592 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, shader.quad_index_array); |
| 593 | RD::get_singleton()->draw_list_draw(p_draw_list, true); |
| 594 | |
| 595 | } break; |
| 596 | |
| 597 | case Item::Command::TYPE_NINEPATCH: { |
| 598 | const Item::CommandNinePatch *np = static_cast<const Item::CommandNinePatch *>(c); |
| 599 | |
| 600 | //bind pipeline |
| 601 | { |
| 602 | RID pipeline = pipeline_variants->variants[light_mode][PIPELINE_VARIANT_NINEPATCH].get_render_pipeline(RD::INVALID_ID, p_framebuffer_format); |
| 603 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 604 | } |
| 605 | |
| 606 | //bind textures |
| 607 | |
| 608 | _bind_canvas_texture(p_draw_list, np->texture, current_filter, current_repeat, last_texture, push_constant, texpixel_size); |
| 609 | |
| 610 | Rect2 src_rect; |
| 611 | Rect2 dst_rect(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y); |
| 612 | |
| 613 | if (np->texture == RID()) { |
| 614 | texpixel_size = Size2(1, 1); |
| 615 | src_rect = Rect2(0, 0, 1, 1); |
| 616 | |
| 617 | } else { |
| 618 | if (np->source != Rect2()) { |
| 619 | src_rect = Rect2(np->source.position.x * texpixel_size.width, np->source.position.y * texpixel_size.height, np->source.size.x * texpixel_size.width, np->source.size.y * texpixel_size.height); |
| 620 | push_constant.color_texture_pixel_size[0] = 1.0 / np->source.size.width; |
| 621 | push_constant.color_texture_pixel_size[1] = 1.0 / np->source.size.height; |
| 622 | |
| 623 | } else { |
| 624 | src_rect = Rect2(0, 0, 1, 1); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | Color modulated = np->color * base_color; |
| 629 | if (use_linear_colors) { |
| 630 | modulated = modulated.srgb_to_linear(); |
| 631 | } |
| 632 | |
| 633 | push_constant.modulation[0] = modulated.r; |
| 634 | push_constant.modulation[1] = modulated.g; |
| 635 | push_constant.modulation[2] = modulated.b; |
| 636 | push_constant.modulation[3] = modulated.a; |
| 637 | |
| 638 | push_constant.src_rect[0] = src_rect.position.x; |
| 639 | push_constant.src_rect[1] = src_rect.position.y; |
| 640 | push_constant.src_rect[2] = src_rect.size.width; |
| 641 | push_constant.src_rect[3] = src_rect.size.height; |
| 642 | |
| 643 | push_constant.dst_rect[0] = dst_rect.position.x; |
| 644 | push_constant.dst_rect[1] = dst_rect.position.y; |
| 645 | push_constant.dst_rect[2] = dst_rect.size.width; |
| 646 | push_constant.dst_rect[3] = dst_rect.size.height; |
| 647 | |
| 648 | push_constant.flags |= int(np->axis_x) << FLAGS_NINEPATCH_H_MODE_SHIFT; |
| 649 | push_constant.flags |= int(np->axis_y) << FLAGS_NINEPATCH_V_MODE_SHIFT; |
| 650 | |
| 651 | if (np->draw_center) { |
| 652 | push_constant.flags |= FLAGS_NINEPACH_DRAW_CENTER; |
| 653 | } |
| 654 | |
| 655 | push_constant.ninepatch_margins[0] = np->margin[SIDE_LEFT]; |
| 656 | push_constant.ninepatch_margins[1] = np->margin[SIDE_TOP]; |
| 657 | push_constant.ninepatch_margins[2] = np->margin[SIDE_RIGHT]; |
| 658 | push_constant.ninepatch_margins[3] = np->margin[SIDE_BOTTOM]; |
| 659 | |
| 660 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant)); |
| 661 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, shader.quad_index_array); |
| 662 | RD::get_singleton()->draw_list_draw(p_draw_list, true); |
| 663 | |
| 664 | // Restore if overridden. |
| 665 | push_constant.color_texture_pixel_size[0] = texpixel_size.x; |
| 666 | push_constant.color_texture_pixel_size[1] = texpixel_size.y; |
| 667 | |
| 668 | } break; |
| 669 | case Item::Command::TYPE_POLYGON: { |
| 670 | const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c); |
| 671 | |
| 672 | PolygonBuffers *pb = polygon_buffers.polygons.getptr(polygon->polygon.polygon_id); |
| 673 | ERR_CONTINUE(!pb); |
| 674 | //bind pipeline |
| 675 | { |
| 676 | static const PipelineVariant variant[RS::PRIMITIVE_MAX] = { PIPELINE_VARIANT_ATTRIBUTE_POINTS, PIPELINE_VARIANT_ATTRIBUTE_LINES, PIPELINE_VARIANT_ATTRIBUTE_LINES_STRIP, PIPELINE_VARIANT_ATTRIBUTE_TRIANGLES, PIPELINE_VARIANT_ATTRIBUTE_TRIANGLE_STRIP }; |
| 677 | ERR_CONTINUE(polygon->primitive < 0 || polygon->primitive >= RS::PRIMITIVE_MAX); |
| 678 | RID pipeline = pipeline_variants->variants[light_mode][variant[polygon->primitive]].get_render_pipeline(pb->vertex_format_id, p_framebuffer_format); |
| 679 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 680 | } |
| 681 | |
| 682 | if (polygon->primitive == RS::PRIMITIVE_LINES) { |
| 683 | //not supported in most hardware, so pointless |
| 684 | //RD::get_singleton()->draw_list_set_line_width(p_draw_list, polygon->line_width); |
| 685 | } |
| 686 | |
| 687 | //bind textures |
| 688 | |
| 689 | _bind_canvas_texture(p_draw_list, polygon->texture, current_filter, current_repeat, last_texture, push_constant, texpixel_size); |
| 690 | |
| 691 | Color color = base_color; |
| 692 | if (use_linear_colors) { |
| 693 | color = color.srgb_to_linear(); |
| 694 | } |
| 695 | |
| 696 | push_constant.modulation[0] = color.r; |
| 697 | push_constant.modulation[1] = color.g; |
| 698 | push_constant.modulation[2] = color.b; |
| 699 | push_constant.modulation[3] = color.a; |
| 700 | |
| 701 | for (int j = 0; j < 4; j++) { |
| 702 | push_constant.src_rect[j] = 0; |
| 703 | push_constant.dst_rect[j] = 0; |
| 704 | push_constant.ninepatch_margins[j] = 0; |
| 705 | } |
| 706 | |
| 707 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant)); |
| 708 | RD::get_singleton()->draw_list_bind_vertex_array(p_draw_list, pb->vertex_array); |
| 709 | if (pb->indices.is_valid()) { |
| 710 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, pb->indices); |
| 711 | } |
| 712 | RD::get_singleton()->draw_list_draw(p_draw_list, pb->indices.is_valid()); |
| 713 | |
| 714 | } break; |
| 715 | case Item::Command::TYPE_PRIMITIVE: { |
| 716 | const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c); |
| 717 | |
| 718 | //bind pipeline |
| 719 | { |
| 720 | static const PipelineVariant variant[4] = { PIPELINE_VARIANT_PRIMITIVE_POINTS, PIPELINE_VARIANT_PRIMITIVE_LINES, PIPELINE_VARIANT_PRIMITIVE_TRIANGLES, PIPELINE_VARIANT_PRIMITIVE_TRIANGLES }; |
| 721 | ERR_CONTINUE(primitive->point_count == 0 || primitive->point_count > 4); |
| 722 | RID pipeline = pipeline_variants->variants[light_mode][variant[primitive->point_count - 1]].get_render_pipeline(RD::INVALID_ID, p_framebuffer_format); |
| 723 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 724 | } |
| 725 | |
| 726 | //bind textures |
| 727 | |
| 728 | _bind_canvas_texture(p_draw_list, primitive->texture, current_filter, current_repeat, last_texture, push_constant, texpixel_size); |
| 729 | |
| 730 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, primitive_arrays.index_array[MIN(3u, primitive->point_count) - 1]); |
| 731 | |
| 732 | for (uint32_t j = 0; j < MIN(3u, primitive->point_count); j++) { |
| 733 | push_constant.points[j * 2 + 0] = primitive->points[j].x; |
| 734 | push_constant.points[j * 2 + 1] = primitive->points[j].y; |
| 735 | push_constant.uvs[j * 2 + 0] = primitive->uvs[j].x; |
| 736 | push_constant.uvs[j * 2 + 1] = primitive->uvs[j].y; |
| 737 | Color col = primitive->colors[j] * base_color; |
| 738 | if (use_linear_colors) { |
| 739 | col = col.srgb_to_linear(); |
| 740 | } |
| 741 | push_constant.colors[j * 2 + 0] = (uint32_t(Math::make_half_float(col.g)) << 16) | Math::make_half_float(col.r); |
| 742 | push_constant.colors[j * 2 + 1] = (uint32_t(Math::make_half_float(col.a)) << 16) | Math::make_half_float(col.b); |
| 743 | } |
| 744 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant)); |
| 745 | RD::get_singleton()->draw_list_draw(p_draw_list, true); |
| 746 | |
| 747 | if (primitive->point_count == 4) { |
| 748 | for (uint32_t j = 1; j < 3; j++) { |
| 749 | //second half of triangle |
| 750 | push_constant.points[j * 2 + 0] = primitive->points[j + 1].x; |
| 751 | push_constant.points[j * 2 + 1] = primitive->points[j + 1].y; |
| 752 | push_constant.uvs[j * 2 + 0] = primitive->uvs[j + 1].x; |
| 753 | push_constant.uvs[j * 2 + 1] = primitive->uvs[j + 1].y; |
| 754 | Color col = primitive->colors[j + 1] * base_color; |
| 755 | if (use_linear_colors) { |
| 756 | col = col.srgb_to_linear(); |
| 757 | } |
| 758 | push_constant.colors[j * 2 + 0] = (uint32_t(Math::make_half_float(col.g)) << 16) | Math::make_half_float(col.r); |
| 759 | push_constant.colors[j * 2 + 1] = (uint32_t(Math::make_half_float(col.a)) << 16) | Math::make_half_float(col.b); |
| 760 | } |
| 761 | |
| 762 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant)); |
| 763 | RD::get_singleton()->draw_list_draw(p_draw_list, true); |
| 764 | } |
| 765 | |
| 766 | } break; |
| 767 | case Item::Command::TYPE_MESH: |
| 768 | case Item::Command::TYPE_MULTIMESH: |
| 769 | case Item::Command::TYPE_PARTICLES: { |
| 770 | RID mesh; |
| 771 | RID mesh_instance; |
| 772 | RID texture; |
| 773 | Color modulate(1, 1, 1, 1); |
| 774 | float world_backup[6]; |
| 775 | int instance_count = 1; |
| 776 | |
| 777 | for (int j = 0; j < 6; j++) { |
| 778 | world_backup[j] = push_constant.world[j]; |
| 779 | } |
| 780 | |
| 781 | if (c->type == Item::Command::TYPE_MESH) { |
| 782 | const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(c); |
| 783 | mesh = m->mesh; |
| 784 | mesh_instance = m->mesh_instance; |
| 785 | texture = m->texture; |
| 786 | modulate = m->modulate; |
| 787 | _update_transform_2d_to_mat2x3(base_transform * draw_transform * m->transform, push_constant.world); |
| 788 | } else if (c->type == Item::Command::TYPE_MULTIMESH) { |
| 789 | const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(c); |
| 790 | RID multimesh = mm->multimesh; |
| 791 | mesh = mesh_storage->multimesh_get_mesh(multimesh); |
| 792 | texture = mm->texture; |
| 793 | |
| 794 | if (mesh_storage->multimesh_get_transform_format(multimesh) != RS::MULTIMESH_TRANSFORM_2D) { |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | instance_count = mesh_storage->multimesh_get_instances_to_draw(multimesh); |
| 799 | |
| 800 | if (instance_count == 0) { |
| 801 | break; |
| 802 | } |
| 803 | |
| 804 | RID uniform_set = mesh_storage->multimesh_get_2d_uniform_set(multimesh, shader.default_version_rd_shader, TRANSFORMS_UNIFORM_SET); |
| 805 | RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set, TRANSFORMS_UNIFORM_SET); |
| 806 | push_constant.flags |= 1; //multimesh, trails disabled |
| 807 | if (mesh_storage->multimesh_uses_colors(multimesh)) { |
| 808 | push_constant.flags |= FLAGS_INSTANCING_HAS_COLORS; |
| 809 | } |
| 810 | if (mesh_storage->multimesh_uses_custom_data(multimesh)) { |
| 811 | push_constant.flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA; |
| 812 | } |
| 813 | } else if (c->type == Item::Command::TYPE_PARTICLES) { |
| 814 | const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(c); |
| 815 | ERR_BREAK(particles_storage->particles_get_mode(pt->particles) != RS::PARTICLES_MODE_2D); |
| 816 | particles_storage->particles_request_process(pt->particles); |
| 817 | |
| 818 | if (particles_storage->particles_is_inactive(pt->particles) || particles_storage->particles_get_frame_counter(pt->particles) == 0) { |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | RenderingServerDefault::redraw_request(); // active particles means redraw request |
| 823 | |
| 824 | int dpc = particles_storage->particles_get_draw_passes(pt->particles); |
| 825 | if (dpc == 0) { |
| 826 | break; //nothing to draw |
| 827 | } |
| 828 | uint32_t divisor = 1; |
| 829 | instance_count = particles_storage->particles_get_amount(pt->particles, divisor); |
| 830 | |
| 831 | RID uniform_set = particles_storage->particles_get_instance_buffer_uniform_set(pt->particles, shader.default_version_rd_shader, TRANSFORMS_UNIFORM_SET); |
| 832 | RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set, TRANSFORMS_UNIFORM_SET); |
| 833 | |
| 834 | push_constant.flags |= divisor; |
| 835 | instance_count /= divisor; |
| 836 | |
| 837 | push_constant.flags |= FLAGS_INSTANCING_HAS_COLORS; |
| 838 | push_constant.flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA; |
| 839 | |
| 840 | mesh = particles_storage->particles_get_draw_pass_mesh(pt->particles, 0); //higher ones are ignored |
| 841 | texture = pt->texture; |
| 842 | |
| 843 | if (particles_storage->particles_has_collision(pt->particles) && texture_storage->render_target_is_sdf_enabled(p_render_target)) { |
| 844 | //pass collision information |
| 845 | Transform2D xform = p_item->final_transform; |
| 846 | |
| 847 | RID sdf_texture = texture_storage->render_target_get_sdf_texture(p_render_target); |
| 848 | |
| 849 | Rect2 to_screen; |
| 850 | { |
| 851 | Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_render_target); |
| 852 | |
| 853 | to_screen.size = Vector2(1.0 / sdf_rect.size.width, 1.0 / sdf_rect.size.height); |
| 854 | to_screen.position = -sdf_rect.position * to_screen.size; |
| 855 | } |
| 856 | |
| 857 | particles_storage->particles_set_canvas_sdf_collision(pt->particles, true, xform, to_screen, sdf_texture); |
| 858 | } else { |
| 859 | particles_storage->particles_set_canvas_sdf_collision(pt->particles, false, Transform2D(), Rect2(), RID()); |
| 860 | } |
| 861 | |
| 862 | // Signal that SDF texture needs to be updated. |
| 863 | r_sdf_used |= particles_storage->particles_has_collision(pt->particles); |
| 864 | } |
| 865 | |
| 866 | if (mesh.is_null()) { |
| 867 | break; |
| 868 | } |
| 869 | |
| 870 | _bind_canvas_texture(p_draw_list, texture, current_filter, current_repeat, last_texture, push_constant, texpixel_size); |
| 871 | |
| 872 | uint32_t surf_count = mesh_storage->mesh_get_surface_count(mesh); |
| 873 | static const PipelineVariant variant[RS::PRIMITIVE_MAX] = { PIPELINE_VARIANT_ATTRIBUTE_POINTS, PIPELINE_VARIANT_ATTRIBUTE_LINES, PIPELINE_VARIANT_ATTRIBUTE_LINES_STRIP, PIPELINE_VARIANT_ATTRIBUTE_TRIANGLES, PIPELINE_VARIANT_ATTRIBUTE_TRIANGLE_STRIP }; |
| 874 | |
| 875 | Color modulated = modulate * base_color; |
| 876 | if (use_linear_colors) { |
| 877 | modulated = modulated.srgb_to_linear(); |
| 878 | } |
| 879 | |
| 880 | push_constant.modulation[0] = modulated.r; |
| 881 | push_constant.modulation[1] = modulated.g; |
| 882 | push_constant.modulation[2] = modulated.b; |
| 883 | push_constant.modulation[3] = modulated.a; |
| 884 | |
| 885 | for (int j = 0; j < 4; j++) { |
| 886 | push_constant.src_rect[j] = 0; |
| 887 | push_constant.dst_rect[j] = 0; |
| 888 | push_constant.ninepatch_margins[j] = 0; |
| 889 | } |
| 890 | |
| 891 | for (uint32_t j = 0; j < surf_count; j++) { |
| 892 | void *surface = mesh_storage->mesh_get_surface(mesh, j); |
| 893 | |
| 894 | RS::PrimitiveType primitive = mesh_storage->mesh_surface_get_primitive(surface); |
| 895 | ERR_CONTINUE(primitive < 0 || primitive >= RS::PRIMITIVE_MAX); |
| 896 | |
| 897 | uint32_t input_mask = pipeline_variants->variants[light_mode][variant[primitive]].get_vertex_input_mask(); |
| 898 | |
| 899 | RID vertex_array; |
| 900 | RD::VertexFormatID vertex_format = RD::INVALID_FORMAT_ID; |
| 901 | |
| 902 | if (mesh_instance.is_valid()) { |
| 903 | mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(mesh_instance, j, input_mask, false, vertex_array, vertex_format); |
| 904 | } else { |
| 905 | mesh_storage->mesh_surface_get_vertex_arrays_and_format(surface, input_mask, false, vertex_array, vertex_format); |
| 906 | } |
| 907 | |
| 908 | RID pipeline = pipeline_variants->variants[light_mode][variant[primitive]].get_render_pipeline(vertex_format, p_framebuffer_format); |
| 909 | RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline); |
| 910 | |
| 911 | RID index_array = mesh_storage->mesh_surface_get_index_array(surface, 0); |
| 912 | |
| 913 | if (index_array.is_valid()) { |
| 914 | RD::get_singleton()->draw_list_bind_index_array(p_draw_list, index_array); |
| 915 | } |
| 916 | |
| 917 | RD::get_singleton()->draw_list_bind_vertex_array(p_draw_list, vertex_array); |
| 918 | RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant)); |
| 919 | |
| 920 | RD::get_singleton()->draw_list_draw(p_draw_list, index_array.is_valid(), instance_count); |
| 921 | } |
| 922 | |
| 923 | for (int j = 0; j < 6; j++) { |
| 924 | push_constant.world[j] = world_backup[j]; |
| 925 | } |
| 926 | } break; |
| 927 | case Item::Command::TYPE_TRANSFORM: { |
| 928 | const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c); |
| 929 | draw_transform = transform->xform; |
| 930 | _update_transform_2d_to_mat2x3(base_transform * transform->xform, push_constant.world); |
| 931 | |
| 932 | } break; |
| 933 | case Item::Command::TYPE_CLIP_IGNORE: { |
| 934 | const Item::CommandClipIgnore *ci = static_cast<const Item::CommandClipIgnore *>(c); |
| 935 | if (current_clip) { |
| 936 | if (ci->ignore != reclip) { |
| 937 | if (ci->ignore) { |
| 938 | RD::get_singleton()->draw_list_disable_scissor(p_draw_list); |
| 939 | reclip = true; |
| 940 | } else { |
| 941 | RD::get_singleton()->draw_list_enable_scissor(p_draw_list, current_clip->final_clip_rect); |
| 942 | reclip = false; |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | } break; |
| 948 | case Item::Command::TYPE_ANIMATION_SLICE: { |
| 949 | const Item::CommandAnimationSlice *as = static_cast<const Item::CommandAnimationSlice *>(c); |
| 950 | double current_time = RendererCompositorRD::get_singleton()->get_total_time(); |
| 951 | double local_time = Math::fposmod(current_time - as->offset, as->animation_length); |
| 952 | skipping = !(local_time >= as->slice_begin && local_time < as->slice_end); |
| 953 | |
| 954 | RenderingServerDefault::redraw_request(); // animation visible means redraw request |
| 955 | } break; |
| 956 | } |
| 957 | |
| 958 | c = c->next; |
| 959 | } |
| 960 | |
| 961 | if (current_clip && reclip) { |
| 962 | //will make it re-enable clipping if needed afterwards |
| 963 | current_clip = nullptr; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | RID RendererCanvasRenderRD::_create_base_uniform_set(RID p_to_render_target, bool p_backbuffer) { |
| 968 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 969 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 970 | |
| 971 | //re create canvas state |
| 972 | Vector<RD::Uniform> uniforms; |
| 973 | |
| 974 | { |
| 975 | RD::Uniform u; |
| 976 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 977 | u.binding = 1; |
| 978 | u.append_id(state.canvas_state_buffer); |
| 979 | uniforms.push_back(u); |
| 980 | } |
| 981 | |
| 982 | { |
| 983 | RD::Uniform u; |
| 984 | u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER; |
| 985 | u.binding = 2; |
| 986 | u.append_id(state.lights_uniform_buffer); |
| 987 | uniforms.push_back(u); |
| 988 | } |
| 989 | |
| 990 | { |
| 991 | RD::Uniform u; |
| 992 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 993 | u.binding = 3; |
| 994 | u.append_id(RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture()); |
| 995 | uniforms.push_back(u); |
| 996 | } |
| 997 | |
| 998 | { |
| 999 | RD::Uniform u; |
| 1000 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 1001 | u.binding = 4; |
| 1002 | u.append_id(state.shadow_texture); |
| 1003 | uniforms.push_back(u); |
| 1004 | } |
| 1005 | |
| 1006 | { |
| 1007 | RD::Uniform u; |
| 1008 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
| 1009 | u.binding = 5; |
| 1010 | u.append_id(state.shadow_sampler); |
| 1011 | uniforms.push_back(u); |
| 1012 | } |
| 1013 | |
| 1014 | { |
| 1015 | RD::Uniform u; |
| 1016 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 1017 | u.binding = 6; |
| 1018 | RID screen; |
| 1019 | if (p_backbuffer) { |
| 1020 | screen = texture_storage->render_target_get_rd_texture(p_to_render_target); |
| 1021 | } else { |
| 1022 | screen = texture_storage->render_target_get_rd_backbuffer(p_to_render_target); |
| 1023 | if (screen.is_null()) { //unallocated backbuffer |
| 1024 | screen = RendererRD::TextureStorage::get_singleton()->texture_rd_get_default(RendererRD::TextureStorage::DEFAULT_RD_TEXTURE_WHITE); |
| 1025 | } |
| 1026 | } |
| 1027 | u.append_id(screen); |
| 1028 | uniforms.push_back(u); |
| 1029 | } |
| 1030 | |
| 1031 | { |
| 1032 | RD::Uniform u; |
| 1033 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
| 1034 | u.binding = 7; |
| 1035 | RID sdf = texture_storage->render_target_get_sdf_texture(p_to_render_target); |
| 1036 | u.append_id(sdf); |
| 1037 | uniforms.push_back(u); |
| 1038 | } |
| 1039 | |
| 1040 | { |
| 1041 | RD::Uniform u; |
| 1042 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1043 | u.binding = 9; |
| 1044 | u.append_id(RendererRD::MaterialStorage::get_singleton()->global_shader_uniforms_get_storage_buffer()); |
| 1045 | uniforms.push_back(u); |
| 1046 | } |
| 1047 | |
| 1048 | uniforms.append_array(material_storage->get_default_sampler_uniforms(SAMPLERS_BINDING_FIRST_INDEX)); |
| 1049 | |
| 1050 | RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader.default_version_rd_shader, BASE_UNIFORM_SET); |
| 1051 | if (p_backbuffer) { |
| 1052 | texture_storage->render_target_set_backbuffer_uniform_set(p_to_render_target, uniform_set); |
| 1053 | } else { |
| 1054 | texture_storage->render_target_set_framebuffer_uniform_set(p_to_render_target, uniform_set); |
| 1055 | } |
| 1056 | |
| 1057 | return uniform_set; |
| 1058 | } |
| 1059 | |
| 1060 | void RendererCanvasRenderRD::_render_items(RID 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) { |
| 1061 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 1062 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 1063 | |
| 1064 | Item *current_clip = nullptr; |
| 1065 | |
| 1066 | Transform2D canvas_transform_inverse = p_canvas_transform_inverse; |
| 1067 | |
| 1068 | RID framebuffer; |
| 1069 | RID fb_uniform_set; |
| 1070 | bool clear = false; |
| 1071 | Vector<Color> clear_colors; |
| 1072 | |
| 1073 | if (p_to_backbuffer) { |
| 1074 | framebuffer = texture_storage->render_target_get_rd_backbuffer_framebuffer(p_to_render_target); |
| 1075 | fb_uniform_set = texture_storage->render_target_get_backbuffer_uniform_set(p_to_render_target); |
| 1076 | } else { |
| 1077 | framebuffer = texture_storage->render_target_get_rd_framebuffer(p_to_render_target); |
| 1078 | |
| 1079 | if (texture_storage->render_target_is_clear_requested(p_to_render_target)) { |
| 1080 | clear = true; |
| 1081 | clear_colors.push_back(texture_storage->render_target_get_clear_request_color(p_to_render_target)); |
| 1082 | texture_storage->render_target_disable_clear_request(p_to_render_target); |
| 1083 | } |
| 1084 | // TODO: Obtain from framebuffer format eventually when this is implemented. |
| 1085 | fb_uniform_set = texture_storage->render_target_get_framebuffer_uniform_set(p_to_render_target); |
| 1086 | } |
| 1087 | |
| 1088 | if (fb_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(fb_uniform_set)) { |
| 1089 | fb_uniform_set = _create_base_uniform_set(p_to_render_target, p_to_backbuffer); |
| 1090 | } |
| 1091 | |
| 1092 | RD::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(framebuffer); |
| 1093 | |
| 1094 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, clear ? RD::INITIAL_ACTION_CLEAR : RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, clear_colors); |
| 1095 | |
| 1096 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, fb_uniform_set, BASE_UNIFORM_SET); |
| 1097 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, state.default_transforms_uniform_set, TRANSFORMS_UNIFORM_SET); |
| 1098 | |
| 1099 | RID prev_material; |
| 1100 | |
| 1101 | PipelineVariants *pipeline_variants = &shader.pipeline_variants; |
| 1102 | |
| 1103 | for (int i = 0; i < p_item_count; i++) { |
| 1104 | Item *ci = items[i]; |
| 1105 | |
| 1106 | if (current_clip != ci->final_clip_owner) { |
| 1107 | current_clip = ci->final_clip_owner; |
| 1108 | |
| 1109 | //setup clip |
| 1110 | if (current_clip) { |
| 1111 | RD::get_singleton()->draw_list_enable_scissor(draw_list, current_clip->final_clip_rect); |
| 1112 | |
| 1113 | } else { |
| 1114 | RD::get_singleton()->draw_list_disable_scissor(draw_list); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material; |
| 1119 | |
| 1120 | if (ci->use_canvas_group) { |
| 1121 | if (ci->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) { |
| 1122 | material = default_clip_children_material; |
| 1123 | } else { |
| 1124 | if (material.is_null()) { |
| 1125 | if (ci->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_ONLY) { |
| 1126 | material = default_clip_children_material; |
| 1127 | } else { |
| 1128 | material = default_canvas_group_material; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | if (material != prev_material) { |
| 1135 | CanvasMaterialData *material_data = nullptr; |
| 1136 | if (material.is_valid()) { |
| 1137 | material_data = static_cast<CanvasMaterialData *>(material_storage->material_get_data(material, RendererRD::MaterialStorage::SHADER_TYPE_2D)); |
| 1138 | } |
| 1139 | |
| 1140 | if (material_data) { |
| 1141 | if (material_data->shader_data->version.is_valid() && material_data->shader_data->valid) { |
| 1142 | pipeline_variants = &material_data->shader_data->pipeline_variants; |
| 1143 | // Update uniform set. |
| 1144 | RID uniform_set = texture_storage->render_target_is_using_hdr(p_to_render_target) ? material_data->uniform_set : material_data->uniform_set_srgb; |
| 1145 | if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) { // Material may not have a uniform set. |
| 1146 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set, MATERIAL_UNIFORM_SET); |
| 1147 | material_data->set_as_used(); |
| 1148 | } |
| 1149 | } else { |
| 1150 | pipeline_variants = &shader.pipeline_variants; |
| 1151 | } |
| 1152 | } else { |
| 1153 | pipeline_variants = &shader.pipeline_variants; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | _render_item(draw_list, p_to_render_target, ci, fb_format, canvas_transform_inverse, current_clip, p_lights, pipeline_variants, r_sdf_used); |
| 1158 | |
| 1159 | prev_material = material; |
| 1160 | } |
| 1161 | |
| 1162 | RD::get_singleton()->draw_list_end(); |
| 1163 | } |
| 1164 | |
| 1165 | 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) { |
| 1166 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 1167 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 1168 | RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); |
| 1169 | |
| 1170 | r_sdf_used = false; |
| 1171 | int item_count = 0; |
| 1172 | |
| 1173 | //setup canvas state uniforms if needed |
| 1174 | |
| 1175 | Transform2D canvas_transform_inverse = p_canvas_transform.affine_inverse(); |
| 1176 | |
| 1177 | //setup directional lights if exist |
| 1178 | |
| 1179 | uint32_t light_count = 0; |
| 1180 | uint32_t directional_light_count = 0; |
| 1181 | { |
| 1182 | Light *l = p_directional_light_list; |
| 1183 | uint32_t index = 0; |
| 1184 | |
| 1185 | while (l) { |
| 1186 | if (index == state.max_lights_per_render) { |
| 1187 | l->render_index_cache = -1; |
| 1188 | l = l->next_ptr; |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | CanvasLight *clight = canvas_light_owner.get_or_null(l->light_internal); |
| 1193 | if (!clight) { //unused or invalid texture |
| 1194 | l->render_index_cache = -1; |
| 1195 | l = l->next_ptr; |
| 1196 | ERR_CONTINUE(!clight); |
| 1197 | } |
| 1198 | |
| 1199 | Vector2 canvas_light_dir = l->xform_cache.columns[1].normalized(); |
| 1200 | |
| 1201 | state.light_uniforms[index].position[0] = -canvas_light_dir.x; |
| 1202 | state.light_uniforms[index].position[1] = -canvas_light_dir.y; |
| 1203 | |
| 1204 | _update_transform_2d_to_mat2x4(clight->shadow.directional_xform, state.light_uniforms[index].shadow_matrix); |
| 1205 | |
| 1206 | state.light_uniforms[index].height = l->height; //0..1 here |
| 1207 | |
| 1208 | for (int i = 0; i < 4; i++) { |
| 1209 | state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255)); |
| 1210 | state.light_uniforms[index].color[i] = l->color[i]; |
| 1211 | } |
| 1212 | |
| 1213 | state.light_uniforms[index].color[3] *= l->energy; //use alpha for energy, so base color can go separate |
| 1214 | |
| 1215 | if (state.shadow_fb.is_valid()) { |
| 1216 | state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth); |
| 1217 | state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far; |
| 1218 | state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset; |
| 1219 | } else { |
| 1220 | state.light_uniforms[index].shadow_pixel_size = 1.0; |
| 1221 | state.light_uniforms[index].shadow_z_far_inv = 1.0; |
| 1222 | state.light_uniforms[index].shadow_y_ofs = 0; |
| 1223 | } |
| 1224 | |
| 1225 | state.light_uniforms[index].flags = l->blend_mode << LIGHT_FLAGS_BLEND_SHIFT; |
| 1226 | state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT; |
| 1227 | if (clight->shadow.enabled) { |
| 1228 | state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW; |
| 1229 | } |
| 1230 | |
| 1231 | l->render_index_cache = index; |
| 1232 | |
| 1233 | index++; |
| 1234 | l = l->next_ptr; |
| 1235 | } |
| 1236 | |
| 1237 | light_count = index; |
| 1238 | directional_light_count = light_count; |
| 1239 | using_directional_lights = directional_light_count > 0; |
| 1240 | } |
| 1241 | |
| 1242 | //setup lights if exist |
| 1243 | |
| 1244 | { |
| 1245 | Light *l = p_light_list; |
| 1246 | uint32_t index = light_count; |
| 1247 | |
| 1248 | while (l) { |
| 1249 | if (index == state.max_lights_per_render) { |
| 1250 | l->render_index_cache = -1; |
| 1251 | l = l->next_ptr; |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
| 1255 | CanvasLight *clight = canvas_light_owner.get_or_null(l->light_internal); |
| 1256 | if (!clight) { //unused or invalid texture |
| 1257 | l->render_index_cache = -1; |
| 1258 | l = l->next_ptr; |
| 1259 | ERR_CONTINUE(!clight); |
| 1260 | } |
| 1261 | |
| 1262 | Vector2 canvas_light_pos = p_canvas_transform.xform(l->xform.get_origin()); //convert light position to canvas coordinates, as all computation is done in canvas coords to avoid precision loss |
| 1263 | state.light_uniforms[index].position[0] = canvas_light_pos.x; |
| 1264 | state.light_uniforms[index].position[1] = canvas_light_pos.y; |
| 1265 | |
| 1266 | _update_transform_2d_to_mat2x4(l->light_shader_xform.affine_inverse(), state.light_uniforms[index].matrix); |
| 1267 | _update_transform_2d_to_mat2x4(l->xform_cache.affine_inverse(), state.light_uniforms[index].shadow_matrix); |
| 1268 | |
| 1269 | 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 |
| 1270 | for (int i = 0; i < 4; i++) { |
| 1271 | state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255)); |
| 1272 | state.light_uniforms[index].color[i] = l->color[i]; |
| 1273 | } |
| 1274 | |
| 1275 | state.light_uniforms[index].color[3] *= l->energy; //use alpha for energy, so base color can go separate |
| 1276 | |
| 1277 | if (state.shadow_fb.is_valid()) { |
| 1278 | state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth); |
| 1279 | state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far; |
| 1280 | state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset; |
| 1281 | } else { |
| 1282 | state.light_uniforms[index].shadow_pixel_size = 1.0; |
| 1283 | state.light_uniforms[index].shadow_z_far_inv = 1.0; |
| 1284 | state.light_uniforms[index].shadow_y_ofs = 0; |
| 1285 | } |
| 1286 | |
| 1287 | state.light_uniforms[index].flags = l->blend_mode << LIGHT_FLAGS_BLEND_SHIFT; |
| 1288 | state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT; |
| 1289 | if (clight->shadow.enabled) { |
| 1290 | state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW; |
| 1291 | } |
| 1292 | |
| 1293 | if (clight->texture.is_valid()) { |
| 1294 | Rect2 atlas_rect = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture_rect(clight->texture); |
| 1295 | state.light_uniforms[index].atlas_rect[0] = atlas_rect.position.x; |
| 1296 | state.light_uniforms[index].atlas_rect[1] = atlas_rect.position.y; |
| 1297 | state.light_uniforms[index].atlas_rect[2] = atlas_rect.size.width; |
| 1298 | state.light_uniforms[index].atlas_rect[3] = atlas_rect.size.height; |
| 1299 | |
| 1300 | } else { |
| 1301 | state.light_uniforms[index].atlas_rect[0] = 0; |
| 1302 | state.light_uniforms[index].atlas_rect[1] = 0; |
| 1303 | state.light_uniforms[index].atlas_rect[2] = 0; |
| 1304 | state.light_uniforms[index].atlas_rect[3] = 0; |
| 1305 | } |
| 1306 | |
| 1307 | l->render_index_cache = index; |
| 1308 | |
| 1309 | index++; |
| 1310 | l = l->next_ptr; |
| 1311 | } |
| 1312 | |
| 1313 | light_count = index; |
| 1314 | } |
| 1315 | |
| 1316 | if (light_count > 0) { |
| 1317 | RD::get_singleton()->buffer_update(state.lights_uniform_buffer, 0, sizeof(LightUniform) * light_count, &state.light_uniforms[0]); |
| 1318 | } |
| 1319 | |
| 1320 | { |
| 1321 | //update canvas state uniform buffer |
| 1322 | State::Buffer state_buffer; |
| 1323 | |
| 1324 | Size2i ssize = texture_storage->render_target_get_size(p_to_render_target); |
| 1325 | |
| 1326 | Transform3D screen_transform; |
| 1327 | screen_transform.translate_local(-(ssize.width / 2.0f), -(ssize.height / 2.0f), 0.0f); |
| 1328 | screen_transform.scale(Vector3(2.0f / ssize.width, 2.0f / ssize.height, 1.0f)); |
| 1329 | _update_transform_to_mat4(screen_transform, state_buffer.screen_transform); |
| 1330 | _update_transform_2d_to_mat4(p_canvas_transform, state_buffer.canvas_transform); |
| 1331 | |
| 1332 | Transform2D normal_transform = p_canvas_transform; |
| 1333 | normal_transform.columns[0].normalize(); |
| 1334 | normal_transform.columns[1].normalize(); |
| 1335 | normal_transform.columns[2] = Vector2(); |
| 1336 | _update_transform_2d_to_mat4(normal_transform, state_buffer.canvas_normal_transform); |
| 1337 | |
| 1338 | state_buffer.canvas_modulate[0] = p_modulate.r; |
| 1339 | state_buffer.canvas_modulate[1] = p_modulate.g; |
| 1340 | state_buffer.canvas_modulate[2] = p_modulate.b; |
| 1341 | state_buffer.canvas_modulate[3] = p_modulate.a; |
| 1342 | |
| 1343 | Size2 render_target_size = texture_storage->render_target_get_size(p_to_render_target); |
| 1344 | state_buffer.screen_pixel_size[0] = 1.0 / render_target_size.x; |
| 1345 | state_buffer.screen_pixel_size[1] = 1.0 / render_target_size.y; |
| 1346 | |
| 1347 | state_buffer.time = state.time; |
| 1348 | state_buffer.use_pixel_snap = p_snap_2d_vertices_to_pixel; |
| 1349 | |
| 1350 | state_buffer.directional_light_count = directional_light_count; |
| 1351 | |
| 1352 | Vector2 canvas_scale = p_canvas_transform.get_scale(); |
| 1353 | |
| 1354 | state_buffer.sdf_to_screen[0] = render_target_size.width / canvas_scale.x; |
| 1355 | state_buffer.sdf_to_screen[1] = render_target_size.height / canvas_scale.y; |
| 1356 | |
| 1357 | state_buffer.screen_to_sdf[0] = 1.0 / state_buffer.sdf_to_screen[0]; |
| 1358 | state_buffer.screen_to_sdf[1] = 1.0 / state_buffer.sdf_to_screen[1]; |
| 1359 | |
| 1360 | Rect2 sdf_rect = texture_storage->render_target_get_sdf_rect(p_to_render_target); |
| 1361 | Rect2 sdf_tex_rect(sdf_rect.position / canvas_scale, sdf_rect.size / canvas_scale); |
| 1362 | |
| 1363 | state_buffer.sdf_to_tex[0] = 1.0 / sdf_tex_rect.size.width; |
| 1364 | state_buffer.sdf_to_tex[1] = 1.0 / sdf_tex_rect.size.height; |
| 1365 | state_buffer.sdf_to_tex[2] = -sdf_tex_rect.position.x / sdf_tex_rect.size.width; |
| 1366 | state_buffer.sdf_to_tex[3] = -sdf_tex_rect.position.y / sdf_tex_rect.size.height; |
| 1367 | |
| 1368 | //print_line("w: " + itos(ssize.width) + " s: " + rtos(canvas_scale)); |
| 1369 | state_buffer.tex_to_sdf = 1.0 / ((canvas_scale.x + canvas_scale.y) * 0.5); |
| 1370 | |
| 1371 | RD::get_singleton()->buffer_update(state.canvas_state_buffer, 0, sizeof(State::Buffer), &state_buffer); |
| 1372 | } |
| 1373 | |
| 1374 | { //default filter/repeat |
| 1375 | default_filter = p_default_filter; |
| 1376 | default_repeat = p_default_repeat; |
| 1377 | } |
| 1378 | |
| 1379 | Item *ci = p_item_list; |
| 1380 | |
| 1381 | //fill the list until rendering is possible. |
| 1382 | bool material_screen_texture_cached = false; |
| 1383 | bool material_screen_texture_mipmaps_cached = false; |
| 1384 | |
| 1385 | Rect2 back_buffer_rect; |
| 1386 | bool backbuffer_copy = false; |
| 1387 | bool backbuffer_gen_mipmaps = false; |
| 1388 | |
| 1389 | Item *canvas_group_owner = nullptr; |
| 1390 | bool skip_item = false; |
| 1391 | |
| 1392 | bool update_skeletons = false; |
| 1393 | bool time_used = false; |
| 1394 | |
| 1395 | bool backbuffer_cleared = false; |
| 1396 | |
| 1397 | while (ci) { |
| 1398 | if (ci->copy_back_buffer && canvas_group_owner == nullptr) { |
| 1399 | backbuffer_copy = true; |
| 1400 | |
| 1401 | if (ci->copy_back_buffer->full) { |
| 1402 | back_buffer_rect = Rect2(); |
| 1403 | } else { |
| 1404 | back_buffer_rect = ci->copy_back_buffer->rect; |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | RID material = ci->material_owner == nullptr ? ci->material : ci->material_owner->material; |
| 1409 | |
| 1410 | if (material.is_valid()) { |
| 1411 | CanvasMaterialData *md = static_cast<CanvasMaterialData *>(material_storage->material_get_data(material, RendererRD::MaterialStorage::SHADER_TYPE_2D)); |
| 1412 | if (md && md->shader_data->valid) { |
| 1413 | if (md->shader_data->uses_screen_texture && canvas_group_owner == nullptr) { |
| 1414 | if (!material_screen_texture_cached) { |
| 1415 | backbuffer_copy = true; |
| 1416 | back_buffer_rect = Rect2(); |
| 1417 | backbuffer_gen_mipmaps = md->shader_data->uses_screen_texture_mipmaps; |
| 1418 | } else if (!material_screen_texture_mipmaps_cached) { |
| 1419 | backbuffer_gen_mipmaps = md->shader_data->uses_screen_texture_mipmaps; |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | if (md->shader_data->uses_sdf) { |
| 1424 | r_sdf_used = true; |
| 1425 | } |
| 1426 | if (md->shader_data->uses_time) { |
| 1427 | time_used = true; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | if (ci->skeleton.is_valid()) { |
| 1433 | const Item::Command *c = ci->commands; |
| 1434 | |
| 1435 | while (c) { |
| 1436 | if (c->type == Item::Command::TYPE_MESH) { |
| 1437 | const Item::CommandMesh *cm = static_cast<const Item::CommandMesh *>(c); |
| 1438 | if (cm->mesh_instance.is_valid()) { |
| 1439 | mesh_storage->mesh_instance_check_for_update(cm->mesh_instance); |
| 1440 | mesh_storage->mesh_instance_set_canvas_item_transform(cm->mesh_instance, canvas_transform_inverse * ci->final_transform); |
| 1441 | update_skeletons = true; |
| 1442 | } |
| 1443 | } |
| 1444 | c = c->next; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | if (ci->canvas_group_owner != nullptr) { |
| 1449 | if (canvas_group_owner == nullptr) { |
| 1450 | // Canvas group begins here, render until before this item |
| 1451 | if (update_skeletons) { |
| 1452 | mesh_storage->update_mesh_instances(); |
| 1453 | update_skeletons = false; |
| 1454 | } |
| 1455 | |
| 1456 | _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used); |
| 1457 | item_count = 0; |
| 1458 | |
| 1459 | if (ci->canvas_group_owner->canvas_group->mode != RS::CANVAS_GROUP_MODE_TRANSPARENT) { |
| 1460 | Rect2i group_rect = ci->canvas_group_owner->global_rect_cache; |
| 1461 | texture_storage->render_target_copy_to_back_buffer(p_to_render_target, group_rect, false); |
| 1462 | if (ci->canvas_group_owner->canvas_group->mode == RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) { |
| 1463 | ci->canvas_group_owner->use_canvas_group = false; |
| 1464 | items[item_count++] = ci->canvas_group_owner; |
| 1465 | } |
| 1466 | } else if (!backbuffer_cleared) { |
| 1467 | texture_storage->render_target_clear_back_buffer(p_to_render_target, Rect2i(), Color(0, 0, 0, 0)); |
| 1468 | backbuffer_cleared = true; |
| 1469 | } |
| 1470 | |
| 1471 | backbuffer_copy = false; |
| 1472 | canvas_group_owner = ci->canvas_group_owner; //continue until owner found |
| 1473 | } |
| 1474 | |
| 1475 | ci->canvas_group_owner = nullptr; //must be cleared |
| 1476 | } |
| 1477 | |
| 1478 | if (canvas_group_owner == nullptr && ci->canvas_group != nullptr && ci->canvas_group->mode != RS::CANVAS_GROUP_MODE_CLIP_AND_DRAW) { |
| 1479 | skip_item = true; |
| 1480 | } |
| 1481 | |
| 1482 | if (ci == canvas_group_owner) { |
| 1483 | if (update_skeletons) { |
| 1484 | mesh_storage->update_mesh_instances(); |
| 1485 | update_skeletons = false; |
| 1486 | } |
| 1487 | |
| 1488 | _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, true); |
| 1489 | item_count = 0; |
| 1490 | |
| 1491 | if (ci->canvas_group->blur_mipmaps) { |
| 1492 | texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, ci->global_rect_cache); |
| 1493 | } |
| 1494 | |
| 1495 | canvas_group_owner = nullptr; |
| 1496 | // Backbuffer is dirty now and needs to be re-cleared if another CanvasGroup needs it. |
| 1497 | backbuffer_cleared = false; |
| 1498 | |
| 1499 | // Tell the renderer to paint this as a canvas group |
| 1500 | ci->use_canvas_group = true; |
| 1501 | } else { |
| 1502 | ci->use_canvas_group = false; |
| 1503 | } |
| 1504 | |
| 1505 | if (backbuffer_copy) { |
| 1506 | //render anything pending, including clearing if no items |
| 1507 | if (update_skeletons) { |
| 1508 | mesh_storage->update_mesh_instances(); |
| 1509 | update_skeletons = false; |
| 1510 | } |
| 1511 | |
| 1512 | _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used); |
| 1513 | item_count = 0; |
| 1514 | |
| 1515 | texture_storage->render_target_copy_to_back_buffer(p_to_render_target, back_buffer_rect, backbuffer_gen_mipmaps); |
| 1516 | |
| 1517 | backbuffer_copy = false; |
| 1518 | material_screen_texture_cached = true; // After a backbuffer copy, screen texture makes no further copies. |
| 1519 | material_screen_texture_mipmaps_cached = backbuffer_gen_mipmaps; |
| 1520 | backbuffer_gen_mipmaps = false; |
| 1521 | } |
| 1522 | |
| 1523 | if (backbuffer_gen_mipmaps) { |
| 1524 | texture_storage->render_target_gen_back_buffer_mipmaps(p_to_render_target, back_buffer_rect); |
| 1525 | |
| 1526 | backbuffer_gen_mipmaps = false; |
| 1527 | material_screen_texture_mipmaps_cached = true; |
| 1528 | } |
| 1529 | |
| 1530 | if (skip_item) { |
| 1531 | skip_item = false; |
| 1532 | } else { |
| 1533 | items[item_count++] = ci; |
| 1534 | } |
| 1535 | |
| 1536 | if (!ci->next || item_count == MAX_RENDER_ITEMS - 1) { |
| 1537 | if (update_skeletons) { |
| 1538 | mesh_storage->update_mesh_instances(); |
| 1539 | update_skeletons = false; |
| 1540 | } |
| 1541 | |
| 1542 | _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, r_sdf_used, canvas_group_owner != nullptr); |
| 1543 | //then reset |
| 1544 | item_count = 0; |
| 1545 | } |
| 1546 | |
| 1547 | ci = ci->next; |
| 1548 | } |
| 1549 | |
| 1550 | if (time_used) { |
| 1551 | RenderingServerDefault::redraw_request(); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | RID RendererCanvasRenderRD::light_create() { |
| 1556 | CanvasLight canvas_light; |
| 1557 | return canvas_light_owner.make_rid(canvas_light); |
| 1558 | } |
| 1559 | |
| 1560 | void RendererCanvasRenderRD::light_set_texture(RID p_rid, RID p_texture) { |
| 1561 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 1562 | |
| 1563 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 1564 | ERR_FAIL_COND(!cl); |
| 1565 | if (cl->texture == p_texture) { |
| 1566 | return; |
| 1567 | } |
| 1568 | |
| 1569 | ERR_FAIL_COND(p_texture.is_valid() && !texture_storage->owns_texture(p_texture)); |
| 1570 | |
| 1571 | if (cl->texture.is_valid()) { |
| 1572 | texture_storage->texture_remove_from_decal_atlas(cl->texture); |
| 1573 | } |
| 1574 | cl->texture = p_texture; |
| 1575 | |
| 1576 | if (cl->texture.is_valid()) { |
| 1577 | texture_storage->texture_add_to_decal_atlas(cl->texture); |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | void RendererCanvasRenderRD::light_set_use_shadow(RID p_rid, bool p_enable) { |
| 1582 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 1583 | ERR_FAIL_COND(!cl); |
| 1584 | |
| 1585 | cl->shadow.enabled = p_enable; |
| 1586 | } |
| 1587 | |
| 1588 | void RendererCanvasRenderRD::_update_shadow_atlas() { |
| 1589 | if (state.shadow_fb == RID()) { |
| 1590 | //ah, we lack the shadow texture.. |
| 1591 | RD::get_singleton()->free(state.shadow_texture); //erase placeholder |
| 1592 | |
| 1593 | Vector<RID> fb_textures; |
| 1594 | |
| 1595 | { //texture |
| 1596 | RD::TextureFormat tf; |
| 1597 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 1598 | tf.width = state.shadow_texture_size; |
| 1599 | tf.height = state.max_lights_per_render * 2; |
| 1600 | tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT; |
| 1601 | tf.format = RD::DATA_FORMAT_R32_SFLOAT; |
| 1602 | |
| 1603 | state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 1604 | fb_textures.push_back(state.shadow_texture); |
| 1605 | } |
| 1606 | { |
| 1607 | RD::TextureFormat tf; |
| 1608 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 1609 | tf.width = state.shadow_texture_size; |
| 1610 | tf.height = state.max_lights_per_render * 2; |
| 1611 | tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 1612 | tf.format = RD::DATA_FORMAT_D32_SFLOAT; |
| 1613 | //chunks to write |
| 1614 | state.shadow_depth_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 1615 | fb_textures.push_back(state.shadow_depth_texture); |
| 1616 | } |
| 1617 | |
| 1618 | state.shadow_fb = RD::get_singleton()->framebuffer_create(fb_textures); |
| 1619 | } |
| 1620 | } |
| 1621 | 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) { |
| 1622 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 1623 | ERR_FAIL_COND(!cl->shadow.enabled); |
| 1624 | |
| 1625 | _update_shadow_atlas(); |
| 1626 | |
| 1627 | cl->shadow.z_far = p_far; |
| 1628 | cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(state.max_lights_per_render * 2); |
| 1629 | Vector<Color> cc; |
| 1630 | cc.push_back(Color(p_far, p_far, p_far, 1.0)); |
| 1631 | |
| 1632 | for (int i = 0; i < 4; i++) { |
| 1633 | //make sure it remains orthogonal, makes easy to read angle later |
| 1634 | |
| 1635 | //light.basis.scale(Vector3(to_light.elements[0].length(),to_light.elements[1].length(),1)); |
| 1636 | |
| 1637 | Rect2i rect((state.shadow_texture_size / 4) * i, p_shadow_index * 2, (state.shadow_texture_size / 4), 2); |
| 1638 | RD::InitialAction initial_action = i == 0 ? RD::INITIAL_ACTION_CLEAR_REGION : RD::INITIAL_ACTION_CLEAR_REGION_CONTINUE; |
| 1639 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(state.shadow_fb, initial_action, i != 3 ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ, initial_action, RD::FINAL_ACTION_DISCARD, cc, 1.0, 0, rect); |
| 1640 | |
| 1641 | Projection projection; |
| 1642 | { |
| 1643 | real_t fov = 90; |
| 1644 | real_t nearp = p_near; |
| 1645 | real_t farp = p_far; |
| 1646 | real_t aspect = 1.0; |
| 1647 | |
| 1648 | real_t ymax = nearp * Math::tan(Math::deg_to_rad(fov * 0.5)); |
| 1649 | real_t ymin = -ymax; |
| 1650 | real_t xmin = ymin * aspect; |
| 1651 | real_t xmax = ymax * aspect; |
| 1652 | |
| 1653 | projection.set_frustum(xmin, xmax, ymin, ymax, nearp, farp); |
| 1654 | } |
| 1655 | |
| 1656 | Vector3 cam_target = Basis::from_euler(Vector3(0, 0, Math_TAU * ((i + 3) / 4.0))).xform(Vector3(0, 1, 0)); |
| 1657 | projection = projection * Projection(Transform3D().looking_at(cam_target, Vector3(0, 0, -1)).affine_inverse()); |
| 1658 | |
| 1659 | ShadowRenderPushConstant push_constant; |
| 1660 | for (int y = 0; y < 4; y++) { |
| 1661 | for (int x = 0; x < 4; x++) { |
| 1662 | push_constant.projection[y * 4 + x] = projection.columns[y][x]; |
| 1663 | } |
| 1664 | } |
| 1665 | static const Vector2 directions[4] = { Vector2(1, 0), Vector2(0, 1), Vector2(-1, 0), Vector2(0, -1) }; |
| 1666 | push_constant.direction[0] = directions[i].x; |
| 1667 | push_constant.direction[1] = directions[i].y; |
| 1668 | push_constant.z_far = p_far; |
| 1669 | push_constant.pad = 0; |
| 1670 | |
| 1671 | LightOccluderInstance *instance = p_occluders; |
| 1672 | |
| 1673 | while (instance) { |
| 1674 | OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder); |
| 1675 | |
| 1676 | if (!co || co->index_array.is_null() || !(p_light_mask & instance->light_mask)) { |
| 1677 | instance = instance->next; |
| 1678 | continue; |
| 1679 | } |
| 1680 | |
| 1681 | _update_transform_2d_to_mat2x4(p_light_xform * instance->xform_cache, push_constant.modelview); |
| 1682 | |
| 1683 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, shadow_render.render_pipelines[co->cull_mode]); |
| 1684 | RD::get_singleton()->draw_list_bind_vertex_array(draw_list, co->vertex_array); |
| 1685 | RD::get_singleton()->draw_list_bind_index_array(draw_list, co->index_array); |
| 1686 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowRenderPushConstant)); |
| 1687 | |
| 1688 | RD::get_singleton()->draw_list_draw(draw_list, true); |
| 1689 | |
| 1690 | instance = instance->next; |
| 1691 | } |
| 1692 | |
| 1693 | RD::get_singleton()->draw_list_end(); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | 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) { |
| 1698 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 1699 | ERR_FAIL_COND(!cl->shadow.enabled); |
| 1700 | |
| 1701 | _update_shadow_atlas(); |
| 1702 | |
| 1703 | Vector2 light_dir = p_light_xform.columns[1].normalized(); |
| 1704 | |
| 1705 | Vector2 center = p_clip_rect.get_center(); |
| 1706 | |
| 1707 | float to_edge_distance = ABS(light_dir.dot(p_clip_rect.get_support(light_dir)) - light_dir.dot(center)); |
| 1708 | |
| 1709 | Vector2 from_pos = center - light_dir * (to_edge_distance + p_cull_distance); |
| 1710 | float distance = to_edge_distance * 2.0 + p_cull_distance; |
| 1711 | float half_size = p_clip_rect.size.length() * 0.5; //shadow length, must keep this no matter the angle |
| 1712 | |
| 1713 | cl->shadow.z_far = distance; |
| 1714 | cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(state.max_lights_per_render * 2); |
| 1715 | |
| 1716 | Transform2D to_light_xform; |
| 1717 | |
| 1718 | to_light_xform[2] = from_pos; |
| 1719 | to_light_xform[1] = light_dir; |
| 1720 | to_light_xform[0] = -light_dir.orthogonal(); |
| 1721 | |
| 1722 | to_light_xform.invert(); |
| 1723 | |
| 1724 | Vector<Color> cc; |
| 1725 | cc.push_back(Color(1, 1, 1, 1)); |
| 1726 | |
| 1727 | Rect2i rect(0, p_shadow_index * 2, state.shadow_texture_size, 2); |
| 1728 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(state.shadow_fb, RD::INITIAL_ACTION_CLEAR_REGION, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR_REGION, RD::FINAL_ACTION_DISCARD, cc, 1.0, 0, rect); |
| 1729 | |
| 1730 | Projection projection; |
| 1731 | projection.set_orthogonal(-half_size, half_size, -0.5, 0.5, 0.0, distance); |
| 1732 | projection = projection * Projection(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, -1)).affine_inverse()); |
| 1733 | |
| 1734 | ShadowRenderPushConstant push_constant; |
| 1735 | for (int y = 0; y < 4; y++) { |
| 1736 | for (int x = 0; x < 4; x++) { |
| 1737 | push_constant.projection[y * 4 + x] = projection.columns[y][x]; |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | push_constant.direction[0] = 0.0; |
| 1742 | push_constant.direction[1] = 1.0; |
| 1743 | push_constant.z_far = distance; |
| 1744 | push_constant.pad = 0; |
| 1745 | |
| 1746 | LightOccluderInstance *instance = p_occluders; |
| 1747 | |
| 1748 | while (instance) { |
| 1749 | OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder); |
| 1750 | |
| 1751 | if (!co || co->index_array.is_null() || !(p_light_mask & instance->light_mask)) { |
| 1752 | instance = instance->next; |
| 1753 | continue; |
| 1754 | } |
| 1755 | |
| 1756 | _update_transform_2d_to_mat2x4(to_light_xform * instance->xform_cache, push_constant.modelview); |
| 1757 | |
| 1758 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, shadow_render.render_pipelines[co->cull_mode]); |
| 1759 | RD::get_singleton()->draw_list_bind_vertex_array(draw_list, co->vertex_array); |
| 1760 | RD::get_singleton()->draw_list_bind_index_array(draw_list, co->index_array); |
| 1761 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowRenderPushConstant)); |
| 1762 | |
| 1763 | RD::get_singleton()->draw_list_draw(draw_list, true); |
| 1764 | |
| 1765 | instance = instance->next; |
| 1766 | } |
| 1767 | |
| 1768 | RD::get_singleton()->draw_list_end(); |
| 1769 | |
| 1770 | Transform2D to_shadow; |
| 1771 | to_shadow.columns[0].x = 1.0 / -(half_size * 2.0); |
| 1772 | to_shadow.columns[2].x = 0.5; |
| 1773 | |
| 1774 | cl->shadow.directional_xform = to_shadow * to_light_xform; |
| 1775 | } |
| 1776 | |
| 1777 | void RendererCanvasRenderRD::render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) { |
| 1778 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 1779 | |
| 1780 | RID fb = texture_storage->render_target_get_sdf_framebuffer(p_render_target); |
| 1781 | Rect2i rect = texture_storage->render_target_get_sdf_rect(p_render_target); |
| 1782 | |
| 1783 | Transform2D to_sdf; |
| 1784 | to_sdf.columns[0] *= rect.size.width; |
| 1785 | to_sdf.columns[1] *= rect.size.height; |
| 1786 | to_sdf.columns[2] = rect.position; |
| 1787 | |
| 1788 | Transform2D to_clip; |
| 1789 | to_clip.columns[0] *= 2.0; |
| 1790 | to_clip.columns[1] *= 2.0; |
| 1791 | to_clip.columns[2] = -Vector2(1.0, 1.0); |
| 1792 | |
| 1793 | to_clip = to_clip * to_sdf.affine_inverse(); |
| 1794 | |
| 1795 | Vector<Color> cc; |
| 1796 | cc.push_back(Color(0, 0, 0, 0)); |
| 1797 | |
| 1798 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(fb, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, cc); |
| 1799 | |
| 1800 | Projection projection; |
| 1801 | |
| 1802 | ShadowRenderPushConstant push_constant; |
| 1803 | for (int y = 0; y < 4; y++) { |
| 1804 | for (int x = 0; x < 4; x++) { |
| 1805 | push_constant.projection[y * 4 + x] = projection.columns[y][x]; |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | push_constant.direction[0] = 0.0; |
| 1810 | push_constant.direction[1] = 0.0; |
| 1811 | push_constant.z_far = 0; |
| 1812 | push_constant.pad = 0; |
| 1813 | |
| 1814 | LightOccluderInstance *instance = p_occluders; |
| 1815 | |
| 1816 | while (instance) { |
| 1817 | OccluderPolygon *co = occluder_polygon_owner.get_or_null(instance->occluder); |
| 1818 | |
| 1819 | if (!co || co->sdf_index_array.is_null()) { |
| 1820 | instance = instance->next; |
| 1821 | continue; |
| 1822 | } |
| 1823 | |
| 1824 | _update_transform_2d_to_mat2x4(to_clip * instance->xform_cache, push_constant.modelview); |
| 1825 | |
| 1826 | 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]); |
| 1827 | RD::get_singleton()->draw_list_bind_vertex_array(draw_list, co->sdf_vertex_array); |
| 1828 | RD::get_singleton()->draw_list_bind_index_array(draw_list, co->sdf_index_array); |
| 1829 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowRenderPushConstant)); |
| 1830 | |
| 1831 | RD::get_singleton()->draw_list_draw(draw_list, true); |
| 1832 | |
| 1833 | instance = instance->next; |
| 1834 | } |
| 1835 | |
| 1836 | RD::get_singleton()->draw_list_end(); |
| 1837 | |
| 1838 | texture_storage->render_target_sdf_process(p_render_target); //done rendering, process it |
| 1839 | } |
| 1840 | |
| 1841 | RID RendererCanvasRenderRD::occluder_polygon_create() { |
| 1842 | OccluderPolygon occluder; |
| 1843 | occluder.line_point_count = 0; |
| 1844 | occluder.sdf_point_count = 0; |
| 1845 | occluder.sdf_index_count = 0; |
| 1846 | occluder.cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED; |
| 1847 | return occluder_polygon_owner.make_rid(occluder); |
| 1848 | } |
| 1849 | |
| 1850 | void RendererCanvasRenderRD::occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) { |
| 1851 | OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder); |
| 1852 | ERR_FAIL_COND(!oc); |
| 1853 | |
| 1854 | Vector<Vector2> lines; |
| 1855 | |
| 1856 | if (p_points.size()) { |
| 1857 | int lc = p_points.size() * 2; |
| 1858 | |
| 1859 | lines.resize(lc - (p_closed ? 0 : 2)); |
| 1860 | { |
| 1861 | Vector2 *w = lines.ptrw(); |
| 1862 | const Vector2 *r = p_points.ptr(); |
| 1863 | |
| 1864 | int max = lc / 2; |
| 1865 | if (!p_closed) { |
| 1866 | max--; |
| 1867 | } |
| 1868 | for (int i = 0; i < max; i++) { |
| 1869 | Vector2 a = r[i]; |
| 1870 | Vector2 b = r[(i + 1) % (lc / 2)]; |
| 1871 | w[i * 2 + 0] = a; |
| 1872 | w[i * 2 + 1] = b; |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | if ((oc->line_point_count != lines.size() || lines.size() == 0) && oc->vertex_array.is_valid()) { |
| 1878 | RD::get_singleton()->free(oc->vertex_array); |
| 1879 | RD::get_singleton()->free(oc->vertex_buffer); |
| 1880 | RD::get_singleton()->free(oc->index_array); |
| 1881 | RD::get_singleton()->free(oc->index_buffer); |
| 1882 | |
| 1883 | oc->vertex_array = RID(); |
| 1884 | oc->vertex_buffer = RID(); |
| 1885 | oc->index_array = RID(); |
| 1886 | oc->index_buffer = RID(); |
| 1887 | |
| 1888 | oc->line_point_count = lines.size(); |
| 1889 | } |
| 1890 | |
| 1891 | if (lines.size()) { |
| 1892 | oc->line_point_count = lines.size(); |
| 1893 | Vector<uint8_t> geometry; |
| 1894 | Vector<uint8_t> indices; |
| 1895 | int lc = lines.size(); |
| 1896 | |
| 1897 | geometry.resize(lc * 6 * sizeof(float)); |
| 1898 | indices.resize(lc * 3 * sizeof(uint16_t)); |
| 1899 | |
| 1900 | { |
| 1901 | uint8_t *vw = geometry.ptrw(); |
| 1902 | float *vwptr = reinterpret_cast<float *>(vw); |
| 1903 | uint8_t *iw = indices.ptrw(); |
| 1904 | uint16_t *iwptr = (uint16_t *)iw; |
| 1905 | |
| 1906 | const Vector2 *lr = lines.ptr(); |
| 1907 | |
| 1908 | const int POLY_HEIGHT = 16384; |
| 1909 | |
| 1910 | for (int i = 0; i < lc / 2; i++) { |
| 1911 | vwptr[i * 12 + 0] = lr[i * 2 + 0].x; |
| 1912 | vwptr[i * 12 + 1] = lr[i * 2 + 0].y; |
| 1913 | vwptr[i * 12 + 2] = POLY_HEIGHT; |
| 1914 | |
| 1915 | vwptr[i * 12 + 3] = lr[i * 2 + 1].x; |
| 1916 | vwptr[i * 12 + 4] = lr[i * 2 + 1].y; |
| 1917 | vwptr[i * 12 + 5] = POLY_HEIGHT; |
| 1918 | |
| 1919 | vwptr[i * 12 + 6] = lr[i * 2 + 1].x; |
| 1920 | vwptr[i * 12 + 7] = lr[i * 2 + 1].y; |
| 1921 | vwptr[i * 12 + 8] = -POLY_HEIGHT; |
| 1922 | |
| 1923 | vwptr[i * 12 + 9] = lr[i * 2 + 0].x; |
| 1924 | vwptr[i * 12 + 10] = lr[i * 2 + 0].y; |
| 1925 | vwptr[i * 12 + 11] = -POLY_HEIGHT; |
| 1926 | |
| 1927 | iwptr[i * 6 + 0] = i * 4 + 0; |
| 1928 | iwptr[i * 6 + 1] = i * 4 + 1; |
| 1929 | iwptr[i * 6 + 2] = i * 4 + 2; |
| 1930 | |
| 1931 | iwptr[i * 6 + 3] = i * 4 + 2; |
| 1932 | iwptr[i * 6 + 4] = i * 4 + 3; |
| 1933 | iwptr[i * 6 + 5] = i * 4 + 0; |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | //if same buffer len is being set, just use buffer_update to avoid a pipeline flush |
| 1938 | |
| 1939 | if (oc->vertex_array.is_null()) { |
| 1940 | //create from scratch |
| 1941 | //vertices |
| 1942 | // TODO: geometry is always of length lc * 6 * sizeof(float), so in doubles builds this will receive half the data it needs |
| 1943 | oc->vertex_buffer = RD::get_singleton()->vertex_buffer_create(lc * 6 * sizeof(real_t), geometry); |
| 1944 | |
| 1945 | Vector<RID> buffer; |
| 1946 | buffer.push_back(oc->vertex_buffer); |
| 1947 | oc->vertex_array = RD::get_singleton()->vertex_array_create(4 * lc / 2, shadow_render.vertex_format, buffer); |
| 1948 | //indices |
| 1949 | |
| 1950 | oc->index_buffer = RD::get_singleton()->index_buffer_create(3 * lc, RD::INDEX_BUFFER_FORMAT_UINT16, indices); |
| 1951 | oc->index_array = RD::get_singleton()->index_array_create(oc->index_buffer, 0, 3 * lc); |
| 1952 | |
| 1953 | } else { |
| 1954 | //update existing |
| 1955 | const uint8_t *vr = geometry.ptr(); |
| 1956 | RD::get_singleton()->buffer_update(oc->vertex_buffer, 0, geometry.size(), vr); |
| 1957 | const uint8_t *ir = indices.ptr(); |
| 1958 | RD::get_singleton()->buffer_update(oc->index_buffer, 0, indices.size(), ir); |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | // sdf |
| 1963 | |
| 1964 | Vector<int> sdf_indices; |
| 1965 | |
| 1966 | if (p_points.size()) { |
| 1967 | if (p_closed) { |
| 1968 | sdf_indices = Geometry2D::triangulate_polygon(p_points); |
| 1969 | oc->sdf_is_lines = false; |
| 1970 | } else { |
| 1971 | int max = p_points.size(); |
| 1972 | sdf_indices.resize(max * 2); |
| 1973 | |
| 1974 | int *iw = sdf_indices.ptrw(); |
| 1975 | for (int i = 0; i < max; i++) { |
| 1976 | iw[i * 2 + 0] = i; |
| 1977 | iw[i * 2 + 1] = (i + 1) % max; |
| 1978 | } |
| 1979 | oc->sdf_is_lines = true; |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | if (((oc->sdf_index_count != sdf_indices.size() && oc->sdf_point_count != p_points.size()) || p_points.size() == 0) && oc->sdf_vertex_array.is_valid()) { |
| 1984 | RD::get_singleton()->free(oc->sdf_vertex_array); |
| 1985 | RD::get_singleton()->free(oc->sdf_vertex_buffer); |
| 1986 | RD::get_singleton()->free(oc->sdf_index_array); |
| 1987 | RD::get_singleton()->free(oc->sdf_index_buffer); |
| 1988 | |
| 1989 | oc->sdf_vertex_array = RID(); |
| 1990 | oc->sdf_vertex_buffer = RID(); |
| 1991 | oc->sdf_index_array = RID(); |
| 1992 | oc->sdf_index_buffer = RID(); |
| 1993 | |
| 1994 | oc->sdf_index_count = sdf_indices.size(); |
| 1995 | oc->sdf_point_count = p_points.size(); |
| 1996 | |
| 1997 | oc->sdf_is_lines = false; |
| 1998 | } |
| 1999 | |
| 2000 | if (sdf_indices.size()) { |
| 2001 | if (oc->sdf_vertex_array.is_null()) { |
| 2002 | //create from scratch |
| 2003 | //vertices |
| 2004 | oc->sdf_vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_points.size() * 2 * sizeof(real_t), p_points.to_byte_array()); |
| 2005 | oc->sdf_index_buffer = RD::get_singleton()->index_buffer_create(sdf_indices.size(), RD::INDEX_BUFFER_FORMAT_UINT32, sdf_indices.to_byte_array()); |
| 2006 | oc->sdf_index_array = RD::get_singleton()->index_array_create(oc->sdf_index_buffer, 0, sdf_indices.size()); |
| 2007 | |
| 2008 | Vector<RID> buffer; |
| 2009 | buffer.push_back(oc->sdf_vertex_buffer); |
| 2010 | oc->sdf_vertex_array = RD::get_singleton()->vertex_array_create(p_points.size(), shadow_render.sdf_vertex_format, buffer); |
| 2011 | //indices |
| 2012 | |
| 2013 | } else { |
| 2014 | //update existing |
| 2015 | RD::get_singleton()->buffer_update(oc->sdf_vertex_buffer, 0, sizeof(real_t) * 2 * p_points.size(), p_points.ptr()); |
| 2016 | RD::get_singleton()->buffer_update(oc->sdf_index_buffer, 0, sdf_indices.size() * sizeof(int32_t), sdf_indices.ptr()); |
| 2017 | } |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | void RendererCanvasRenderRD::occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) { |
| 2022 | OccluderPolygon *oc = occluder_polygon_owner.get_or_null(p_occluder); |
| 2023 | ERR_FAIL_COND(!oc); |
| 2024 | oc->cull_mode = p_mode; |
| 2025 | } |
| 2026 | |
| 2027 | void RendererCanvasRenderRD::CanvasShaderData::set_code(const String &p_code) { |
| 2028 | //compile |
| 2029 | |
| 2030 | code = p_code; |
| 2031 | valid = false; |
| 2032 | ubo_size = 0; |
| 2033 | uniforms.clear(); |
| 2034 | uses_screen_texture = false; |
| 2035 | uses_screen_texture_mipmaps = false; |
| 2036 | uses_sdf = false; |
| 2037 | uses_time = false; |
| 2038 | |
| 2039 | if (code.is_empty()) { |
| 2040 | return; //just invalid, but no error |
| 2041 | } |
| 2042 | |
| 2043 | ShaderCompiler::GeneratedCode gen_code; |
| 2044 | |
| 2045 | int blend_mode = BLEND_MODE_MIX; |
| 2046 | |
| 2047 | ShaderCompiler::IdentifierActions actions; |
| 2048 | actions.entry_point_stages["vertex" ] = ShaderCompiler::STAGE_VERTEX; |
| 2049 | actions.entry_point_stages["fragment" ] = ShaderCompiler::STAGE_FRAGMENT; |
| 2050 | actions.entry_point_stages["light" ] = ShaderCompiler::STAGE_FRAGMENT; |
| 2051 | |
| 2052 | actions.render_mode_values["blend_add" ] = Pair<int *, int>(&blend_mode, BLEND_MODE_ADD); |
| 2053 | actions.render_mode_values["blend_mix" ] = Pair<int *, int>(&blend_mode, BLEND_MODE_MIX); |
| 2054 | actions.render_mode_values["blend_sub" ] = Pair<int *, int>(&blend_mode, BLEND_MODE_SUB); |
| 2055 | actions.render_mode_values["blend_mul" ] = Pair<int *, int>(&blend_mode, BLEND_MODE_MUL); |
| 2056 | actions.render_mode_values["blend_premul_alpha" ] = Pair<int *, int>(&blend_mode, BLEND_MODE_PMALPHA); |
| 2057 | actions.render_mode_values["blend_disabled" ] = Pair<int *, int>(&blend_mode, BLEND_MODE_DISABLED); |
| 2058 | |
| 2059 | actions.usage_flag_pointers["texture_sdf" ] = &uses_sdf; |
| 2060 | actions.usage_flag_pointers["TIME" ] = &uses_time; |
| 2061 | |
| 2062 | actions.uniforms = &uniforms; |
| 2063 | |
| 2064 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 2065 | |
| 2066 | Error err = canvas_singleton->shader.compiler.compile(RS::SHADER_CANVAS_ITEM, code, &actions, path, gen_code); |
| 2067 | ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed." ); |
| 2068 | |
| 2069 | uses_screen_texture_mipmaps = gen_code.uses_screen_texture_mipmaps; |
| 2070 | uses_screen_texture = gen_code.uses_screen_texture; |
| 2071 | |
| 2072 | if (version.is_null()) { |
| 2073 | version = canvas_singleton->shader.canvas_shader.version_create(); |
| 2074 | } |
| 2075 | |
| 2076 | #if 0 |
| 2077 | print_line("**compiling shader:" ); |
| 2078 | print_line("**defines:\n" ); |
| 2079 | for (int i = 0; i < gen_code.defines.size(); i++) { |
| 2080 | print_line(gen_code.defines[i]); |
| 2081 | } |
| 2082 | |
| 2083 | HashMap<String, String>::Iterator el = gen_code.code.begin(); |
| 2084 | while (el) { |
| 2085 | print_line("\n**code " + el->key + ":\n" + el->value); |
| 2086 | ++el; |
| 2087 | } |
| 2088 | |
| 2089 | print_line("\n**uniforms:\n" + gen_code.uniforms); |
| 2090 | print_line("\n**vertex_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX]); |
| 2091 | print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]); |
| 2092 | #endif |
| 2093 | 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); |
| 2094 | ERR_FAIL_COND(!canvas_singleton->shader.canvas_shader.version_is_valid(version)); |
| 2095 | |
| 2096 | ubo_size = gen_code.uniform_total_size; |
| 2097 | ubo_offsets = gen_code.uniform_offsets; |
| 2098 | texture_uniforms = gen_code.texture_uniforms; |
| 2099 | |
| 2100 | //update them pipelines |
| 2101 | |
| 2102 | RD::PipelineColorBlendState::Attachment attachment; |
| 2103 | |
| 2104 | switch (blend_mode) { |
| 2105 | case BLEND_MODE_DISABLED: { |
| 2106 | // nothing to do here, disabled by default |
| 2107 | |
| 2108 | } break; |
| 2109 | case BLEND_MODE_MIX: { |
| 2110 | attachment.enable_blend = true; |
| 2111 | attachment.color_blend_op = RD::BLEND_OP_ADD; |
| 2112 | attachment.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA; |
| 2113 | attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 2114 | |
| 2115 | attachment.alpha_blend_op = RD::BLEND_OP_ADD; |
| 2116 | attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2117 | attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 2118 | |
| 2119 | } break; |
| 2120 | case BLEND_MODE_ADD: { |
| 2121 | attachment.enable_blend = true; |
| 2122 | attachment.alpha_blend_op = RD::BLEND_OP_ADD; |
| 2123 | attachment.color_blend_op = RD::BLEND_OP_ADD; |
| 2124 | attachment.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA; |
| 2125 | attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2126 | attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA; |
| 2127 | attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2128 | |
| 2129 | } break; |
| 2130 | case BLEND_MODE_SUB: { |
| 2131 | attachment.enable_blend = true; |
| 2132 | attachment.alpha_blend_op = RD::BLEND_OP_REVERSE_SUBTRACT; |
| 2133 | attachment.color_blend_op = RD::BLEND_OP_REVERSE_SUBTRACT; |
| 2134 | attachment.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA; |
| 2135 | attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2136 | attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA; |
| 2137 | attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2138 | |
| 2139 | } break; |
| 2140 | case BLEND_MODE_MUL: { |
| 2141 | attachment.enable_blend = true; |
| 2142 | attachment.alpha_blend_op = RD::BLEND_OP_ADD; |
| 2143 | attachment.color_blend_op = RD::BLEND_OP_ADD; |
| 2144 | attachment.src_color_blend_factor = RD::BLEND_FACTOR_DST_COLOR; |
| 2145 | attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ZERO; |
| 2146 | attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_DST_ALPHA; |
| 2147 | attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ZERO; |
| 2148 | |
| 2149 | } break; |
| 2150 | case BLEND_MODE_PMALPHA: { |
| 2151 | attachment.enable_blend = true; |
| 2152 | attachment.alpha_blend_op = RD::BLEND_OP_ADD; |
| 2153 | attachment.color_blend_op = RD::BLEND_OP_ADD; |
| 2154 | attachment.src_color_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2155 | attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 2156 | attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2157 | attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 2158 | |
| 2159 | } break; |
| 2160 | } |
| 2161 | |
| 2162 | RD::PipelineColorBlendState blend_state; |
| 2163 | blend_state.attachments.push_back(attachment); |
| 2164 | |
| 2165 | RD::PipelineColorBlendState::Attachment attachment_lcd; |
| 2166 | attachment_lcd.enable_blend = true; |
| 2167 | attachment_lcd.alpha_blend_op = RD::BLEND_OP_ADD; |
| 2168 | attachment_lcd.color_blend_op = RD::BLEND_OP_ADD; |
| 2169 | attachment_lcd.src_color_blend_factor = RD::BLEND_FACTOR_CONSTANT_COLOR; |
| 2170 | attachment_lcd.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_COLOR; |
| 2171 | attachment_lcd.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2172 | attachment_lcd.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 2173 | |
| 2174 | RD::PipelineColorBlendState blend_state_lcd; |
| 2175 | blend_state_lcd.attachments.push_back(attachment_lcd); |
| 2176 | |
| 2177 | //update pipelines |
| 2178 | |
| 2179 | for (int i = 0; i < PIPELINE_LIGHT_MODE_MAX; i++) { |
| 2180 | for (int j = 0; j < PIPELINE_VARIANT_MAX; j++) { |
| 2181 | RD::RenderPrimitive primitive[PIPELINE_VARIANT_MAX] = { |
| 2182 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2183 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2184 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2185 | RD::RENDER_PRIMITIVE_LINES, |
| 2186 | RD::RENDER_PRIMITIVE_POINTS, |
| 2187 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2188 | RD::RENDER_PRIMITIVE_TRIANGLE_STRIPS, |
| 2189 | RD::RENDER_PRIMITIVE_LINES, |
| 2190 | RD::RENDER_PRIMITIVE_LINESTRIPS, |
| 2191 | RD::RENDER_PRIMITIVE_POINTS, |
| 2192 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2193 | }; |
| 2194 | |
| 2195 | ShaderVariant shader_variants[PIPELINE_LIGHT_MODE_MAX][PIPELINE_VARIANT_MAX] = { |
| 2196 | { |
| 2197 | //non lit |
| 2198 | SHADER_VARIANT_QUAD, |
| 2199 | SHADER_VARIANT_NINEPATCH, |
| 2200 | SHADER_VARIANT_PRIMITIVE, |
| 2201 | SHADER_VARIANT_PRIMITIVE, |
| 2202 | SHADER_VARIANT_PRIMITIVE_POINTS, |
| 2203 | SHADER_VARIANT_ATTRIBUTES, |
| 2204 | SHADER_VARIANT_ATTRIBUTES, |
| 2205 | SHADER_VARIANT_ATTRIBUTES, |
| 2206 | SHADER_VARIANT_ATTRIBUTES, |
| 2207 | SHADER_VARIANT_ATTRIBUTES_POINTS, |
| 2208 | SHADER_VARIANT_QUAD, |
| 2209 | }, |
| 2210 | { |
| 2211 | //lit |
| 2212 | SHADER_VARIANT_QUAD_LIGHT, |
| 2213 | SHADER_VARIANT_NINEPATCH_LIGHT, |
| 2214 | SHADER_VARIANT_PRIMITIVE_LIGHT, |
| 2215 | SHADER_VARIANT_PRIMITIVE_LIGHT, |
| 2216 | SHADER_VARIANT_PRIMITIVE_POINTS_LIGHT, |
| 2217 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
| 2218 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
| 2219 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
| 2220 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
| 2221 | SHADER_VARIANT_ATTRIBUTES_POINTS_LIGHT, |
| 2222 | SHADER_VARIANT_QUAD_LIGHT, |
| 2223 | }, |
| 2224 | }; |
| 2225 | |
| 2226 | RID shader_variant = canvas_singleton->shader.canvas_shader.version_get_shader(version, shader_variants[i][j]); |
| 2227 | if (j == PIPELINE_VARIANT_QUAD_LCD_BLEND) { |
| 2228 | pipeline_variants.variants[i][j].setup(shader_variant, primitive[j], RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state_lcd, RD::DYNAMIC_STATE_BLEND_CONSTANTS); |
| 2229 | } else { |
| 2230 | pipeline_variants.variants[i][j].setup(shader_variant, primitive[j], RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0); |
| 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | valid = true; |
| 2236 | } |
| 2237 | |
| 2238 | bool RendererCanvasRenderRD::CanvasShaderData::is_animated() const { |
| 2239 | return false; |
| 2240 | } |
| 2241 | |
| 2242 | bool RendererCanvasRenderRD::CanvasShaderData::casts_shadows() const { |
| 2243 | return false; |
| 2244 | } |
| 2245 | |
| 2246 | RS::ShaderNativeSourceCode RendererCanvasRenderRD::CanvasShaderData::get_native_source_code() const { |
| 2247 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 2248 | return canvas_singleton->shader.canvas_shader.version_get_native_source_code(version); |
| 2249 | } |
| 2250 | |
| 2251 | RendererCanvasRenderRD::CanvasShaderData::~CanvasShaderData() { |
| 2252 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 2253 | ERR_FAIL_COND(!canvas_singleton); |
| 2254 | //pipeline variants will clear themselves if shader is gone |
| 2255 | if (version.is_valid()) { |
| 2256 | canvas_singleton->shader.canvas_shader.version_free(version); |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | RendererRD::MaterialStorage::ShaderData *RendererCanvasRenderRD::_create_shader_func() { |
| 2261 | CanvasShaderData *shader_data = memnew(CanvasShaderData); |
| 2262 | return shader_data; |
| 2263 | } |
| 2264 | |
| 2265 | bool RendererCanvasRenderRD::CanvasMaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) { |
| 2266 | RendererCanvasRenderRD *canvas_singleton = static_cast<RendererCanvasRenderRD *>(RendererCanvasRender::singleton); |
| 2267 | 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, canvas_singleton->shader.canvas_shader.version_get_shader(shader_data->version, 0), MATERIAL_UNIFORM_SET, true, false, RD::BARRIER_MASK_ALL_BARRIERS); |
| 2268 | 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, canvas_singleton->shader.canvas_shader.version_get_shader(shader_data->version, 0), MATERIAL_UNIFORM_SET, false, false, RD::BARRIER_MASK_ALL_BARRIERS); |
| 2269 | return uniform_set_changed || uniform_set_srgb_changed; |
| 2270 | } |
| 2271 | |
| 2272 | RendererCanvasRenderRD::CanvasMaterialData::~CanvasMaterialData() { |
| 2273 | free_parameters_uniform_set(uniform_set); |
| 2274 | free_parameters_uniform_set(uniform_set_srgb); |
| 2275 | } |
| 2276 | |
| 2277 | RendererRD::MaterialStorage::MaterialData *RendererCanvasRenderRD::_create_material_func(CanvasShaderData *p_shader) { |
| 2278 | CanvasMaterialData *material_data = memnew(CanvasMaterialData); |
| 2279 | material_data->shader_data = p_shader; |
| 2280 | //update will happen later anyway so do nothing. |
| 2281 | return material_data; |
| 2282 | } |
| 2283 | |
| 2284 | void RendererCanvasRenderRD::set_time(double p_time) { |
| 2285 | state.time = p_time; |
| 2286 | } |
| 2287 | |
| 2288 | void RendererCanvasRenderRD::update() { |
| 2289 | } |
| 2290 | |
| 2291 | RendererCanvasRenderRD::RendererCanvasRenderRD() { |
| 2292 | RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); |
| 2293 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 2294 | |
| 2295 | { //create default samplers |
| 2296 | |
| 2297 | default_samplers.default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR; |
| 2298 | default_samplers.default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED; |
| 2299 | } |
| 2300 | |
| 2301 | { //shader variants |
| 2302 | |
| 2303 | String global_defines; |
| 2304 | |
| 2305 | uint64_t uniform_max_size = RD::get_singleton()->limit_get(RD::LIMIT_MAX_UNIFORM_BUFFER_SIZE); |
| 2306 | if (uniform_max_size < 65536) { |
| 2307 | //Yes, you guessed right, ARM again |
| 2308 | state.max_lights_per_render = 64; |
| 2309 | global_defines += "#define MAX_LIGHTS 64\n" ; |
| 2310 | } else { |
| 2311 | state.max_lights_per_render = DEFAULT_MAX_LIGHTS_PER_RENDER; |
| 2312 | global_defines += "#define MAX_LIGHTS " + itos(DEFAULT_MAX_LIGHTS_PER_RENDER) + "\n" ; |
| 2313 | } |
| 2314 | |
| 2315 | global_defines += "\n#define SAMPLERS_BINDING_FIRST_INDEX " + itos(SAMPLERS_BINDING_FIRST_INDEX) + "\n" ; |
| 2316 | |
| 2317 | state.light_uniforms = memnew_arr(LightUniform, state.max_lights_per_render); |
| 2318 | Vector<String> variants; |
| 2319 | //non light variants |
| 2320 | variants.push_back("" ); //none by default is first variant |
| 2321 | variants.push_back("#define USE_NINEPATCH\n" ); //ninepatch is the second variant |
| 2322 | variants.push_back("#define USE_PRIMITIVE\n" ); //primitive is the third |
| 2323 | variants.push_back("#define USE_PRIMITIVE\n#define USE_POINT_SIZE\n" ); //points need point size |
| 2324 | variants.push_back("#define USE_ATTRIBUTES\n" ); // attributes for vertex arrays |
| 2325 | variants.push_back("#define USE_ATTRIBUTES\n#define USE_POINT_SIZE\n" ); //attributes with point size |
| 2326 | //light variants |
| 2327 | variants.push_back("#define USE_LIGHTING\n" ); //none by default is first variant |
| 2328 | variants.push_back("#define USE_LIGHTING\n#define USE_NINEPATCH\n" ); //ninepatch is the second variant |
| 2329 | variants.push_back("#define USE_LIGHTING\n#define USE_PRIMITIVE\n" ); //primitive is the third |
| 2330 | variants.push_back("#define USE_LIGHTING\n#define USE_PRIMITIVE\n#define USE_POINT_SIZE\n" ); //points need point size |
| 2331 | variants.push_back("#define USE_LIGHTING\n#define USE_ATTRIBUTES\n" ); // attributes for vertex arrays |
| 2332 | variants.push_back("#define USE_LIGHTING\n#define USE_ATTRIBUTES\n#define USE_POINT_SIZE\n" ); //attributes with point size |
| 2333 | |
| 2334 | shader.canvas_shader.initialize(variants, global_defines); |
| 2335 | |
| 2336 | shader.default_version = shader.canvas_shader.version_create(); |
| 2337 | shader.default_version_rd_shader = shader.canvas_shader.version_get_shader(shader.default_version, SHADER_VARIANT_QUAD); |
| 2338 | |
| 2339 | RD::PipelineColorBlendState blend_state; |
| 2340 | RD::PipelineColorBlendState::Attachment blend_attachment; |
| 2341 | |
| 2342 | blend_attachment.enable_blend = true; |
| 2343 | blend_attachment.color_blend_op = RD::BLEND_OP_ADD; |
| 2344 | blend_attachment.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA; |
| 2345 | blend_attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 2346 | |
| 2347 | blend_attachment.alpha_blend_op = RD::BLEND_OP_ADD; |
| 2348 | blend_attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2349 | blend_attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 2350 | |
| 2351 | blend_state.attachments.push_back(blend_attachment); |
| 2352 | |
| 2353 | RD::PipelineColorBlendState::Attachment attachment_lcd; |
| 2354 | attachment_lcd.enable_blend = true; |
| 2355 | attachment_lcd.alpha_blend_op = RD::BLEND_OP_ADD; |
| 2356 | attachment_lcd.color_blend_op = RD::BLEND_OP_ADD; |
| 2357 | attachment_lcd.src_color_blend_factor = RD::BLEND_FACTOR_CONSTANT_COLOR; |
| 2358 | attachment_lcd.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_COLOR; |
| 2359 | attachment_lcd.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
| 2360 | attachment_lcd.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 2361 | |
| 2362 | RD::PipelineColorBlendState blend_state_lcd; |
| 2363 | blend_state_lcd.attachments.push_back(attachment_lcd); |
| 2364 | |
| 2365 | for (int i = 0; i < PIPELINE_LIGHT_MODE_MAX; i++) { |
| 2366 | for (int j = 0; j < PIPELINE_VARIANT_MAX; j++) { |
| 2367 | RD::RenderPrimitive primitive[PIPELINE_VARIANT_MAX] = { |
| 2368 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2369 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2370 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2371 | RD::RENDER_PRIMITIVE_LINES, |
| 2372 | RD::RENDER_PRIMITIVE_POINTS, |
| 2373 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2374 | RD::RENDER_PRIMITIVE_TRIANGLE_STRIPS, |
| 2375 | RD::RENDER_PRIMITIVE_LINES, |
| 2376 | RD::RENDER_PRIMITIVE_LINESTRIPS, |
| 2377 | RD::RENDER_PRIMITIVE_POINTS, |
| 2378 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 2379 | }; |
| 2380 | |
| 2381 | ShaderVariant shader_variants[PIPELINE_LIGHT_MODE_MAX][PIPELINE_VARIANT_MAX] = { |
| 2382 | { |
| 2383 | //non lit |
| 2384 | SHADER_VARIANT_QUAD, |
| 2385 | SHADER_VARIANT_NINEPATCH, |
| 2386 | SHADER_VARIANT_PRIMITIVE, |
| 2387 | SHADER_VARIANT_PRIMITIVE, |
| 2388 | SHADER_VARIANT_PRIMITIVE_POINTS, |
| 2389 | SHADER_VARIANT_ATTRIBUTES, |
| 2390 | SHADER_VARIANT_ATTRIBUTES, |
| 2391 | SHADER_VARIANT_ATTRIBUTES, |
| 2392 | SHADER_VARIANT_ATTRIBUTES, |
| 2393 | SHADER_VARIANT_ATTRIBUTES_POINTS, |
| 2394 | SHADER_VARIANT_QUAD, |
| 2395 | }, |
| 2396 | { |
| 2397 | //lit |
| 2398 | SHADER_VARIANT_QUAD_LIGHT, |
| 2399 | SHADER_VARIANT_NINEPATCH_LIGHT, |
| 2400 | SHADER_VARIANT_PRIMITIVE_LIGHT, |
| 2401 | SHADER_VARIANT_PRIMITIVE_LIGHT, |
| 2402 | SHADER_VARIANT_PRIMITIVE_POINTS_LIGHT, |
| 2403 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
| 2404 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
| 2405 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
| 2406 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
| 2407 | SHADER_VARIANT_ATTRIBUTES_POINTS_LIGHT, |
| 2408 | SHADER_VARIANT_QUAD_LIGHT, |
| 2409 | }, |
| 2410 | }; |
| 2411 | |
| 2412 | RID shader_variant = shader.canvas_shader.version_get_shader(shader.default_version, shader_variants[i][j]); |
| 2413 | if (j == PIPELINE_VARIANT_QUAD_LCD_BLEND) { |
| 2414 | shader.pipeline_variants.variants[i][j].setup(shader_variant, primitive[j], RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state_lcd, RD::DYNAMIC_STATE_BLEND_CONSTANTS); |
| 2415 | } else { |
| 2416 | shader.pipeline_variants.variants[i][j].setup(shader_variant, primitive[j], RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0); |
| 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | { |
| 2423 | //shader compiler |
| 2424 | ShaderCompiler::DefaultIdentifierActions actions; |
| 2425 | |
| 2426 | actions.renames["VERTEX" ] = "vertex" ; |
| 2427 | actions.renames["LIGHT_VERTEX" ] = "light_vertex" ; |
| 2428 | actions.renames["SHADOW_VERTEX" ] = "shadow_vertex" ; |
| 2429 | actions.renames["UV" ] = "uv" ; |
| 2430 | actions.renames["POINT_SIZE" ] = "point_size" ; |
| 2431 | |
| 2432 | actions.renames["MODEL_MATRIX" ] = "model_matrix" ; |
| 2433 | actions.renames["CANVAS_MATRIX" ] = "canvas_data.canvas_transform" ; |
| 2434 | actions.renames["SCREEN_MATRIX" ] = "canvas_data.screen_transform" ; |
| 2435 | actions.renames["TIME" ] = "canvas_data.time" ; |
| 2436 | actions.renames["PI" ] = _MKSTR(Math_PI); |
| 2437 | actions.renames["TAU" ] = _MKSTR(Math_TAU); |
| 2438 | actions.renames["E" ] = _MKSTR(Math_E); |
| 2439 | actions.renames["AT_LIGHT_PASS" ] = "false" ; |
| 2440 | actions.renames["INSTANCE_CUSTOM" ] = "instance_custom" ; |
| 2441 | |
| 2442 | actions.renames["COLOR" ] = "color" ; |
| 2443 | actions.renames["NORMAL" ] = "normal" ; |
| 2444 | actions.renames["NORMAL_MAP" ] = "normal_map" ; |
| 2445 | actions.renames["NORMAL_MAP_DEPTH" ] = "normal_map_depth" ; |
| 2446 | actions.renames["TEXTURE" ] = "color_texture" ; |
| 2447 | actions.renames["TEXTURE_PIXEL_SIZE" ] = "draw_data.color_texture_pixel_size" ; |
| 2448 | actions.renames["NORMAL_TEXTURE" ] = "normal_texture" ; |
| 2449 | actions.renames["SPECULAR_SHININESS_TEXTURE" ] = "specular_texture" ; |
| 2450 | actions.renames["SPECULAR_SHININESS" ] = "specular_shininess" ; |
| 2451 | actions.renames["SCREEN_UV" ] = "screen_uv" ; |
| 2452 | actions.renames["SCREEN_PIXEL_SIZE" ] = "canvas_data.screen_pixel_size" ; |
| 2453 | actions.renames["FRAGCOORD" ] = "gl_FragCoord" ; |
| 2454 | actions.renames["POINT_COORD" ] = "gl_PointCoord" ; |
| 2455 | actions.renames["INSTANCE_ID" ] = "gl_InstanceIndex" ; |
| 2456 | actions.renames["VERTEX_ID" ] = "gl_VertexIndex" ; |
| 2457 | |
| 2458 | actions.renames["LIGHT_POSITION" ] = "light_position" ; |
| 2459 | actions.renames["LIGHT_DIRECTION" ] = "light_direction" ; |
| 2460 | actions.renames["LIGHT_IS_DIRECTIONAL" ] = "is_directional" ; |
| 2461 | actions.renames["LIGHT_COLOR" ] = "light_color" ; |
| 2462 | actions.renames["LIGHT_ENERGY" ] = "light_energy" ; |
| 2463 | actions.renames["LIGHT" ] = "light" ; |
| 2464 | actions.renames["SHADOW_MODULATE" ] = "shadow_modulate" ; |
| 2465 | |
| 2466 | actions.renames["texture_sdf" ] = "texture_sdf" ; |
| 2467 | actions.renames["texture_sdf_normal" ] = "texture_sdf_normal" ; |
| 2468 | actions.renames["sdf_to_screen_uv" ] = "sdf_to_screen_uv" ; |
| 2469 | actions.renames["screen_uv_to_sdf" ] = "screen_uv_to_sdf" ; |
| 2470 | |
| 2471 | actions.usage_defines["COLOR" ] = "#define COLOR_USED\n" ; |
| 2472 | actions.usage_defines["SCREEN_UV" ] = "#define SCREEN_UV_USED\n" ; |
| 2473 | actions.usage_defines["SCREEN_PIXEL_SIZE" ] = "@SCREEN_UV" ; |
| 2474 | actions.usage_defines["NORMAL" ] = "#define NORMAL_USED\n" ; |
| 2475 | actions.usage_defines["NORMAL_MAP" ] = "#define NORMAL_MAP_USED\n" ; |
| 2476 | actions.usage_defines["LIGHT" ] = "#define LIGHT_SHADER_CODE_USED\n" ; |
| 2477 | actions.usage_defines["SPECULAR_SHININESS" ] = "#define SPECULAR_SHININESS_USED\n" ; |
| 2478 | actions.usage_defines["POINT_SIZE" ] = "#define USE_POINT_SIZE\n" ; |
| 2479 | |
| 2480 | actions.render_mode_defines["skip_vertex_transform" ] = "#define SKIP_TRANSFORM_USED\n" ; |
| 2481 | actions.render_mode_defines["unshaded" ] = "#define MODE_UNSHADED\n" ; |
| 2482 | actions.render_mode_defines["light_only" ] = "#define MODE_LIGHT_ONLY\n" ; |
| 2483 | |
| 2484 | actions.custom_samplers["TEXTURE" ] = "texture_sampler" ; |
| 2485 | actions.custom_samplers["NORMAL_TEXTURE" ] = "texture_sampler" ; |
| 2486 | actions.custom_samplers["SPECULAR_SHININESS_TEXTURE" ] = "texture_sampler" ; |
| 2487 | actions.base_texture_binding_index = 1; |
| 2488 | actions.texture_layout_set = MATERIAL_UNIFORM_SET; |
| 2489 | actions.base_uniform_string = "material." ; |
| 2490 | actions.default_filter = ShaderLanguage::FILTER_LINEAR; |
| 2491 | actions.default_repeat = ShaderLanguage::REPEAT_DISABLE; |
| 2492 | actions.base_varying_index = 4; |
| 2493 | |
| 2494 | actions.global_buffer_array_variable = "global_shader_uniforms.data" ; |
| 2495 | |
| 2496 | shader.compiler.initialize(actions); |
| 2497 | } |
| 2498 | |
| 2499 | { //shadow rendering |
| 2500 | Vector<String> versions; |
| 2501 | versions.push_back("\n#define MODE_SHADOW\n" ); //shadow |
| 2502 | versions.push_back("\n#define MODE_SDF\n" ); //sdf |
| 2503 | shadow_render.shader.initialize(versions); |
| 2504 | |
| 2505 | { |
| 2506 | Vector<RD::AttachmentFormat> attachments; |
| 2507 | |
| 2508 | RD::AttachmentFormat af_color; |
| 2509 | af_color.format = RD::DATA_FORMAT_R32_SFLOAT; |
| 2510 | af_color.usage_flags = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; |
| 2511 | |
| 2512 | attachments.push_back(af_color); |
| 2513 | |
| 2514 | RD::AttachmentFormat af_depth; |
| 2515 | af_depth.format = RD::DATA_FORMAT_D32_SFLOAT; |
| 2516 | af_depth.usage_flags = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 2517 | |
| 2518 | attachments.push_back(af_depth); |
| 2519 | |
| 2520 | shadow_render.framebuffer_format = RD::get_singleton()->framebuffer_format_create(attachments); |
| 2521 | } |
| 2522 | |
| 2523 | { |
| 2524 | Vector<RD::AttachmentFormat> attachments; |
| 2525 | |
| 2526 | RD::AttachmentFormat af_color; |
| 2527 | af_color.format = RD::DATA_FORMAT_R8_UNORM; |
| 2528 | af_color.usage_flags = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; |
| 2529 | |
| 2530 | attachments.push_back(af_color); |
| 2531 | |
| 2532 | shadow_render.sdf_framebuffer_format = RD::get_singleton()->framebuffer_format_create(attachments); |
| 2533 | } |
| 2534 | |
| 2535 | //pipelines |
| 2536 | Vector<RD::VertexAttribute> vf; |
| 2537 | RD::VertexAttribute vd; |
| 2538 | vd.format = sizeof(real_t) == sizeof(float) ? RD::DATA_FORMAT_R32G32B32_SFLOAT : RD::DATA_FORMAT_R64G64B64_SFLOAT; |
| 2539 | vd.location = 0; |
| 2540 | vd.offset = 0; |
| 2541 | vd.stride = sizeof(real_t) * 3; |
| 2542 | vf.push_back(vd); |
| 2543 | shadow_render.vertex_format = RD::get_singleton()->vertex_format_create(vf); |
| 2544 | |
| 2545 | vd.format = sizeof(real_t) == sizeof(float) ? RD::DATA_FORMAT_R32G32_SFLOAT : RD::DATA_FORMAT_R64G64_SFLOAT; |
| 2546 | vd.stride = sizeof(real_t) * 2; |
| 2547 | |
| 2548 | vf.write[0] = vd; |
| 2549 | shadow_render.sdf_vertex_format = RD::get_singleton()->vertex_format_create(vf); |
| 2550 | |
| 2551 | shadow_render.shader_version = shadow_render.shader.version_create(); |
| 2552 | |
| 2553 | for (int i = 0; i < 3; i++) { |
| 2554 | RD::PipelineRasterizationState rs; |
| 2555 | rs.cull_mode = i == 0 ? RD::POLYGON_CULL_DISABLED : (i == 1 ? RD::POLYGON_CULL_FRONT : RD::POLYGON_CULL_BACK); |
| 2556 | RD::PipelineDepthStencilState ds; |
| 2557 | ds.enable_depth_write = true; |
| 2558 | ds.enable_depth_test = true; |
| 2559 | ds.depth_compare_operator = RD::COMPARE_OP_LESS; |
| 2560 | shadow_render.render_pipelines[i] = RD::get_singleton()->render_pipeline_create(shadow_render.shader.version_get_shader(shadow_render.shader_version, SHADOW_RENDER_MODE_SHADOW), shadow_render.framebuffer_format, shadow_render.vertex_format, RD::RENDER_PRIMITIVE_TRIANGLES, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0); |
| 2561 | } |
| 2562 | |
| 2563 | for (int i = 0; i < 2; i++) { |
| 2564 | 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); |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | { //bindings |
| 2569 | |
| 2570 | state.canvas_state_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(State::Buffer)); |
| 2571 | state.lights_uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(LightUniform) * state.max_lights_per_render); |
| 2572 | |
| 2573 | RD::SamplerState shadow_sampler_state; |
| 2574 | shadow_sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR; |
| 2575 | shadow_sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR; |
| 2576 | shadow_sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT; //shadow wrap around |
| 2577 | shadow_sampler_state.compare_op = RD::COMPARE_OP_GREATER; |
| 2578 | shadow_sampler_state.enable_compare = true; |
| 2579 | state.shadow_sampler = RD::get_singleton()->sampler_create(shadow_sampler_state); |
| 2580 | } |
| 2581 | |
| 2582 | { |
| 2583 | //polygon buffers |
| 2584 | polygon_buffers.last_id = 1; |
| 2585 | } |
| 2586 | |
| 2587 | { // default index buffer |
| 2588 | |
| 2589 | Vector<uint8_t> pv; |
| 2590 | pv.resize(6 * 2); |
| 2591 | { |
| 2592 | uint8_t *w = pv.ptrw(); |
| 2593 | uint16_t *p16 = (uint16_t *)w; |
| 2594 | p16[0] = 0; |
| 2595 | p16[1] = 1; |
| 2596 | p16[2] = 2; |
| 2597 | p16[3] = 0; |
| 2598 | p16[4] = 2; |
| 2599 | p16[5] = 3; |
| 2600 | } |
| 2601 | shader.quad_index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT16, pv); |
| 2602 | shader.quad_index_array = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 6); |
| 2603 | } |
| 2604 | |
| 2605 | { //primitive |
| 2606 | primitive_arrays.index_array[0] = shader.quad_index_array = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 1); |
| 2607 | primitive_arrays.index_array[1] = shader.quad_index_array = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 2); |
| 2608 | primitive_arrays.index_array[2] = shader.quad_index_array = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 3); |
| 2609 | primitive_arrays.index_array[3] = shader.quad_index_array = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 6); |
| 2610 | } |
| 2611 | |
| 2612 | { |
| 2613 | //default shadow texture to keep uniform set happy |
| 2614 | RD::TextureFormat tf; |
| 2615 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 2616 | tf.width = 4; |
| 2617 | tf.height = 4; |
| 2618 | tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT; |
| 2619 | tf.format = RD::DATA_FORMAT_R32_SFLOAT; |
| 2620 | |
| 2621 | state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 2622 | } |
| 2623 | |
| 2624 | { |
| 2625 | Vector<RD::Uniform> uniforms; |
| 2626 | |
| 2627 | { |
| 2628 | RD::Uniform u; |
| 2629 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 2630 | u.binding = 0; |
| 2631 | u.append_id(RendererRD::MeshStorage::get_singleton()->get_default_rd_storage_buffer()); |
| 2632 | uniforms.push_back(u); |
| 2633 | } |
| 2634 | |
| 2635 | state.default_transforms_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader.default_version_rd_shader, TRANSFORMS_UNIFORM_SET); |
| 2636 | } |
| 2637 | |
| 2638 | default_canvas_texture = texture_storage->canvas_texture_allocate(); |
| 2639 | texture_storage->canvas_texture_initialize(default_canvas_texture); |
| 2640 | |
| 2641 | state.shadow_texture_size = GLOBAL_GET("rendering/2d/shadow_atlas/size" ); |
| 2642 | |
| 2643 | //create functions for shader and material |
| 2644 | material_storage->shader_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_2D, _create_shader_funcs); |
| 2645 | material_storage->material_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_2D, _create_material_funcs); |
| 2646 | |
| 2647 | state.time = 0; |
| 2648 | |
| 2649 | { |
| 2650 | default_canvas_group_shader = material_storage->shader_allocate(); |
| 2651 | material_storage->shader_initialize(default_canvas_group_shader); |
| 2652 | |
| 2653 | material_storage->shader_set_code(default_canvas_group_shader, R"( |
| 2654 | // Default CanvasGroup shader. |
| 2655 | |
| 2656 | shader_type canvas_item; |
| 2657 | render_mode unshaded; |
| 2658 | |
| 2659 | uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest; |
| 2660 | |
| 2661 | void fragment() { |
| 2662 | vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0); |
| 2663 | |
| 2664 | if (c.a > 0.0001) { |
| 2665 | c.rgb /= c.a; |
| 2666 | } |
| 2667 | |
| 2668 | COLOR *= c; |
| 2669 | } |
| 2670 | )" ); |
| 2671 | default_canvas_group_material = material_storage->material_allocate(); |
| 2672 | material_storage->material_initialize(default_canvas_group_material); |
| 2673 | |
| 2674 | material_storage->material_set_shader(default_canvas_group_material, default_canvas_group_shader); |
| 2675 | } |
| 2676 | |
| 2677 | { |
| 2678 | default_clip_children_shader = material_storage->shader_allocate(); |
| 2679 | material_storage->shader_initialize(default_clip_children_shader); |
| 2680 | |
| 2681 | material_storage->shader_set_code(default_clip_children_shader, R"( |
| 2682 | // Default clip children shader. |
| 2683 | |
| 2684 | shader_type canvas_item; |
| 2685 | render_mode unshaded; |
| 2686 | |
| 2687 | uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest; |
| 2688 | |
| 2689 | void fragment() { |
| 2690 | vec4 c = textureLod(screen_texture, SCREEN_UV, 0.0); |
| 2691 | COLOR.rgb = c.rgb; |
| 2692 | } |
| 2693 | )" ); |
| 2694 | default_clip_children_material = material_storage->material_allocate(); |
| 2695 | material_storage->material_initialize(default_clip_children_material); |
| 2696 | |
| 2697 | material_storage->material_set_shader(default_clip_children_material, default_clip_children_shader); |
| 2698 | } |
| 2699 | |
| 2700 | static_assert(sizeof(PushConstant) == 128); |
| 2701 | } |
| 2702 | |
| 2703 | bool RendererCanvasRenderRD::free(RID p_rid) { |
| 2704 | if (canvas_light_owner.owns(p_rid)) { |
| 2705 | CanvasLight *cl = canvas_light_owner.get_or_null(p_rid); |
| 2706 | ERR_FAIL_COND_V(!cl, false); |
| 2707 | light_set_use_shadow(p_rid, false); |
| 2708 | canvas_light_owner.free(p_rid); |
| 2709 | } else if (occluder_polygon_owner.owns(p_rid)) { |
| 2710 | occluder_polygon_set_shape(p_rid, Vector<Vector2>(), false); |
| 2711 | occluder_polygon_owner.free(p_rid); |
| 2712 | } else { |
| 2713 | return false; |
| 2714 | } |
| 2715 | |
| 2716 | return true; |
| 2717 | } |
| 2718 | |
| 2719 | void RendererCanvasRenderRD::set_shadow_texture_size(int p_size) { |
| 2720 | p_size = nearest_power_of_2_templated(p_size); |
| 2721 | if (p_size == state.shadow_texture_size) { |
| 2722 | return; |
| 2723 | } |
| 2724 | state.shadow_texture_size = p_size; |
| 2725 | if (state.shadow_fb.is_valid()) { |
| 2726 | RD::get_singleton()->free(state.shadow_texture); |
| 2727 | RD::get_singleton()->free(state.shadow_depth_texture); |
| 2728 | state.shadow_fb = RID(); |
| 2729 | |
| 2730 | { |
| 2731 | //create a default shadow texture to keep uniform set happy (and that it gets erased when a new one is created) |
| 2732 | RD::TextureFormat tf; |
| 2733 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
| 2734 | tf.width = 4; |
| 2735 | tf.height = 4; |
| 2736 | tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT; |
| 2737 | tf.format = RD::DATA_FORMAT_R32_SFLOAT; |
| 2738 | |
| 2739 | state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); |
| 2740 | } |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | RendererCanvasRenderRD::~RendererCanvasRenderRD() { |
| 2745 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 2746 | //canvas state |
| 2747 | |
| 2748 | material_storage->material_free(default_canvas_group_material); |
| 2749 | material_storage->shader_free(default_canvas_group_shader); |
| 2750 | |
| 2751 | material_storage->material_free(default_clip_children_material); |
| 2752 | material_storage->shader_free(default_clip_children_shader); |
| 2753 | |
| 2754 | { |
| 2755 | if (state.canvas_state_buffer.is_valid()) { |
| 2756 | RD::get_singleton()->free(state.canvas_state_buffer); |
| 2757 | } |
| 2758 | |
| 2759 | memdelete_arr(state.light_uniforms); |
| 2760 | RD::get_singleton()->free(state.lights_uniform_buffer); |
| 2761 | } |
| 2762 | |
| 2763 | //shadow rendering |
| 2764 | { |
| 2765 | shadow_render.shader.version_free(shadow_render.shader_version); |
| 2766 | //this will also automatically clear all pipelines |
| 2767 | RD::get_singleton()->free(state.shadow_sampler); |
| 2768 | } |
| 2769 | //bindings |
| 2770 | |
| 2771 | //shaders |
| 2772 | |
| 2773 | shader.canvas_shader.version_free(shader.default_version); |
| 2774 | |
| 2775 | //buffers |
| 2776 | { |
| 2777 | RD::get_singleton()->free(shader.quad_index_array); |
| 2778 | RD::get_singleton()->free(shader.quad_index_buffer); |
| 2779 | //primitives are erase by dependency |
| 2780 | } |
| 2781 | |
| 2782 | if (state.shadow_fb.is_valid()) { |
| 2783 | RD::get_singleton()->free(state.shadow_depth_texture); |
| 2784 | } |
| 2785 | RD::get_singleton()->free(state.shadow_texture); |
| 2786 | |
| 2787 | RendererRD::TextureStorage::get_singleton()->canvas_texture_free(default_canvas_texture); |
| 2788 | //pipelines don't need freeing, they are all gone after shaders are gone |
| 2789 | } |
| 2790 | |