[Bf-blender-cvs] [6b372e3a22] blender2.8: Clay Engine: support for overrides (not working yet)

Clément Foucault noreply at git.blender.org
Thu Feb 9 11:27:24 CET 2017


Commit: 6b372e3a22cbcdcb17a6239238189d6659418516
Author: Clément Foucault
Date:   Wed Feb 8 21:44:13 2017 +0100
Branches: blender2.8
https://developer.blender.org/rB6b372e3a22cbcdcb17a6239238189d6659418516

Clay Engine: support for overrides (not working yet)

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

M	source/blender/draw/engines/clay/clay.c
M	source/blender/draw/engines/clay/shaders/clay_frag.glsl
M	source/blender/draw/intern/DRW_render.h
M	source/blender/draw/intern/draw_manager.c
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/gpu/GPU_viewport.h
M	source/blender/gpu/intern/gpu_viewport.c

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

diff --git a/source/blender/draw/engines/clay/clay.c b/source/blender/draw/engines/clay/clay.c
index 3a844d328f..5feac48960 100644
--- a/source/blender/draw/engines/clay/clay.c
+++ b/source/blender/draw/engines/clay/clay.c
@@ -58,8 +58,10 @@ typedef struct CLAY_UBO_Material {
 	float pad[2]; /* ensure 16 bytes alignement */
 } CLAY_UBO_Material; /* 48 bytes */
 
+#define MAX_CLAY_MAT 512 /* 512 = 9 bit material id */
+
 typedef struct CLAY_UBO_Storage {
-	CLAY_UBO_Material materials[512]; /* 512 = 9 bit material id */
+	CLAY_UBO_Material materials[MAX_CLAY_MAT];
 } CLAY_UBO_Storage;
 
 static struct CLAY_data {
@@ -68,11 +70,6 @@ static struct CLAY_data {
 	/* Shading Pass */
 	struct GPUShader *clay_sh;
 
-	/* Materials Parameter UBO */
-	struct GPUUniformBuffer *mat_ubo;
-	CLAY_UBO_Storage mat_storage;
-	short ubo_flag;
-
 	/* Matcap textures */
 	struct GPUTexture *matcap_array;
 	float matcap_colors[24][3];
@@ -85,11 +82,23 @@ static struct CLAY_data {
 	struct GPUTexture *sampling_tx;
 } data = {NULL};
 
-/* CLAY_data.ubo_flag */
-enum {
-	CLAY_UBO_CLEAR    = (1 << 0),
-	CLAY_UBO_REFRESH  = (1 << 1),
-};
+/* GPUViewport.storage
+ * Is freed everytime the viewport engine changes */
+typedef struct CLAY_Storage {
+	/* Materials Parameter UBO */
+	CLAY_UBO_Storage mat_storage;
+	int ubo_current_id;
+	DRWShadingGroup *shgrps[MAX_CLAY_MAT];
+} CLAY_Storage;
+
+/* Just a serie of int from 0 to MAX_CLAY_MAT-1 */
+static int ubo_mat_idxs[MAX_CLAY_MAT] = {0};
+
+/* keep it under MAX_STORAGE */
+typedef struct CLAY_StorageList {
+	struct CLAY_Storage *storage;
+	struct GPUUniformBuffer *mat_ubo;
+} CLAY_StorageList;
 
 /* keep it under MAX_BUFFERS */
 typedef struct CLAY_FramebufferList{
@@ -274,10 +283,8 @@ MaterialEngineSettings *CLAY_material_settings_create(void)
 	return (MaterialEngineSettings *)settings;
 }
 
-static void CLAY_engine_init(const bContext *C)
+static void CLAY_engine_init(CLAY_StorageList *stl)
 {
-	Main *bmain = CTX_data_main(C);
-
 	/* Create Texture Array */
 	if (!data.matcap_array) {
 		PreviewImage *prv[24]; /* For now use all of the 24 internal matcaps */
@@ -327,10 +334,6 @@ static void CLAY_engine_init(const bContext *C)
 		data.depth_sh = DRW_shader_create_3D_depth_only();
 	}
 
-	if (!data.mat_ubo) {
-		data.mat_ubo = DRW_uniformbuffer_create(sizeof(CLAY_UBO_Storage), NULL);
-	}
-
 	/* Shading pass */
 	if (!data.clay_sh) {
 		DynStr *ds = BLI_dynstr_new();
@@ -356,27 +359,20 @@ static void CLAY_engine_init(const bContext *C)
 		MEM_freeN(matcap_with_ao);
 	}
 
-	/* Cleanup all runtime data loaded from file */
-	for (Scene *sce = bmain->scene.first; sce; sce = sce->id.next) {
-		/* Using render settings as material settings */
-		MaterialEngineSettingsClay *res = DRW_render_settings_get(sce, RE_engine_id_BLENDER_CLAY);
-		res->flag = CLAY_OUTDATED;
-		res->ubo_index = -1;
-
-		/* Update Collections Materials */
-		for (SceneLayer *sl = sce->render_layers.first; sl; sl = sl->next) {
-			for (LayerCollection *lc = sl->layer_collections.first; lc; lc = lc->next) {
-				CollectionEngineSettings *ces;
-				ces = BKE_layer_collection_engine_get(lc, RE_engine_id_BLENDER_CLAY);
-				if (ces) { /* May not exists */
-					BKE_collection_engine_property_value_set_int(ces, "flag", CLAY_OUTDATED);
-					BKE_collection_engine_property_value_set_int(ces, "ubo_index", -1);
-				}
-			}
-		}
+	if (!stl->storage) {
+		stl->storage = MEM_callocN(sizeof(CLAY_Storage), "CLAY_Storage");
+	}
+
+	if (!stl->mat_ubo) {
+		stl->mat_ubo = DRW_uniformbuffer_create(sizeof(CLAY_UBO_Storage), NULL);
 	}
 
-	data.ubo_flag |= CLAY_UBO_REFRESH;
+	if (ubo_mat_idxs[1] == 0) {
+		/* Just int to have pointers to them */
+		for (int i = 0; i < MAX_CLAY_MAT; ++i) {
+			ubo_mat_idxs[i] = i;
+		}
+	}
 }
 
 static void CLAY_ssao_setup(void)
@@ -430,7 +426,7 @@ static void CLAY_ssao_setup(void)
 	}
 }
 
-static DRWShadingGroup *CLAY_shgroup_create(DRWPass *pass, int *UNUSED(material_id))
+static DRWShadingGroup *CLAY_shgroup_create(DRWPass *pass, int *material_id)
 {
 	const int depthloc = 0, matcaploc = 1, jitterloc = 2, sampleloc = 3;
 
@@ -445,7 +441,7 @@ static DRWShadingGroup *CLAY_shgroup_create(DRWPass *pass, int *UNUSED(material_
 	DRW_shgroup_uniform_vec4(grp, "ssao_params", data.ssao_params, 1);
 	DRW_shgroup_uniform_vec3(grp, "matcaps_color", (float *)data.matcap_colors, 24);
 
-	//DRW_shgroup_uniform_int(grp, "material_id", material_id, 1);
+	DRW_shgroup_uniform_int(grp, "mat_id", material_id, 1);
 
 #ifndef GTAO
 	DRW_shgroup_uniform_texture(grp, "ssao_jitter", data.jitter_tx, jitterloc);
@@ -455,11 +451,39 @@ static DRWShadingGroup *CLAY_shgroup_create(DRWPass *pass, int *UNUSED(material_
 	return grp;
 }
 
-static void update_ubo_storage(float matcap_rot, float matcap_hue, float matcap_sat, float matcap_val,
-                               float ssao_distance, float ssao_factor_cavity, float ssao_factor_edge,
-                               float ssao_attenuation, int matcap_icon, unsigned int current_id)
+static int search_mat_to_ubo(CLAY_Storage *storage, float matcap_rot, float matcap_hue, float matcap_sat,
+                             float matcap_val, float ssao_distance, float ssao_factor_cavity,
+                             float ssao_factor_edge, float ssao_attenuation, int matcap_icon)
 {
-	CLAY_UBO_Material *ubo = &data.mat_storage.materials[current_id];
+	/* For now just use a linear search and test all parameters */
+	/* TODO make a hash table */
+	for (int i = 0; i < storage->ubo_current_id; ++i)
+	{
+		CLAY_UBO_Material *ubo = &storage->mat_storage.materials[i];
+
+		if ((ubo->matcap_rot[0] == cosf(matcap_rot * 3.14159f * 2.0f)) &&
+		    (ubo->matcap_hsv[0] == matcap_hue + 0.5f) &&
+		    (ubo->matcap_hsv[1] == matcap_sat * 2.0f) &&
+		    (ubo->matcap_hsv[2] == matcap_val * 2.0f) &&
+		    (ubo->ssao_params_var[0] == ssao_distance) &&
+		    (ubo->ssao_params_var[1] == ssao_factor_cavity) &&
+		    (ubo->ssao_params_var[2] == ssao_factor_edge) &&
+		    (ubo->ssao_params_var[3] == ssao_attenuation) &&
+		    (ubo->matcap_id == matcap_to_index(matcap_icon)))
+		{
+			return i;
+		}
+	}
+
+	return -1;
+}
+
+static int push_mat_to_ubo(CLAY_Storage *storage, float matcap_rot, float matcap_hue, float matcap_sat,
+                            float matcap_val, float ssao_distance, float ssao_factor_cavity,
+                            float ssao_factor_edge, float ssao_attenuation, int matcap_icon)
+{
+	int id = storage->ubo_current_id;
+	CLAY_UBO_Material *ubo = &storage->mat_storage.materials[id];
 
 	ubo->matcap_rot[0] = cosf(matcap_rot * 3.14159f * 2.0f);
 	ubo->matcap_rot[1] = sinf(matcap_rot * 3.14159f * 2.0f);
@@ -473,92 +497,115 @@ static void update_ubo_storage(float matcap_rot, float matcap_hue, float matcap_
 	ubo->ssao_params_var[2] = ssao_factor_edge;
 	ubo->ssao_params_var[3] = ssao_attenuation;
 
-
 	ubo->matcap_id = matcap_to_index(matcap_icon);
-}
 
-static void CLAY_update_material_ubo(const struct bContext *C)
-{
-	Main *bmain = CTX_data_main(C);
+	storage->ubo_current_id++;
 
-	/* Update Default materials */
-	for (Scene *sce = bmain->scene.first; sce; sce = sce->id.next) {
-		/* Using render settings as material settings */
-		MaterialEngineSettingsClay *res = DRW_render_settings_get(sce, RE_engine_id_BLENDER_CLAY);
-
-		if (res->flag & CLAY_OUTDATED)
-			data.ubo_flag |= CLAY_UBO_REFRESH;
+	return id;
+}
 
-		if (res->matcap_icon < ICON_MATCAP_01 ||
-		    res->matcap_icon > ICON_MATCAP_24)
-		{
-			res->matcap_icon = ICON_MATCAP_01;
-		}
+static int mat_in_ubo(CLAY_Storage *storage, struct GPUUniformBuffer *ubo, DRWPass *pass,
+                      float matcap_rot, float matcap_hue, float matcap_sat,
+                      float matcap_val, float ssao_distance, float ssao_factor_cavity,
+                      float ssao_factor_edge, float ssao_attenuation, int matcap_icon)
+{
+	int id;
 
-		res->flag &= ~CLAY_OUTDATED;
+	/* Search material in UBO */
+	id = search_mat_to_ubo(storage, matcap_rot, matcap_hue, matcap_sat, matcap_val,
+	                       ssao_distance, ssao_factor_cavity, ssao_factor_edge,
+	                       ssao_attenuation, matcap_icon);
 
+	/* if not found create it */
+	if (id == -1) {
+		id = push_mat_to_ubo(storage, matcap_rot, matcap_hue, matcap_sat, matcap_val,
+		                     ssao_distance, ssao_factor_cavity, ssao_factor_edge,
+		                     ssao_attenuation, matcap_icon);
 
-		/* Update Collections Materials */
-		for (SceneLayer *sl = sce->render_layers.first; sl; sl = sl->next) {
-			for (LayerCollection *lc = sl->layer_collections.first; lc; lc = lc->next) {
-				CollectionEngineSettings *ces;
-				ces = BKE_layer_collection_engine_get(lc, RE_engine_id_BLENDER_CLAY);
+		storage->shgrps[id] = CLAY_shgroup_create(pass, &ubo_mat_idxs[id]);
 
-				BKE_collection_engine_property_value_set_int(ces, "flag", 0);
-				BKE_collection_engine_property_value_set_int(ces, "ubo_index", 0);
-			}
+		/* if it's the first shgrp, pass bind the material UBO */
+		if (storage->ubo_current_id == 1) {
+			DRW_shgroup_uniform_block(storage->shgrps[0], "material_block", ubo, 0);
 		}
 	}
 
-	if (data.ubo_flag & CLAY_UBO_REFRESH) {
-		int current_id = 0;
+	return id;
+}
 
+/* Safe way to get override values */
+static void override_setting(CollectionEngineSettings *ces, const char *name, void *ret)
+{
+	CollectionEngineProperty *cep = BKE_collection_engine_property_get(ces, name);
 
-		/* Default materials */
-		for (Scene *sce = bmain->scene.first; sce; sce = sce->id.next) {
-			MaterialEngineSettingsClay *res = DRW_render_settings_get(sce, RE_engine_id_BLENDER_CLAY);
+	if (!cep) return;
+	if ((cep->flag & COLLECTION_PROP_USE) == 0) return;
 
-			update_ubo_storage(res->matcap_rot, res->matcap_hue, res->matcap_sat, res->matcap_val,
-			                   res->ssao_distance, res->ssao_factor_cavity, res->ssao_factor_edge,
-			                   res->ssao_attenuation, res->matcap_icon, current_id);
-			current_id++;
+	if (cep->type == COLLECTION_PROP_TYPE_INT) {
+		CollectionEnginePropertyInt *prop = (CollectionEnginePropertyInt *)cep;
+		*((int *)ret) = prop->value;
+	}
+	else {
+		CollectionEnginePropertyInt *prop = (CollectionEnginePropertyInt *)cep;
+		*((float *)ret) = prop->valu

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list