From b8bd304bd45397b8c5a5ce850c4ceb2fdefe4961 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sat, 18 Jun 2022 13:32:46 +0200 Subject: Geometry Nodes: speedup Separate XYZ node This speeds up the node ~20% in common cases, e.g. when only the X axis is used. The main optimization comes from not writing to memory that's not used afterwards anymore anyway. The "optimal code" for just extracting the x axis in a separate loop was not faster for me. That indicates that the node is bottlenecked by memory bandwidth, which seems reasonable. --- .../nodes/shader/nodes/node_shader_sepcomb_xyz.cc | 38 +++++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'source') diff --git a/source/blender/nodes/shader/nodes/node_shader_sepcomb_xyz.cc b/source/blender/nodes/shader/nodes/node_shader_sepcomb_xyz.cc index 94a6febe92e..d4413036555 100644 --- a/source/blender/nodes/shader/nodes/node_shader_sepcomb_xyz.cc +++ b/source/blender/nodes/shader/nodes/node_shader_sepcomb_xyz.cc @@ -48,16 +48,36 @@ class MF_SeparateXYZ : public fn::MultiFunction { void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override { const VArray &vectors = params.readonly_single_input(0, "XYZ"); - MutableSpan xs = params.uninitialized_single_output(1, "X"); - MutableSpan ys = params.uninitialized_single_output(2, "Y"); - MutableSpan zs = params.uninitialized_single_output(3, "Z"); - - for (int64_t i : mask) { - float3 xyz = vectors[i]; - xs[i] = xyz.x; - ys[i] = xyz.y; - zs[i] = xyz.z; + MutableSpan xs = params.uninitialized_single_output_if_required(1, "X"); + MutableSpan ys = params.uninitialized_single_output_if_required(2, "Y"); + MutableSpan zs = params.uninitialized_single_output_if_required(3, "Z"); + + std::array, 3> outputs = {xs, ys, zs}; + Vector used_outputs; + if (!xs.is_empty()) { + used_outputs.append(0); } + if (!ys.is_empty()) { + used_outputs.append(1); + } + if (!zs.is_empty()) { + used_outputs.append(2); + } + + devirtualize_varray(vectors, [&](auto vectors) { + mask.to_best_mask_type([&](auto mask) { + const int used_outputs_num = used_outputs.size(); + const int *used_outputs_data = used_outputs.data(); + + for (const int64_t i : mask) { + const float3 &vector = vectors[i]; + for (const int out_i : IndexRange(used_outputs_num)) { + const int coordinate = used_outputs_data[out_i]; + outputs[coordinate][i] = vector[coordinate]; + } + } + }); + }); } }; -- cgit v1.2.3