|
| 1 | +// |
| 2 | +// Copyright 2024 Pixar |
| 3 | +// |
| 4 | +// Licensed under the terms set forth in the LICENSE.txt file available at |
| 5 | +// https://openusd.org/license. |
| 6 | +// |
| 7 | +#include "pxr/imaging/plugin/hdEmbree/light.h" |
| 8 | + |
| 9 | +#include "light.h" |
| 10 | +#include "pxr/imaging/plugin/hdEmbree/debugCodes.h" |
| 11 | +#include "pxr/imaging/plugin/hdEmbree/renderParam.h" |
| 12 | +#include "pxr/imaging/plugin/hdEmbree/renderer.h" |
| 13 | + |
| 14 | +#include "pxr/imaging/hd/sceneDelegate.h" |
| 15 | +#include "pxr/imaging/hio/image.h" |
| 16 | + |
| 17 | +#include <embree3/rtcore_buffer.h> |
| 18 | +#include <embree3/rtcore_scene.h> |
| 19 | + |
| 20 | +#include <fstream> |
| 21 | +#include <sstream> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +PXR_NAMESPACE_OPEN_SCOPE |
| 25 | + |
| 26 | +HdEmbree_Light::HdEmbree_Light(SdfPath const& id, TfToken const& lightType) |
| 27 | + : HdLight(id) { |
| 28 | + if (id.IsEmpty()) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // Set the variant to the right type - Sync will fill rest of data |
| 33 | + if (lightType == HdSprimTypeTokens->cylinderLight) { |
| 34 | + _lightData.lightVariant = HdEmbree_Cylinder(); |
| 35 | + } else if (lightType == HdSprimTypeTokens->diskLight) { |
| 36 | + _lightData.lightVariant = HdEmbree_Disk(); |
| 37 | + } else if (lightType == HdSprimTypeTokens->rectLight) { |
| 38 | + // Get shape parameters |
| 39 | + _lightData.lightVariant = HdEmbree_Rect(); |
| 40 | + } else if (lightType == HdSprimTypeTokens->sphereLight) { |
| 41 | + _lightData.lightVariant = HdEmbree_Sphere(); |
| 42 | + } else { |
| 43 | + TF_WARN("HdEmbree - Unrecognized light type: %s", lightType.GetText()); |
| 44 | + _lightData.lightVariant = HdEmbree_UnknownLight(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +HdEmbree_Light::~HdEmbree_Light() = default; |
| 49 | + |
| 50 | +void |
| 51 | +HdEmbree_Light::Sync(HdSceneDelegate *sceneDelegate, |
| 52 | + HdRenderParam *renderParam, HdDirtyBits *dirtyBits) |
| 53 | +{ |
| 54 | + HD_TRACE_FUNCTION(); |
| 55 | + HF_MALLOC_TAG_FUNCTION(); |
| 56 | + |
| 57 | + HdEmbreeRenderParam *embreeRenderParam = |
| 58 | + static_cast<HdEmbreeRenderParam*>(renderParam); |
| 59 | + |
| 60 | + // calling this bumps the scene version and causes a re-render |
| 61 | + embreeRenderParam->AcquireSceneForEdit(); |
| 62 | + |
| 63 | + SdfPath const& id = GetId(); |
| 64 | + |
| 65 | + // Get _lightData's transform. We'll only consider the first time sample for now |
| 66 | + HdTimeSampleArray<GfMatrix4d, 1> xformSamples; |
| 67 | + sceneDelegate->SampleTransform(id, &xformSamples); |
| 68 | + _lightData.xformLightToWorld = GfMatrix4f(xformSamples.values[0]); |
| 69 | + _lightData.xformWorldToLight = _lightData.xformLightToWorld.GetInverse(); |
| 70 | + _lightData.normalXformLightToWorld = |
| 71 | + _lightData.xformWorldToLight.ExtractRotationMatrix().GetTranspose(); |
| 72 | + |
| 73 | + // Store luminance parameters |
| 74 | + _lightData.intensity = sceneDelegate->GetLightParamValue( |
| 75 | + id, HdLightTokens->intensity).GetWithDefault(1.0f); |
| 76 | + _lightData.exposure = sceneDelegate->GetLightParamValue( |
| 77 | + id, HdLightTokens->exposure).GetWithDefault(0.0f); |
| 78 | + _lightData.color = sceneDelegate->GetLightParamValue( |
| 79 | + id, HdLightTokens->color).GetWithDefault(GfVec3f{1.0f, 1.0f, 1.0f}); |
| 80 | + _lightData.normalize = sceneDelegate->GetLightParamValue( |
| 81 | + id, HdLightTokens->normalize).GetWithDefault(false); |
| 82 | + _lightData.colorTemperature = sceneDelegate->GetLightParamValue( |
| 83 | + id, HdLightTokens->colorTemperature).GetWithDefault(6500.0f); |
| 84 | + _lightData.enableColorTemperature = sceneDelegate->GetLightParamValue( |
| 85 | + id, HdLightTokens->enableColorTemperature).GetWithDefault(false); |
| 86 | + |
| 87 | + // Get visibility |
| 88 | + _lightData.visible = sceneDelegate->GetVisible(id); |
| 89 | + |
| 90 | + // Switch on the _lightData type and pull the relevant attributes from the scene |
| 91 | + // delegate |
| 92 | + std::visit([this, &id, &sceneDelegate](auto& typedLight) { |
| 93 | + using T = std::decay_t<decltype(typedLight)>; |
| 94 | + if constexpr (std::is_same_v<T, HdEmbree_UnknownLight>) { |
| 95 | + // Do nothing |
| 96 | + } else if constexpr (std::is_same_v<T, HdEmbree_Cylinder>) { |
| 97 | + typedLight = HdEmbree_Cylinder{ |
| 98 | + sceneDelegate->GetLightParamValue(id, HdLightTokens->radius) |
| 99 | + .GetWithDefault(0.5f), |
| 100 | + sceneDelegate->GetLightParamValue(id, HdLightTokens->length) |
| 101 | + .GetWithDefault(1.0f), |
| 102 | + }; |
| 103 | + } else if constexpr (std::is_same_v<T, HdEmbree_Disk>) { |
| 104 | + typedLight = HdEmbree_Disk{ |
| 105 | + sceneDelegate->GetLightParamValue(id, HdLightTokens->radius) |
| 106 | + .GetWithDefault(0.5f), |
| 107 | + }; |
| 108 | + } else if constexpr (std::is_same_v<T, HdEmbree_Rect>) { |
| 109 | + typedLight = HdEmbree_Rect{ |
| 110 | + sceneDelegate->GetLightParamValue(id, HdLightTokens->width) |
| 111 | + .Get<float>(), |
| 112 | + sceneDelegate->GetLightParamValue(id, HdLightTokens->height) |
| 113 | + .Get<float>(), |
| 114 | + }; |
| 115 | + } else if constexpr (std::is_same_v<T, HdEmbree_Sphere>) { |
| 116 | + typedLight = HdEmbree_Sphere{ |
| 117 | + sceneDelegate->GetLightParamValue(id, HdLightTokens->radius) |
| 118 | + .GetWithDefault(0.5f), |
| 119 | + }; |
| 120 | + } else { |
| 121 | + static_assert(false, "non-exhaustive _LightVariant visitor"); |
| 122 | + } |
| 123 | + }, _lightData.lightVariant); |
| 124 | + |
| 125 | + if (const auto value = sceneDelegate->GetLightParamValue( |
| 126 | + id, HdLightTokens->shapingFocus); |
| 127 | + value.IsHolding<float>()) { |
| 128 | + _lightData.shaping.focus = value.UncheckedGet<float>(); |
| 129 | + } |
| 130 | + |
| 131 | + if (const auto value = sceneDelegate->GetLightParamValue( |
| 132 | + id, HdLightTokens->shapingFocusTint); |
| 133 | + value.IsHolding<GfVec3f>()) { |
| 134 | + _lightData.shaping.focusTint = value.UncheckedGet<GfVec3f>(); |
| 135 | + } |
| 136 | + |
| 137 | + if (const auto value = sceneDelegate->GetLightParamValue( |
| 138 | + id, HdLightTokens->shapingConeAngle); |
| 139 | + value.IsHolding<float>()) { |
| 140 | + _lightData.shaping.coneAngle = value.UncheckedGet<float>(); |
| 141 | + } |
| 142 | + |
| 143 | + if (const auto value = sceneDelegate->GetLightParamValue( |
| 144 | + id, HdLightTokens->shapingConeSoftness); |
| 145 | + value.IsHolding<float>()) { |
| 146 | + _lightData.shaping.coneSoftness = value.UncheckedGet<float>(); |
| 147 | + } |
| 148 | + |
| 149 | + HdEmbreeRenderer *renderer = embreeRenderParam->GetRenderer(); |
| 150 | + renderer->AddLight(id, this); |
| 151 | + |
| 152 | + *dirtyBits &= ~HdLight::AllDirty; |
| 153 | +} |
| 154 | + |
| 155 | +HdDirtyBits |
| 156 | +HdEmbree_Light::GetInitialDirtyBitsMask() const |
| 157 | +{ |
| 158 | + return HdLight::AllDirty; |
| 159 | +} |
| 160 | + |
| 161 | +void |
| 162 | +HdEmbree_Light::Finalize(HdRenderParam *renderParam) |
| 163 | +{ |
| 164 | + auto* embreeParam = static_cast<HdEmbreeRenderParam*>(renderParam); |
| 165 | + |
| 166 | + // Remove from renderer's light map |
| 167 | + HdEmbreeRenderer *renderer = embreeParam->GetRenderer(); |
| 168 | + renderer->RemoveLight(GetId(), this); |
| 169 | +} |
| 170 | + |
| 171 | +PXR_NAMESPACE_CLOSE_SCOPE |
0 commit comments