Cleanup: naming for GPUSelectResult

Prefer the name 'hit_result' since 'result' was sometimes used for
a vector of GPUSelectResult and is often used a functions return value.

Use hit_results for the span/vector and hit_result for a single hit.

Also assign struct members for new GPUSelectResult as it reads better
and avoids depending on struct order.
This commit is contained in:
Campbell Barton
2023-12-01 09:45:02 +11:00
parent ac6e854335
commit 8aff65daf2
8 changed files with 64 additions and 49 deletions

View File

@@ -218,14 +218,17 @@ struct SelectMap {
GPU_memory_barrier(GPU_BARRIER_BUFFER_UPDATE);
select_output_buf.read();
Vector<GPUSelectResult> result;
Vector<GPUSelectResult> hit_results;
/* Convert raw data from GPU to #GPUSelectResult. */
switch (info_buf.mode) {
case SelectType::SELECT_ALL:
for (auto i : IndexRange(select_id_map.size())) {
if (((select_output_buf[i / 32] >> (i % 32)) & 1) != 0) {
result.append({select_id_map[i], 0xFFFFu});
GPUSelectResult hit_result{};
hit_result.id = select_id_map[i];
hit_result.depth = 0xFFFF;
hit_results.append(hit_result);
}
}
break;
@@ -236,13 +239,16 @@ struct SelectMap {
if (select_output_buf[i] != 0xFFFFFFFFu) {
/* NOTE: For `SELECT_PICK_NEAREST`, `select_output_buf` also contains the screen
* distance to cursor in the lowest bits. */
result.append({select_id_map[i], select_output_buf[i]});
GPUSelectResult hit_result{};
hit_result.id = select_id_map[i];
hit_result.depth = select_output_buf[i];
hit_results.append(hit_result);
}
}
break;
}
gpu_select_next_set_result(result.data(), result.size());
gpu_select_next_set_result(hit_results.data(), hit_results.size());
}
};

View File

@@ -171,8 +171,8 @@ static void *ed_armature_pick_bone_from_selectbuffer_impl(
bool takeNext = false;
int minsel = 0xffffffff, minunsel = 0xffffffff;
for (const GPUSelectResult &result : hit_results) {
uint hit_id = result.id;
for (const GPUSelectResult &hit_result : hit_results) {
uint hit_id = hit_result.id;
if (hit_id & BONESEL_ANY) { /* to avoid including objects in selection */
Base *base = nullptr;
@@ -212,10 +212,10 @@ static void *ed_armature_pick_bone_from_selectbuffer_impl(
if (data) {
if (sel) {
if (do_nearest) {
if (minsel > result.depth) {
if (minsel > hit_result.depth) {
firstSel = data;
firstSel_base = base;
minsel = result.depth;
minsel = hit_result.depth;
}
}
else {
@@ -228,10 +228,10 @@ static void *ed_armature_pick_bone_from_selectbuffer_impl(
}
else {
if (do_nearest) {
if (minunsel > result.depth) {
if (minunsel > hit_result.depth) {
firstunSel = data;
firstunSel_base = base;
minunsel = result.depth;
minunsel = hit_result.depth;
}
}
else {
@@ -629,18 +629,19 @@ void ARMATURE_OT_select_linked_pick(wmOperatorType *ot)
* \{ */
/* utility function for get_nearest_editbonepoint */
static int selectbuffer_ret_hits_12(blender::MutableSpan<GPUSelectResult> /*results*/,
static int selectbuffer_ret_hits_12(blender::MutableSpan<GPUSelectResult> /*hit_results*/,
const int hits12)
{
return hits12;
}
static int selectbuffer_ret_hits_5(blender::MutableSpan<GPUSelectResult> results,
static int selectbuffer_ret_hits_5(blender::MutableSpan<GPUSelectResult> hit_results,
const int hits12,
const int hits5)
{
const int ofs = hits12;
results.slice(0, hits5).copy_from(results.slice(ofs, hits5)); /* Shift results to beginning. */
/* Shift results to beginning. */
hit_results.slice(0, hits5).copy_from(hit_results.slice(ofs, hits5));
return hits5;
}

View File

@@ -1703,11 +1703,11 @@ static bool object_mouse_select_menu(bContext *C,
/* two selection methods, the CTRL select uses max dist of 15 */
if (!hit_results.is_empty()) {
for (const GPUSelectResult &result : hit_results) {
for (const GPUSelectResult &hit_result : hit_results) {
/* index was converted */
if (base->object->runtime->select_id == (result.id & ~0xFFFF0000)) {
if (base->object->runtime->select_id == (hit_result.id & ~0xFFFF0000)) {
ok = true;
depth_id = result.depth;
depth_id = hit_result.depth;
break;
}
}
@@ -1913,10 +1913,10 @@ static bool bone_mouse_select_menu(bContext *C,
/* Select logic taken from #ed_armature_pick_bone_from_selectbuffer_impl
* in `armature_select.cc`. */
for (const GPUSelectResult &result : hit_results) {
for (const GPUSelectResult &hit_result : hit_results) {
void *bone_ptr = nullptr;
Base *bone_base = nullptr;
uint hitresult = result.id;
uint hitresult = hit_result.id;
if (!(hitresult & BONESEL_ANY)) {
/* To avoid including objects in selection. */
@@ -1969,7 +1969,7 @@ static bool bone_mouse_select_menu(bContext *C,
BoneRefWithDepth *bone_ref = MEM_new<BoneRefWithDepth>(__func__);
bone_ref->base = bone_base;
bone_ref->bone_ptr = bone_ptr;
bone_ref->depth_id = result.depth;
bone_ref->depth_id = hit_result.depth;
BLI_addtail(&bone_ref_list, (void *)bone_ref);
BLI_gset_insert(added_bones, bone_ptr);
@@ -2037,8 +2037,8 @@ static bool bone_mouse_select_menu(bContext *C,
static bool selectbuffer_has_bones(const blender::Span<GPUSelectResult> hit_results)
{
for (const GPUSelectResult &result : hit_results) {
if (result.id & 0xFFFF0000) {
for (const GPUSelectResult &hit_result : hit_results) {
if (hit_result.id & 0xFFFF0000) {
return true;
}
}
@@ -2046,28 +2046,30 @@ static bool selectbuffer_has_bones(const blender::Span<GPUSelectResult> hit_resu
}
/* utility function for mixed_bones_object_selectbuffer */
static int selectbuffer_ret_hits_15(blender::MutableSpan<GPUSelectResult> /*results*/,
static int selectbuffer_ret_hits_15(blender::MutableSpan<GPUSelectResult> /*hit_results*/,
const int hits15)
{
return hits15;
}
static int selectbuffer_ret_hits_9(blender::MutableSpan<GPUSelectResult> results,
static int selectbuffer_ret_hits_9(blender::MutableSpan<GPUSelectResult> hit_results,
const int hits15,
const int hits9)
{
const int ofs = hits15;
results.slice(0, hits9).copy_from(results.slice(ofs, hits9)); /* Shift results to beginning. */
/* Shift results to beginning. */
hit_results.slice(0, hits9).copy_from(hit_results.slice(ofs, hits9));
return hits9;
}
static int selectbuffer_ret_hits_5(blender::MutableSpan<GPUSelectResult> results,
static int selectbuffer_ret_hits_5(blender::MutableSpan<GPUSelectResult> hit_results,
const int hits15,
const int hits9,
const int hits5)
{
const int ofs = hits15 + hits9;
results.slice(0, hits5).copy_from(results.slice(ofs, hits5)); /* Shift results to beginning. */
/* Shift results to beginning. */
hit_results.slice(0, hits5).copy_from(hit_results.slice(ofs, hits5));
return hits5;
}

View File

@@ -244,16 +244,16 @@ bool GPU_select_is_cached()
const GPUSelectResult *GPU_select_buffer_near(const blender::Span<GPUSelectResult> hit_results)
{
const GPUSelectResult *buffer_near = nullptr;
const GPUSelectResult *hit_result_near = nullptr;
uint depth_min = uint(-1);
for (const GPUSelectResult &result : hit_results) {
if (result.depth < depth_min) {
BLI_assert(result.id != -1);
depth_min = result.depth;
buffer_near = &result;
for (const GPUSelectResult &hit_result : hit_results) {
if (hit_result.depth < depth_min) {
BLI_assert(hit_result.id != -1);
depth_min = hit_result.depth;
hit_result_near = &hit_result;
}
}
return buffer_near;
return hit_result_near;
}
uint GPU_select_buffer_remove_by_id(blender::MutableSpan<GPUSelectResult> hit_results,
@@ -262,10 +262,10 @@ uint GPU_select_buffer_remove_by_id(blender::MutableSpan<GPUSelectResult> hit_re
uint index_src = 0;
uint index_dst = 0;
uint hits_final = 0;
for (const GPUSelectResult &result : hit_results) {
if (result.id != select_id) {
for (const GPUSelectResult &hit_result : hit_results) {
if (hit_result.id != select_id) {
if (index_dst != index_src) {
hit_results[index_dst] = result;
hit_results[index_dst] = hit_result;
}
index_dst++;
hits_final++;

View File

@@ -51,25 +51,25 @@ eGPUSelectMode gpu_select_next_get_mode()
void gpu_select_next_set_result(GPUSelectResult *hit_buf, uint hit_len)
{
g_state.buffer->storage.resize(hit_len);
blender::MutableSpan<GPUSelectResult> result = g_state.buffer->storage.as_mutable_span();
blender::MutableSpan<GPUSelectResult> hit_results = g_state.buffer->storage.as_mutable_span();
const blender::Span<GPUSelectResult> hits(hit_buf, hit_len);
/* TODO(fclem): There might be some conversion to do to align to the other APIs output. */
switch (g_state.mode) {
case eGPUSelectMode::GPU_SELECT_ALL:
result.copy_from(hits);
hit_results.copy_from(hits);
break;
case eGPUSelectMode::GPU_SELECT_NEAREST_FIRST_PASS:
result.copy_from(hits);
hit_results.copy_from(hits);
break;
case eGPUSelectMode::GPU_SELECT_NEAREST_SECOND_PASS:
result.copy_from(hits);
hit_results.copy_from(hits);
break;
case eGPUSelectMode::GPU_SELECT_PICK_ALL:
result.copy_from(hits);
hit_results.copy_from(hits);
break;
case eGPUSelectMode::GPU_SELECT_PICK_NEAREST:
result.copy_from(hits);
hit_results.copy_from(hits);
break;
}

View File

@@ -664,7 +664,10 @@ uint gpu_select_pick_end()
#ifdef DEBUG_PRINT
printf(" hit: %u: depth %u\n", depth_data[i].id, depth_data[i].depth);
#endif
g_pick_state.buffer->storage.append_unchecked({depth_data[i].id, depth_data[i].depth});
GPUSelectResult hit_result{};
hit_result.id = depth_data[i].id;
hit_result.depth = depth_data[i].depth;
g_pick_state.buffer->storage.append_unchecked(hit_result);
hits++;
}

View File

@@ -150,7 +150,10 @@ uint gpu_select_query_end()
for (int i = 0; i < result.size(); i++) {
if (result[i] != 0) {
if (g_query_state.mode != GPU_SELECT_NEAREST_SECOND_PASS) {
g_query_state.buffer->storage.append({ids[i], 0xFFFF});
GPUSelectResult hit_result{};
hit_result.id = ids[i];
hit_result.depth = 0xFFFF;
g_query_state.buffer->storage.append(hit_result);
hits++;
}
else {

View File

@@ -612,11 +612,11 @@ static int gizmo_find_intersected_3d_intern(wmGizmo **visible_gizmos,
int hit_found = -1;
float dot_best = FLT_MAX;
for (const GPUSelectResult &result : hit_results) {
BLI_assert(result.id != -1);
wmGizmo *gz = visible_gizmos[result.id >> 8];
for (const GPUSelectResult &hit_result : hit_results) {
BLI_assert(hit_result.id != -1);
wmGizmo *gz = visible_gizmos[hit_result.id >> 8];
float co_3d[3];
co_screen[2] = float(double(result.depth) / double(UINT_MAX));
co_screen[2] = float(double(hit_result.depth) / double(UINT_MAX));
GPU_matrix_unproject_3fv(co_screen, rv3d->viewinv, rv3d->winmat, viewport, co_3d);
float select_bias = gz->select_bias;
if ((gz->flag & WM_GIZMO_DRAW_NO_SCALE) == 0) {
@@ -626,7 +626,7 @@ static int gizmo_find_intersected_3d_intern(wmGizmo **visible_gizmos,
const float dot_test = dot_v3v3(co_3d, co_direction) - select_bias;
if (dot_best > dot_test) {
dot_best = dot_test;
hit_found = result.id;
hit_found = hit_result.id;
}
}
return hit_found;