Cleanup: Simplify mesh primitive grid and cuboid quad creation
Fill face offsets in one multithreaded loop with an offset indices utility function instead of keeping track of the index and setting the offset for each face.
This commit is contained in:
@@ -110,29 +110,22 @@ static void calculate_positions(const CuboidConfig &config, MutableSpan<float3>
|
||||
* Hence they are passed as 1,4,3,2 when calculating faces clockwise, and 1,2,3,4 for
|
||||
* anti-clockwise.
|
||||
*/
|
||||
static void define_quad(MutableSpan<int> face_offsets,
|
||||
MutableSpan<int> corner_verts,
|
||||
const int face_index,
|
||||
static void define_quad(MutableSpan<int> corner_verts,
|
||||
const int loop_index,
|
||||
const int vert_1,
|
||||
const int vert_2,
|
||||
const int vert_3,
|
||||
const int vert_4)
|
||||
{
|
||||
face_offsets[face_index] = loop_index;
|
||||
|
||||
corner_verts[loop_index] = vert_1;
|
||||
corner_verts[loop_index + 1] = vert_2;
|
||||
corner_verts[loop_index + 2] = vert_3;
|
||||
corner_verts[loop_index + 3] = vert_4;
|
||||
}
|
||||
|
||||
static void calculate_faces(const CuboidConfig &config,
|
||||
MutableSpan<int> face_offsets,
|
||||
MutableSpan<int> corner_verts)
|
||||
static void calculate_corner_verts(const CuboidConfig &config, MutableSpan<int> corner_verts)
|
||||
{
|
||||
int loop_index = 0;
|
||||
int face_index = 0;
|
||||
|
||||
/* Number of vertices in an XY cross-section of the cube (barring top and bottom faces). */
|
||||
const int xy_cross_section_vert_count = config.verts_x * config.verts_y -
|
||||
@@ -148,10 +141,8 @@ static void calculate_faces(const CuboidConfig &config,
|
||||
const int vert_3 = vert_2 + 1;
|
||||
const int vert_4 = vert_1 + 1;
|
||||
|
||||
define_quad(
|
||||
face_offsets, corner_verts, face_index, loop_index, vert_1, vert_2, vert_3, vert_4);
|
||||
define_quad(corner_verts, loop_index, vert_1, vert_2, vert_3, vert_4);
|
||||
loop_index += 4;
|
||||
face_index++;
|
||||
}
|
||||
vert_1_start += config.verts_x;
|
||||
}
|
||||
@@ -162,16 +153,14 @@ static void calculate_faces(const CuboidConfig &config,
|
||||
|
||||
for ([[maybe_unused]] const int z : IndexRange(config.edges_z)) {
|
||||
for (const int x : IndexRange(config.edges_x)) {
|
||||
define_quad(face_offsets,
|
||||
corner_verts,
|
||||
face_index,
|
||||
define_quad(corner_verts,
|
||||
|
||||
loop_index,
|
||||
vert_1_start + x,
|
||||
vert_1_start + x + 1,
|
||||
vert_2_start + x + 1,
|
||||
vert_2_start + x);
|
||||
loop_index += 4;
|
||||
face_index++;
|
||||
}
|
||||
vert_1_start = vert_2_start;
|
||||
vert_2_start += config.verts_x * config.verts_y - (config.verts_x - 2) * (config.verts_y - 2);
|
||||
@@ -185,16 +174,14 @@ static void calculate_faces(const CuboidConfig &config,
|
||||
|
||||
for ([[maybe_unused]] const int y : IndexRange(config.edges_y)) {
|
||||
for (const int x : IndexRange(config.edges_x)) {
|
||||
define_quad(face_offsets,
|
||||
corner_verts,
|
||||
face_index,
|
||||
define_quad(corner_verts,
|
||||
|
||||
loop_index,
|
||||
vert_1_start + x,
|
||||
vert_1_start + x + 1,
|
||||
vert_2_start + x + 1,
|
||||
vert_2_start + x);
|
||||
loop_index += 4;
|
||||
face_index++;
|
||||
}
|
||||
vert_2_start += config.verts_x;
|
||||
vert_1_start += config.verts_x;
|
||||
@@ -209,16 +196,14 @@ static void calculate_faces(const CuboidConfig &config,
|
||||
vert_2_start += (config.verts_x - 2) * (config.verts_y - 2);
|
||||
}
|
||||
for (const int x : IndexRange(config.edges_x)) {
|
||||
define_quad(face_offsets,
|
||||
corner_verts,
|
||||
face_index,
|
||||
define_quad(corner_verts,
|
||||
|
||||
loop_index,
|
||||
vert_1_start + x,
|
||||
vert_2_start + x,
|
||||
vert_2_start + x + 1,
|
||||
vert_1_start + x + 1);
|
||||
loop_index += 4;
|
||||
face_index++;
|
||||
}
|
||||
vert_2_start += xy_cross_section_vert_count;
|
||||
vert_1_start += xy_cross_section_vert_count;
|
||||
@@ -255,10 +240,8 @@ static void calculate_faces(const CuboidConfig &config,
|
||||
vert_3 = vert_2 + 2;
|
||||
}
|
||||
|
||||
define_quad(
|
||||
face_offsets, corner_verts, face_index, loop_index, vert_1, vert_2, vert_3, vert_4);
|
||||
define_quad(corner_verts, loop_index, vert_1, vert_2, vert_3, vert_4);
|
||||
loop_index += 4;
|
||||
face_index++;
|
||||
}
|
||||
if (z == 0) {
|
||||
vert_1_start += config.verts_x * config.verts_y;
|
||||
@@ -303,10 +286,8 @@ static void calculate_faces(const CuboidConfig &config,
|
||||
vert_4 = vert_1 + config.verts_x;
|
||||
}
|
||||
|
||||
define_quad(
|
||||
face_offsets, corner_verts, face_index, loop_index, vert_1, vert_4, vert_3, vert_2);
|
||||
define_quad(corner_verts, loop_index, vert_1, vert_4, vert_3, vert_2);
|
||||
loop_index += 4;
|
||||
face_index++;
|
||||
}
|
||||
if (z == 0) {
|
||||
vert_1_start += config.verts_x * config.verts_y;
|
||||
@@ -404,13 +385,12 @@ Mesh *create_cuboid_mesh(const float3 &size,
|
||||
|
||||
Mesh *mesh = BKE_mesh_new_nomain(config.vertex_count, 0, config.face_count, config.loop_count);
|
||||
MutableSpan<float3> positions = mesh->vert_positions_for_write();
|
||||
MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
|
||||
MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
|
||||
BKE_mesh_smooth_flag_set(mesh, false);
|
||||
|
||||
calculate_positions(config, positions);
|
||||
|
||||
calculate_faces(config, face_offsets, corner_verts);
|
||||
offset_indices::fill_constant_group_size(4, 0, mesh->face_offsets_for_write());
|
||||
calculate_corner_verts(config, corner_verts);
|
||||
BKE_mesh_calc_edges(mesh, false, false);
|
||||
|
||||
if (uv_id) {
|
||||
|
||||
@@ -57,16 +57,11 @@ Mesh *create_grid_mesh(const int verts_x,
|
||||
edges_x * edges_y * 4);
|
||||
MutableSpan<float3> positions = mesh->vert_positions_for_write();
|
||||
MutableSpan<int2> edges = mesh->edges_for_write();
|
||||
MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
|
||||
MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
|
||||
MutableSpan<int> corner_edges = mesh->corner_edges_for_write();
|
||||
BKE_mesh_smooth_flag_set(mesh, false);
|
||||
|
||||
threading::parallel_for(face_offsets.index_range(), 4096, [face_offsets](IndexRange range) {
|
||||
for (const int i : range) {
|
||||
face_offsets[i] = i * 4;
|
||||
}
|
||||
});
|
||||
offset_indices::fill_constant_group_size(4, 0, mesh->face_offsets_for_write());
|
||||
|
||||
{
|
||||
const float dx = edges_x == 0 ? 0.0f : size_x / edges_x;
|
||||
@@ -99,9 +94,7 @@ Mesh *create_grid_mesh(const int verts_x,
|
||||
threading::parallel_for(IndexRange(edges_y), 512, [&](IndexRange y_range) {
|
||||
for (const int y : y_range) {
|
||||
const int vert_index = y_vert_offset + y;
|
||||
int2 &edge = edges[y_edge_offset + y];
|
||||
edge[0] = vert_index;
|
||||
edge[1] = vert_index + 1;
|
||||
edges[y_edge_offset + y] = int2(vert_index, vert_index + 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -114,9 +107,7 @@ Mesh *create_grid_mesh(const int verts_x,
|
||||
threading::parallel_for(IndexRange(edges_x), 512, [&](IndexRange x_range) {
|
||||
for (const int x : x_range) {
|
||||
const int vert_index = x * verts_y + y;
|
||||
int2 &edge = edges[x_edge_offset + x];
|
||||
edge[0] = vert_index;
|
||||
edge[1] = vert_index + verts_y;
|
||||
edges[x_edge_offset + x] = int2(vert_index, vert_index + verts_y);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user