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

Pr/hydra updates to pxr hd renderer #1774

Merged
merged 5 commits into from
Oct 21, 2021
Merged
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
77 changes: 70 additions & 7 deletions lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <mayaUsd/render/pxrUsdMayaGL/renderParams.h>
#include <mayaUsd/render/pxrUsdMayaGL/shapeAdapter.h>

#include <pxr/base/gf/camera.h>
#include <pxr/base/gf/matrix4d.h>
#include <pxr/base/gf/vec2f.h>
#include <pxr/base/gf/vec4d.h>
Expand Down Expand Up @@ -137,9 +138,36 @@ PxrMayaHdSceneDelegate::PxrMayaHdSceneDelegate(

renderIndex->InsertSprim(HdPrimTypeTokens->camera, this, _cameraId);
_ValueCache& cache = _valueCacheMap[_cameraId];
cache[HdTokens->transform] = VtValue(GfMatrix4d(1.0));
cache[HdCameraTokens->windowPolicy] = VtValue(CameraUtilFit);
cache[HdCameraTokens->clipPlanes] = VtValue(std::vector<GfVec4d>());
#if PXR_VERSION >= 2102
cache[HdCameraTokens->projection] = VtValue(HdCamera::Orthographic);
cache[HdCameraTokens->horizontalAperture] = VtValue(2.0f * float(GfCamera::APERTURE_UNIT));
cache[HdCameraTokens->verticalAperture] = VtValue(2.0f * float(GfCamera::APERTURE_UNIT));
cache[HdCameraTokens->horizontalApertureOffset] = VtValue(0.0f);
cache[HdCameraTokens->verticalApertureOffset] = VtValue(0.0f);
cache[HdCameraTokens->focalLength] = VtValue(5.0f * float(GfCamera::FOCAL_LENGTH_UNIT));
cache[HdCameraTokens->clippingRange] = VtValue(GfRange1f(-1.0f, 1.0f));
#else
cache[HdCameraTokens->worldToViewMatrix] = VtValue(GfMatrix4d(1.0));
cache[HdCameraTokens->projectionMatrix] = VtValue(GfMatrix4d(1.0));
cache[HdCameraTokens->windowPolicy] = VtValue(CameraUtilFit);

cache[HdCameraTokens->horizontalAperture] = VtValue();
cache[HdCameraTokens->verticalAperture] = VtValue();
cache[HdCameraTokens->horizontalApertureOffset] = VtValue();
cache[HdCameraTokens->verticalApertureOffset] = VtValue();
cache[HdCameraTokens->focalLength] = VtValue();
cache[HdCameraTokens->clippingRange] = VtValue();
#endif

cache[HdCameraTokens->fStop] = VtValue();
cache[HdCameraTokens->focusDistance] = VtValue();
cache[HdCameraTokens->shutterOpen] = VtValue();
cache[HdCameraTokens->shutterClose] = VtValue();
#if PXR_VERSION >= 2011
cache[HdCameraTokens->exposure] = VtValue();
#endif
}

// Simple lighting task.
Expand Down Expand Up @@ -220,36 +248,71 @@ PxrMayaHdSceneDelegate::GetCameraParamValue(SdfPath const& cameraId, TfToken con
return Get(cameraId, paramName);
}

/*virtual*/
GfMatrix4d PxrMayaHdSceneDelegate::GetTransform(SdfPath const& id)
{
static const GfMatrix4d fallback(1.0);
return Get(id, HdTokens->transform).GetWithDefault<GfMatrix4d>(fallback);
}

TfTokenVector PxrMayaHdSceneDelegate::GetTaskRenderTags(SdfPath const& taskId)
{
VtValue value = Get(taskId, HdTokens->renderTags);

return value.Get<TfTokenVector>();
}

#if PXR_VERSION >= 2102
static HdCamera::Projection _ToHd(const GfCamera::Projection projection)
{
switch (projection) {
case GfCamera::Perspective: return HdCamera::Perspective;
case GfCamera::Orthographic: return HdCamera::Orthographic;
}
TF_CODING_ERROR("Bad GfCamera::Projection value");
return HdCamera::Perspective;
}
#endif

void PxrMayaHdSceneDelegate::SetCameraState(
const GfMatrix4d& worldToViewMatrix,
const GfMatrix4d& projectionMatrix,
const GfVec4d& viewport)
{
// cache the camera matrices
_ValueCache& cache = _valueCacheMap[_cameraId];
cache[HdCameraTokens->worldToViewMatrix] = VtValue(worldToViewMatrix);
cache[HdCameraTokens->projectionMatrix] = VtValue(projectionMatrix);
cache[HdCameraTokens->windowPolicy] = VtValue(CameraUtilFit);
cache[HdCameraTokens->clipPlanes] = VtValue(std::vector<GfVec4d>());

// Provide other keys so that pulling on those in HdCamera::Sync doesn't
// trigger coding errors.
#if PXR_VERSION >= 2102
cache[HdCameraTokens->projection] = VtValue();
#endif
GfCamera cam;
cam.SetFromViewAndProjectionMatrix(worldToViewMatrix, projectionMatrix);
cache[HdTokens->transform] = VtValue(cam.GetTransform());

cache[HdCameraTokens->projection] = VtValue(_ToHd(cam.GetProjection()));
cache[HdCameraTokens->horizontalAperture]
= VtValue(cam.GetHorizontalAperture() * float(GfCamera::APERTURE_UNIT));
cache[HdCameraTokens->verticalAperture]
= VtValue(cam.GetVerticalAperture() * float(GfCamera::APERTURE_UNIT));
cache[HdCameraTokens->horizontalApertureOffset]
= VtValue(cam.GetHorizontalApertureOffset() * float(GfCamera::APERTURE_UNIT));
cache[HdCameraTokens->verticalApertureOffset]
= VtValue(cam.GetVerticalApertureOffset() * float(GfCamera::APERTURE_UNIT));
cache[HdCameraTokens->focalLength]
= VtValue(cam.GetFocalLength() * float(GfCamera::FOCAL_LENGTH_UNIT));
cache[HdCameraTokens->clippingRange] = VtValue(cam.GetClippingRange());
#else
cache[HdTokens->transform] = VtValue(worldToViewMatrix.GetInverse());
cache[HdCameraTokens->worldToViewMatrix] = VtValue(worldToViewMatrix);
cache[HdCameraTokens->projectionMatrix] = VtValue(projectionMatrix);
cache[HdCameraTokens->horizontalAperture] = VtValue();
cache[HdCameraTokens->verticalAperture] = VtValue();
cache[HdCameraTokens->horizontalApertureOffset] = VtValue();
cache[HdCameraTokens->verticalApertureOffset] = VtValue();
cache[HdCameraTokens->focalLength] = VtValue();
cache[HdCameraTokens->clippingRange] = VtValue();
#endif

cache[HdCameraTokens->clipPlanes] = VtValue();
cache[HdCameraTokens->fStop] = VtValue();
cache[HdCameraTokens->focusDistance] = VtValue();
Expand Down
3 changes: 3 additions & 0 deletions lib/mayaUsd/render/pxrUsdMayaGL/sceneDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class PxrMayaHdSceneDelegate : public HdSceneDelegate
MAYAUSD_CORE_PUBLIC
VtValue GetCameraParamValue(SdfPath const& cameraId, TfToken const& paramName) override;

MAYAUSD_CORE_PUBLIC
GfMatrix4d GetTransform(SdfPath const& id) override;

MAYAUSD_CORE_PUBLIC
TfTokenVector GetTaskRenderTags(SdfPath const& taskId) override;

Expand Down
40 changes: 36 additions & 4 deletions lib/usd/hdMaya/adapters/cameraAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ void HdMayaCameraAdapter::Populate()
void HdMayaCameraAdapter::MarkDirty(HdDirtyBits dirtyBits)
{
if (_isPopulated && dirtyBits != 0) {
#if PXR_VERSION < 2102
if (dirtyBits & HdChangeTracker::DirtyTransform) {
dirtyBits |= HdCamera::DirtyViewMatrix;
}
#endif
dirtyBits = dirtyBits & HdCamera::AllDirty;
GetDelegate()->GetChangeTracker().MarkSprimDirty(GetID(), dirtyBits);
}
Expand Down Expand Up @@ -104,7 +106,11 @@ void HdMayaCameraAdapter::CreateCallbacks()
dag,
+[](MObject& transformNode, MDagMessage::MatrixModifiedFlags& modified, void* clientData) {
auto* adapter = reinterpret_cast<HdMayaCameraAdapter*>(clientData);
#if PXR_VERSION < 2102
adapter->MarkDirty(HdCamera::DirtyViewMatrix);
#else
adapter->MarkDirty(HdCamera::DirtyTransform);
#endif
adapter->InvalidateTransform();
},
reinterpret_cast<void*>(this),
Expand Down Expand Up @@ -176,6 +182,8 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName)
aspectRatio, apertureX, apertureY, offsetX, offsetY, true, false, true);
};

#if PXR_VERSION < 2102

auto projectionMatrix
= [&](const MFnCamera& camera, bool isOrtho, const GfVec4d* viewport) -> GfMatrix4d {
double left, right, bottom, top, cameraNear = camera.nearClippingPlane(),
Expand Down Expand Up @@ -266,6 +274,8 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName)
0);
};

#endif

auto hadError = [&](MStatus& status) -> bool {
if (ARCH_LIKELY(status))
return false;
Expand All @@ -285,6 +295,7 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName)
return {};
}

#if PXR_VERSION < 2102
if (paramName == HdCameraTokens->projectionMatrix) {
const auto projMatrix = projectionMatrix(camera, isOrtho, _viewport.get());
if (hadError(status))
Expand All @@ -294,6 +305,8 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName)
if (paramName == HdCameraTokens->worldToViewMatrix) {
return VtValue(GetTransform().GetInverse());
}
#endif

if (paramName == HdCameraTokens->shutterOpen) {
// No motion samples, instantaneous shutter
if (!GetDelegate()->GetParams().motionSamplesEnabled())
Expand Down Expand Up @@ -324,10 +337,19 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName)
return VtValue(float(focusDistance * mayaInchToHydraCentimeter));
}
if (paramName == HdCameraTokens->focalLength) {
GfMatrix4d glProjMatrix = projectionMatrix(camera, isOrtho, _viewport.get());
const int index
= convertFit(camera) == CameraUtilConformWindowPolicy::CameraUtilMatchVertically;
const auto focalLen = glProjMatrix[index][index];
const double aspectRatio = _viewport
? (((*_viewport)[2] - (*_viewport)[0]) / ((*_viewport)[3] - (*_viewport)[1]))
: camera.aspectRatio();

double left, right, bottom, top;
status = camera.getViewingFrustum(aspectRatio, left, right, bottom, top, true, false, true);

const double cameraNear = camera.nearClippingPlane();

const double focalLen
= (convertFit(camera) == CameraUtilConformWindowPolicy::CameraUtilMatchVertically)
? (2.0 * cameraNear) / (top - bottom)
: (2.0 * cameraNear) / (right - left);
return VtValue(float(focalLen * mayaFocaLenToHydra));
}
if (paramName == HdCameraTokens->fStop) {
Expand Down Expand Up @@ -373,6 +395,16 @@ VtValue HdMayaCameraAdapter::GetCameraParamValue(const TfToken& paramName)
return {};
return VtValue(windowPolicy);
}
#if PXR_VERSION >= 2102
if (paramName == HdCameraTokens->projection) {
if (isOrtho) {
return VtValue(HdCamera::Orthographic);
} else {
return VtValue(HdCamera::Perspective);
}
}

#endif

return {};
}
Expand Down