Mesh Batch Cache: Port vertex paint surface to batch request
authorClément Foucault <foucault.clem@gmail.com>
Tue, 18 Dec 2018 01:18:55 +0000 (02:18 +0100)
committerClément Foucault <foucault.clem@gmail.com>
Tue, 18 Dec 2018 01:19:52 +0000 (02:19 +0100)
source/blender/draw/intern/draw_cache.c
source/blender/draw/intern/draw_cache_impl.h
source/blender/draw/intern/draw_cache_impl_mesh.c
source/blender/draw/modes/paint_texture_mode.c
source/blender/draw/modes/paint_vertex_mode.c
source/blender/draw/modes/paint_weight_mode.c
source/blender/draw/modes/shaders/paint_vertex_vert.glsl

index dc18ddb..945de6e 100644 (file)
@@ -3053,7 +3053,7 @@ GPUBatch *DRW_cache_mesh_surface_vert_colors_get(Object *ob)
        BLI_assert(ob->type == OB_MESH);
 
        Mesh *me = ob->data;
-       return DRW_mesh_batch_cache_get_triangles_with_normals_and_vert_colors(me);
+       return DRW_mesh_batch_cache_get_surface_vertpaint(me);
 }
 
 /* Return list of batches */
index 872f519..d1c5afe 100644 (file)
@@ -103,11 +103,11 @@ struct GPUBatch **DRW_mesh_batch_cache_get_surface_shaded(
         char **auto_layer_names, int **auto_layer_is_srgb, int *auto_layer_count);
 struct GPUBatch **DRW_mesh_batch_cache_get_surface_texpaint(struct Mesh *me);
 struct GPUBatch *DRW_mesh_batch_cache_get_surface_texpaint_single(struct Mesh *me);
+struct GPUBatch *DRW_mesh_batch_cache_get_surface_vertpaint(struct Mesh *me);
 struct GPUBatch *DRW_mesh_batch_cache_get_wire_loops(struct Mesh *me);
 struct GPUBatch *DRW_mesh_batch_cache_get_all_edges(struct Mesh *me);
 struct GPUBatch *DRW_mesh_batch_cache_get_triangles_with_normals(struct Mesh *me);
 struct GPUBatch *DRW_mesh_batch_cache_get_triangles_with_normals_and_weights(struct Mesh *me);
-struct GPUBatch *DRW_mesh_batch_cache_get_triangles_with_normals_and_vert_colors(struct Mesh *me);
 struct GPUBatch *DRW_mesh_batch_cache_get_triangles_with_select_id(struct Mesh *me, bool use_hide, uint select_id_offset);
 struct GPUBatch *DRW_mesh_batch_cache_get_triangles_with_select_mask(struct Mesh *me, bool use_hide);
 struct GPUBatch *DRW_mesh_batch_cache_get_loose_edges_with_normals(struct Mesh *me);
index b144ae2..b0080c7 100644 (file)
@@ -357,6 +357,17 @@ static void mesh_cd_calc_active_uv_layer(
        }
 }
 
+static void mesh_cd_calc_active_vcol_layer(
+        const Mesh *me, ushort cd_lused[CD_NUMTYPES])
+{
+       const CustomData *cd_ldata = (me->edit_btmesh) ? &me->edit_btmesh->bm->ldata : &me->ldata;
+
+       int layer = CustomData_get_active_layer(cd_ldata, CD_MLOOPCOL);
+       if (layer != -1) {
+               cd_lused[CD_MLOOPCOL] |= (1 << layer);
+       }
+}
+
 static void mesh_cd_calc_used_gpu_layers(
         const Mesh *me, uchar cd_vused[CD_NUMTYPES], ushort cd_lused[CD_NUMTYPES],
         struct GPUMaterial **gpumat_array, int gpumat_array_len)
@@ -1310,7 +1321,7 @@ static void mesh_render_data_ensure_vert_normals_pack(MeshRenderData *rdata)
 
 
 /** Ensure #MeshRenderData.vert_color */
-static void mesh_render_data_ensure_vert_color(MeshRenderData *rdata)
+static void UNUSED_FUNCTION(mesh_render_data_ensure_vert_color)(MeshRenderData *rdata)
 {
        char (*vcol)[3] = rdata->vert_color;
        if (vcol == NULL) {
@@ -2838,14 +2849,6 @@ static GPUVertBuf *mesh_batch_cache_get_tri_pos_and_normals_edit(
                use_hide ? &cache->pos_with_normals_visible_only_edit : &cache->pos_with_normals_edit);
 }
 
-static GPUVertBuf *mesh_batch_cache_get_tri_pos_and_normals_final(
-        MeshRenderData *rdata, MeshBatchCache *cache, bool use_hide)
-{
-       return mesh_batch_cache_get_tri_pos_and_normals_ex(
-               rdata, use_hide,
-               use_hide ? &cache->pos_with_normals_visible_only : &cache->pos_with_normals);
-}
-
 /* DEPRECATED Need to be ported */
 static GPUVertBuf *mesh_batch_cache_get_facedot_pos_with_normals_and_flag(
         MeshRenderData *rdata, MeshBatchCache *cache)
@@ -3188,66 +3191,6 @@ static GPUVertBuf *mesh_create_verts_select_id(
        return vbo;
 }
 
-static GPUVertBuf *mesh_create_tri_vert_colors(
-        MeshRenderData *rdata, bool use_hide)
-{
-       BLI_assert(
-               rdata->types &
-               (MR_DATATYPE_VERT | MR_DATATYPE_LOOPTRI | MR_DATATYPE_LOOP | MR_DATATYPE_POLY | MR_DATATYPE_LOOPCOL));
-
-       GPUVertBuf *vbo;
-       {
-               uint cidx = 0;
-
-               static GPUVertFormat format = { 0 };
-               static struct { uint col; } attr_id;
-               if (format.attr_len == 0) {
-                       attr_id.col = GPU_vertformat_attr_add(&format, "color", GPU_COMP_U8, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
-               }
-
-               const int tri_len = mesh_render_data_looptri_len_get(rdata);
-
-               vbo = GPU_vertbuf_create_with_format(&format);
-
-               const uint vbo_len_capacity = tri_len * 3;
-               GPU_vertbuf_data_alloc(vbo, vbo_len_capacity);
-
-               mesh_render_data_ensure_vert_color(rdata);
-               const char (*vert_color)[3] = rdata->vert_color;
-
-               if (rdata->edit_bmesh) {
-                       for (int i = 0; i < tri_len; i++) {
-                               const BMLoop **ltri = (const BMLoop **)rdata->edit_bmesh->looptris[i];
-                               /* Assume 'use_hide' */
-                               if (!BM_elem_flag_test(ltri[0]->f, BM_ELEM_HIDDEN)) {
-                                       for (uint tri_corner = 0; tri_corner < 3; tri_corner++) {
-                                               const int l_index = BM_elem_index_get(ltri[tri_corner]);
-                                               GPU_vertbuf_attr_set(vbo, attr_id.col, cidx++, vert_color[l_index]);
-                                       }
-                               }
-                       }
-               }
-               else {
-                       for (int i = 0; i < tri_len; i++) {
-                               const MLoopTri *mlt = &rdata->mlooptri[i];
-                               if (!(use_hide && (rdata->mpoly[mlt->poly].flag & ME_HIDE))) {
-                                       for (uint tri_corner = 0; tri_corner < 3; tri_corner++) {
-                                               const uint l_index = mlt->tri[tri_corner];
-                                               GPU_vertbuf_attr_set(vbo, attr_id.col, cidx++, vert_color[l_index]);
-                                       }
-                               }
-                       }
-               }
-               const uint vbo_len_used = cidx;
-
-               if (vbo_len_capacity != vbo_len_used) {
-                       GPU_vertbuf_data_resize(vbo, vbo_len_used);
-               }
-       }
-
-       return vbo;
-}
-
 static GPUVertBuf *mesh_create_tri_select_id(
         MeshRenderData *rdata, bool use_hide, uint select_id_offset)
 {
@@ -4617,29 +4560,6 @@ GPUBatch *DRW_mesh_batch_cache_get_triangles_with_normals_and_weights(Mesh *me)
        return DRW_batch_request(&cache->batch.surface_weights);
 }
 
-GPUBatch *DRW_mesh_batch_cache_get_triangles_with_normals_and_vert_colors(Mesh *me)
-{
-       MeshBatchCache *cache = mesh_batch_cache_get(me);
-
-       if (cache->triangles_with_vert_colors == NULL) {
-               const bool use_hide = (me->editflag & (ME_EDIT_PAINT_VERT_SEL | ME_EDIT_PAINT_FACE_SEL)) != 0;
-               const int datatype =
-                       MR_DATATYPE_VERT | MR_DATATYPE_LOOPTRI | MR_DATATYPE_LOOP | MR_DATATYPE_POLY | MR_DATATYPE_LOOPCOL;
-               MeshRenderData *rdata = mesh_render_data_create(me, datatype);
-
-               cache->triangles_with_vert_colors = GPU_batch_create_ex(
-                       GPU_PRIM_TRIS, mesh_create_tri_vert_colors(rdata, use_hide), NULL, GPU_BATCH_OWNS_VBO);
-
-               GPUVertBuf *vbo_tris = mesh_batch_cache_get_tri_pos_and_normals_final(rdata, cache, use_hide);
-               GPU_batch_vertbuf_add(cache->triangles_with_vert_colors, vbo_tris);
-
-               mesh_render_data_free(rdata);
-       }
-
-       return cache->triangles_with_vert_colors;
-}
-
-
 struct GPUBatch *DRW_mesh_batch_cache_get_triangles_with_select_id(
         struct Mesh *me, bool use_hide, uint select_id_offset)
 {
@@ -5009,6 +4929,31 @@ GPUBatch *DRW_mesh_batch_cache_get_surface_texpaint_single(Mesh *me)
        return DRW_batch_request(&cache->batch.surface);
 }
 
+static void texpaint_request_active_vcol(MeshBatchCache *cache, Mesh *me)
+{
+       uchar cd_vneeded[CD_NUMTYPES] = {0};
+       ushort cd_lneeded[CD_NUMTYPES] = {0};
+       mesh_cd_calc_active_vcol_layer(me, cd_lneeded);
+       if (cd_lneeded[CD_MLOOPCOL] == 0) {
+               /* This should not happen. */
+               BLI_assert(!"No vcol layer available in vertpaint, but batches requested anyway!");
+       }
+       bool cd_overlap = mesh_cd_layers_type_overlap(cache->cd_vused, cache->cd_lused,
+                                                     cd_vneeded, cd_lneeded);
+       if (cd_overlap == false) {
+               /* XXX TODO(fclem): We are writting to batch cache here. Need to make this thread safe. */
+               mesh_cd_layers_type_merge(cache->cd_vneeded, cache->cd_lneeded,
+                                         cd_vneeded, cd_lneeded);
+       }
+}
+
+GPUBatch *DRW_mesh_batch_cache_get_surface_vertpaint(Mesh *me)
+{
+       MeshBatchCache *cache = mesh_batch_cache_get(me);
+       texpaint_request_active_vcol(cache, me);
+       return DRW_batch_request(&cache->batch.surface);
+}
+
 /* TODO port to batch request. Is basically batch.wire_loops. */
 GPUBatch *DRW_mesh_batch_cache_get_texpaint_loop_wire(Mesh *me)
 {
@@ -5583,6 +5528,9 @@ void DRW_mesh_batch_cache_create_requested(Object *ob, Mesh *me)
                if (cache->cd_lused[CD_MLOOPUV] != 0) {
                        DRW_vbo_request(cache->batch.surface, &cache->ordered.loop_uv_tan);
                }
+               if (cache->cd_lused[CD_MLOOPCOL] != 0) {
+                       DRW_vbo_request(cache->batch.surface, &cache->ordered.loop_vcol);
+               }
        }
        if (DRW_batch_requested(cache->batch.all_verts, GPU_PRIM_POINTS)) {
                DRW_vbo_request(cache->batch.all_verts, &cache->ordered.pos_nor);
index 985ab54..3386c79 100644 (file)
@@ -237,7 +237,7 @@ static void PAINT_TEXTURE_cache_init(void *vedata)
        {
                psl->wire_overlay = DRW_pass_create(
                        "Wire Pass",
-                       DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
+                       DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
 
                stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
                DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", globals_ubo);
index 21f87ef..7d3aa0f 100644 (file)
@@ -45,8 +45,11 @@ extern char datatoc_paint_vertex_vert_glsl[];
 extern char datatoc_paint_vertex_frag_glsl[];
 extern char datatoc_paint_wire_vert_glsl[];
 extern char datatoc_paint_wire_frag_glsl[];
+extern char datatoc_paint_face_vert_glsl[];
 extern char datatoc_common_globals_lib_glsl[];
 
+extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
+
 /* *********** LISTS *********** */
 
 typedef struct PAINT_VERTEX_PassList {
@@ -95,7 +98,9 @@ static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
                        datatoc_paint_wire_frag_glsl,
                        datatoc_common_globals_lib_glsl, "#define VERTEX_MODE\n");
 
-               e_data.face_overlay_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
+               e_data.face_overlay_shader = DRW_shader_create(
+                       datatoc_paint_face_vert_glsl, NULL,
+                       datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
        }
 }
 
@@ -124,7 +129,7 @@ static void PAINT_VERTEX_cache_init(void *vedata)
        {
                psl->wire_overlay = DRW_pass_create(
                        "Wire Pass",
-                       DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
+                       DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_OFFSET_NEGATIVE);
 
                stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
                DRW_shgroup_uniform_block(stl->g_data->lwire_shgrp, "globalsBlock", globals_ubo);
@@ -155,6 +160,10 @@ static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
                const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
                struct GPUBatch *geom;
 
+               if (me->mloopcol == NULL) {
+                       return;
+               }
+
                if (use_surface) {
                        geom = DRW_cache_mesh_surface_vert_colors_get(ob);
                        DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
@@ -185,6 +194,7 @@ static void PAINT_VERTEX_engine_free(void)
 {
        DRW_SHADER_FREE_SAFE(e_data.vcolor_face_shader);
        DRW_SHADER_FREE_SAFE(e_data.wire_overlay_shader);
+       DRW_SHADER_FREE_SAFE(e_data.face_overlay_shader);
 }
 
 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
index 0f17603..e3993ee 100644 (file)
@@ -102,22 +102,16 @@ static void PAINT_WEIGHT_engine_init(void *UNUSED(vedata))
                        datatoc_paint_weight_vert_glsl, NULL,
                        datatoc_paint_weight_frag_glsl,
                        datatoc_common_globals_lib_glsl, NULL);
-       }
 
-       if (!e_data.wire_overlay_shader) {
                e_data.wire_overlay_shader = DRW_shader_create_with_lib(
                        datatoc_paint_wire_vert_glsl, NULL,
                        datatoc_paint_wire_frag_glsl,
                        datatoc_common_globals_lib_glsl, "#define WEIGHT_MODE\n");
-       }
 
-       if (!e_data.face_overlay_shader) {
                e_data.face_overlay_shader = DRW_shader_create(
                        datatoc_paint_face_vert_glsl, NULL,
                        datatoc_gpu_shader_uniform_color_frag_glsl, NULL);
-       }
 
-       if (!e_data.vert_overlay_shader) {
                e_data.vert_overlay_shader = DRW_shader_create_with_lib(
                        datatoc_paint_wire_vert_glsl, NULL,
                        datatoc_paint_vert_frag_glsl,
index 178f77c..e52c17e 100644 (file)
@@ -2,7 +2,7 @@
 uniform mat4 ModelViewProjectionMatrix;
 
 in vec3 pos;
-in vec3 color;
+in vec3 c; /* active color */
 
 out vec3 finalColor;
 
@@ -17,5 +17,5 @@ void main()
 {
        gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
 
-       finalColor = srgb_to_linear_attrib(color);
+       finalColor = srgb_to_linear_attrib(c);
 }