Skip to content

Commit 63c124f

Browse files
committed
Expose RS.mesh_surface_update_index_region
And `mesh_surface_get_format_index_stride`
1 parent 1b37dac commit 63c124f

File tree

10 files changed

+78
-6
lines changed

10 files changed

+78
-6
lines changed

doc/classes/RenderingServer.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,14 @@
24692469
Returns the stride of the attribute buffer for a mesh with given [param format].
24702470
</description>
24712471
</method>
2472+
<method name="mesh_surface_get_format_index_stride" qualifiers="const">
2473+
<return type="int" />
2474+
<param index="0" name="format" type="int" enum="RenderingServer.ArrayFormat" is_bitfield="true" />
2475+
<param index="1" name="vertex_count" type="int" />
2476+
<description>
2477+
Returns the stride of the index buffer for a mesh with the given [param format].
2478+
</description>
2479+
</method>
24722480
<method name="mesh_surface_get_format_normal_tangent_stride" qualifiers="const">
24732481
<return type="int" />
24742482
<param index="0" name="format" type="int" enum="RenderingServer.ArrayFormat" is_bitfield="true" />
@@ -2536,6 +2544,16 @@
25362544
<description>
25372545
</description>
25382546
</method>
2547+
<method name="mesh_surface_update_index_region">
2548+
<return type="void" />
2549+
<param index="0" name="mesh" type="RID" />
2550+
<param index="1" name="surface" type="int" />
2551+
<param index="2" name="offset" type="int" />
2552+
<param index="3" name="data" type="PackedByteArray" />
2553+
<description>
2554+
Updates the index buffer of the mesh surface with the given [param data]. The expected data are 16 or 32-bit unsigned integers, which can be determined with [method mesh_surface_get_format_index_stride].
2555+
</description>
2556+
</method>
25392557
<method name="mesh_surface_update_skin_region">
25402558
<return type="void" />
25412559
<param index="0" name="mesh" type="RID" />

drivers/gles3/storage/mesh_storage.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,10 @@ RS::BlendShapeMode MeshStorage::mesh_get_blend_shape_mode(RID p_mesh) const {
534534
}
535535

536536
void MeshStorage::mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
537+
ERR_FAIL_COND(p_data.is_empty());
537538
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
538539
ERR_FAIL_NULL(mesh);
539540
ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
540-
ERR_FAIL_COND(p_data.is_empty());
541541

542542
uint64_t data_size = p_data.size();
543543
ERR_FAIL_COND(p_offset + data_size > mesh->surfaces[p_surface]->vertex_buffer_size);
@@ -549,10 +549,10 @@ void MeshStorage::mesh_surface_update_vertex_region(RID p_mesh, int p_surface, i
549549
}
550550

551551
void MeshStorage::mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
552+
ERR_FAIL_COND(p_data.is_empty());
552553
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
553554
ERR_FAIL_NULL(mesh);
554555
ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
555-
ERR_FAIL_COND(p_data.is_empty());
556556

557557
uint64_t data_size = p_data.size();
558558
ERR_FAIL_COND(p_offset + data_size > mesh->surfaces[p_surface]->attribute_buffer_size);
@@ -564,10 +564,10 @@ void MeshStorage::mesh_surface_update_attribute_region(RID p_mesh, int p_surface
564564
}
565565

566566
void MeshStorage::mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
567+
ERR_FAIL_COND(p_data.is_empty());
567568
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
568569
ERR_FAIL_NULL(mesh);
569570
ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
570-
ERR_FAIL_COND(p_data.is_empty());
571571

572572
uint64_t data_size = p_data.size();
573573
ERR_FAIL_COND(p_offset + data_size > mesh->surfaces[p_surface]->skin_buffer_size);
@@ -578,6 +578,21 @@ void MeshStorage::mesh_surface_update_skin_region(RID p_mesh, int p_surface, int
578578
glBindBuffer(GL_ARRAY_BUFFER, 0);
579579
}
580580

581+
void MeshStorage::mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
582+
ERR_FAIL_COND(p_data.is_empty());
583+
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
584+
ERR_FAIL_NULL(mesh);
585+
ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
586+
587+
uint64_t data_size = p_data.size();
588+
ERR_FAIL_COND(p_offset + data_size > mesh->surfaces[p_surface]->index_buffer_size);
589+
const uint8_t *r = p_data.ptr();
590+
591+
glBindBuffer(GL_ARRAY_BUFFER, mesh->surfaces[p_surface]->index_buffer);
592+
glBufferSubData(GL_ARRAY_BUFFER, p_offset, data_size, r);
593+
glBindBuffer(GL_ARRAY_BUFFER, 0);
594+
}
595+
581596
void MeshStorage::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
582597
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
583598
ERR_FAIL_NULL(mesh);

drivers/gles3/storage/mesh_storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class MeshStorage : public RendererMeshStorage {
301301
virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
302302
virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
303303
virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
304+
virtual void mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
304305

305306
virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) override;
306307
virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const override;

servers/rendering/dummy/storage/mesh_storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class MeshStorage : public RendererMeshStorage {
103103
virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override {}
104104
virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override {}
105105
virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override {}
106+
virtual void mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override {}
106107

107108
virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) override {}
108109
virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const override { return RID(); }

servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,41 +565,57 @@ RS::BlendShapeMode MeshStorage::mesh_get_blend_shape_mode(RID p_mesh) const {
565565
}
566566

567567
void MeshStorage::mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
568+
ERR_FAIL_COND(p_data.is_empty());
568569
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
569570
ERR_FAIL_NULL(mesh);
570571
ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
571-
ERR_FAIL_COND(p_data.is_empty());
572572
ERR_FAIL_COND(mesh->surfaces[p_surface]->vertex_buffer.is_null());
573+
573574
uint64_t data_size = p_data.size();
574575
const uint8_t *r = p_data.ptr();
575576

576577
RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->vertex_buffer, p_offset, data_size, r);
577578
}
578579

579580
void MeshStorage::mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
581+
ERR_FAIL_COND(p_data.is_empty());
580582
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
581583
ERR_FAIL_NULL(mesh);
582584
ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
583-
ERR_FAIL_COND(p_data.is_empty());
584585
ERR_FAIL_COND(mesh->surfaces[p_surface]->attribute_buffer.is_null());
586+
585587
uint64_t data_size = p_data.size();
586588
const uint8_t *r = p_data.ptr();
587589

588590
RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->attribute_buffer, p_offset, data_size, r);
589591
}
590592

591593
void MeshStorage::mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
594+
ERR_FAIL_COND(p_data.is_empty());
592595
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
593596
ERR_FAIL_NULL(mesh);
594597
ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
595-
ERR_FAIL_COND(p_data.is_empty());
596598
ERR_FAIL_COND(mesh->surfaces[p_surface]->skin_buffer.is_null());
599+
597600
uint64_t data_size = p_data.size();
598601
const uint8_t *r = p_data.ptr();
599602

600603
RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->skin_buffer, p_offset, data_size, r);
601604
}
602605

606+
void RendererRD::MeshStorage::mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
607+
ERR_FAIL_COND(p_data.is_empty());
608+
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
609+
ERR_FAIL_NULL(mesh);
610+
ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
611+
ERR_FAIL_COND(mesh->surfaces[p_surface]->index_buffer.is_null());
612+
613+
uint64_t data_size = p_data.size();
614+
const uint8_t *r = p_data.ptr();
615+
616+
RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->index_buffer, p_offset, data_size, r);
617+
}
618+
603619
void MeshStorage::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
604620
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
605621
ERR_FAIL_NULL(mesh);

servers/rendering/renderer_rd/storage_rd/mesh_storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ class MeshStorage : public RendererMeshStorage {
383383
virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
384384
virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
385385
virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
386+
virtual void mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
386387

387388
virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) override;
388389
virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const override;

servers/rendering/rendering_server_default.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ class RenderingServerDefault : public RenderingServer {
366366
FUNC4(mesh_surface_update_vertex_region, RID, int, int, const Vector<uint8_t> &)
367367
FUNC4(mesh_surface_update_attribute_region, RID, int, int, const Vector<uint8_t> &)
368368
FUNC4(mesh_surface_update_skin_region, RID, int, int, const Vector<uint8_t> &)
369+
FUNC4(mesh_surface_update_index_region, RID, int, int, const Vector<uint8_t> &)
369370

370371
FUNC3(mesh_surface_set_material, RID, int, RID)
371372
FUNC2RC(RID, mesh_surface_get_material, RID, int)

servers/rendering/storage/mesh_storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class RendererMeshStorage {
5656
virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
5757
virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
5858
virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
59+
virtual void mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
5960

6061
virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) = 0;
6162
virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const = 0;

servers/rendering_server.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ uint32_t RenderingServer::mesh_surface_get_format_attribute_stride(BitField<Arra
10221022
mesh_surface_make_offsets_from_format(p_format, p_vertex_len, 0, offsets, vstr, ntstr, astr, sstr);
10231023
return astr;
10241024
}
1025+
10251026
uint32_t RenderingServer::mesh_surface_get_format_skin_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const {
10261027
p_format = uint64_t(p_format) & ~ARRAY_FORMAT_INDEX;
10271028
uint32_t offsets[ARRAY_MAX];
@@ -1033,6 +1034,19 @@ uint32_t RenderingServer::mesh_surface_get_format_skin_stride(BitField<ArrayForm
10331034
return sstr;
10341035
}
10351036

1037+
uint32_t RenderingServer::mesh_surface_get_format_index_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const {
1038+
if (!(p_format & ARRAY_FORMAT_INDEX)) {
1039+
return 0;
1040+
}
1041+
1042+
// Determine whether using 16 or 32 bits indices.
1043+
if (p_vertex_len <= (1 << 16) && p_vertex_len > 0) {
1044+
return 2;
1045+
} else {
1046+
return 4;
1047+
}
1048+
}
1049+
10361050
void RenderingServer::mesh_surface_make_offsets_from_format(uint64_t p_format, int p_vertex_len, int p_index_len, uint32_t *r_offsets, uint32_t &r_vertex_element_size, uint32_t &r_normal_element_size, uint32_t &r_attrib_element_size, uint32_t &r_skin_element_size) const {
10371051
r_vertex_element_size = 0;
10381052
r_normal_element_size = 0;
@@ -2337,6 +2351,7 @@ void RenderingServer::_bind_methods() {
23372351
ClassDB::bind_method(D_METHOD("mesh_surface_get_format_normal_tangent_stride", "format", "vertex_count"), &RenderingServer::mesh_surface_get_format_normal_tangent_stride);
23382352
ClassDB::bind_method(D_METHOD("mesh_surface_get_format_attribute_stride", "format", "vertex_count"), &RenderingServer::mesh_surface_get_format_attribute_stride);
23392353
ClassDB::bind_method(D_METHOD("mesh_surface_get_format_skin_stride", "format", "vertex_count"), &RenderingServer::mesh_surface_get_format_skin_stride);
2354+
ClassDB::bind_method(D_METHOD("mesh_surface_get_format_index_stride", "format", "vertex_count"), &RenderingServer::mesh_surface_get_format_index_stride);
23402355
ClassDB::bind_method(D_METHOD("mesh_add_surface", "mesh", "surface"), &RenderingServer::_mesh_add_surface);
23412356
ClassDB::bind_method(D_METHOD("mesh_add_surface_from_arrays", "mesh", "primitive", "arrays", "blend_shapes", "lods", "compress_format"), &RenderingServer::mesh_add_surface_from_arrays, DEFVAL(Array()), DEFVAL(Dictionary()), DEFVAL(0));
23422357
ClassDB::bind_method(D_METHOD("mesh_get_blend_shape_count", "mesh"), &RenderingServer::mesh_get_blend_shape_count);
@@ -2357,6 +2372,7 @@ void RenderingServer::_bind_methods() {
23572372
ClassDB::bind_method(D_METHOD("mesh_surface_update_vertex_region", "mesh", "surface", "offset", "data"), &RenderingServer::mesh_surface_update_vertex_region);
23582373
ClassDB::bind_method(D_METHOD("mesh_surface_update_attribute_region", "mesh", "surface", "offset", "data"), &RenderingServer::mesh_surface_update_attribute_region);
23592374
ClassDB::bind_method(D_METHOD("mesh_surface_update_skin_region", "mesh", "surface", "offset", "data"), &RenderingServer::mesh_surface_update_skin_region);
2375+
ClassDB::bind_method(D_METHOD("mesh_surface_update_index_region", "mesh", "surface", "offset", "data"), &RenderingServer::mesh_surface_update_index_region);
23602376

23612377
ClassDB::bind_method(D_METHOD("mesh_set_shadow_mesh", "mesh", "shadow_mesh"), &RenderingServer::mesh_set_shadow_mesh);
23622378

servers/rendering_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ class RenderingServer : public Object {
401401
virtual uint32_t mesh_surface_get_format_normal_tangent_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const;
402402
virtual uint32_t mesh_surface_get_format_attribute_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const;
403403
virtual uint32_t mesh_surface_get_format_skin_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const;
404+
virtual uint32_t mesh_surface_get_format_index_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const;
404405

405406
/// Returns stride
406407
virtual void mesh_surface_make_offsets_from_format(uint64_t p_format, int p_vertex_len, int p_index_len, uint32_t *r_offsets, uint32_t &r_vertex_element_size, uint32_t &r_normal_element_size, uint32_t &r_attrib_element_size, uint32_t &r_skin_element_size) const;
@@ -426,6 +427,7 @@ class RenderingServer : public Object {
426427
virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
427428
virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
428429
virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
430+
virtual void mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
429431

430432
virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) = 0;
431433
virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const = 0;

0 commit comments

Comments
 (0)