Draw Manager: lattice editmode drawing

This commit is contained in:
Campbell Barton
2017-04-13 23:29:45 +10:00
parent 1c01811cce
commit 02273441dc
9 changed files with 195 additions and 70 deletions

View File

@@ -33,5 +33,6 @@ void BKE_lattice_batch_cache_clear(struct Lattice *lt);
void BKE_lattice_batch_cache_free(struct Lattice *lt);
struct Batch *BKE_lattice_batch_cache_get_all_edges(struct Lattice *lt);
struct Batch *BKE_lattice_batch_cache_get_all_verts(struct Lattice *lt);
struct Batch *BKE_lattice_batch_cache_get_overlay_verts(struct Lattice *me);
#endif /* __BKE_LATTICE_RENDER_H__ */

View File

@@ -129,7 +129,7 @@ typedef struct LatticeRenderData {
bool show_only_outside;
struct EditLatt *edit_latt;
struct BPoint *bp;
BPoint *bp;
int actbp;
} LatticeRenderData;
@@ -164,20 +164,21 @@ static LatticeRenderData *lattice_render_data_create(Lattice *lt, const int type
else {
if (types & (MR_DATATYPE_VERT)) {
lrdata->totvert = lattice_render_verts_num_get(lt);
lrdata->bp = lt->def;
}
if (types & (MR_DATATYPE_EDGE)) {
lrdata->totedge = lattice_render_edges_num_get(lt);
lrdata->bp = lt->def;
/*no edge data */
}
}
lrdata->bp = lt->def;
lrdata->dims.u_len = lt->pntsu;
lrdata->dims.v_len = lt->pntsv;
lrdata->dims.w_len = lt->pntsw;
lrdata->show_only_outside = (lt->flag & LT_OUTSIDE) != 0;
lrdata->actbp = lt->actbp;
return lrdata;
}
@@ -204,52 +205,17 @@ static int lattice_render_data_edges_num_get(const LatticeRenderData *lrdata)
return lrdata->totedge;
}
static float *lattice_render_data_vert_co(const LatticeRenderData *lrdata, const int vert_idx)
static const BPoint *lattice_render_data_vert_bpoint(const LatticeRenderData *lrdata, const int vert_idx)
{
BLI_assert(lrdata->types & MR_DATATYPE_VERT);
if (lrdata->edit_latt) {
Lattice *lt = lrdata->edit_latt->latt;
BPoint *bp = &lt->def[vert_idx];
return bp->vec;
}
else {
return lrdata->bp[vert_idx].vec;
}
return &lrdata->bp[vert_idx];
}
/* First 2 bytes are bit flags
* 3rd is for sharp edges
* 4rd is for creased edges */
enum {
VFLAG_VERTEX_ACTIVE = 1 << 0,
VFLAG_VERTEX_SELECTED = 1 << 1,
VFLAG_VERTEX_SELECTED = 1 << 0,
VFLAG_VERTEX_ACTIVE = 1 << 1,
};
#if 0
static unsigned char lattice_render_data_vertex_flag(LatticeRenderData *lrdata, const int v)
{
unsigned char vflag = 0;
if (lrdata->edit_latt) {
Lattice *lt = lrdata->edit_latt->latt;
BPoint *bp = &lt->def[v];
/* Current vertex */
if (v == lrdata->actbp) {
vflag |= VFLAG_VERTEX_ACTIVE;
}
if (bp->f1 & SELECT) {
vflag |= VFLAG_VERTEX_SELECTED;
}
}
return vflag;
}
#endif
/* ---------------------------------------------------------------------- */
/* Lattice Batch Cache */
@@ -260,6 +226,8 @@ typedef struct LatticeBatchCache {
Batch *all_verts;
Batch *all_edges;
Batch *overlay_verts;
/* settings to determine if cache is invalid */
bool is_dirty;
@@ -334,7 +302,7 @@ static LatticeBatchCache *lattice_batch_cache_get(Lattice *lt)
return lt->batch_cache;
}
void BKE_lattice_batch_cache_dirty(struct Lattice *lt)
void BKE_lattice_batch_cache_dirty(Lattice *lt)
{
LatticeBatchCache *cache = lt->batch_cache;
if (cache) {
@@ -342,17 +310,12 @@ void BKE_lattice_batch_cache_dirty(struct Lattice *lt)
}
}
void BKE_lattice_batch_selection_dirty(struct Lattice *lt)
void BKE_lattice_batch_selection_dirty(Lattice *lt)
{
LatticeBatchCache *cache = lt->batch_cache;
if (cache) {
/* TODO Separate Flag vbo */
#if 0
if (cache->overlay_loose_verts) {
Batch_discard_all(cache->overlay_loose_verts);
cache->overlay_loose_verts = NULL;
}
#endif
BATCH_DISCARD_ALL_SAFE(cache->overlay_verts);
}
}
@@ -365,6 +328,7 @@ void BKE_lattice_batch_cache_clear(Lattice *lt)
BATCH_DISCARD_SAFE(cache->all_verts);
BATCH_DISCARD_SAFE(cache->all_edges);
BATCH_DISCARD_ALL_SAFE(cache->overlay_verts);
VERTEXBUFFER_DISCARD_SAFE(cache->pos);
ELEMENTLIST_DISCARD_SAFE(cache->edges);
@@ -394,7 +358,8 @@ static VertexBuffer *lattice_batch_cache_get_pos(LatticeRenderData *lrdata, Latt
cache->pos = VertexBuffer_create_with_format(&format);
VertexBuffer_allocate_data(cache->pos, vertex_ct);
for (int i = 0; i < vertex_ct; ++i) {
VertexBuffer_set_attrib(cache->pos, pos_id, i, lattice_render_data_vert_co(lrdata, i));
const BPoint *bp = lattice_render_data_vert_bpoint(lrdata, i);
VertexBuffer_set_attrib(cache->pos, pos_id, i, bp->vec);
}
}
@@ -406,7 +371,6 @@ static ElementList *lattice_batch_cache_get_edges(LatticeRenderData *lrdata, Lat
BLI_assert(lrdata->types & (MR_DATATYPE_VERT | MR_DATATYPE_EDGE));
if (cache->edges == NULL) {
printf("Caching edges in order...\n");
const int vertex_ct = lattice_render_data_verts_num_get(lrdata);
const int edge_len = lattice_render_data_edges_num_get(lrdata);
int edge_len_real = 0;
@@ -458,6 +422,50 @@ static ElementList *lattice_batch_cache_get_edges(LatticeRenderData *lrdata, Lat
return cache->edges;
}
static void lattice_batch_cache_create_overlay_batches(Lattice *lt)
{
/* Since MR_DATATYPE_OVERLAY is slow to generate, generate them all at once */
int options = MR_DATATYPE_VERT | MR_DATATYPE_OVERLAY;
LatticeBatchCache *cache = lattice_batch_cache_get(lt);
LatticeRenderData *lrdata = lattice_render_data_create(lt, options);
if (cache->overlay_verts == NULL) {
static VertexFormat format = { 0 };
static unsigned pos_id, data_id;
if (format.attrib_ct == 0) {
/* initialize vertex format */
pos_id = VertexFormat_add_attrib(&format, "pos", COMP_F32, 3, KEEP_FLOAT);
data_id = VertexFormat_add_attrib(&format, "data", COMP_U8, 1, KEEP_INT);
}
const int vertex_ct = lattice_render_data_verts_num_get(lrdata);
VertexBuffer *vbo = VertexBuffer_create_with_format(&format);
VertexBuffer_allocate_data(vbo, vertex_ct);
for (int i = 0; i < vertex_ct; ++i) {
const BPoint *bp = lattice_render_data_vert_bpoint(lrdata, i);
char vflag = 0;
if (bp->f1 & SELECT) {
if (i == lrdata->actbp) {
vflag |= VFLAG_VERTEX_ACTIVE;
}
else {
vflag |= VFLAG_VERTEX_SELECTED;
}
}
VertexBuffer_set_attrib(vbo, pos_id, i, bp->vec);
VertexBuffer_set_attrib(vbo, data_id, i, &vflag);
}
cache->overlay_verts = Batch_create(PRIM_POINTS, vbo, NULL);
}
lattice_render_data_free(lrdata);
}
Batch *BKE_lattice_batch_cache_get_all_edges(Lattice *lt)
{
LatticeBatchCache *cache = lattice_batch_cache_get(lt);
@@ -480,7 +488,6 @@ Batch *BKE_lattice_batch_cache_get_all_verts(Lattice *lt)
LatticeBatchCache *cache = lattice_batch_cache_get(lt);
if (cache->all_verts == NULL) {
/* create batch from DM */
LatticeRenderData *lrdata = lattice_render_data_create(lt, MR_DATATYPE_VERT);
cache->all_verts = Batch_create(PRIM_POINTS, lattice_batch_cache_get_pos(lrdata, cache), NULL);
@@ -491,4 +498,15 @@ Batch *BKE_lattice_batch_cache_get_all_verts(Lattice *lt)
return cache->all_verts;
}
Batch *BKE_lattice_batch_cache_get_overlay_verts(Lattice *lt)
{
LatticeBatchCache *cache = lattice_batch_cache_get(lt);
if (cache->overlay_verts == NULL) {
lattice_batch_cache_create_overlay_batches(lt);
}
return cache->overlay_verts;
}
#undef MESH_RENDER_FUNCTION

View File

@@ -122,6 +122,8 @@ data_to_c_simple(modes/shaders/edit_mesh_overlay_facedot_vert.glsl SRC)
data_to_c_simple(modes/shaders/edit_mesh_overlay_mix_frag.glsl SRC)
data_to_c_simple(modes/shaders/edit_mesh_overlay_facefill_vert.glsl SRC)
data_to_c_simple(modes/shaders/edit_mesh_overlay_facefill_frag.glsl SRC)
data_to_c_simple(modes/shaders/edit_lattice_overlay_frag.glsl SRC)
data_to_c_simple(modes/shaders/edit_lattice_overlay_loosevert_vert.glsl SRC)
data_to_c_simple(modes/shaders/edit_normals_vert.glsl SRC)
data_to_c_simple(modes/shaders/edit_normals_geom.glsl SRC)
data_to_c_simple(modes/shaders/object_outline_resolve_frag.glsl SRC)

View File

@@ -1558,6 +1558,18 @@ Batch *DRW_cache_lattice_wire_get(Object *ob)
return surface;
}
Batch *DRW_cache_lattice_vert_overlay_get(Object *ob)
{
Batch *surface = NULL;
BLI_assert(ob->type == OB_LATTICE);
struct Lattice *lt = ob->data;
surface = BKE_lattice_batch_cache_get_overlay_verts(lt);
return surface;
}
#if 0 /* TODO */
struct Batch *DRW_cache_surface_material_get(Object *ob, int nr) {
/* TODO */

View File

@@ -90,5 +90,6 @@ struct Batch *DRW_cache_mesh_verts_get(struct Object *ob);
/* Lattice */
struct Batch *DRW_cache_lattice_verts_get(struct Object *ob);
struct Batch *DRW_cache_lattice_wire_get(struct Object *ob);
struct Batch *DRW_cache_lattice_vert_overlay_get(struct Object *ob);
#endif /* __DRAW_CACHE_H__ */

View File

@@ -39,6 +39,14 @@
extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
extern struct GlobalsUboStorage ts; /* draw_common.c */
extern char datatoc_common_globals_lib_glsl[];
extern char datatoc_edit_lattice_overlay_loosevert_vert_glsl[];
extern char datatoc_edit_lattice_overlay_frag_glsl[];
extern char datatoc_gpu_shader_3D_vert_glsl[];
extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
extern char datatoc_gpu_shader_point_uniform_color_frag_glsl[];
/* *********** LISTS *********** */
/* All lists are per viewport specific datas.
* They are all free when viewport changes engines
@@ -50,7 +58,8 @@ typedef struct EDIT_LATTICE_PassList {
/* Declare all passes here and init them in
* EDIT_LATTICE_cache_init().
* Only contains (DRWPass *) */
struct DRWPass *pass;
struct DRWPass *wire_pass;
struct DRWPass *vert_pass;
} EDIT_LATTICE_PassList;
typedef struct EDIT_LATTICE_FramebufferList {
@@ -93,7 +102,10 @@ static struct {
* Add sources to source/blender/draw/modes/shaders
* init in EDIT_LATTICE_engine_init();
* free in EDIT_LATTICE_engine_free(); */
struct GPUShader *custom_shader;
struct GPUShader *wire_sh;
struct GPUShader *overlay_vert_sh;
} e_data = {NULL}; /* Engine data */
typedef struct g_data {
@@ -131,8 +143,15 @@ static void EDIT_LATTICE_engine_init(void *vedata)
* tex, 2);
*/
if (!e_data.custom_shader) {
e_data.custom_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
if (!e_data.wire_sh) {
e_data.wire_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
}
if (!e_data.overlay_vert_sh) {
e_data.overlay_vert_sh = DRW_shader_create_with_lib(
datatoc_edit_lattice_overlay_loosevert_vert_glsl, NULL,
datatoc_edit_lattice_overlay_frag_glsl,
datatoc_common_globals_lib_glsl, NULL);
}
}
@@ -149,16 +168,17 @@ static void EDIT_LATTICE_cache_init(void *vedata)
}
{
/* Create a pass */
DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_WIRE;
psl->pass = DRW_pass_create("My Pass", state);
psl->wire_pass = DRW_pass_create(
"Lattice Wire",
DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_WIRE);
stl->g_data->wire_shgrp = DRW_shgroup_create(e_data.custom_shader, psl->pass);
stl->g_data->wire_shgrp = DRW_shgroup_create(e_data.wire_sh, psl->wire_pass);
DRW_shgroup_uniform_vec4(stl->g_data->wire_shgrp, "color", ts.colorWireEdit, 1);
psl->vert_pass = DRW_pass_create(
"Lattice Verts",
DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH);
stl->g_data->vert_shgrp = DRW_shgroup_create(e_data.custom_shader, psl->pass);
DRW_shgroup_uniform_vec4(stl->g_data->vert_shgrp, "color", ts.colorVertexSelect, 1);
stl->g_data->vert_shgrp = DRW_shgroup_create(e_data.overlay_vert_sh, psl->vert_pass);
}
}
@@ -181,7 +201,7 @@ static void EDIT_LATTICE_cache_populate(void *vedata, Object *ob)
geom = DRW_cache_lattice_wire_get(ob);
DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
geom = DRW_cache_lattice_verts_get(ob);
geom = DRW_cache_lattice_vert_overlay_get(ob);
DRW_shgroup_call_add(stl->g_data->vert_shgrp, geom, ob->obmat);
}
}
@@ -219,7 +239,8 @@ static void EDIT_LATTICE_draw_scene(void *vedata)
*/
/* ... or just render passes on default framebuffer. */
DRW_draw_pass(psl->pass);
DRW_draw_pass(psl->wire_pass);
DRW_draw_pass(psl->vert_pass);
/* If you changed framebuffer, double check you rebind
* the default one with its textures attached before finishing */
@@ -231,7 +252,7 @@ static void EDIT_LATTICE_draw_scene(void *vedata)
static void EDIT_LATTICE_engine_free(void)
{
// Currently built-in, dont free
// DRW_SHADER_FREE_SAFE(e_data.custom_shader);
DRW_SHADER_FREE_SAFE(e_data.overlay_vert_sh);
}
/* Create collection settings here.

View File

@@ -0,0 +1,26 @@
flat in int vertFlag;
#define VERTEX_SELECTED (1 << 0)
#define VERTEX_ACTIVE (1 << 1)
#if __VERSION__ == 120
#define FragColor gl_FragColor
#else
out vec4 FragColor;
#endif
void main()
{
/* TODO: vertex size */
if ((vertFlag & VERTEX_SELECTED) != 0) {
FragColor = colorVertexSelect;
}
else if ((vertFlag & VERTEX_ACTIVE) != 0) {
FragColor = colorEditMeshActive;
}
else {
FragColor = colorVertex;
}
}

View File

@@ -0,0 +1,39 @@
/* Draw Lattice Vertices */
uniform mat4 ModelViewProjectionMatrix;
uniform vec2 viewportSize;
in vec3 pos;
in int data;
/* these are the same for all vertices
* and does not need interpolation */
flat out int vertFlag;
flat out int clipCase;
/* See fragment shader */
noperspective out vec4 eData1;
flat out vec4 eData2;
/* project to screen space */
vec2 proj(vec4 pos)
{
return (0.5 * (pos.xy / pos.w) + 0.5) * viewportSize;
}
void main()
{
clipCase = 0;
vec4 pPos = ModelViewProjectionMatrix * vec4(pos, 1.0);
/* only verterx position 0 is used */
eData1 = eData2 = vec4(1e10);
eData2.zw = proj(pPos);
vertFlag = data;
gl_PointSize = sizeEdgeFix;
gl_Position = pPos;
}

View File

@@ -47,6 +47,7 @@
#include "BKE_icons.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_lattice_render.h"
#include "BKE_mesh_render.h"
#include "BKE_object.h"
#include "BKE_scene.h"
@@ -922,9 +923,13 @@ static void view3d_main_region_listener(bScreen *sc, ScrArea *sa, ARegion *ar, w
if (scene->obedit) {
Object *ob = scene->obedit;
if (ob->type == OB_MESH) {
struct Mesh *me = ob->data;
BKE_mesh_batch_selection_dirty(me);
switch (ob->type) {
case OB_MESH:
BKE_mesh_batch_selection_dirty(ob->data);
break;
case OB_LATTICE:
BKE_lattice_batch_selection_dirty(ob->data);
break;
}
}
}