Select Engine: Move some private members out of select context

This commit is contained in:
2019-08-20 11:36:18 -03:00
parent 0827110a35
commit c48d1d54f1
5 changed files with 65 additions and 53 deletions

View File

@@ -58,9 +58,6 @@ struct ObjectOffsets {
};
struct SELECTID_Context {
struct GPUFrameBuffer *framebuffer_select_id;
struct GPUTexture *texture_u32;
/* All context objects */
struct Object **objects;
uint objects_len;

View File

@@ -44,48 +44,6 @@
/** \name Draw Utilities
* \{ */
static void select_id_framebuffer_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);
}
}
/* Remove all tags from drawn or culled objects. */
void select_id_context_clear(struct SELECTID_Context *select_ctx)
{
select_ctx->objects_drawn_len = 0;
select_ctx->index_drawn_len = 1;
select_id_framebuffer_setup(select_ctx);
GPU_framebuffer_bind(select_ctx->framebuffer_select_id);
GPU_framebuffer_clear_color_depth(
select_ctx->framebuffer_select_id, (const float[4]){0.0f}, 1.0f);
}
void select_id_object_min_max(Object *obj, float r_min[3], float r_max[3])
{
BoundBox *bb;

View File

@@ -40,16 +40,54 @@
/* *********** STATIC *********** */
static struct {
struct GPUFrameBuffer *framebuffer_select_id;
struct GPUTexture *texture_u32;
SELECTID_Shaders sh_data[GPU_SHADER_CFG_LEN];
struct SELECTID_Context context;
uint runtime_new_objects;
} e_data = {{{NULL}}}; /* Engine data */
} e_data = {NULL}; /* Engine data */
/* Shaders */
extern char datatoc_common_view_lib_glsl[];
extern char datatoc_selection_id_3D_vert_glsl[];
extern char datatoc_selection_id_frag_glsl[];
/* -------------------------------------------------------------------- */
/** \name Utils
* \{ */
static void select_engine_framebuffer_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
* \{ */
@@ -186,8 +224,13 @@ static void select_cache_init(void *vedata)
float(*persmat)[4] = draw_ctx->rv3d->persmat;
e_data.context.is_dirty = !compare_m4m4(e_data.context.persmat, persmat, FLT_EPSILON);
if (e_data.context.is_dirty) {
/* Remove all tags from drawn or culled objects. */
copy_m4_m4(e_data.context.persmat, persmat);
select_id_context_clear(&e_data.context);
e_data.context.objects_drawn_len = 0;
e_data.context.index_drawn_len = 1;
select_engine_framebuffer_setup();
GPU_framebuffer_bind(e_data.framebuffer_select_id);
GPU_framebuffer_clear_color_depth(e_data.framebuffer_select_id, (const float[4]){0.0f}, 1.0f);
}
e_data.runtime_new_objects = 0;
}
@@ -283,7 +326,7 @@ static void select_draw_scene(void *vedata)
}
/* Setup framebuffer */
GPU_framebuffer_bind(e_data.context.framebuffer_select_id);
GPU_framebuffer_bind(e_data.framebuffer_select_id);
DRW_draw_pass(psl->select_id_face_pass);
@@ -306,8 +349,8 @@ static void select_engine_free(void)
DRW_SHADER_FREE_SAFE(sh_data->select_id_uniform);
}
DRW_TEXTURE_FREE_SAFE(e_data.context.texture_u32);
GPU_FRAMEBUFFER_FREE_SAFE(e_data.context.framebuffer_select_id);
DRW_TEXTURE_FREE_SAFE(e_data.texture_u32);
GPU_FRAMEBUFFER_FREE_SAFE(e_data.framebuffer_select_id);
MEM_SAFE_FREE(e_data.context.objects);
MEM_SAFE_FREE(e_data.context.index_offsets);
MEM_SAFE_FREE(e_data.context.objects_drawn);
@@ -368,6 +411,16 @@ struct SELECTID_Context *DRW_select_engine_context_get(void)
return &e_data.context;
}
GPUFrameBuffer *DRW_engine_select_framebuffer_get(void)
{
return e_data.framebuffer_select_id;
}
GPUTexture *DRW_engine_select_texture_get(void)
{
return e_data.texture_u32;
}
/** \} */
#undef SELECT_ENGINE

View File

@@ -28,4 +28,7 @@ extern RenderEngineType DRW_engine_viewport_select_type;
struct SELECTID_Context *DRW_select_engine_context_get(void);
struct GPUFrameBuffer *DRW_engine_select_framebuffer_get(void);
struct GPUTexture *DRW_engine_select_texture_get(void);
#endif /* __SELECT_ENGINE_H__ */

View File

@@ -75,14 +75,15 @@ uint *DRW_select_buffer_read(struct Depsgraph *depsgraph,
DRW_draw_select_id(depsgraph, ar, v3d, rect);
if (select_ctx->index_drawn_len > 1) {
BLI_assert(ar->winx == GPU_texture_width(select_ctx->texture_u32) &&
ar->winy == GPU_texture_height(select_ctx->texture_u32));
BLI_assert(ar->winx == GPU_texture_width(DRW_engine_select_texture_get()) &&
ar->winy == GPU_texture_height(DRW_engine_select_texture_get()));
/* Read the UI32 pixels. */
buf_len = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
r_buf = MEM_mallocN(buf_len * sizeof(*r_buf), __func__);
GPU_framebuffer_bind(select_ctx->framebuffer_select_id);
GPUFrameBuffer *select_id_fb = DRW_engine_select_framebuffer_get();
GPU_framebuffer_bind(select_id_fb);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(rect_clamp.xmin,
rect_clamp.ymin,