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 support for lighting double-sided meshes (hdEmbree-UsdLux-PR08) #3195

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
173 changes: 173 additions & 0 deletions pxr/imaging/plugin/hdEmbree/light.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
//
// 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>

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.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>(),
};
} 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
109 changes: 109 additions & 0 deletions pxr/imaging/plugin/hdEmbree/light.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// 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_LIGHT_H
#define PXR_IMAGING_PLUGIN_HD_EMBREE_LIGHT_H

#include "pxr/base/gf/vec3f.h"
#include "pxr/base/gf/matrix3f.h"
#include "pxr/base/gf/matrix4f.h"
#include "pxr/imaging/hd/light.h"

#include <embree3/rtcore_common.h>
#include <embree3/rtcore_geometry.h>

#include <limits>
#include <variant>

PXR_NAMESPACE_OPEN_SCOPE

class HdEmbreeRenderer;

struct HdEmbree_UnknownLight
{};
struct HdEmbree_Cylinder
{
float radius;
float length;
};

struct HdEmbree_Disk
{
float radius;
};

struct HdEmbree_Rect
{
float width;
float height;
};

struct HdEmbree_Sphere
{
float radius;
};

using HdEmbree_LightVariant = std::variant<
HdEmbree_UnknownLight,
HdEmbree_Cylinder,
HdEmbree_Disk,
HdEmbree_Rect,
HdEmbree_Sphere>;

struct HdEmbree_Shaping
{
GfVec3f focusTint;
float focus = 0.0f;
float coneAngle = 180.0f;
float coneSoftness = 0.0f;
};

struct HdEmbree_LightData
{
GfMatrix4f xformLightToWorld;
GfMatrix3f normalXformLightToWorld;
GfMatrix4f xformWorldToLight;
GfVec3f color;
float intensity = 1.0f;
float exposure = 0.0f;
float colorTemperature = 6500.0f;
bool enableColorTemperature = false;
HdEmbree_LightVariant lightVariant;
bool normalize = false;
bool visible = true;
HdEmbree_Shaping shaping;
};

class HdEmbree_Light final : public HdLight
{
public:
HdEmbree_Light(SdfPath const& id, TfToken const& lightType);
~HdEmbree_Light();

/// Synchronizes state from the delegate to this object.
void Sync(HdSceneDelegate* sceneDelegate,
HdRenderParam* renderParam,
HdDirtyBits* dirtyBits) override;

/// Returns the minimal set of dirty bits to place in the
/// change tracker for use in the first sync of this prim.
/// Typically this would be all dirty bits.
HdDirtyBits GetInitialDirtyBitsMask() const override;

void Finalize(HdRenderParam *renderParam) override;

HdEmbree_LightData const& LightData() const {
return _lightData;
}

private:
HdEmbree_LightData _lightData;
};


PXR_NAMESPACE_CLOSE_SCOPE

#endif
5 changes: 5 additions & 0 deletions pxr/imaging/plugin/hdEmbree/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class HdEmbreeMesh final : public HdMesh {
/// embree state.
virtual void Finalize(HdRenderParam *renderParam) override;

bool EmbreeMeshIsDoubleSided() const
{
return _doubleSided;
}

protected:
// Initialize the given representation of this Rprim.
// This is called prior to syncing the prim, the first time the repr
Expand Down
2 changes: 1 addition & 1 deletion pxr/imaging/plugin/hdEmbree/renderBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class HdEmbreeRenderBuffer : public HdRenderBuffer
// For multisampled buffers: the input write buffer.
std::vector<uint8_t> _sampleBuffer;
// For multisampled buffers: the sample count buffer.
std::vector<uint8_t> _sampleCount;
std::vector<uint32_t> _sampleCount;

// The number of callers mapping this buffer.
std::atomic<int> _mappers;
Expand Down
Loading