WIP Make shadows visible for Storm delegate #80

Closed
Vasyl Pidhirskyi wants to merge 16 commits from Vasyl-Pidhirskyi/blender_bn:BLEN-469 into hydra-render

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
5 changed files with 67 additions and 30 deletions
Showing only changes of commit f6c9fc6e72 - Show all commits

View File

@ -5,6 +5,7 @@
#include <bitset> #include <bitset>
#include "DNA_light_types.h"
#include "DNA_scene_types.h" #include "DNA_scene_types.h"
#include "BLI_set.hh" #include "BLI_set.hh"
@ -238,6 +239,39 @@ void HydraSceneDelegate::clear()
view3d = nullptr; view3d = nullptr;
} }
bool HydraSceneDelegate::has_shadows()
{
bool has_shadows = false;
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph;
settings.flags = DEG_OBJECT_ITER_FOR_RENDER_ENGINE_FLAGS;
DEGObjectIterData data = {0};
data.settings = &settings;
data.graph = settings.depsgraph;
data.flag = settings.flags;
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data,
Object *,
object)
{
if (object->type == OB_LAMP) {
Light *light = (Light *)object->data;
has_shadows |= light->mode & LA_SHADOW;
if (has_shadows) {
break;
}
}
}
ITER_END;
return has_shadows;
}
pxr::SdfPath HydraSceneDelegate::prim_id(ID *id, const char *prefix) const pxr::SdfPath HydraSceneDelegate::prim_id(ID *id, const char *prefix) const
{ {
/* Making id of object in form like <prefix>_<pointer in 16 hex digits format> */ /* Making id of object in form like <prefix>_<pointer in 16 hex digits format> */

View File

@ -44,7 +44,6 @@ class HydraSceneDelegate : public pxr::HdSceneDelegate {
std::string studiolight_name; std::string studiolight_name;
float studiolight_rotation; float studiolight_rotation;
float studiolight_intensity; float studiolight_intensity;
bool use_storm_shadows = false;
Vasyl-Pidhirskyi marked this conversation as resolved Outdated

This parameter not needed, instead create function like HydraSceneDelegate::has_shadows()

This parameter not needed, instead create function like `HydraSceneDelegate::has_shadows()`
bool operator==(const ShadingSettings &other); bool operator==(const ShadingSettings &other);
}; };
@ -91,6 +90,7 @@ class HydraSceneDelegate : public pxr::HdSceneDelegate {
void populate(Depsgraph *depsgraph, View3D *v3d); void populate(Depsgraph *depsgraph, View3D *v3d);
void clear(); void clear();
bool has_shadows();
private: private:
pxr::SdfPath prim_id(ID *id, const char *prefix) const; pxr::SdfPath prim_id(ID *id, const char *prefix) const;

View File

@ -148,32 +148,32 @@ void LightData::write_shadow_params()
we verified if any of light's shadow is turned on, if yes shadow task will be added*/ we verified if any of light's shadow is turned on, if yes shadow task will be added*/
Light *light = (Light *)((Object *)id)->data; Light *light = (Light *)((Object *)id)->data;
bool use_shadow = (light->mode & LA_SHADOW) && light->type == LA_SUN; bool use_shadow = (light->mode & LA_SHADOW) && light->type == LA_SUN;
scene_delegate_->shading_settings.use_storm_shadows |= use_shadow; data_[pxr::HdLightTokens->hasShadow] = use_shadow;
if (use_shadow) { if (!use_shadow) {
data_[pxr::HdLightTokens->hasShadow] = use_shadow; return;
pxr::HdxShadowParams shadow_params = pxr::HdxShadowParams();
/* ShadowMatrix class is used to calculates shadow matrix from light frustum.*/
ShadowMatrix *shadow_matrix = new ShadowMatrix();
shadow_matrix->set_near_far(0.1, light->cascade_max_dist);
shadow_matrix->set_transform(gf_matrix_from_transform(((Object *)id)->object_to_world));
/* SetWindow sets size of projected shadow texture texture in
the approximate value is calculated using sun_angle property
180 degrees is window size 1000 by 1000 */
shadow_matrix->set_window(light->sun_angle * 0.5f / M_PI * 1000,
light->sun_angle * 0.5f / M_PI * 1000);
shadow_matrix->set_projection_type(pxr::GfFrustum::Orthographic);
shadow_params.enabled = use_shadow;
shadow_params.resolution = scene_delegate_->scene->eevee.shadow_cascade_size;
shadow_params.shadowMatrix = pxr::HdxShadowMatrixComputationSharedPtr(shadow_matrix);
shadow_params.bias = -1.0 * light->bias;
shadow_params.blur = light->cascade_fade;
data_[pxr::HdLightTokens->shadowParams] = shadow_params;
data_[pxr::HdLightTokens->shadowCollection] = pxr::HdRprimCollection(
pxr::HdTokens->geometry, pxr::HdReprSelector(pxr::HdReprTokens->refined));
} }
pxr::HdxShadowParams shadow_params = pxr::HdxShadowParams();
/* ShadowMatrix class is used to calculates shadow matrix from light frustum.*/
ShadowMatrix *shadow_matrix = new ShadowMatrix();
shadow_matrix->set_near_far(0.1, light->cascade_max_dist);
shadow_matrix->set_transform(gf_matrix_from_transform(((Object *)id)->object_to_world));
/* SetWindow sets size of projected shadow texture texture in
the approximate value is calculated using sun_angle property
180 degrees is window size 1000 by 1000 */
shadow_matrix->set_window(light->sun_angle * 0.5f / M_PI * 1000,
light->sun_angle * 0.5f / M_PI * 1000);
shadow_matrix->set_projection_type(pxr::GfFrustum::Orthographic);
shadow_params.enabled = use_shadow;
shadow_params.resolution = scene_delegate_->scene->eevee.shadow_cascade_size;
shadow_params.shadowMatrix = pxr::HdxShadowMatrixComputationSharedPtr(shadow_matrix);
shadow_params.bias = -1.0 * light->bias;
shadow_params.blur = light->cascade_fade;
data_[pxr::HdLightTokens->shadowParams] = shadow_params;
data_[pxr::HdLightTokens->shadowCollection] = pxr::HdRprimCollection(
pxr::HdTokens->geometry, pxr::HdReprSelector(pxr::HdReprTokens->refined));
} }
void LightData::insert() void LightData::insert()

View File

@ -27,6 +27,7 @@ void FinalEngine::render(Depsgraph *depsgraph)
{ {
const Scene *scene = DEG_get_evaluated_scene(depsgraph); const Scene *scene = DEG_get_evaluated_scene(depsgraph);
const ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph); const ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
bool has_shadows = hydra_scene_delegate_->has_shadows();
char scene_name[MAX_ID_FULL_NAME]; char scene_name[MAX_ID_FULL_NAME];
BKE_id_full_name_get(scene_name, &scene->id, 0); BKE_id_full_name_get(scene_name, &scene->id, 0);
@ -50,7 +51,7 @@ void FinalEngine::render(Depsgraph *depsgraph)
render_task_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); render_task_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1]));
if (light_tasks_delegate_) { if (light_tasks_delegate_) {
light_tasks_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); light_tasks_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1]));
light_tasks_delegate_->set_shadows(hydra_scene_delegate_->shading_settings.use_storm_shadows); light_tasks_delegate_->set_shadows(has_shadows);
} }
render_task_delegate_->add_aov(pxr::HdAovTokens->color); render_task_delegate_->add_aov(pxr::HdAovTokens->color);
@ -73,7 +74,7 @@ void FinalEngine::render(Depsgraph *depsgraph)
tasks.push_back(light_tasks_delegate_->skydome_task()); tasks.push_back(light_tasks_delegate_->skydome_task());
} }
tasks.push_back(light_tasks_delegate_->simple_task()); tasks.push_back(light_tasks_delegate_->simple_task());
if (hydra_scene_delegate_->shading_settings.use_storm_shadows) { if (has_shadows) {
tasks.push_back(light_tasks_delegate_->shadow_task()); tasks.push_back(light_tasks_delegate_->shadow_task());
} }
} }

View File

@ -219,6 +219,8 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
{ {
ViewSettings view_settings(context); ViewSettings view_settings(context);
const Scene *scene = DEG_get_evaluated_scene(depsgraph); const Scene *scene = DEG_get_evaluated_scene(depsgraph);
bool has_shadows = hydra_scene_delegate_->has_shadows();
if (view_settings.width() * view_settings.height() == 0) { if (view_settings.width() * view_settings.height() == 0) {
return; return;
}; };
@ -233,7 +235,7 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
render_task_delegate_->set_viewport(viewport); render_task_delegate_->set_viewport(viewport);
if (light_tasks_delegate_) { if (light_tasks_delegate_) {
light_tasks_delegate_->set_viewport(viewport); light_tasks_delegate_->set_viewport(viewport);
light_tasks_delegate_->set_shadows(hydra_scene_delegate_->shading_settings.use_storm_shadows); light_tasks_delegate_->set_shadows(has_shadows);
} }
render_task_delegate_->add_aov(pxr::HdAovTokens->color); render_task_delegate_->add_aov(pxr::HdAovTokens->color);
@ -245,7 +247,7 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
tasks.push_back(light_tasks_delegate_->skydome_task()); tasks.push_back(light_tasks_delegate_->skydome_task());
} }
tasks.push_back(light_tasks_delegate_->simple_task()); tasks.push_back(light_tasks_delegate_->simple_task());
if (hydra_scene_delegate_->shading_settings.use_storm_shadows) { if (has_shadows) {
tasks.push_back(light_tasks_delegate_->shadow_task()); tasks.push_back(light_tasks_delegate_->shadow_task());
} }
} }