Edit Mesh Selection: Move ED_view3d_select_ functions to bf_draw

It is easier to deal with private values of the DRW_select engine and gives room for improvement.

Reviewers: campbellbarton, fclem

Differential Revision: https://developer.blender.org/D5415
This commit is contained in:
Germano Cavalcante 2019-08-07 12:43:04 -03:00
parent 9d7d34c12a
commit 764cc75e1f
16 changed files with 380 additions and 346 deletions

View File

@ -129,8 +129,10 @@ set(SRC
engines/gpencil/gpencil_shader_fx.c
engines/select/select_engine.c
engines/select/select_draw_utils.c
engines/select/select_buffer.c
DRW_engine.h
DRW_select_buffer.h
intern/DRW_render.h
intern/draw_cache.h
intern/draw_cache_impl.h

View File

@ -176,17 +176,4 @@ void DRW_deferred_shader_remove(struct GPUMaterial *mat);
struct DrawDataList *DRW_drawdatalist_from_id(struct ID *id);
void DRW_drawdata_free(struct ID *id);
/* select_engine.c */
void DRW_select_context_create(struct Base **bases, const uint bases_len, short select_mode);
bool DRW_select_elem_get(const uint sel_id, uint *r_elem, uint *r_base_index, char *r_elem_type);
uint DRW_select_context_offset_for_object_elem(const uint base_index, char elem_type);
uint DRW_select_context_elem_len(void);
uint *DRW_framebuffer_select_id_read(const struct rcti *rect, uint *r_buf_len);
void DRW_draw_select_id_object(struct Depsgraph *depsgraph,
struct ViewLayer *view_layer,
struct ARegion *ar,
struct View3D *v3d,
struct Object *ob,
short select_mode);
#endif /* __DRW_ENGINE_H__ */

View File

@ -0,0 +1,65 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright 2016, Blender Foundation.
*/
/** \file
* \ingroup draw
*/
#ifndef __DRW_SELECT_BUFFER_H__
#define __DRW_SELECT_BUFFER_H__
#include "BLI_sys_types.h" /* for bool and uint */
struct ARegion;
struct Base;
struct Depsgraph;
struct Object;
struct View3D;
struct ViewLayer;
struct rcti;
/* select_buffer.c */
void DRW_select_buffer_context_create(struct Base **bases,
const uint bases_len,
short select_mode);
bool DRW_select_buffer_elem_get(const uint sel_id,
uint *r_elem,
uint *r_base_index,
char *r_elem_type);
uint DRW_select_buffer_context_offset_for_object_elem(const uint base_index, char elem_type);
uint *DRW_select_buffer_read(const struct rcti *rect, uint *r_buf_len);
void DRW_draw_select_id_object(struct Depsgraph *depsgraph,
struct ViewLayer *view_layer,
struct ARegion *ar,
struct View3D *v3d,
struct Object *ob,
short select_mode);
uint *DRW_select_buffer_bitmap_from_rect(const struct rcti *rect, uint *r_bitmap_len);
uint *DRW_select_buffer_bitmap_from_circle(const int center[2],
const int radius,
uint *r_bitmap_len);
uint *DRW_select_buffer_bitmap_from_poly(const int poly[][2],
const int poly_len,
const struct rcti *rect);
uint DRW_select_buffer_sample_point(const int center[2]);
uint DRW_select_buffer_find_nearest_to_point(const int center[2],
const uint id_min,
const uint id_max,
uint *dist);
#endif /* __DRW_SELECT_BUFFER_H__ */

View File

@ -13,17 +13,13 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2008 Blender Foundation.
* All rights reserved.
* Copyright 2019, Blender Foundation.
*/
/** \file
* \ingroup edutil
* \ingroup draw_engine
*
* Generic utilities for handling buffer selection where selection ID's are drawn onto
* an off screen buffer.
*
* All coordinates are relative to the current region.
* Utilities to read id buffer created in select_engine.
*/
#include "MEM_guardedalloc.h"
@ -31,11 +27,67 @@
#include "BLI_bitmap.h"
#include "BLI_bitmap_draw_2d.h"
#include "BLI_rect.h"
#include "BLI_utildefines.h"
#include "DNA_screen_types.h"
#include "GPU_select.h"
#include "DRW_engine.h"
#include "DRW_select_buffer.h"
#include "ED_select_buffer_utils.h"
#include "select_private.h"
#include "select_engine.h"
/* -------------------------------------------------------------------- */
/** \name Buffer of select ID's
* \{ */
/* Read a block of pixels from the select frame buffer. */
uint *DRW_select_buffer_read(const rcti *rect, uint *r_buf_len)
{
struct SELECTID_Context *select_ctx = select_context_get();
/* clamp rect by texture */
rcti r = {
.xmin = 0,
.xmax = GPU_texture_width(select_ctx->texture_u32),
.ymin = 0,
.ymax = GPU_texture_height(select_ctx->texture_u32),
};
rcti rect_clamp = *rect;
if (BLI_rcti_isect(&r, &rect_clamp, &rect_clamp)) {
uint buf_len = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
uint *r_buf = MEM_mallocN(buf_len * sizeof(*r_buf), __func__);
DRW_opengl_context_enable();
GPU_framebuffer_bind(select_ctx->framebuffer_select_id);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(rect_clamp.xmin,
rect_clamp.ymin,
BLI_rcti_size_x(&rect_clamp),
BLI_rcti_size_y(&rect_clamp),
GL_RED_INTEGER,
GL_UNSIGNED_INT,
r_buf);
GPU_framebuffer_restore();
DRW_opengl_context_disable();
if (!BLI_rcti_compare(rect, &rect_clamp)) {
GPU_select_buffer_stride_realign(rect, &rect_clamp, r_buf);
}
if (r_buf_len) {
*r_buf_len = buf_len;
}
return r_buf;
}
return NULL;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Select Bitmap from ID's
@ -46,13 +98,15 @@
* \{ */
/**
* \param bitmap_len: Number of indices in the selection id buffer.
* \param rect: The rectangle to sample indices from (min/max inclusive).
* \param mask: Specifies the rect pixels (optional).
* \returns a #BLI_bitmap the length of \a bitmap_len or NULL on failure.
*/
uint *ED_select_buffer_bitmap_from_rect(const rcti *rect, uint *r_bitmap_len)
uint *DRW_select_buffer_bitmap_from_rect(const rcti *rect, uint *r_bitmap_len)
{
const uint bitmap_len = DRW_select_context_elem_len();
struct SELECTID_Context *select_ctx = select_context_get();
const uint bitmap_len = select_ctx->last_index_drawn;
if (bitmap_len == 0) {
return NULL;
}
@ -62,16 +116,13 @@ uint *ED_select_buffer_bitmap_from_rect(const rcti *rect, uint *r_bitmap_len)
rect_px.ymax += 1;
uint buf_len;
const uint *buf = DRW_framebuffer_select_id_read(&rect_px, &buf_len);
uint *buf = DRW_select_buffer_read(&rect_px, &buf_len);
if (buf == NULL) {
return NULL;
}
const uint *buf_iter = buf;
BLI_bitmap *bitmap_buf = BLI_BITMAP_NEW(bitmap_len, __func__);
const uint *buf_iter = buf;
while (buf_len--) {
const uint index = *buf_iter - 1;
if (index < bitmap_len) {
@ -79,12 +130,8 @@ uint *ED_select_buffer_bitmap_from_rect(const rcti *rect, uint *r_bitmap_len)
}
buf_iter++;
}
MEM_freeN((void *)buf);
if (r_bitmap_len) {
*r_bitmap_len = bitmap_len;
}
return bitmap_buf;
}
@ -94,11 +141,13 @@ uint *ED_select_buffer_bitmap_from_rect(const rcti *rect, uint *r_bitmap_len)
* \param radius: Circle radius.
* \returns a #BLI_bitmap the length of \a bitmap_len or NULL on failure.
*/
uint *ED_select_buffer_bitmap_from_circle(const int center[2],
const int radius,
uint *r_bitmap_len)
uint *DRW_select_buffer_bitmap_from_circle(const int center[2],
const int radius,
uint *r_bitmap_len)
{
const uint bitmap_len = DRW_select_context_elem_len();
struct SELECTID_Context *select_ctx = select_context_get();
const uint bitmap_len = select_ctx->last_index_drawn;
if (bitmap_len == 0) {
return NULL;
}
@ -110,7 +159,7 @@ uint *ED_select_buffer_bitmap_from_circle(const int center[2],
.ymax = center[1] + radius + 1,
};
const uint *buf = DRW_framebuffer_select_id_read(&rect, NULL);
const uint *buf = DRW_select_buffer_read(&rect, NULL);
if (buf == NULL) {
return NULL;
@ -145,7 +194,7 @@ struct PolyMaskData {
int width;
};
static void ed_select_buffer_mask_px_cb(int x, int x_end, int y, void *user_data)
static void drw_select_mask_px_cb(int x, int x_end, int y, void *user_data)
{
struct PolyMaskData *data = user_data;
BLI_bitmap *px = data->px;
@ -157,18 +206,16 @@ static void ed_select_buffer_mask_px_cb(int x, int x_end, int y, void *user_data
}
/**
* \param bitmap_len: Number of indices in the selection id buffer.
* \param center: Circle center.
* \param radius: Circle radius.
* \returns a #BLI_bitmap the length of \a bitmap_len or NULL on failure.
* \param poly: The polygon coordinates.
* \param poly_len: Length of the polygon.
* \param rect: Polygon boundaries.
* \returns a #BLI_bitmap.
*/
uint *ED_select_buffer_bitmap_from_poly(const int poly[][2],
const int poly_len,
const rcti *rect,
uint *r_bitmap_len)
uint *DRW_select_buffer_bitmap_from_poly(const int poly[][2], const int poly_len, const rcti *rect)
{
const uint bitmap_len = DRW_select_context_elem_len();
struct SELECTID_Context *select_ctx = select_context_get();
const uint bitmap_len = select_ctx->last_index_drawn;
if (bitmap_len == 0) {
return NULL;
}
@ -177,15 +224,15 @@ uint *ED_select_buffer_bitmap_from_poly(const int poly[][2],
rect_px.xmax += 1;
rect_px.ymax += 1;
struct PolyMaskData poly_mask_data;
uint buf_len;
const uint *buf = DRW_framebuffer_select_id_read(&rect_px, &buf_len);
uint *buf = DRW_select_buffer_read(&rect_px, &buf_len);
if (buf == NULL) {
return NULL;
}
BLI_bitmap *buf_mask = BLI_BITMAP_NEW(buf_len, __func__);
struct PolyMaskData poly_mask_data;
poly_mask_data.px = buf_mask;
poly_mask_data.width = (rect->xmax - rect->xmin) + 1;
@ -195,12 +242,11 @@ uint *ED_select_buffer_bitmap_from_poly(const int poly[][2],
rect_px.ymax,
poly,
poly_len,
ed_select_buffer_mask_px_cb,
drw_select_mask_px_cb,
&poly_mask_data);
/* Build selection lookup. */
const uint *buf_iter = buf;
BLI_bitmap *bitmap_buf = BLI_BITMAP_NEW(bitmap_len, __func__);
const uint *buf_iter = buf;
int i = 0;
while (buf_len--) {
const uint index = *buf_iter - 1;
@ -213,10 +259,6 @@ uint *ED_select_buffer_bitmap_from_poly(const int poly[][2],
MEM_freeN((void *)buf);
MEM_freeN(buf_mask);
if (r_bitmap_len) {
*r_bitmap_len = bitmap_len;
}
return bitmap_buf;
}
@ -232,7 +274,7 @@ uint *ED_select_buffer_bitmap_from_poly(const int poly[][2],
/**
* Samples a single pixel.
*/
uint ED_select_buffer_sample_point(const int center[2])
uint DRW_select_buffer_sample_point(const int center[2])
{
const rcti rect = {
.xmin = center[0],
@ -242,7 +284,7 @@ uint ED_select_buffer_sample_point(const int center[2])
};
uint buf_len;
uint *buf = DRW_framebuffer_select_id_read(&rect, &buf_len);
uint *buf = DRW_select_buffer_read(&rect, &buf_len);
BLI_assert(0 != buf_len);
uint ret = buf[0];
MEM_freeN(buf);
@ -254,10 +296,10 @@ uint ED_select_buffer_sample_point(const int center[2])
* \param dist[in,out]: Use to initialize the distance,
* when found, this value is set to the distance of the selection that's returned.
*/
uint ED_select_buffer_find_nearest_to_point(const int center[2],
const uint id_min,
const uint id_max,
uint *dist)
uint DRW_select_buffer_find_nearest_to_point(const int center[2],
const uint id_min,
const uint id_max,
uint *dist)
{
/* Smart function to sample a rect spiraling outside, nice for selection ID. */
@ -279,7 +321,7 @@ uint ED_select_buffer_find_nearest_to_point(const int center[2],
/* Read from selection framebuffer. */
uint buf_len;
const uint *buf = DRW_framebuffer_select_id_read(&rect, &buf_len);
const uint *buf = DRW_select_buffer_read(&rect, &buf_len);
if (buf == NULL) {
return index;
@ -343,3 +385,112 @@ exit:
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Object Utils
* \{ */
bool DRW_select_buffer_elem_get(const uint sel_id,
uint *r_elem,
uint *r_base_index,
char *r_elem_type)
{
struct SELECTID_Context *select_ctx = select_context_get();
char elem_type = 0;
uint elem_id;
uint base_index = 0;
for (; base_index < select_ctx->objects_len; base_index++) {
struct BaseOffset *base_ofs = &select_ctx->index_offsets[base_index];
if (base_ofs->face > sel_id) {
elem_id = sel_id - base_ofs->face_start;
elem_type = SCE_SELECT_FACE;
break;
}
if (base_ofs->edge > sel_id) {
elem_id = sel_id - base_ofs->edge_start;
elem_type = SCE_SELECT_EDGE;
break;
}
if (base_ofs->vert > sel_id) {
elem_id = sel_id - base_ofs->vert_start;
elem_type = SCE_SELECT_VERTEX;
break;
}
}
if (base_index == select_ctx->objects_len) {
return false;
}
*r_elem = elem_id;
if (r_base_index) {
*r_base_index = base_index;
}
if (r_elem_type) {
*r_elem_type = elem_type;
}
return true;
}
uint DRW_select_buffer_context_offset_for_object_elem(const uint base_index, char elem_type)
{
struct SELECTID_Context *select_ctx = select_context_get();
struct BaseOffset *base_ofs = &select_ctx->index_offsets[base_index];
if (elem_type == SCE_SELECT_VERTEX) {
return base_ofs->vert_start - 1;
}
if (elem_type == SCE_SELECT_EDGE) {
return base_ofs->edge_start - 1;
}
if (elem_type == SCE_SELECT_FACE) {
return base_ofs->face_start - 1;
}
BLI_assert(0);
return 0;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Context
* \{ */
void DRW_select_buffer_context_create(Base **UNUSED(bases),
const uint bases_len,
short select_mode)
{
struct SELECTID_Context *select_ctx = select_context_get();
select_ctx->select_mode = select_mode;
select_ctx->objects_len = bases_len;
MEM_SAFE_FREE(select_ctx->index_offsets);
select_ctx->index_offsets = MEM_mallocN(sizeof(*select_ctx->index_offsets) * bases_len,
__func__);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Legacy
* \{ */
void DRW_draw_select_id_object(Depsgraph *depsgraph,
ViewLayer *view_layer,
ARegion *ar,
View3D *v3d,
Object *ob,
short select_mode)
{
Base *base = BKE_view_layer_base_find(view_layer, ob);
DRW_draw_select_id(depsgraph, ar, v3d, &base, 1, select_mode);
}
/** \} */

View File

@ -40,6 +40,37 @@
/** \name Draw Utilities
* \{ */
void draw_select_framebuffer_select_id_setup(struct SELECTID_Context *select_ctx)
{
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
int size[2];
size[0] = GPU_texture_width(dtxl->depth);
size[1] = GPU_texture_height(dtxl->depth);
if (select_ctx->framebuffer_select_id == NULL) {
select_ctx->framebuffer_select_id = GPU_framebuffer_create();
}
if ((select_ctx->texture_u32 != NULL) &&
((GPU_texture_width(select_ctx->texture_u32) != size[0]) ||
(GPU_texture_height(select_ctx->texture_u32) != size[1]))) {
GPU_texture_free(select_ctx->texture_u32);
select_ctx->texture_u32 = NULL;
}
/* Make sure the depth texture is attached.
* It may disappear when loading another Blender session. */
GPU_framebuffer_texture_attach(select_ctx->framebuffer_select_id, dtxl->depth, 0, 0);
if (select_ctx->texture_u32 == NULL) {
select_ctx->texture_u32 = GPU_texture_create_2d(size[0], size[1], GPU_R32UI, NULL, NULL);
GPU_framebuffer_texture_attach(
select_ctx->framebuffer_select_id, select_ctx->texture_u32, 0, 0);
GPU_framebuffer_check_valid(select_ctx->framebuffer_select_id, NULL);
}
}
short select_id_get_object_select_mode(Scene *scene, Object *ob)
{
short r_select_mode = 0;

View File

@ -22,12 +22,9 @@
* Engine for drawing a selection map where the pixels indicate the selection indices.
*/
#include "BLI_rect.h"
#include "DNA_screen_types.h"
#include "GPU_shader.h"
#include "GPU_select.h"
#include "UI_resources.h"
@ -42,19 +39,7 @@
static struct {
SELECTID_Shaders sh_data[GPU_SHADER_CFG_LEN];
struct GPUFrameBuffer *framebuffer_select_id;
struct GPUTexture *texture_u32;
struct {
struct BaseOffset *base_array_index_offsets;
uint bases_len;
uint last_base_drawn;
/** Total number of items `base_array_index_offsets[bases_len - 1].vert`. */
uint last_index_drawn;
short select_mode;
} context;
struct SELECTID_Context context;
} e_data = {{{NULL}}}; /* Engine data */
/* Shaders */
@ -62,41 +47,6 @@ extern char datatoc_common_view_lib_glsl[];
extern char datatoc_selection_id_3D_vert_glsl[];
extern char datatoc_selection_id_frag_glsl[];
/* -------------------------------------------------------------------- */
/** \name Selection Utilities
* \{ */
static void draw_select_framebuffer_select_id_setup(void)
{
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
int size[2];
size[0] = GPU_texture_width(dtxl->depth);
size[1] = GPU_texture_height(dtxl->depth);
if (e_data.framebuffer_select_id == NULL) {
e_data.framebuffer_select_id = GPU_framebuffer_create();
}
if ((e_data.texture_u32 != NULL) && ((GPU_texture_width(e_data.texture_u32) != size[0]) ||
(GPU_texture_height(e_data.texture_u32) != size[1]))) {
GPU_texture_free(e_data.texture_u32);
e_data.texture_u32 = NULL;
}
/* Make sure the depth texture is attached.
* It may disappear when loading another Blender session. */
GPU_framebuffer_texture_attach(e_data.framebuffer_select_id, dtxl->depth, 0, 0);
if (e_data.texture_u32 == NULL) {
e_data.texture_u32 = GPU_texture_create_2d(size[0], size[1], GPU_R32UI, NULL, NULL);
GPU_framebuffer_texture_attach(e_data.framebuffer_select_id, e_data.texture_u32, 0, 0);
GPU_framebuffer_check_valid(e_data.framebuffer_select_id, NULL);
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Engine Functions
* \{ */
@ -206,15 +156,14 @@ static void select_cache_init(void *vedata)
}
}
e_data.context.last_base_drawn = 0;
e_data.context.last_object_drawn = 0;
e_data.context.last_index_drawn = 1;
}
static void select_cache_populate(void *vedata, Object *ob)
{
const DRWContextState *draw_ctx = DRW_context_state_get();
struct BaseOffset *base_ofs =
&e_data.context.base_array_index_offsets[e_data.context.last_base_drawn++];
struct BaseOffset *base_ofs = &e_data.context.index_offsets[e_data.context.last_object_drawn++];
uint offset = e_data.context.last_index_drawn;
@ -237,13 +186,14 @@ static void select_draw_scene(void *vedata)
SELECTID_PassList *psl = ((SELECTID_Data *)vedata)->psl;
/* Setup framebuffer */
draw_select_framebuffer_select_id_setup();
GPU_framebuffer_bind(e_data.framebuffer_select_id);
draw_select_framebuffer_select_id_setup(&e_data.context);
GPU_framebuffer_bind(e_data.context.framebuffer_select_id);
/* dithering and AA break color coding, so disable */
glDisable(GL_DITHER);
GPU_framebuffer_clear_color_depth(e_data.framebuffer_select_id, (const float[4]){0.0f}, 1.0f);
GPU_framebuffer_clear_color_depth(
e_data.context.framebuffer_select_id, (const float[4]){0.0f}, 1.0f);
DRW_view_set_active(stl->g_data->view_faces);
DRW_draw_pass(psl->select_id_face_pass);
@ -267,150 +217,9 @@ static void select_engine_free(void)
DRW_SHADER_FREE_SAFE(sh_data->select_id_uniform);
}
DRW_TEXTURE_FREE_SAFE(e_data.texture_u32);
GPU_FRAMEBUFFER_FREE_SAFE(e_data.framebuffer_select_id);
MEM_SAFE_FREE(e_data.context.base_array_index_offsets);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Exposed `DRW_engine.h` functions
* \{ */
bool DRW_select_elem_get(const uint sel_id, uint *r_elem, uint *r_base_index, char *r_elem_type)
{
char elem_type = 0;
uint elem_id;
uint base_index = 0;
for (; base_index < e_data.context.bases_len; base_index++) {
struct BaseOffset *base_ofs = &e_data.context.base_array_index_offsets[base_index];
if (base_ofs->face > sel_id) {
elem_id = sel_id - base_ofs->face_start;
elem_type = SCE_SELECT_FACE;
break;
}
if (base_ofs->edge > sel_id) {
elem_id = sel_id - base_ofs->edge_start;
elem_type = SCE_SELECT_EDGE;
break;
}
if (base_ofs->vert > sel_id) {
elem_id = sel_id - base_ofs->vert_start;
elem_type = SCE_SELECT_VERTEX;
break;
}
}
if (base_index == e_data.context.bases_len) {
return false;
}
*r_elem = elem_id;
if (r_base_index) {
*r_base_index = base_index;
}
if (r_elem_type) {
*r_elem_type = elem_type;
}
return true;
}
uint DRW_select_context_offset_for_object_elem(const uint base_index, char elem_type)
{
struct BaseOffset *base_ofs = &e_data.context.base_array_index_offsets[base_index];
if (elem_type == SCE_SELECT_VERTEX) {
return base_ofs->vert_start - 1;
}
if (elem_type == SCE_SELECT_EDGE) {
return base_ofs->edge_start - 1;
}
if (elem_type == SCE_SELECT_FACE) {
return base_ofs->face_start - 1;
}
BLI_assert(0);
return 0;
}
uint DRW_select_context_elem_len(void)
{
return e_data.context.last_index_drawn;
}
/* Read a block of pixels from the select frame buffer. */
uint *DRW_framebuffer_select_id_read(const rcti *rect, uint *r_buf_len)
{
/* clamp rect by texture */
rcti r = {
.xmin = 0,
.xmax = GPU_texture_width(e_data.texture_u32),
.ymin = 0,
.ymax = GPU_texture_height(e_data.texture_u32),
};
rcti rect_clamp = *rect;
if (BLI_rcti_isect(&r, &rect_clamp, &rect_clamp)) {
size_t buf_len = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
uint *r_buf = MEM_mallocN(buf_len * sizeof(*r_buf), __func__);
DRW_opengl_context_enable();
GPU_framebuffer_bind(e_data.framebuffer_select_id);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(rect_clamp.xmin,
rect_clamp.ymin,
BLI_rcti_size_x(&rect_clamp),
BLI_rcti_size_y(&rect_clamp),
GL_RED_INTEGER,
GL_UNSIGNED_INT,
r_buf);
GPU_framebuffer_restore();
DRW_opengl_context_disable();
if (!BLI_rcti_compare(rect, &rect_clamp)) {
GPU_select_buffer_stride_realign(rect, &rect_clamp, r_buf);
}
if (r_buf_len) {
*r_buf_len = buf_len;
}
return r_buf;
}
return NULL;
}
void DRW_select_context_create(Base **UNUSED(bases), const uint bases_len, short select_mode)
{
e_data.context.select_mode = select_mode;
e_data.context.bases_len = bases_len;
MEM_SAFE_FREE(e_data.context.base_array_index_offsets);
e_data.context.base_array_index_offsets = MEM_mallocN(
sizeof(*e_data.context.base_array_index_offsets) * bases_len, __func__);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Legacy
* \{ */
void DRW_draw_select_id_object(Depsgraph *depsgraph,
ViewLayer *view_layer,
ARegion *ar,
View3D *v3d,
Object *ob,
short select_mode)
{
Base *base = BKE_view_layer_base_find(view_layer, ob);
DRW_draw_select_id(depsgraph, ar, v3d, &base, 1, select_mode);
DRW_TEXTURE_FREE_SAFE(e_data.context.texture_u32);
GPU_FRAMEBUFFER_FREE_SAFE(e_data.context.framebuffer_select_id);
MEM_SAFE_FREE(e_data.context.index_offsets);
}
/** \} */
@ -459,4 +268,15 @@ RenderEngineType DRW_engine_viewport_select_type = {
/** \} */
/* -------------------------------------------------------------------- */
/** \name Exposed `select_private.h` functions
* \{ */
struct SELECTID_Context *select_context_get(void)
{
return &e_data.context;
}
/** \} */
#undef SELECT_ENGINE

View File

@ -79,8 +79,25 @@ struct BaseOffset {
uint vert;
};
short select_id_get_object_select_mode(Scene *scene, Object *ob);
struct SELECTID_Context {
struct GPUFrameBuffer *framebuffer_select_id;
struct GPUTexture *texture_u32;
struct BaseOffset *index_offsets;
uint objects_len;
uint last_object_drawn;
/** Total number of items `base_array_index_offsets[bases_len - 1].vert`. */
uint last_index_drawn;
short select_mode;
};
/* select_engine.c */
struct SELECTID_Context *select_context_get(void);
/* select_draw_utils.c */
void draw_select_framebuffer_select_id_setup(struct SELECTID_Context *r_select_ctx);
short select_id_get_object_select_mode(Scene *scene, Object *ob);
void select_id_draw_object(void *vedata,
View3D *v3d,
Object *ob,

View File

@ -96,6 +96,8 @@
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
#include "DRW_select_buffer.h"
#ifdef USE_GPU_SELECT
# include "GPU_select.h"
#endif
@ -2563,7 +2565,7 @@ void DRW_draw_select_id(Depsgraph *depsgraph,
Scene *scene = DEG_get_evaluated_scene(depsgraph);
ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
DRW_select_context_create(bases, bases_len, select_mode);
DRW_select_buffer_context_create(bases, bases_len, select_mode);
DRW_opengl_context_enable();

View File

@ -1,43 +0,0 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup editors
*/
#ifndef __ED_SELECT_BUFFER_UTILS_H__
#define __ED_SELECT_BUFFER_UTILS_H__
struct rcti;
/* Boolean array from selection ID's. */
uint *ED_select_buffer_bitmap_from_rect(const struct rcti *rect, uint *r_bitmap_len);
uint *ED_select_buffer_bitmap_from_circle(const int center[2],
const int radius,
uint *r_bitmap_len);
uint *ED_select_buffer_bitmap_from_poly(const int poly[][2],
const int poly_len,
const rcti *rect,
uint *r_bitmap_len);
/* Single result from selection ID's. */
uint ED_select_buffer_sample_point(const int center[2]);
uint ED_select_buffer_find_nearest_to_point(const int center[2],
const uint id_min,
const uint id_max,
uint *dist);
#endif /* __ED_SELECT_BUFFER_UTILS_H__ */

View File

@ -53,7 +53,6 @@
#include "ED_mesh.h"
#include "ED_screen.h"
#include "ED_transform.h"
#include "ED_select_buffer_utils.h"
#include "ED_select_utils.h"
#include "ED_view3d.h"
@ -69,6 +68,7 @@
#include "DEG_depsgraph_query.h"
#include "DRW_engine.h"
#include "DRW_select_buffer.h"
#include "mesh_intern.h" /* own include */
@ -203,7 +203,7 @@ static BMElem *edbm_select_id_bm_elem_get(Base **bases, const uint sel_id, uint
{
uint elem_id;
char elem_type = 0;
bool success = DRW_select_elem_get(sel_id, &elem_id, r_base_index, &elem_type);
bool success = DRW_select_buffer_elem_get(sel_id, &elem_id, r_base_index, &elem_type);
if (success) {
Object *obedit = bases[*r_base_index]->object;
@ -319,7 +319,7 @@ BMVert *EDBM_vert_find_nearest_ex(ViewContext *vc,
{
DRW_draw_select_id(vc->depsgraph, vc->ar, vc->v3d, bases, bases_len, SCE_SELECT_VERTEX);
index = ED_select_buffer_find_nearest_to_point(vc->mval, 1, UINT_MAX, &dist_px);
index = DRW_select_buffer_find_nearest_to_point(vc->mval, 1, UINT_MAX, &dist_px);
if (index) {
eve = (BMVert *)edbm_select_id_bm_elem_get(bases, index, &base_index);
@ -541,7 +541,7 @@ BMEdge *EDBM_edge_find_nearest_ex(ViewContext *vc,
{
DRW_draw_select_id(vc->depsgraph, vc->ar, vc->v3d, bases, bases_len, SCE_SELECT_EDGE);
index = ED_select_buffer_find_nearest_to_point(vc->mval, 1, UINT_MAX, &dist_px);
index = DRW_select_buffer_find_nearest_to_point(vc->mval, 1, UINT_MAX, &dist_px);
if (index) {
eed = (BMEdge *)edbm_select_id_bm_elem_get(bases, index, &base_index);
@ -747,7 +747,7 @@ BMFace *EDBM_face_find_nearest_ex(ViewContext *vc,
{
DRW_draw_select_id(vc->depsgraph, vc->ar, vc->v3d, bases, bases_len, SCE_SELECT_FACE);
index = ED_select_buffer_sample_point(vc->mval);
index = DRW_select_buffer_sample_point(vc->mval);
if (index) {
efa = (BMFace *)edbm_select_id_bm_elem_get(bases, index, &base_index);

View File

@ -60,8 +60,9 @@
#include "DEG_depsgraph_build.h"
#include "DEG_depsgraph_query.h"
#include "DRW_select_buffer.h"
#include "ED_mesh.h"
#include "ED_select_buffer_utils.h"
#include "ED_object.h"
#include "ED_view3d.h"
@ -1115,11 +1116,11 @@ bool ED_mesh_pick_face(bContext *C, Object *ob, const int mval[2], uint dist_px,
if (dist_px) {
/* sample rect to increase chances of selecting, so that when clicking
* on an edge in the backbuf, we can still select a face */
*r_index = ED_select_buffer_find_nearest_to_point(mval, 1, me->totpoly + 1, &dist_px);
*r_index = DRW_select_buffer_find_nearest_to_point(mval, 1, me->totpoly + 1, &dist_px);
}
else {
/* sample only on the exact position */
*r_index = ED_select_buffer_sample_point(mval);
*r_index = DRW_select_buffer_sample_point(mval);
}
if ((*r_index) == 0 || (*r_index) > (unsigned int)me->totpoly) {
@ -1296,11 +1297,11 @@ bool ED_mesh_pick_vert(
if (dist_px > 0) {
/* sample rect to increase chances of selecting, so that when clicking
* on an face in the backbuf, we can still select a vert */
*r_index = ED_select_buffer_find_nearest_to_point(mval, 1, me->totvert + 1, &dist_px);
*r_index = DRW_select_buffer_find_nearest_to_point(mval, 1, me->totvert + 1, &dist_px);
}
else {
/* sample only on the exact position */
*r_index = ED_select_buffer_sample_point(mval);
*r_index = DRW_select_buffer_sample_point(mval);
}
if ((*r_index) == 0 || (*r_index) > (uint)me->totvert) {

View File

@ -23,6 +23,7 @@ set(INC
../../blentranslation
../../bmesh
../../depsgraph
../../draw
../../gpu
../../imbuf
../../makesdna

View File

@ -71,7 +71,8 @@
#include "BLI_sys_types.h"
#include "ED_mesh.h" /* for face mask functions */
#include "ED_select_buffer_utils.h"
#include "DRW_select_buffer.h"
#include "WM_api.h"
#include "WM_types.h"
@ -391,7 +392,7 @@ static int imapaint_pick_face(ViewContext *vc,
/* sample only on the exact position */
ED_view3d_select_id_validate(vc);
*r_index = ED_select_buffer_sample_point(mval);
*r_index = DRW_select_buffer_sample_point(mval);
if ((*r_index) == 0 || (*r_index) > (unsigned int)totpoly) {
return 0;

View File

@ -102,6 +102,7 @@
#include "RE_engine.h"
#include "DRW_engine.h"
#include "DRW_select_buffer.h"
#include "view3d_intern.h" /* own include */

View File

@ -89,7 +89,6 @@
#include "ED_mesh.h"
#include "ED_object.h"
#include "ED_screen.h"
#include "ED_select_buffer_utils.h"
#include "ED_select_utils.h"
#include "ED_sculpt.h"
#include "ED_mball.h"
@ -105,6 +104,7 @@
#include "DEG_depsgraph_query.h"
#include "DRW_engine.h"
#include "DRW_select_buffer.h"
#include "view3d_intern.h" /* own include */
@ -269,7 +269,8 @@ static bool edbm_backbuf_check_and_select_verts(struct EditSelectBuf_Cache *esel
bool changed = false;
const BLI_bitmap *select_bitmap = esel->select_bitmap;
uint index = DRW_select_context_offset_for_object_elem(ob->runtime.select_id, SCE_SELECT_VERTEX);
uint index = DRW_select_buffer_context_offset_for_object_elem(ob->runtime.select_id,
SCE_SELECT_VERTEX);
BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
@ -296,7 +297,8 @@ static bool edbm_backbuf_check_and_select_edges(struct EditSelectBuf_Cache *esel
bool changed = false;
const BLI_bitmap *select_bitmap = esel->select_bitmap;
uint index = DRW_select_context_offset_for_object_elem(ob->runtime.select_id, SCE_SELECT_EDGE);
uint index = DRW_select_buffer_context_offset_for_object_elem(ob->runtime.select_id,
SCE_SELECT_EDGE);
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
@ -323,7 +325,8 @@ static bool edbm_backbuf_check_and_select_faces(struct EditSelectBuf_Cache *esel
bool changed = false;
const BLI_bitmap *select_bitmap = esel->select_bitmap;
uint index = DRW_select_context_offset_for_object_elem(ob->runtime.select_id, SCE_SELECT_FACE);
uint index = DRW_select_buffer_context_offset_for_object_elem(ob->runtime.select_id,
SCE_SELECT_FACE);
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
@ -828,7 +831,7 @@ static bool do_lasso_select_mesh(ViewContext *vc,
if (wm_userdata->data == NULL) {
editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, ts->selectmode);
esel = wm_userdata->data;
esel->select_bitmap = ED_select_buffer_bitmap_from_poly(mcords, moves, &rect, NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_poly(mcords, moves, &rect);
}
}
@ -846,7 +849,7 @@ static bool do_lasso_select_mesh(ViewContext *vc,
struct LassoSelectUserData_ForMeshEdge data_for_edge = {
.data = &data,
.esel = use_zbuf ? esel : NULL,
.backbuf_offset = use_zbuf ? DRW_select_context_offset_for_object_elem(
.backbuf_offset = use_zbuf ? DRW_select_buffer_context_offset_for_object_elem(
vc->obedit->runtime.select_id, SCE_SELECT_EDGE) :
0,
};
@ -1138,8 +1141,7 @@ static bool do_lasso_select_paintvert(ViewContext *vc,
if (wm_userdata->data == NULL) {
editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, SCE_SELECT_VERTEX);
esel = wm_userdata->data;
const uint buffer_len = DRW_select_context_elem_len();
esel->select_bitmap = ED_select_buffer_bitmap_from_poly(mcords, moves, &rect, NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_poly(mcords, moves, &rect);
}
}
@ -1197,8 +1199,7 @@ static bool do_lasso_select_paintface(ViewContext *vc,
if (esel == NULL) {
editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, SCE_SELECT_FACE);
esel = wm_userdata->data;
const uint buffer_len = DRW_select_context_elem_len();
esel->select_bitmap = ED_select_buffer_bitmap_from_poly(mcords, moves, &rect, NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_poly(mcords, moves, &rect);
}
if (esel->select_bitmap) {
@ -2553,7 +2554,7 @@ static bool do_paintvert_box_select(ViewContext *vc,
if (wm_userdata->data == NULL) {
editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, SCE_SELECT_VERTEX);
esel = wm_userdata->data;
esel->select_bitmap = ED_select_buffer_bitmap_from_rect(rect, NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_rect(rect, NULL);
}
if (esel->select_bitmap != NULL) {
changed |= edbm_backbuf_check_and_select_verts_obmode(me, esel, sel_op);
@ -2607,7 +2608,7 @@ static bool do_paintface_box_select(ViewContext *vc,
if (wm_userdata->data == NULL) {
editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, SCE_SELECT_FACE);
esel = wm_userdata->data;
esel->select_bitmap = ED_select_buffer_bitmap_from_rect(rect, NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_rect(rect, NULL);
}
if (esel->select_bitmap != NULL) {
changed |= edbm_backbuf_check_and_select_faces_obmode(me, esel, sel_op);
@ -2804,7 +2805,7 @@ static bool do_mesh_box_select(ViewContext *vc,
if (wm_userdata->data == NULL) {
editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, ts->selectmode);
esel = wm_userdata->data;
esel->select_bitmap = ED_select_buffer_bitmap_from_rect(rect, NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_rect(rect, NULL);
}
}
@ -2822,7 +2823,7 @@ static bool do_mesh_box_select(ViewContext *vc,
struct BoxSelectUserData_ForMeshEdge cb_data = {
.data = &data,
.esel = use_zbuf ? esel : NULL,
.backbuf_offset = use_zbuf ? DRW_select_context_offset_for_object_elem(
.backbuf_offset = use_zbuf ? DRW_select_buffer_context_offset_for_object_elem(
vc->obedit->runtime.select_id, SCE_SELECT_EDGE) :
0,
};
@ -3391,7 +3392,7 @@ static bool mesh_circle_select(ViewContext *vc,
struct EditSelectBuf_Cache *esel = wm_userdata->data;
if (use_zbuf) {
esel->select_bitmap = ED_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
}
if (ts->selectmode & SCE_SELECT_VERTEX) {
@ -3468,7 +3469,7 @@ static bool paint_facesel_circle_select(ViewContext *vc,
{
struct EditSelectBuf_Cache *esel = wm_userdata->data;
esel->select_bitmap = ED_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
if (esel->select_bitmap != NULL) {
changed |= edbm_backbuf_check_and_select_faces_obmode(me, esel, sel_op);
MEM_freeN(esel->select_bitmap);
@ -3522,7 +3523,7 @@ static bool paint_vertsel_circle_select(ViewContext *vc,
if (use_zbuf) {
struct EditSelectBuf_Cache *esel = wm_userdata->data;
esel->select_bitmap = ED_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
esel->select_bitmap = DRW_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
if (esel->select_bitmap != NULL) {
changed |= edbm_backbuf_check_and_select_verts_obmode(me, esel, sel_op);
MEM_freeN(esel->select_bitmap);

View File

@ -22,7 +22,6 @@ set(INC
../../blentranslation
../../bmesh
../../depsgraph
../../draw
../../gpu
../../imbuf
../../makesdna
@ -42,7 +41,6 @@ set(SRC
ed_util.c
gizmo_utils.c
numinput.c
select_buffer_utils.c
select_utils.c
# general includes
@ -81,7 +79,6 @@ set(SRC
../include/ED_screen.h
../include/ED_screen_types.h
../include/ED_sculpt.h
../include/ED_select_buffer_utils.h
../include/ED_select_utils.h
../include/ED_sequencer.h
../include/ED_sound.h