[Bf-blender-cvs] [819b8adb94f] blender2.8: Eevee: Move Spherical Harmonics to a new Probe UBO.

Clément Foucault noreply at git.blender.org
Sun Jun 4 23:12:08 CEST 2017


Commit: 819b8adb94f5ab617a0b2bcac37678eb4874dfbd
Author: Clément Foucault
Date:   Sun Jun 4 16:50:22 2017 +0200
Branches: blender2.8
https://developer.blender.org/rB819b8adb94f5ab617a0b2bcac37678eb4874dfbd

Eevee: Move Spherical Harmonics to a new Probe UBO.

Keep data packing tight to prevent use of padding floats

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

M	source/blender/draw/engines/eevee/eevee_materials.c
M	source/blender/draw/engines/eevee/eevee_private.h
M	source/blender/draw/engines/eevee/eevee_probes.c
M	source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
M	source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl

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

diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c
index 23f87a41b24..7297f3a1e90 100644
--- a/source/blender/draw/engines/eevee/eevee_materials.c
+++ b/source/blender/draw/engines/eevee/eevee_materials.c
@@ -37,6 +37,7 @@
 
 #define SHADER_DEFINES \
 	"#define EEVEE_ENGINE\n" \
+	"#define MAX_PROBE " STRINGIFY(MAX_PROBE) "\n" \
 	"#define MAX_LIGHT " STRINGIFY(MAX_LIGHT) "\n" \
 	"#define MAX_SHADOW_CUBE " STRINGIFY(MAX_SHADOW_CUBE) "\n" \
 	"#define MAX_SHADOW_MAP " STRINGIFY(MAX_SHADOW_MAP) "\n" \
@@ -257,12 +258,14 @@ struct GPUMaterial *EEVEE_material_mesh_get(struct Scene *scene, Material *ma)
 
 static void add_standard_uniforms(DRWShadingGroup *shgrp, EEVEE_SceneLayerData *sldata)
 {
+	DRW_shgroup_uniform_block(shgrp, "probe_block", sldata->probe_ubo);
 	DRW_shgroup_uniform_block(shgrp, "light_block", sldata->light_ubo);
 	DRW_shgroup_uniform_block(shgrp, "shadow_block", sldata->shadow_ubo);
 	DRW_shgroup_uniform_int(shgrp, "light_count", &sldata->lamps->num_light, 1);
+	DRW_shgroup_uniform_int(shgrp, "probe_count", &sldata->probes->num_cube, 1);
+	DRW_shgroup_uniform_float(shgrp, "lodMax", &sldata->probes->lodmax, 1);
 	DRW_shgroup_uniform_texture(shgrp, "utilTex", e_data.util_tex);
 	DRW_shgroup_uniform_buffer(shgrp, "probeCubes", &sldata->probe_pool);
-	DRW_shgroup_uniform_float(shgrp, "lodMax", &sldata->probes->lodmax, 1);
 	DRW_shgroup_uniform_buffer(shgrp, "shadowCubes", &sldata->shadow_depth_cube_pool);
 	DRW_shgroup_uniform_buffer(shgrp, "shadowCascades", &sldata->shadow_depth_cascade_pool);
 }
@@ -339,8 +342,7 @@ void EEVEE_materials_cache_init(EEVEE_Data *vedata)
 	}
 }
 
-/* Macro to call the right  */
-#define ADD_MATERIAL_CALL(shgrp, ob, geom) do { \
+#define ADD_SHGROUP_CALL(shgrp, ob, geom) do { \
 	if (is_sculpt_mode) { \
 		DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat); \
 	} \
@@ -363,7 +365,7 @@ void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_SceneLayerData *sl
 
 	/* Depth Prepass */
 	DRWShadingGroup *depth_shgrp = do_cull ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp;
-	ADD_MATERIAL_CALL(depth_shgrp, ob, geom);
+	ADD_SHGROUP_CALL(depth_shgrp, ob, geom);
 
 	/* Get per-material split surface */
 	struct Batch **mat_geom = DRW_cache_object_surface_material_get(ob);
@@ -398,7 +400,7 @@ void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_SceneLayerData *sl
 				if (shgrp) {
 					add_standard_uniforms(shgrp, sldata);
 
-					ADD_MATERIAL_CALL(shgrp, ob, mat_geom[i]);
+					ADD_SHGROUP_CALL(shgrp, ob, mat_geom[i]);
 				}
 				else {
 					/* Shader failed : pink color */
@@ -418,7 +420,7 @@ void EEVEE_materials_cache_populate(EEVEE_Data *vedata, EEVEE_SceneLayerData *sl
 				DRW_shgroup_uniform_float(shgrp, "specular", spec_p, 1);
 				DRW_shgroup_uniform_float(shgrp, "roughness", rough_p, 1);
 
-				ADD_MATERIAL_CALL(shgrp, ob, mat_geom[i]);
+				ADD_SHGROUP_CALL(shgrp, ob, mat_geom[i]);
 			}
 		}
 	}
diff --git a/source/blender/draw/engines/eevee/eevee_private.h b/source/blender/draw/engines/eevee/eevee_private.h
index 3e4547e76ac..0d5ab592563 100644
--- a/source/blender/draw/engines/eevee/eevee_private.h
+++ b/source/blender/draw/engines/eevee/eevee_private.h
@@ -167,6 +167,7 @@ enum {
 /* ************ PROBE UBO ************* */
 typedef struct EEVEE_Probe {
 	float position[3], dist;
+	float shcoefs[9][3], pad;
 } EEVEE_Probe;
 
 /* ************ PROBE DATA ************* */
@@ -185,7 +186,6 @@ typedef struct EEVEE_ProbesInfo {
 	float lodmax;
 	int shres;
 	int shnbr;
-	float shcoefs[9][3]; /* Temp */
 	struct GPUTexture *backgroundtex;
 	/* List of probes in the scene. */
 	/* XXX This is fragile, can get out of sync quickly. */
diff --git a/source/blender/draw/engines/eevee/eevee_probes.c b/source/blender/draw/engines/eevee/eevee_probes.c
index a5097080646..70e1ac02ef9 100644
--- a/source/blender/draw/engines/eevee/eevee_probes.c
+++ b/source/blender/draw/engines/eevee/eevee_probes.c
@@ -128,6 +128,7 @@ void EEVEE_probes_init(EEVEE_SceneLayerData *sldata)
 
 	if (!sldata->probes) {
 		sldata->probes = MEM_callocN(sizeof(EEVEE_ProbesInfo), "EEVEE_ProbesInfo");
+		sldata->probe_ubo = DRW_uniformbuffer_create(sizeof(EEVEE_Probe) * MAX_PROBE, NULL);
 	}
 
 	/* Setup Render Target Cubemap */
@@ -151,7 +152,7 @@ void EEVEE_probes_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl)
 {
 	EEVEE_ProbesInfo *pinfo = sldata->probes;
 
-	pinfo->num_cube = 0;
+	pinfo->num_cube = 1; /* at least one for the world */
 	memset(pinfo->probes_ref, 0, sizeof(pinfo->probes_ref));
 
 	{
@@ -260,15 +261,19 @@ void EEVEE_probes_cache_finish(EEVEE_SceneLayerData *sldata)
 	DRWFboTexture tex_filter = {&sldata->probe_pool, DRW_TEX_RGBA_16, DRW_TEX_FILTER | DRW_TEX_MIPMAP};
 
 	DRW_framebuffer_init(&sldata->probe_filter_fb, &draw_engine_eevee_type, PROBE_SIZE, PROBE_SIZE, &tex_filter, 1);
+
+	DRW_uniformbuffer_update(sldata->probe_ubo, &sldata->probes->probe_data);
 }
 
 /* Renders the probe with index probe_idx.
  * Renders the world probe if probe_idx = -1. */
-void render_one_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, int probe_idx)
+static void render_one_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, int probe_idx)
 {
 	EEVEE_ProbesInfo *pinfo = sldata->probes;
+	EEVEE_Probe *eprobe = &pinfo->probe_data[probe_idx];
 	Object *ob = NULL;
-	bool is_object_probe = (probe_idx != -1);
+	struct DRWPass *probe_pass;
+	bool is_object_probe = (probe_idx > 0);
 
 	float projmat[4][4], posmat[4][4];
 	float near, far;
@@ -283,11 +288,13 @@ void render_one_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, int pro
 
 		/* Move to capture position */
 		negate_v3_v3(posmat[3], ob->obmat[3]);
+		probe_pass = psl->probe_background; /* TODO use objects in the scene */
 	}
 	else {
 		/* World cubemap */
 		near = 0.1f;
 		far = 100.0f;
+		probe_pass = psl->probe_background;
 	}
 
 	/* 1 - Render to cubemap target using geometry shader. */
@@ -302,7 +309,7 @@ void render_one_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, int pro
 	}
 
 	DRW_framebuffer_bind(sldata->probe_fb);
-	DRW_draw_pass(psl->probe_background);
+	DRW_draw_pass(probe_pass);
 
 	if (is_object_probe) {
 		DRW_framebuffer_clear(true, true, false, clear_color, 1.0);
@@ -337,7 +344,7 @@ void render_one_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, int pro
 		else if (pinfo->padding_size > 4) {
 			pinfo->padding_size += 1;
 		}
-		pinfo->layer = probe_idx + 1; /* 0 is world */
+		pinfo->layer = probe_idx;
 		pinfo->roughness = (float)i / ((float)maxlevel - 4.0f);
 		pinfo->roughness *= pinfo->roughness; /* Disney Roughness */
 		pinfo->roughness *= pinfo->roughness; /* Distribute Roughness accros lod more evenly */
@@ -369,8 +376,6 @@ void render_one_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, int pro
 	}
 	/* For shading, save max level of the octahedron map */
 	pinfo->lodmax = (float)(maxlevel - min_lod_level) - 1.0f;
-	/* reattach to have a valid framebuffer. */
-	DRW_framebuffer_texture_attach(sldata->probe_filter_fb, sldata->probe_pool, 0, 0);
 
 	/* 4 - Compute spherical harmonics */
 	/* Tweaking parameters to balance perf. vs precision */
@@ -378,14 +383,19 @@ void render_one_probe(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, int pro
 	pinfo->lodfactor = 4.0f; /* Improve cache reuse */
 	DRW_framebuffer_bind(sldata->probe_sh_fb);
 	DRW_draw_pass(psl->probe_sh_compute);
-	DRW_framebuffer_read_data(0, 0, 9, 1, 3, 0, (float *)pinfo->shcoefs);
+	DRW_framebuffer_read_data(0, 0, 9, 1, 3, 0, (float *)eprobe->shcoefs);
+
+	/* reattach to have a valid framebuffer. */
+	DRW_framebuffer_texture_attach(sldata->probe_filter_fb, sldata->probe_pool, 0, 0);
 }
 
 void EEVEE_probes_refresh(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl)
 {
 	if (e_data.update_world) {
-		render_one_probe(sldata, psl, -1);
+		render_one_probe(sldata, psl, 0);
 		e_data.update_world = false;
+
+		DRW_uniformbuffer_update(sldata->probe_ubo, &sldata->probes->probe_data);
 	}
 }
 
diff --git a/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl b/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
index 8963ad2b93e..b0b11c489b6 100644
--- a/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
+++ b/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
@@ -9,6 +9,23 @@
 
 /* ------- Structures -------- */
 
+struct ProbeData {
+	vec4 position_influence;      /* w : InfluenceRadius */
+	vec4 shcoefs[7];
+};
+
+#define p_position     position_influence.xyz
+#define p_spec         position_influence.w
+#define shcoef0        shcoefs[0].rgb
+#define shcoef1        vec3(shcoefs[0].a, shcoefs[1].rg)
+#define shcoef2        vec3(shcoefs[1].ba, shcoefs[2].r)
+#define shcoef3        shcoefs[2].gba
+#define shcoef4        shcoefs[3].rgb
+#define shcoef5        vec3(shcoefs[3].a, shcoefs[4].rg)
+#define shcoef6        vec3(shcoefs[4].ba, shcoefs[5].r)
+#define shcoef7        shcoefs[5].gba
+#define shcoef8        shcoefs[6].rgb
+
 struct LightData {
 	vec4 position_influence;      /* w : InfluenceRadius */
 	vec4 color_spec;              /* w : Spec Intensity */
@@ -172,34 +189,34 @@ float buffer_depth(bool is_persp, float z, float zf, float zn)
 #define spherical_harmonics spherical_harmonics_L2
 
 /* http://seblagarde.wordpress.com/2012/01/08/pi-or-not-to-pi-in-game-lighting-equation/ */
-vec3 spherical_harmonics_L1(vec3 N, vec3 shcoefs[9])
+vec3 spherical_harmonics_L1(vec3 N, vec4 shcoefs[3])
 {
 	vec3 sh = vec3(0.0);
 
-	sh += 0.282095 * shcoefs[0];
+	sh += 0.282095 * shcoef0;
 
-	sh += -0.488603 * N.z * shcoefs[1];
-	sh += 0.488603 * N.y * shcoefs[2];
-	sh += -0.488603 * N.x * shcoefs[3];
+	sh += -0.488603 * N.z * shcoef1;
+	sh += 0.488603 * N.y * shcoef2;
+	sh += -0.488603 * N.x * shcoef3;
 
 	return sh;
 }
 
-vec3 spherical_harmonics_L2(vec3 N, vec3 shcoefs[9])
+vec3 spherical_harmonics_L2(vec3 N, vec4 shcoefs[7])
 {
 	vec3 sh = vec3(0.0);
 
-	sh += 0.282095 * shcoefs[0];
+	sh += 0.282095 * shcoef0;
 
-	sh += -0.488603 * N.z * shcoefs[1];
-	sh += 0.488603 * N.y * shcoefs[2];
-	sh += -0.488603 * N.x * shcoefs[3];
+	sh += -0.488603 * N.z * shcoef1;
+	sh += 0.488603 * N.y * shcoef2;
+	sh += -0.4886

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list