[Bf-blender-cvs] [19f86a3d5f7] blender2.8: Mesh Batch Cache: Add support for uv/vcol/tangent data layers.

Clément Foucault noreply at git.blender.org
Mon May 1 18:11:31 CEST 2017


Commit: 19f86a3d5f7f1f8267fef480604c39cf53c491fd
Author: Clément Foucault
Date:   Mon May 1 17:58:15 2017 +0200
Branches: blender2.8
https://developer.blender.org/rB19f86a3d5f7f1f8267fef480604c39cf53c491fd

Mesh Batch Cache: Add support for uv/vcol/tangent data layers.

This is a bad implementation waiting for some improvement that are :
- Gawain support for multiple names. -> will remove duplication of attribute alloc
- Glsl safe name string escaping. -> will remove any risk of hash colision.

Old glsl drawing was relying on attribute being choosen by DerivedMesh at drawing time.
For this reason, we declare all possible attrib "name" for each data layer inside the batches.
In the glsl code, we declare required data by type and name.
Then Gawain only bind vbos if names correspond.
This is way cleaner as we do not need to access the CD itself when drawing.

One other problem is that the hash maybe 11 caracters long and rapidly overload gawain's attrib name buffer.

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

M	intern/gawain/gawain/vertex_format.h
M	release/scripts/startup/bl_ui/properties_data_mesh.py
M	source/blender/draw/intern/draw_cache_impl_mesh.c

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

diff --git a/intern/gawain/gawain/vertex_format.h b/intern/gawain/gawain/vertex_format.h
index 3756b089325..dfe7e36a68d 100644
--- a/intern/gawain/gawain/vertex_format.h
+++ b/intern/gawain/gawain/vertex_format.h
@@ -14,7 +14,7 @@
 #include "common.h"
 
 #define MAX_VERTEX_ATTRIBS 16
-#define AVG_VERTEX_ATTRIB_NAME_LEN 5
+#define AVG_VERTEX_ATTRIB_NAME_LEN 11
 #define VERTEX_ATTRIB_NAMES_BUFFER_LEN ((AVG_VERTEX_ATTRIB_NAME_LEN + 1) * MAX_VERTEX_ATTRIBS)
 
 #if defined(WITH_GL_PROFILE_CORE) || defined(_WIN32)
diff --git a/release/scripts/startup/bl_ui/properties_data_mesh.py b/release/scripts/startup/bl_ui/properties_data_mesh.py
index 3e90eacd547..e7e7d87bf5b 100644
--- a/release/scripts/startup/bl_ui/properties_data_mesh.py
+++ b/release/scripts/startup/bl_ui/properties_data_mesh.py
@@ -24,7 +24,7 @@ from rna_prop_ui import PropertyPanel
 
 class MESH_MT_vertex_group_specials(Menu):
     bl_label = "Vertex Group Specials"
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     def draw(self, context):
         layout = self.layout
@@ -48,7 +48,7 @@ class MESH_MT_vertex_group_specials(Menu):
 
 class MESH_MT_shape_key_specials(Menu):
     bl_label = "Shape Key Specials"
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     def draw(self, context):
         layout = self.layout
@@ -126,7 +126,7 @@ class MeshButtonsPanel:
 class DATA_PT_context_mesh(MeshButtonsPanel, Panel):
     bl_label = ""
     bl_options = {'HIDE_HEADER'}
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     def draw(self, context):
         layout = self.layout
@@ -143,7 +143,7 @@ class DATA_PT_context_mesh(MeshButtonsPanel, Panel):
 
 class DATA_PT_normals(MeshButtonsPanel, Panel):
     bl_label = "Normals"
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     def draw(self, context):
         layout = self.layout
@@ -164,7 +164,7 @@ class DATA_PT_normals(MeshButtonsPanel, Panel):
 class DATA_PT_texture_space(MeshButtonsPanel, Panel):
     bl_label = "Texture Space"
     bl_options = {'DEFAULT_CLOSED'}
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     def draw(self, context):
         layout = self.layout
@@ -183,7 +183,7 @@ class DATA_PT_texture_space(MeshButtonsPanel, Panel):
 
 class DATA_PT_vertex_groups(MeshButtonsPanel, Panel):
     bl_label = "Vertex Groups"
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     @classmethod
     def poll(cls, context):
@@ -229,7 +229,7 @@ class DATA_PT_vertex_groups(MeshButtonsPanel, Panel):
 
 class DATA_PT_shape_keys(MeshButtonsPanel, Panel):
     bl_label = "Shape Keys"
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     @classmethod
     def poll(cls, context):
@@ -322,7 +322,7 @@ class DATA_PT_shape_keys(MeshButtonsPanel, Panel):
 
 class DATA_PT_uv_texture(MeshButtonsPanel, Panel):
     bl_label = "UV Maps"
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     def draw(self, context):
         layout = self.layout
@@ -341,7 +341,7 @@ class DATA_PT_uv_texture(MeshButtonsPanel, Panel):
 
 class DATA_PT_vertex_colors(MeshButtonsPanel, Panel):
     bl_label = "Vertex Colors"
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     def draw(self, context):
         layout = self.layout
@@ -361,7 +361,7 @@ class DATA_PT_vertex_colors(MeshButtonsPanel, Panel):
 class DATA_PT_customdata(MeshButtonsPanel, Panel):
     bl_label = "Geometry Data"
     bl_options = {'DEFAULT_CLOSED'}
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
 
     def draw(self, context):
         layout = self.layout
@@ -387,7 +387,7 @@ class DATA_PT_customdata(MeshButtonsPanel, Panel):
 
 
 class DATA_PT_custom_props_mesh(MeshButtonsPanel, PropertyPanel, Panel):
-    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY'}
+    COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_CLAY', 'BLENDER_EEVEE'}
     _context_path = "object.data"
     _property_type = bpy.types.Mesh
 
diff --git a/source/blender/draw/intern/draw_cache_impl_mesh.c b/source/blender/draw/intern/draw_cache_impl_mesh.c
index 66fd0663927..fbddcd60d46 100644
--- a/source/blender/draw/intern/draw_cache_impl_mesh.c
+++ b/source/blender/draw/intern/draw_cache_impl_mesh.c
@@ -33,6 +33,7 @@
 
 #include "BLI_utildefines.h"
 #include "BLI_math_vector.h"
+#include "BLI_string.h"
 
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
@@ -115,13 +116,34 @@ typedef struct MeshRenderData {
 	MEdge *medge;
 	MLoop *mloop;
 	MPoly *mpoly;
+	float (*orco)[3];
+	MLoopUV **mloopuv;
+	MCol **mcol;
+	float (**mtangent)[4];
 
 	BMVert *eve_act;
 	BMEdge *eed_act;
 	BMFace *efa_act;
 
+	int uv_ct;
+	int vcol_ct;
+
+	bool *auto_vcol;
+
+	int uv_active;
+	int vcol_active;
+	int tangent_active;
+
 	int crease_ofs;
 	int bweight_ofs;
+	int *uv_ofs;
+	int *vcol_ofs;
+	int *tangent_ofs;
+
+	char (*auto_names)[32];
+	char (*uv_names)[32];
+	char (*vcol_names)[32];
+	char (*tangent_names)[32];
 
 	/* Data created on-demand (usually not for bmesh-based data). */
 	EdgeAdjacentPolys *edges_adjacent_polys;
@@ -141,6 +163,7 @@ enum {
 	MR_DATATYPE_LOOP       = 1 << 3,
 	MR_DATATYPE_POLY       = 1 << 4,
 	MR_DATATYPE_OVERLAY    = 1 << 5,
+	MR_DATATYPE_SHADING    = 1 << 6,
 };
 
 /**
@@ -277,35 +300,189 @@ static MeshRenderData *mesh_render_data_create(Mesh *me, const int types)
 		}
 	}
 
+	if (types & MR_DATATYPE_SHADING) {
+		rdata->uv_ct = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
+		rdata->vcol_ct = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
+
+		rdata->mloopuv = MEM_mallocN(sizeof(*rdata->mloopuv) * rdata->uv_ct, "rdata->mloopuv");
+		rdata->mcol = MEM_mallocN(sizeof(*rdata->mcol) * rdata->vcol_ct, "rdata->mcol");
+		rdata->mtangent = MEM_mallocN(sizeof(*rdata->mtangent) * rdata->uv_ct, "rdata->mtangent");
+
+		rdata->uv_names = MEM_mallocN(sizeof(*rdata->uv_names) * rdata->uv_ct, "rdata->uv_names");
+		rdata->vcol_names = MEM_mallocN(sizeof(*rdata->vcol_names) * rdata->vcol_ct, "rdata->vcol_names");
+		rdata->tangent_names = MEM_mallocN(sizeof(*rdata->tangent_names) * rdata->uv_ct, "rdata->tangent_names");
+
+		rdata->uv_ofs = MEM_mallocN(sizeof(*rdata->uv_ofs) * rdata->uv_ct, "rdata->uv_ofs");
+		rdata->vcol_ofs = MEM_mallocN(sizeof(*rdata->vcol_ofs) * rdata->vcol_ct, "rdata->vcol_ofs");
+		rdata->tangent_ofs = MEM_mallocN(sizeof(*rdata->tangent_ofs) * rdata->uv_ct, "rdata->tangent_ofs");
+
+		/* Allocate max */
+		rdata->auto_vcol = MEM_callocN(sizeof(*rdata->auto_vcol) * rdata->vcol_ct, "rdata->auto_vcol");
+		rdata->auto_names = MEM_mallocN(sizeof(*rdata->auto_names) * (rdata->vcol_ct + rdata->uv_ct), "rdata->auto_names");
+
+		/* XXX FIXME XXX */
+		/* We use a hash to identify each data layer based on its name.
+		 * Gawain then search for this name in the current shader and bind if it exists.
+		 * NOTE : This is prone to hash collision.
+		 * One solution to hash collision would be to format the cd layer name
+		 * to a safe glsl var name, but without name clash.
+		 * NOTE 2 : Replicate changes to code_generate_vertex_new() in gpu_codegen.c */
+		for (int i = 0; i < rdata->vcol_ct; ++i) {
+			const char *name = CustomData_get_layer_name(&me->ldata, CD_MLOOPCOL, i);
+			unsigned int hash = BLI_ghashutil_strhash_p(name);
+			BLI_snprintf(rdata->vcol_names[i], sizeof(*rdata->vcol_names), "c%u", hash);
+			rdata->mcol[i] = CustomData_get_layer_n(&me->ldata, CD_MLOOPCOL, i);
+			if (rdata->edit_bmesh) {
+				rdata->vcol_ofs[i] = CustomData_get_n_offset(&rdata->edit_bmesh->bm->ldata, CD_MLOOPCOL, i);
+			}
+
+			/* Gather number of auto layers. */
+			/* We only do vcols that are not overridden by uvs */
+			if (CustomData_get_named_layer_index(&me->ldata, CD_MLOOPUV, name) == -1) {
+				BLI_snprintf(rdata->auto_names[rdata->uv_ct + i], sizeof(*rdata->auto_names), "a%u", hash);
+				rdata->auto_vcol[i] = true;
+			}
+		}
+
+		/* Start Fresh */
+		CustomData_free_layers(&me->ldata, CD_MLOOPTANGENT, me->totloop);
+		for (int i = 0; i < rdata->uv_ct; ++i) {
+			const char *name = CustomData_get_layer_name(&me->ldata, CD_MLOOPUV, i);
+			unsigned int hash = BLI_ghashutil_strhash_p(name);
+
+			{
+				/* UVs */
+				BLI_snprintf(rdata->uv_names[i], sizeof(*rdata->uv_names), "u%u", hash);
+				rdata->mloopuv[i] = CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, i);
+				if (rdata->edit_bmesh) {
+					rdata->uv_ofs[i] = CustomData_get_n_offset(&rdata->edit_bmesh->bm->ldata, CD_MLOOPUV, i);
+				}
+				BLI_snprintf(rdata->auto_names[i], sizeof(*rdata->auto_names), "a%u", hash);
+			}
+
+			{
+				/* Tangents*/
+				BLI_snprintf(rdata->tangent_names[i], sizeof(*rdata->tangent_names), "t%u", hash);
+
+				if (rdata->edit_bmesh) {
+#if 0 /* TODO Waiting for the port of emDM_calc_loop_tangents */
+					BMesh *bm = rdata->edit_bmesh->bm;
+
+					float (*loopnors)[3] = CustomData_get_layer(&bm->ldata, CD_NORMAL);
+
+					rdata->mtangent[i] = CustomData_add_layer(&bm->ldata, CD_MLOOPTANGENT, CD_CALLOC, NULL, bm->totloop);
+					CustomData_set_layer_flag(&bm->ldata, CD_MLOOPTANGENT, CD_FLAG_TEMPORARY);
+
+					BKE_mesh_loop_tangents_ex(bm->mvert, bm->totvert, bm->mloop, rdata->mtangent[i],
+					      loopnors, rdata->mloopuv[i], bm->totloop, bm->mpol

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list