Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hdEmbree] add light texture support (hdEmbree-UsdLux-PR10) #3187

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
2 changes: 2 additions & 0 deletions build_scripts/build_usd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2610,6 +2610,7 @@ def _JoinVersion(v):
OpenVDB support: {enableOpenVDB}
OpenImageIO support: {buildOIIO}
OpenColorIO support: {buildOCIO}
Embree support: {buildEmbree}
PRMan support: {buildPrman}
UsdImaging {buildUsdImaging}
usdview: {buildUsdview}
Expand Down Expand Up @@ -2673,6 +2674,7 @@ def FormatBuildArguments(buildArgs):
enableOpenVDB=("On" if context.enableOpenVDB else "Off"),
buildOIIO=("On" if context.buildOIIO else "Off"),
buildOCIO=("On" if context.buildOCIO else "Off"),
buildEmbree=("On" if context.buildEmbree else "Off"),
buildPrman=("On" if context.buildPrman else "Off"),
buildUsdImaging=("On" if context.buildUsdImaging else "Off"),
buildUsdview=("On" if context.buildUsdview else "Off"),
Expand Down
4 changes: 2 additions & 2 deletions pxr/base/work/threadLimits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ WorkGetPhysicalConcurrencyLimit()
#endif
}

// This function always returns an actual thread count >= 1.
// This function always returns either 0 (meaning "no change") or >= 1
static unsigned
Work_NormalizeThreadCount(const int n)
{
// Zero means "no change", and n >= 1 means exactly n threads, so simply
// pass those values through unchanged.
// For negative integers, subtract the absolute value from the total number
// of available cores (denoting all but n cores). If n == number of cores,
// of available cores (denoting all but n cores). If |n| >= number of cores,
// clamp to 1 to set single-threaded mode.
return n >= 0 ? n : std::max<int>(1, n + WorkGetPhysicalConcurrencyLimit());
}
Expand Down
2 changes: 2 additions & 0 deletions pxr/imaging/plugin/hdEmbree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pxr_plugin(hdEmbree
PUBLIC_CLASSES
config
instancer
light
mesh
meshSamplers
renderBuffer
Expand All @@ -45,6 +46,7 @@ pxr_plugin(hdEmbree
renderParam.h

PRIVATE_CLASSES
debugCodes
implicitSurfaceSceneIndexPlugin

RESOURCE_FILES
Expand Down
20 changes: 20 additions & 0 deletions pxr/imaging/plugin/hdEmbree/debugCodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright 2024 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include "pxr/pxr.h"
#include "pxr/imaging/plugin/hdEmbree/debugCodes.h"

#include "pxr/base/tf/debug.h"
#include "pxr/base/tf/registryManager.h"

PXR_NAMESPACE_OPEN_SCOPE

TF_REGISTRY_FUNCTION(TfDebug)
{
TF_DEBUG_ENVIRONMENT_SYMBOL(HDEMBREE_LIGHT_CREATE, "Creation of HdEmbree lights");
}

PXR_NAMESPACE_CLOSE_SCOPE
21 changes: 21 additions & 0 deletions pxr/imaging/plugin/hdEmbree/debugCodes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright 2024 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#ifndef PXR_IMAGING_PLUGIN_HD_EMBREE_DEBUG_CODES_H
#define PXR_IMAGING_PLUGIN_HD_EMBREE_DEBUG_CODES_H

#include "pxr/pxr.h"
#include "pxr/base/tf/debug.h"

PXR_NAMESPACE_OPEN_SCOPE

TF_DEBUG_CODES(
HDEMBREE_LIGHT_CREATE
);

PXR_NAMESPACE_CLOSE_SCOPE

#endif // PXR_IMAGING_PLUGIN_HD_EMBREE_DEBUG_CODES_H
231 changes: 231 additions & 0 deletions pxr/imaging/plugin/hdEmbree/light.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
//
// Copyright 2024 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include "pxr/imaging/plugin/hdEmbree/light.h"

#include "light.h"
#include "pxr/imaging/plugin/hdEmbree/debugCodes.h"
#include "pxr/imaging/plugin/hdEmbree/renderParam.h"
#include "pxr/imaging/plugin/hdEmbree/renderer.h"

#include "pxr/imaging/hd/sceneDelegate.h"
#include "pxr/imaging/hio/image.h"

#include <embree3/rtcore_buffer.h>
#include <embree3/rtcore_scene.h>

#include <fstream>
#include <sstream>
#include <vector>

namespace {

PXR_NAMESPACE_USING_DIRECTIVE

HdEmbree_LightTexture
_LoadLightTexture(std::string const& path)
{
if (path.empty()) {
return HdEmbree_LightTexture();
}

HioImageSharedPtr img = HioImage::OpenForReading(path);
if (!img) {
return HdEmbree_LightTexture();
}

int width = img->GetWidth();
int height = img->GetHeight();

std::vector<GfVec3f> pixels(width * height * 3.0f);

HioImage::StorageSpec storage;
storage.width = width;
storage.height = height;
storage.depth = 1;
storage.format = HioFormatFloat32Vec3;
storage.data = &pixels.front();

if (img->Read(storage)) {
return {std::move(pixels), width, height};
}
TF_WARN("Could not read image %s", path.c_str());
return { std::vector<GfVec3f>(), 0, 0 };
}


void
_SyncLightTexture(const SdfPath& id, HdEmbree_LightData& light, HdSceneDelegate *sceneDelegate)
{
std::string path;
if (VtValue textureValue = sceneDelegate->GetLightParamValue(
id, HdLightTokens->textureFile);
textureValue.IsHolding<SdfAssetPath>()) {
SdfAssetPath texturePath =
textureValue.UncheckedGet<SdfAssetPath>();
path = texturePath.GetResolvedPath();
if (path.empty()) {
path = texturePath.GetAssetPath();
}
}
light.texture = _LoadLightTexture(path);
}


} // anonymous namespace
PXR_NAMESPACE_OPEN_SCOPE

HdEmbree_Light::HdEmbree_Light(SdfPath const& id, TfToken const& lightType)
: HdLight(id) {
if (id.IsEmpty()) {
return;
}

TF_DEBUG(HDEMBREE_LIGHT_CREATE).Msg("Creating light %s: %s\n", id.GetText(), lightType.GetText());

// Set the variant to the right type - Sync will fill rest of data
if (lightType == HdSprimTypeTokens->cylinderLight) {
_lightData.lightVariant = HdEmbree_Cylinder();
} else if (lightType == HdSprimTypeTokens->diskLight) {
_lightData.lightVariant = HdEmbree_Disk();
} else if (lightType == HdSprimTypeTokens->rectLight) {
// Get shape parameters
_lightData.lightVariant = HdEmbree_Rect();
} else if (lightType == HdSprimTypeTokens->sphereLight) {
_lightData.lightVariant = HdEmbree_Sphere();
} else {
TF_WARN("HdEmbree - Unrecognized light type: %s", lightType.GetText());
_lightData.lightVariant = HdEmbree_UnknownLight();
}
}

HdEmbree_Light::~HdEmbree_Light() = default;

void
HdEmbree_Light::Sync(HdSceneDelegate *sceneDelegate,
HdRenderParam *renderParam, HdDirtyBits *dirtyBits)
{
HD_TRACE_FUNCTION();
HF_MALLOC_TAG_FUNCTION();

HdEmbreeRenderParam *embreeRenderParam =
static_cast<HdEmbreeRenderParam*>(renderParam);

// calling this bumps the scene version and causes a re-render
embreeRenderParam->AcquireSceneForEdit();

SdfPath const& id = GetId();

// Get _lightData's transform. We'll only consider the first time sample for now
HdTimeSampleArray<GfMatrix4d, 1> xformSamples;
sceneDelegate->SampleTransform(id, &xformSamples);
_lightData.xformLightToWorld = GfMatrix4f(xformSamples.values[0]);
_lightData.xformWorldToLight = _lightData.xformLightToWorld.GetInverse();
_lightData.normalXformLightToWorld =
_lightData.xformWorldToLight.ExtractRotationMatrix().GetTranspose();

// Store luminance parameters
_lightData.intensity = sceneDelegate->GetLightParamValue(
id, HdLightTokens->intensity).GetWithDefault(1.0f);
_lightData.diffuse = sceneDelegate->GetLightParamValue(
id, HdLightTokens->diffuse).GetWithDefault(1.0f);
_lightData.exposure = sceneDelegate->GetLightParamValue(
id, HdLightTokens->exposure).GetWithDefault(0.0f);
_lightData.color = sceneDelegate->GetLightParamValue(
id, HdLightTokens->color).GetWithDefault(GfVec3f{1.0f, 1.0f, 1.0f});
_lightData.normalize = sceneDelegate->GetLightParamValue(
id, HdLightTokens->normalize).GetWithDefault(false);
_lightData.colorTemperature = sceneDelegate->GetLightParamValue(
id, HdLightTokens->colorTemperature).GetWithDefault(6500.0f);
_lightData.enableColorTemperature = sceneDelegate->GetLightParamValue(
id, HdLightTokens->enableColorTemperature).GetWithDefault(false);

// Get visibility
_lightData.visible = sceneDelegate->GetVisible(id);

// Switch on the _lightData type and pull the relevant attributes from the scene
// delegate
std::visit([this, &id, &sceneDelegate](auto& typedLight) {
using T = std::decay_t<decltype(typedLight)>;
if constexpr (std::is_same_v<T, HdEmbree_UnknownLight>) {
// Do nothing
} else if constexpr (std::is_same_v<T, HdEmbree_Cylinder>) {
typedLight = HdEmbree_Cylinder{
sceneDelegate->GetLightParamValue(id, HdLightTokens->radius)
.GetWithDefault(0.5f),
sceneDelegate->GetLightParamValue(id, HdLightTokens->length)
.GetWithDefault(1.0f),
};
} else if constexpr (std::is_same_v<T, HdEmbree_Disk>) {
typedLight = HdEmbree_Disk{
sceneDelegate->GetLightParamValue(id, HdLightTokens->radius)
.GetWithDefault(0.5f),
};
} else if constexpr (std::is_same_v<T, HdEmbree_Rect>) {
typedLight = HdEmbree_Rect{
sceneDelegate->GetLightParamValue(id, HdLightTokens->width)
.Get<float>(),
sceneDelegate->GetLightParamValue(id, HdLightTokens->height)
.Get<float>(),
};
_SyncLightTexture(id, _lightData, sceneDelegate);
} else if constexpr (std::is_same_v<T, HdEmbree_Sphere>) {
typedLight = HdEmbree_Sphere{
sceneDelegate->GetLightParamValue(id, HdLightTokens->radius)
.GetWithDefault(0.5f),
};
} else {
static_assert(false, "non-exhaustive _LightVariant visitor");
}
}, _lightData.lightVariant);

if (const auto value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->shapingFocus);
value.IsHolding<float>()) {
_lightData.shaping.focus = value.UncheckedGet<float>();
}

if (const auto value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->shapingFocusTint);
value.IsHolding<GfVec3f>()) {
_lightData.shaping.focusTint = value.UncheckedGet<GfVec3f>();
}

if (const auto value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->shapingConeAngle);
value.IsHolding<float>()) {
_lightData.shaping.coneAngle = value.UncheckedGet<float>();
}

if (const auto value = sceneDelegate->GetLightParamValue(
id, HdLightTokens->shapingConeSoftness);
value.IsHolding<float>()) {
_lightData.shaping.coneSoftness = value.UncheckedGet<float>();
}

HdEmbreeRenderer *renderer = embreeRenderParam->GetRenderer();
renderer->AddLight(id, this);

*dirtyBits &= ~HdLight::AllDirty;
}

HdDirtyBits
HdEmbree_Light::GetInitialDirtyBitsMask() const
{
return HdLight::AllDirty;
}

void
HdEmbree_Light::Finalize(HdRenderParam *renderParam)
{
auto* embreeParam = static_cast<HdEmbreeRenderParam*>(renderParam);

// Remove from renderer's light map
HdEmbreeRenderer *renderer = embreeParam->GetRenderer();
renderer->RemoveLight(GetId(), this);
}

PXR_NAMESPACE_CLOSE_SCOPE
Loading