diff options
author | Lukas Tönne <lukas.toenne@gmail.com> | 2017-07-22 14:35:40 +0300 |
---|---|---|
committer | Lukas Tönne <lukas.toenne@gmail.com> | 2017-07-22 14:35:40 +0300 |
commit | 679113fbd5ecef72850b572535534ffde1f03f11 (patch) | |
tree | 38ab7d04759ddf86a999938504a0346dfdd9e51a /source/blender/draw | |
parent | 65d2374c806ecf42ccf106cd28380e7a7071f789 (diff) |
Basic drawing code for hair edit mode in the new 2.8 draw engine system.
Diffstat (limited to 'source/blender/draw')
-rw-r--r-- | source/blender/draw/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/blender/draw/engines/eevee/eevee_materials.c | 2 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_cache.c | 25 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_cache.h | 7 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_cache_impl.h | 10 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_cache_impl_strands.c | 322 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_manager.c | 10 | ||||
-rw-r--r-- | source/blender/draw/modes/draw_mode_engines.h | 3 | ||||
-rw-r--r-- | source/blender/draw/modes/edit_strands_mode.c | 243 |
9 files changed, 622 insertions, 2 deletions
diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt index 4b760f68705..920b6140e2b 100644 --- a/source/blender/draw/CMakeLists.txt +++ b/source/blender/draw/CMakeLists.txt @@ -60,6 +60,7 @@ set(SRC intern/draw_cache_impl_lattice.c intern/draw_cache_impl_mesh.c intern/draw_cache_impl_particles.c + intern/draw_cache_impl_strands.c intern/draw_common.c intern/draw_manager.c intern/draw_manager_text.c @@ -69,6 +70,7 @@ set(SRC modes/edit_lattice_mode.c modes/edit_mesh_mode.c modes/edit_metaball_mode.c + modes/edit_strands_mode.c modes/edit_surface_mode.c modes/edit_text_mode.c modes/object_mode.c diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c index 46ed858603d..2f01a5fd966 100644 --- a/source/blender/draw/engines/eevee/eevee_materials.c +++ b/source/blender/draw/engines/eevee/eevee_materials.c @@ -1046,7 +1046,7 @@ void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_SceneLayerData *sl } if (ob->type == OB_MESH) { - if (ob != draw_ctx->scene->obedit) { + if (ob != draw_ctx->scene->obedit && !(ob->mode & OB_MODE_HAIR_EDIT)) { material_hash = stl->g_data->hair_material_hash; for (ModifierData *md = ob->modifiers.first; md; md = md->next) { diff --git a/source/blender/draw/intern/draw_cache.c b/source/blender/draw/intern/draw_cache.c index c0338de3d54..c6a5a85b4e8 100644 --- a/source/blender/draw/intern/draw_cache.c +++ b/source/blender/draw/intern/draw_cache.c @@ -2584,3 +2584,28 @@ Gwn_Batch *DRW_cache_particles_get_prim(int type) return NULL; } + +/* -------------------------------------------------------------------- */ + +/** \name Strands + * \{ */ + +Gwn_Batch *DRW_cache_editstrands_get_tips(struct BMEditStrands *es) +{ + return DRW_editstrands_batch_cache_get_tips(es); +} + +Gwn_Batch *DRW_cache_editstrands_get_roots(struct BMEditStrands *es) +{ + return DRW_editstrands_batch_cache_get_roots(es); +} + +Gwn_Batch *DRW_cache_editstrands_get_points(struct BMEditStrands *es) +{ + return DRW_editstrands_batch_cache_get_points(es); +} + +Gwn_Batch *DRW_cache_editstrands_get_wires(struct BMEditStrands *es) +{ + return DRW_editstrands_batch_cache_get_wires(es); +} diff --git a/source/blender/draw/intern/draw_cache.h b/source/blender/draw/intern/draw_cache.h index ac7062b3cc8..03ef1eae2eb 100644 --- a/source/blender/draw/intern/draw_cache.h +++ b/source/blender/draw/intern/draw_cache.h @@ -30,6 +30,7 @@ struct Gwn_Batch; struct GPUMaterial; struct Object; struct ModifierData; +struct BMEditStrands; void DRW_shape_cache_free(void); @@ -156,4 +157,10 @@ struct Gwn_Batch *DRW_cache_particles_get_hair(struct ParticleSystem *psys, stru struct Gwn_Batch *DRW_cache_particles_get_dots(struct ParticleSystem *psys); struct Gwn_Batch *DRW_cache_particles_get_prim(int type); +/* Strands */ +struct Gwn_Batch *DRW_cache_editstrands_get_tips(struct BMEditStrands *es); +struct Gwn_Batch *DRW_cache_editstrands_get_roots(struct BMEditStrands *es); +struct Gwn_Batch *DRW_cache_editstrands_get_points(struct BMEditStrands *es); +struct Gwn_Batch *DRW_cache_editstrands_get_wires(struct BMEditStrands *es); + #endif /* __DRAW_CACHE_H__ */ diff --git a/source/blender/draw/intern/draw_cache_impl.h b/source/blender/draw/intern/draw_cache_impl.h index f8feeb37b82..148839f9748 100644 --- a/source/blender/draw/intern/draw_cache_impl.h +++ b/source/blender/draw/intern/draw_cache_impl.h @@ -32,6 +32,7 @@ struct ListBase; struct CurveCache; struct ParticleSystem; struct ModifierData; +struct BMEditStrands; struct Curve; struct Lattice; @@ -50,6 +51,9 @@ void DRW_lattice_batch_cache_free(struct Lattice *lt); void DRW_particle_batch_cache_dirty(struct ParticleSystem *psys, int mode); void DRW_particle_batch_cache_free(struct ParticleSystem *psys); +void DRW_editstrands_batch_cache_dirty(struct BMEditStrands *es, int mode); +void DRW_editstrands_batch_cache_free(struct BMEditStrands *es); + /* Curve */ struct Gwn_Batch *DRW_curve_batch_cache_get_wire_edge(struct Curve *cu, struct CurveCache *ob_curve_cache); struct Gwn_Batch *DRW_curve_batch_cache_get_normal_edge( @@ -102,4 +106,10 @@ void DRW_mesh_cache_sculpt_coords_ensure(struct Mesh *me); struct Gwn_Batch *DRW_particles_batch_cache_get_hair(struct ParticleSystem *psys, struct ModifierData *md); struct Gwn_Batch *DRW_particles_batch_cache_get_dots(struct ParticleSystem *psys); +/* Strands */ +struct Gwn_Batch *DRW_editstrands_batch_cache_get_wires(struct BMEditStrands *es); +struct Gwn_Batch *DRW_editstrands_batch_cache_get_tips(struct BMEditStrands *es); +struct Gwn_Batch *DRW_editstrands_batch_cache_get_roots(struct BMEditStrands *es); +struct Gwn_Batch *DRW_editstrands_batch_cache_get_points(struct BMEditStrands *es); + #endif /* __DRAW_CACHE_IMPL_H__ */ diff --git a/source/blender/draw/intern/draw_cache_impl_strands.c b/source/blender/draw/intern/draw_cache_impl_strands.c new file mode 100644 index 00000000000..685f72410da --- /dev/null +++ b/source/blender/draw/intern/draw_cache_impl_strands.c @@ -0,0 +1,322 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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. + * + * The Original Code is Copyright (C) 2017 by Blender Foundation. + * All rights reserved. + * + * Contributor(s): Blender Foundation, Mike Erwin, Dalai Felinto + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file draw_cache_impl_strands.c + * \ingroup draw + * + * \brief Strands API for render engines + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_utildefines.h" +#include "BLI_math_vector.h" +#include "BLI_ghash.h" + +#include "BKE_editstrands.h" + +#include "GPU_batch.h" + +#include "draw_cache_impl.h" /* own include */ + +/* ---------------------------------------------------------------------- */ +/* Strands Gwn_Batch Cache */ + +typedef enum VertexDrawFlags +{ + STRANDS_VERTEX_SELECT = 1, +} VertexDrawFlags; + +typedef struct StrandsBatchCache { + Gwn_VertBuf *pos; + Gwn_IndexBuf *segments; + Gwn_IndexBuf *tips_idx; + Gwn_IndexBuf *roots_idx; + + Gwn_Batch *wires; + Gwn_Batch *tips; + Gwn_Batch *roots; + Gwn_Batch *points; + + int segment_count; + int point_count; + + /* settings to determine if cache is invalid */ + bool is_dirty; +} StrandsBatchCache; + +/* Gwn_Batch cache management. */ + +static void editstrands_batch_cache_clear(BMEditStrands *es); + +static bool editstrands_batch_cache_valid(BMEditStrands *es) +{ + StrandsBatchCache *cache = es->batch_cache; + + if (cache == NULL) { + return false; + } + + if (cache->is_dirty == false) { + return true; + } + else { + return false; + } + + return true; +} + +static void editstrands_batch_cache_init(BMEditStrands *es) +{ + StrandsBatchCache *cache = es->batch_cache; + + if (!cache) { + cache = es->batch_cache = MEM_callocN(sizeof(*cache), __func__); + } + else { + memset(cache, 0, sizeof(*cache)); + } + + cache->is_dirty = false; +} + +static StrandsBatchCache *editstrands_batch_cache_get(BMEditStrands *es) +{ + if (!editstrands_batch_cache_valid(es)) { + editstrands_batch_cache_clear(es); + editstrands_batch_cache_init(es); + } + return es->batch_cache; +} + +void DRW_editstrands_batch_cache_dirty(BMEditStrands *es, int mode) +{ + StrandsBatchCache *cache = es->batch_cache; + if (cache == NULL) { + return; + } + switch (mode) { + case BKE_STRANDS_BATCH_DIRTY_ALL: + cache->is_dirty = true; + break; + default: + BLI_assert(0); + } +} + +static void editstrands_batch_cache_clear(BMEditStrands *es) +{ + StrandsBatchCache *cache = es->batch_cache; + if (!cache) { + return; + } + + GWN_BATCH_DISCARD_SAFE(cache->wires); + GWN_BATCH_DISCARD_SAFE(cache->points); + GWN_BATCH_DISCARD_SAFE(cache->tips); + GWN_BATCH_DISCARD_SAFE(cache->roots); + + GWN_VERTBUF_DISCARD_SAFE(cache->pos); + GWN_INDEXBUF_DISCARD_SAFE(cache->segments); +} + +void DRW_editstrands_batch_cache_free(BMEditStrands *es) +{ + editstrands_batch_cache_clear(es); + MEM_SAFE_FREE(es->batch_cache); +} + +static void editstrands_batch_cache_ensure_pos(BMEditStrands *es, StrandsBatchCache *cache) +{ + if (cache->pos) { + return; + } + + static Gwn_VertFormat format = { 0 }; + static unsigned pos_id, flag_id; + + GWN_VERTBUF_DISCARD_SAFE(cache->pos); + + /* initialize vertex format */ + if (format.attrib_ct == 0) { + pos_id = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); + flag_id = GWN_vertformat_attr_add(&format, "flags", GWN_COMP_U8, 1, GWN_FETCH_INT); + } + + BMesh *bm = es->base.bm; + BMVert *vert; + BMIter iter; + int curr_point; + + cache->pos = GWN_vertbuf_create_with_format(&format); + GWN_vertbuf_data_alloc(cache->pos, bm->totvert); + + BM_ITER_MESH_INDEX(vert, &iter, bm, BM_VERTS_OF_MESH, curr_point) { + GWN_vertbuf_attr_set(cache->pos, pos_id, curr_point, vert->co); + + uint8_t flag = 0; + if (BM_elem_flag_test(vert, BM_ELEM_SELECT)) + flag |= STRANDS_VERTEX_SELECT; + GWN_vertbuf_attr_set(cache->pos, flag_id, curr_point, &flag); + } +} + +static void editstrands_batch_cache_ensure_segments(BMEditStrands *es, StrandsBatchCache *cache) +{ + if (cache->segments) { + return; + } + + GWN_INDEXBUF_DISCARD_SAFE(cache->segments); + + BMesh *bm = es->base.bm; + BMEdge *edge; + BMIter iter; + + Gwn_IndexBufBuilder elb; + GWN_indexbuf_init(&elb, GWN_PRIM_LINES, bm->totedge, bm->totvert); + + BM_mesh_elem_index_ensure(bm, BM_VERT); + + BM_ITER_MESH(edge, &iter, bm, BM_EDGES_OF_MESH) { + GWN_indexbuf_add_line_verts(&elb, BM_elem_index_get(edge->v1), BM_elem_index_get(edge->v2)); + } + + cache->segments = GWN_indexbuf_build(&elb); +} + +static void editstrands_batch_cache_ensure_tips_idx(BMEditStrands *es, StrandsBatchCache *cache) +{ + if (cache->tips_idx) { + return; + } + + GWN_INDEXBUF_DISCARD_SAFE(cache->tips_idx); + + BMesh *bm = es->base.bm; + int totstrands = BM_strands_count(bm); + BMVert *root, *vert; + BMIter iter, iter_strand; + + Gwn_IndexBufBuilder elb; + GWN_indexbuf_init(&elb, GWN_PRIM_POINTS, totstrands, bm->totvert); + + BM_mesh_elem_index_ensure(bm, BM_VERT); + + BM_ITER_STRANDS(root, &iter, bm, BM_STRANDS_OF_MESH) { + BM_ITER_STRANDS_ELEM(vert, &iter_strand, root, BM_VERTS_OF_STRAND) + { + if (BM_strands_vert_is_tip(vert)) { + GWN_indexbuf_add_point_vert(&elb, BM_elem_index_get(vert)); + break; + } + } + } + + cache->tips_idx = GWN_indexbuf_build(&elb); +} + +static void editstrands_batch_cache_ensure_roots_idx(BMEditStrands *es, StrandsBatchCache *cache) +{ + if (cache->roots_idx) { + return; + } + + GWN_INDEXBUF_DISCARD_SAFE(cache->roots_idx); + + BMesh *bm = es->base.bm; + int totstrands = BM_strands_count(bm); + BMVert *root, *vert; + BMIter iter, iter_strand; + + Gwn_IndexBufBuilder elb; + GWN_indexbuf_init(&elb, GWN_PRIM_POINTS, totstrands, bm->totvert); + + BM_mesh_elem_index_ensure(bm, BM_VERT); + + BM_ITER_STRANDS(root, &iter, bm, BM_STRANDS_OF_MESH) { + BM_ITER_STRANDS_ELEM(vert, &iter_strand, root, BM_VERTS_OF_STRAND) + { + if (BM_strands_vert_is_root(vert)) { + GWN_indexbuf_add_point_vert(&elb, BM_elem_index_get(vert)); + break; + } + } + } + + cache->roots_idx = GWN_indexbuf_build(&elb); +} + +Gwn_Batch *DRW_editstrands_batch_cache_get_wires(BMEditStrands *es) +{ + StrandsBatchCache *cache = editstrands_batch_cache_get(es); + + if (cache->wires == NULL) { + editstrands_batch_cache_ensure_pos(es, cache); + editstrands_batch_cache_ensure_segments(es, cache); + cache->wires = GWN_batch_create(GWN_PRIM_LINES, cache->pos, cache->segments); + } + + return cache->wires; +} + +Gwn_Batch *DRW_editstrands_batch_cache_get_tips(BMEditStrands *es) +{ + StrandsBatchCache *cache = editstrands_batch_cache_get(es); + + if (cache->tips == NULL) { + editstrands_batch_cache_ensure_pos(es, cache); + editstrands_batch_cache_ensure_tips_idx(es, cache); + cache->tips = GWN_batch_create(GWN_PRIM_POINTS, cache->pos, cache->tips_idx); + } + + return cache->tips; +} + +Gwn_Batch *DRW_editstrands_batch_cache_get_roots(BMEditStrands *es) +{ + StrandsBatchCache *cache = editstrands_batch_cache_get(es); + + if (cache->roots == NULL) { + editstrands_batch_cache_ensure_pos(es, cache); + editstrands_batch_cache_ensure_roots_idx(es, cache); + cache->roots = GWN_batch_create(GWN_PRIM_POINTS, cache->pos, cache->roots_idx); + } + + return cache->roots; +} + +Gwn_Batch *DRW_editstrands_batch_cache_get_points(BMEditStrands *es) +{ + StrandsBatchCache *cache = editstrands_batch_cache_get(es); + + if (cache->points == NULL) { + editstrands_batch_cache_ensure_pos(es, cache); + cache->points = GWN_batch_create(GWN_PRIM_POINTS, cache->pos, NULL); + } + + return cache->points; +} diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c index 70e9d985e6c..42a11ebe3e1 100644 --- a/source/blender/draw/intern/draw_manager.c +++ b/source/blender/draw/intern/draw_manager.c @@ -2820,6 +2820,9 @@ static void DRW_engines_enable_from_mode(int mode) case CTX_MODE_PARTICLE: use_drw_engine(&draw_engine_particle_type); break; + case CTX_MODE_HAIR: + use_drw_engine(&draw_engine_edit_strands_type); + break; case CTX_MODE_OBJECT: break; default: @@ -3512,6 +3515,7 @@ void DRW_engines_register(void) DRW_engine_register(&draw_engine_edit_lattice_type); DRW_engine_register(&draw_engine_edit_mesh_type); DRW_engine_register(&draw_engine_edit_metaball_type); + DRW_engine_register(&draw_engine_edit_strands_type); DRW_engine_register(&draw_engine_edit_surface_type); DRW_engine_register(&draw_engine_edit_text_type); DRW_engine_register(&draw_engine_paint_texture_type); @@ -3535,6 +3539,9 @@ void DRW_engines_register(void) /* BKE: particle.c */ extern void *BKE_particle_batch_cache_dirty_cb; extern void *BKE_particle_batch_cache_free_cb; + /* BKE: editstrands.c */ + extern void *BKE_editstrands_batch_cache_dirty_cb; + extern void *BKE_editstrands_batch_cache_free_cb; BKE_curve_batch_cache_dirty_cb = DRW_curve_batch_cache_dirty; BKE_curve_batch_cache_free_cb = DRW_curve_batch_cache_free; @@ -3547,6 +3554,9 @@ void DRW_engines_register(void) BKE_particle_batch_cache_dirty_cb = DRW_particle_batch_cache_dirty; BKE_particle_batch_cache_free_cb = DRW_particle_batch_cache_free; + + BKE_editstrands_batch_cache_dirty_cb = DRW_editstrands_batch_cache_dirty; + BKE_editstrands_batch_cache_free_cb = DRW_editstrands_batch_cache_free; } } diff --git a/source/blender/draw/modes/draw_mode_engines.h b/source/blender/draw/modes/draw_mode_engines.h index 23fedbba5a5..4a0ff1e6b15 100644 --- a/source/blender/draw/modes/draw_mode_engines.h +++ b/source/blender/draw/modes/draw_mode_engines.h @@ -32,6 +32,7 @@ extern DrawEngineType draw_engine_edit_curve_type; extern DrawEngineType draw_engine_edit_lattice_type; extern DrawEngineType draw_engine_edit_mesh_type; extern DrawEngineType draw_engine_edit_metaball_type; +extern DrawEngineType draw_engine_edit_strands_type; extern DrawEngineType draw_engine_edit_surface_type; extern DrawEngineType draw_engine_edit_text_type; extern DrawEngineType draw_engine_paint_texture_type; @@ -41,4 +42,4 @@ extern DrawEngineType draw_engine_particle_type; extern DrawEngineType draw_engine_pose_type; extern DrawEngineType draw_engine_sculpt_type; -#endif /* __DRAW_MODE_ENGINES_H__ */
\ No newline at end of file +#endif /* __DRAW_MODE_ENGINES_H__ */ diff --git a/source/blender/draw/modes/edit_strands_mode.c b/source/blender/draw/modes/edit_strands_mode.c new file mode 100644 index 00000000000..9ebedc8efa8 --- /dev/null +++ b/source/blender/draw/modes/edit_strands_mode.c @@ -0,0 +1,243 @@ +/* + * Copyright 2016, Blender Foundation. + * + * 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. + * + * Contributor(s): Blender Institute + * + */ + +/** \file blender/draw/modes/particle_mode.c + * \ingroup draw + */ + +#include "DRW_engine.h" +#include "DRW_render.h" + +#include "DNA_view3d_types.h" + +#include "BKE_editstrands.h" + +/* If builtin shaders are needed */ +#include "GPU_shader.h" + +#include "draw_common.h" + +#include "draw_mode_engines.h" + +extern GlobalsUboStorage ts; + +/* *********** LISTS *********** */ +/* All lists are per viewport specific datas. + * They are all free when viewport changes engines + * or is free itself. Use PARTICLE_engine_init() to + * initialize most of them and PARTICLE_cache_init() + * for PARTICLE_PassList */ + +typedef struct EDIT_STRANDS_PassList { + /* Declare all passes here and init them in + * PARTICLE_cache_init(). + * Only contains (DRWPass *) */ + struct DRWPass *wires; + struct DRWPass *tips; + struct DRWPass *roots; + struct DRWPass *points; +} EDIT_STRANDS_PassList; + +typedef struct EDIT_STRANDS_StorageList { + /* Contains any other memory block that the engine needs. + * Only directly MEM_(m/c)allocN'ed blocks because they are + * free with MEM_freeN() when viewport is freed. + * (not per object) */ + struct CustomStruct *block; + struct EDIT_STRANDS_PrivateData *g_data; +} EDIT_STRANDS_StorageList; + +typedef struct EDIT_STRANDS_Data { + /* Struct returned by DRW_viewport_engine_data_get. + * If you don't use one of these, just make it a (void *) */ + // void *fbl; + void *engine_type; /* Required */ + DRWViewportEmptyList *fbl; + DRWViewportEmptyList *txl; + EDIT_STRANDS_PassList *psl; + EDIT_STRANDS_StorageList *stl; +} EDIT_STRANDS_Data; + +/* *********** STATIC *********** */ + +typedef struct EDIT_STRANDS_PrivateData { + /* resulting curve as 'wire' for fast editmode drawing */ + DRWShadingGroup *wires_shgrp; + DRWShadingGroup *tips_shgrp; + DRWShadingGroup *roots_shgrp; + DRWShadingGroup *points_shgrp; +} EDIT_STRANDS_PrivateData; /* Transient data */ + +/* *********** FUNCTIONS *********** */ + +/* Here init all passes and shading groups + * Assume that all Passes are NULL */ +static void EDIT_STRANDS_cache_init(void *vedata) +{ + EDIT_STRANDS_PassList *psl = ((EDIT_STRANDS_Data *)vedata)->psl; + EDIT_STRANDS_StorageList *stl = ((EDIT_STRANDS_Data *)vedata)->stl; + + if (!stl->g_data) { + /* Alloc transient pointers */ + stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__); + } + + GPUShader *point_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA); + GPUShader *wire_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR); + + { + /* Strand wires */ + DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND; + psl->wires = DRW_pass_create("Strand Wire Verts Pass", state); + + stl->g_data->wires_shgrp = DRW_shgroup_create(wire_shader, psl->wires); + DRW_shgroup_uniform_vec4(stl->g_data->wires_shgrp, "color", ts.colorWireEdit, 1); + } + + { + /* Tip vertices */ + DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND; + psl->tips = DRW_pass_create("Strand Tip Verts Pass", state); + + stl->g_data->tips_shgrp = DRW_shgroup_create(point_shader, psl->tips); + DRW_shgroup_uniform_vec4(stl->g_data->tips_shgrp, "color", ts.colorVertex, 1); + DRW_shgroup_uniform_float(stl->g_data->tips_shgrp, "size", &ts.sizeVertex, 1); + } + + { + /* Root vertices */ + DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND; + psl->roots = DRW_pass_create("Strand Root Verts Pass", state); + + stl->g_data->roots_shgrp = DRW_shgroup_create(point_shader, psl->roots); + DRW_shgroup_uniform_vec4(stl->g_data->roots_shgrp, "color", ts.colorVertex, 1); + DRW_shgroup_uniform_float(stl->g_data->roots_shgrp, "size", &ts.sizeVertex, 1); + } + + { + /* Interior vertices */ + DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND; + psl->points = DRW_pass_create("Strand Interior Verts Pass", state); + + stl->g_data->points_shgrp = DRW_shgroup_create(point_shader, psl->points); + DRW_shgroup_uniform_vec4(stl->g_data->points_shgrp, "color", ts.colorVertex, 1); + DRW_shgroup_uniform_float(stl->g_data->points_shgrp, "size", &ts.sizeVertex, 1); + } +} + +static void edit_strands_add_ob_to_pass( + Scene *scene, Object *ob, BMEditStrands *edit, + DRWShadingGroup *tips_shgrp, + DRWShadingGroup *roots_shgrp, + DRWShadingGroup *points_shgrp, + DRWShadingGroup *wires_shgrp) +{ + HairEditSettings *tsettings = &scene->toolsettings->hair_edit; + + { + struct Gwn_Batch *geom = DRW_cache_editstrands_get_wires(edit); + DRW_shgroup_call_add(wires_shgrp, geom, ob->obmat); + } + + switch (tsettings->select_mode) + { + case HAIR_SELECT_TIP: + { + struct Gwn_Batch *geom = DRW_cache_editstrands_get_tips(edit); + DRW_shgroup_call_add(tips_shgrp, geom, ob->obmat); + break; + } + + case HAIR_SELECT_STRAND: + { +#if 0 + struct Gwn_Batch *geom = DRW_cache_editstrands_get_roots(edit); + DRW_shgroup_call_add(roots_shgrp, geom, ob->obmat); +#else + UNUSED_VARS(roots_shgrp); +#endif + break; + } + + case HAIR_SELECT_VERTEX: + { + struct Gwn_Batch *geom = DRW_cache_editstrands_get_points(edit); + DRW_shgroup_call_add(points_shgrp, geom, ob->obmat); + break; + } + } +} + +/* Add geometry to shadingGroups. Execute for each objects */ +static void EDIT_STRANDS_cache_populate(void *vedata, Object *ob) +{ + BMEditStrands *edit = BKE_editstrands_from_object(ob); + const DRWContextState *draw_ctx = DRW_context_state_get(); + Scene *scene = draw_ctx->scene; + + if (!edit || ob == scene->obedit) + return; + + EDIT_STRANDS_StorageList *stl = ((EDIT_STRANDS_Data *)vedata)->stl; + + edit_strands_add_ob_to_pass( + scene, ob, edit, stl->g_data->tips_shgrp, stl->g_data->roots_shgrp, + stl->g_data->points_shgrp, stl->g_data->wires_shgrp); +} + +/* Optional: Post-cache_populate callback */ +static void EDIT_STRANDS_cache_finish(void *vedata) +{ + EDIT_STRANDS_PassList *psl = ((EDIT_STRANDS_Data *)vedata)->psl; + EDIT_STRANDS_StorageList *stl = ((EDIT_STRANDS_Data *)vedata)->stl; + + /* Do something here! dependant on the objects gathered */ + UNUSED_VARS(psl, stl); +} + +/* Draw time ! Control rendering pipeline from here */ +static void EDIT_STRANDS_draw_scene(void *vedata) +{ + EDIT_STRANDS_PassList *psl = ((EDIT_STRANDS_Data *)vedata)->psl; + + DRW_draw_pass(psl->wires); + DRW_draw_pass(psl->points); + DRW_draw_pass(psl->roots); + DRW_draw_pass(psl->tips); + + /* If you changed framebuffer, double check you rebind + * the default one with its textures attached before finishing */ +} + +static const DrawEngineDataSize STRANDS_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_STRANDS_Data); + +DrawEngineType draw_engine_edit_strands_type = { + NULL, NULL, + N_("EditStrandsMode"), + &STRANDS_data_size, + NULL, + NULL, + &EDIT_STRANDS_cache_init, + &EDIT_STRANDS_cache_populate, + &EDIT_STRANDS_cache_finish, + NULL, /* draw_background but not needed by mode engines */ + &EDIT_STRANDS_draw_scene +}; |