You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Reduce unnecessary COW on Vector by make writing explicit
This commit makes operator[] on Vector const and adds a write proxy to it. From now on writes to Vectors need to happen through the .write proxy. So for instance: Vector<int> vec; vec.push_back(10); std::cout << vec[0] << std::endl; vec.write[0] = 20; Failing to use the .write proxy will cause a compilation error. In addition COWable datatypes can now embed a CowData pointer to their data. This means that String, CharString, and VMap no longer use or derive from Vector. _ALWAYS_INLINE_ and _FORCE_INLINE_ are now equivalent for debug and non-debug builds. This is a lot faster for Vector in the editor and while running tests. The reason why this difference used to exist is because force-inlined methods used to give a bad debugging experience. After extensive testing with modern compilers this is no longer the case.
This commit is contained in:
@@ -126,16 +126,16 @@ void Mesh::generate_debug_mesh_lines(Vector<Vector3> &r_lines) {
|
||||
PoolVector<Vector3>::Read ver_r = vertices.read();
|
||||
for (int j = 0, x = 0, i = 0; i < triangles_num; j += 6, x += 3, ++i) {
|
||||
// Triangle line 1
|
||||
r_lines[j + 0] = ver_r[ind_r[x + 0]];
|
||||
r_lines[j + 1] = ver_r[ind_r[x + 1]];
|
||||
r_lines.write[j + 0] = ver_r[ind_r[x + 0]];
|
||||
r_lines.write[j + 1] = ver_r[ind_r[x + 1]];
|
||||
|
||||
// Triangle line 2
|
||||
r_lines[j + 2] = ver_r[ind_r[x + 1]];
|
||||
r_lines[j + 3] = ver_r[ind_r[x + 2]];
|
||||
r_lines.write[j + 2] = ver_r[ind_r[x + 1]];
|
||||
r_lines.write[j + 3] = ver_r[ind_r[x + 2]];
|
||||
|
||||
// Triangle line 3
|
||||
r_lines[j + 4] = ver_r[ind_r[x + 2]];
|
||||
r_lines[j + 5] = ver_r[ind_r[x + 0]];
|
||||
r_lines.write[j + 4] = ver_r[ind_r[x + 2]];
|
||||
r_lines.write[j + 5] = ver_r[ind_r[x + 0]];
|
||||
}
|
||||
}
|
||||
void Mesh::generate_debug_mesh_indices(Vector<Vector3> &r_points) {
|
||||
@@ -148,7 +148,7 @@ void Mesh::generate_debug_mesh_indices(Vector<Vector3> &r_points) {
|
||||
int vertices_size = vertices.size();
|
||||
r_points.resize(vertices_size);
|
||||
for (int i = 0; i < vertices_size; ++i) {
|
||||
r_points[i] = vertices[i];
|
||||
r_points.write[i] = vertices[i];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -667,7 +667,7 @@ bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) {
|
||||
bone_aabb.resize(baabb.size());
|
||||
|
||||
for (int i = 0; i < baabb.size(); i++) {
|
||||
bone_aabb[i] = baabb[i];
|
||||
bone_aabb.write[i] = baabb[i];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -839,8 +839,8 @@ void ArrayMesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &
|
||||
aabb.expand_to(vtx[i]);
|
||||
}
|
||||
|
||||
surfaces[surfaces.size() - 1].aabb = aabb;
|
||||
surfaces[surfaces.size() - 1].is_2d = arr.get_type() == Variant::POOL_VECTOR2_ARRAY;
|
||||
surfaces.write[surfaces.size() - 1].aabb = aabb;
|
||||
surfaces.write[surfaces.size() - 1].is_2d = arr.get_type() == Variant::POOL_VECTOR2_ARRAY;
|
||||
|
||||
_recompute_aabb();
|
||||
}
|
||||
@@ -959,7 +959,7 @@ void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material)
|
||||
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
||||
if (surfaces[p_idx].material == p_material)
|
||||
return;
|
||||
surfaces[p_idx].material = p_material;
|
||||
surfaces.write[p_idx].material = p_material;
|
||||
VisualServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid());
|
||||
|
||||
_change_notify("material");
|
||||
@@ -970,7 +970,7 @@ void ArrayMesh::surface_set_name(int p_idx, const String &p_name) {
|
||||
|
||||
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
||||
|
||||
surfaces[p_idx].name = p_name;
|
||||
surfaces.write[p_idx].name = p_name;
|
||||
emit_changed();
|
||||
}
|
||||
|
||||
@@ -990,7 +990,7 @@ void ArrayMesh::surface_update_region(int p_surface, int p_offset, const PoolVec
|
||||
void ArrayMesh::surface_set_custom_aabb(int p_idx, const AABB &p_aabb) {
|
||||
|
||||
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
||||
surfaces[p_idx].aabb = p_aabb;
|
||||
surfaces.write[p_idx].aabb = p_aabb;
|
||||
// set custom aabb too?
|
||||
emit_changed();
|
||||
}
|
||||
@@ -1093,8 +1093,8 @@ void ArrayMesh::regen_normalmaps() {
|
||||
|
||||
for (int i = 0; i < surfs.size(); i++) {
|
||||
|
||||
surfs[i]->generate_tangents();
|
||||
surfs[i]->commit(Ref<ArrayMesh>(this));
|
||||
surfs.write[i]->generate_tangents();
|
||||
surfs.write[i]->commit(Ref<ArrayMesh>(this));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1159,13 +1159,13 @@ Error ArrayMesh::lightmap_unwrap(const Transform &p_base_transform, float p_texe
|
||||
Vector3 v = p_base_transform.xform(r[j]);
|
||||
Vector3 n = p_base_transform.basis.xform(rn[j]).normalized();
|
||||
|
||||
vertices[(j + vertex_ofs) * 3 + 0] = v.x;
|
||||
vertices[(j + vertex_ofs) * 3 + 1] = v.y;
|
||||
vertices[(j + vertex_ofs) * 3 + 2] = v.z;
|
||||
normals[(j + vertex_ofs) * 3 + 0] = n.x;
|
||||
normals[(j + vertex_ofs) * 3 + 1] = n.y;
|
||||
normals[(j + vertex_ofs) * 3 + 2] = n.z;
|
||||
uv_index[j + vertex_ofs] = Pair<int, int>(i, j);
|
||||
vertices.write[(j + vertex_ofs) * 3 + 0] = v.x;
|
||||
vertices.write[(j + vertex_ofs) * 3 + 1] = v.y;
|
||||
vertices.write[(j + vertex_ofs) * 3 + 2] = v.z;
|
||||
normals.write[(j + vertex_ofs) * 3 + 0] = n.x;
|
||||
normals.write[(j + vertex_ofs) * 3 + 1] = n.y;
|
||||
normals.write[(j + vertex_ofs) * 3 + 2] = n.z;
|
||||
uv_index.write[j + vertex_ofs] = Pair<int, int>(i, j);
|
||||
}
|
||||
|
||||
PoolVector<int> rindices = arrays[Mesh::ARRAY_INDEX];
|
||||
@@ -1248,31 +1248,31 @@ Error ArrayMesh::lightmap_unwrap(const Transform &p_base_transform, float p_texe
|
||||
SurfaceTool::Vertex v = surfaces[surface].vertices[uv_index[gen_vertices[gen_indices[i + j]]].second];
|
||||
|
||||
if (surfaces[surface].format & ARRAY_FORMAT_COLOR) {
|
||||
surfaces_tools[surface]->add_color(v.color);
|
||||
surfaces_tools.write[surface]->add_color(v.color);
|
||||
}
|
||||
if (surfaces[surface].format & ARRAY_FORMAT_TEX_UV) {
|
||||
surfaces_tools[surface]->add_uv(v.uv);
|
||||
surfaces_tools.write[surface]->add_uv(v.uv);
|
||||
}
|
||||
if (surfaces[surface].format & ARRAY_FORMAT_NORMAL) {
|
||||
surfaces_tools[surface]->add_normal(v.normal);
|
||||
surfaces_tools.write[surface]->add_normal(v.normal);
|
||||
}
|
||||
if (surfaces[surface].format & ARRAY_FORMAT_TANGENT) {
|
||||
Plane t;
|
||||
t.normal = v.tangent;
|
||||
t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;
|
||||
surfaces_tools[surface]->add_tangent(t);
|
||||
surfaces_tools.write[surface]->add_tangent(t);
|
||||
}
|
||||
if (surfaces[surface].format & ARRAY_FORMAT_BONES) {
|
||||
surfaces_tools[surface]->add_bones(v.bones);
|
||||
surfaces_tools.write[surface]->add_bones(v.bones);
|
||||
}
|
||||
if (surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) {
|
||||
surfaces_tools[surface]->add_weights(v.weights);
|
||||
surfaces_tools.write[surface]->add_weights(v.weights);
|
||||
}
|
||||
|
||||
Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
|
||||
surfaces_tools[surface]->add_uv2(uv2);
|
||||
surfaces_tools.write[surface]->add_uv2(uv2);
|
||||
|
||||
surfaces_tools[surface]->add_vertex(v.vertex);
|
||||
surfaces_tools.write[surface]->add_vertex(v.vertex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1284,8 +1284,8 @@ Error ArrayMesh::lightmap_unwrap(const Transform &p_base_transform, float p_texe
|
||||
//generate surfaces
|
||||
|
||||
for (int i = 0; i < surfaces_tools.size(); i++) {
|
||||
surfaces_tools[i]->index();
|
||||
surfaces_tools[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), surfaces[i].format);
|
||||
surfaces_tools.write[i]->index();
|
||||
surfaces_tools.write[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), surfaces[i].format);
|
||||
}
|
||||
|
||||
set_lightmap_size_hint(Size2(size_x, size_y));
|
||||
|
||||
Reference in New Issue
Block a user