[Bf-blender-cvs] [1d8ed6dcd7c] master: GPUBuffers: Save / expose material index per buffers

Clément Foucault noreply at git.blender.org
Sat May 4 14:11:35 CEST 2019


Commit: 1d8ed6dcd7cd2b3ccfaaacb59387dca481262eb6
Author: Clément Foucault
Date:   Sat May 4 00:46:26 2019 +0200
Branches: master
https://developer.blender.org/rB1d8ed6dcd7cd2b3ccfaaacb59387dca481262eb6

GPUBuffers: Save / expose material index per buffers

===================================================================

M	source/blender/gpu/GPU_buffers.h
M	source/blender/gpu/intern/gpu_buffers.c

===================================================================

diff --git a/source/blender/gpu/GPU_buffers.h b/source/blender/gpu/GPU_buffers.h
index 20047174bb1..b9ec897a9ec 100644
--- a/source/blender/gpu/GPU_buffers.h
+++ b/source/blender/gpu/GPU_buffers.h
@@ -88,6 +88,8 @@ void GPU_pbvh_grid_buffers_update(GPU_PBVH_Buffers *buffers,
 /* draw */
 struct GPUBatch *GPU_pbvh_buffers_batch_get(GPU_PBVH_Buffers *buffers, bool fast, bool wires);
 
+short GPU_pbvh_buffers_material_index_get(GPU_PBVH_Buffers *buffers);
+
 bool GPU_pbvh_buffers_has_mask(GPU_PBVH_Buffers *buffers);
 
 /* debug PBVH draw */
diff --git a/source/blender/gpu/intern/gpu_buffers.c b/source/blender/gpu/intern/gpu_buffers.c
index 9290d073295..092fd1e3a2b 100644
--- a/source/blender/gpu/intern/gpu_buffers.c
+++ b/source/blender/gpu/intern/gpu_buffers.c
@@ -83,6 +83,8 @@ struct GPU_PBVH_Buffers {
 
   uint tot_tri, tot_quad;
 
+  short material_index;
+
   /* The PBVH ensures that either all faces in the node are
    * smooth-shaded or all faces are flat-shaded */
   bool smooth;
@@ -289,6 +291,11 @@ void GPU_pbvh_mesh_buffers_update(GPU_PBVH_Buffers *buffers,
     }
   }
 
+  /* Get material index from the first face of this buffer. */
+  const MLoopTri *lt = &buffers->looptri[buffers->face_indices[0]];
+  const MPoly *mp = &buffers->mpoly[lt->poly];
+  buffers->material_index = mp->mat_nr;
+
   buffers->show_mask = !empty_mask;
   buffers->mvert = mvert;
 }
@@ -538,7 +545,6 @@ void GPU_pbvh_grid_buffers_update(GPU_PBVH_Buffers *buffers,
   int i, j, k, x, y;
 
   const bool smooth = grid_flag_mats[grid_indices[0]].flag & ME_SMOOTH;
-  static char vcol[4] = {255, 255, 255, 255};
 
   /* Build VBO */
   const int has_mask = key->has_mask;
@@ -603,6 +609,7 @@ void GPU_pbvh_grid_buffers_update(GPU_PBVH_Buffers *buffers,
             }
 
             if (show_vcol) {
+              char vcol[4] = {255, 255, 255, 255};
               GPU_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index, &vcol);
             }
 
@@ -652,6 +659,8 @@ void GPU_pbvh_grid_buffers_update(GPU_PBVH_Buffers *buffers,
               GPU_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.msk, vbo_index + 3, &fmask);
               empty_mask = empty_mask && (fmask == 0.0f);
             }
+
+            char vcol[4] = {255, 255, 255, 255};
             GPU_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index + 0, &vcol);
             GPU_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index + 1, &vcol);
             GPU_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index + 2, &vcol);
@@ -666,14 +675,15 @@ void GPU_pbvh_grid_buffers_update(GPU_PBVH_Buffers *buffers,
     gpu_pbvh_batch_init(buffers, GPU_PRIM_TRIS);
   }
 
+  /* Get material index from the first face of this buffer. */
+  buffers->material_index = grid_flag_mats[grid_indices[0]].mat_nr;
+
   buffers->grids = grids;
   buffers->grid_indices = grid_indices;
   buffers->totgrid = totgrid;
   buffers->grid_flag_mats = grid_flag_mats;
   buffers->gridkey = *key;
   buffers->show_mask = !empty_mask;
-
-  // printf("node updated %p\n", buffers);
 }
 
 GPU_PBVH_Buffers *GPU_pbvh_grid_buffers_build(int totgrid, BLI_bitmap **grid_hidden)
@@ -793,6 +803,7 @@ void GPU_pbvh_bmesh_buffers_update(GPU_PBVH_Buffers *buffers,
   const bool show_vcol = (update_flags & GPU_PBVH_BUFFERS_SHOW_VCOL) != 0;
   int tottri, totvert, maxvert = 0;
   bool empty_mask = true;
+  BMFace *f;
 
   /* TODO, make mask layer optional for bmesh buffer */
   const int cd_vert_mask_offset = CustomData_get_offset(&bm->vdata, CD_PAINT_MASK);
@@ -864,7 +875,7 @@ void GPU_pbvh_bmesh_buffers_update(GPU_PBVH_Buffers *buffers,
       GPU_indexbuf_init(&elb_lines, GPU_PRIM_LINES, tottri * 3, totvert);
 
       GSET_ITER (gs_iter, bm_faces) {
-        BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
+        f = BLI_gsetIterator_getKey(&gs_iter);
 
         BLI_assert(f->len == 3);
 
@@ -922,7 +933,7 @@ void GPU_pbvh_bmesh_buffers_update(GPU_PBVH_Buffers *buffers,
       GSetIterator gs_iter;
 
       GSET_ITER (gs_iter, bm_faces) {
-        BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
+        f = BLI_gsetIterator_getKey(&gs_iter);
 
         if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
           BMVert *v[3];
@@ -952,6 +963,9 @@ void GPU_pbvh_bmesh_buffers_update(GPU_PBVH_Buffers *buffers,
     }
   }
 
+  /* Get material index from the last face we iterated on. */
+  buffers->material_index = f->mat_nr;
+
   buffers->show_mask = !empty_mask;
 
   gpu_pbvh_batch_init(buffers, GPU_PRIM_TRIS);
@@ -990,6 +1004,11 @@ bool GPU_pbvh_buffers_has_mask(GPU_PBVH_Buffers *buffers)
   return buffers->show_mask;
 }
 
+short GPU_pbvh_buffers_material_index_get(GPU_PBVH_Buffers *buffers)
+{
+  return buffers->material_index;
+}
+
 void GPU_pbvh_buffers_free(GPU_PBVH_Buffers *buffers)
 {
   if (buffers) {



More information about the Bf-blender-cvs mailing list