Clay: Performance: Disable AO codepath if not necessary.

This optimisation only works if no material in the scene require the AO pass.
For this either set the AO distance to 0 or both Cavity and Edges factors to 0.

This double the performance of scenes with very high triangle count.
This commit is contained in:
Clément Foucault 2018-01-21 19:12:59 +01:00
parent 6700ee9855
commit bdfd9a11e8
3 changed files with 29 additions and 6 deletions

View File

@ -183,6 +183,7 @@ typedef struct CLAY_PrivateData {
DRWShadingGroup *depth_shgrp_cull;
DRWShadingGroup *depth_shgrp_cull_select;
DRWShadingGroup *depth_shgrp_cull_active;
bool enable_ao;
} CLAY_PrivateData; /* Transient data */
/* Functions */
@ -607,7 +608,7 @@ static int hair_mat_in_ubo(CLAY_Storage *storage, const CLAY_HAIR_UBO_Material *
return id;
}
static void ubo_mat_from_object(Object *ob, CLAY_UBO_Material *r_ubo)
static void ubo_mat_from_object(Object *ob, CLAY_UBO_Material *r_ubo, bool *r_needs_ao)
{
IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_CLAY);
@ -622,6 +623,12 @@ static void ubo_mat_from_object(Object *ob, CLAY_UBO_Material *r_ubo)
float ssao_attenuation = BKE_collection_engine_property_value_get_float(props, "ssao_attenuation");
int matcap_icon = BKE_collection_engine_property_value_get_int(props, "matcap_icon");
if (((ssao_factor_cavity > 0.0) || (ssao_factor_edge > 0.0)) &&
(ssao_distance > 0.0))
{
*r_needs_ao = true;
}
memset(r_ubo, 0x0, sizeof(*r_ubo));
r_ubo->matcap_rot[0] = cosf(matcap_rot * 3.14159f * 2.0f);
@ -667,7 +674,7 @@ static DRWShadingGroup *CLAY_object_shgrp_get(
DRWShadingGroup **shgrps = use_flat ? stl->storage->shgrps_flat : stl->storage->shgrps;
CLAY_UBO_Material mat_ubo_test;
ubo_mat_from_object(ob, &mat_ubo_test);
ubo_mat_from_object(ob, &mat_ubo_test, &stl->g_data->enable_ao);
int id = mat_in_ubo(stl->storage, &mat_ubo_test);
@ -712,6 +719,9 @@ static void clay_cache_init(void *vedata)
stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
}
/* Disable AO unless a material needs it. */
stl->g_data->enable_ao = false;
/* Depth Pass */
{
psl->depth_pass = DRW_pass_create("Depth Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
@ -851,18 +861,25 @@ static void clay_cache_finish(void *vedata)
static void clay_draw_scene(void *vedata)
{
CLAY_StorageList *stl = ((CLAY_Data *)vedata)->stl;
CLAY_PassList *psl = ((CLAY_Data *)vedata)->psl;
CLAY_FramebufferList *fbl = ((CLAY_Data *)vedata)->fbl;
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
/* Pass 1 : Depth pre-pass */
DRW_draw_pass(psl->depth_pass);
DRW_draw_pass(psl->depth_pass_cull);
if (stl->g_data->enable_ao) {
DRW_draw_pass(psl->depth_pass);
DRW_draw_pass(psl->depth_pass_cull);
}
else {
DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
DRW_pass_state_set(psl->clay_pass, state);
DRW_pass_state_set(psl->clay_pass_flat, state);
}
/* Pass 2 : Duplicate depth */
/* Unless we go for deferred shading we need this to avoid manual depth test and artifacts */
if (DRW_state_is_fbo()) {
if (DRW_state_is_fbo() && stl->g_data->enable_ao) {
/* attach temp textures */
DRW_framebuffer_texture_attach(fbl->dupli_depth, e_data.depth_dup, 0, 0);

View File

@ -358,6 +358,7 @@ void DRW_shgroup_uniform_mat4(DRWShadingGroup *shgroup, const char *name, const
/* Passes */
DRWPass *DRW_pass_create(const char *name, DRWState state);
void DRW_pass_state_set(DRWPass *pass, DRWState state);
void DRW_pass_foreach_shgroup(DRWPass *pass, void (*callback)(void *userData, DRWShadingGroup *shgrp), void *userData);
void DRW_pass_sort_shgroup_z(DRWPass *pass);

View File

@ -1290,6 +1290,11 @@ DRWPass *DRW_pass_create(const char *name, DRWState state)
return pass;
}
void DRW_pass_state_set(DRWPass *pass, DRWState state)
{
pass->state = state;
}
void DRW_pass_free(DRWPass *pass)
{
for (DRWShadingGroup *shgroup = pass->shgroups; shgroup; shgroup = shgroup->next) {