Skip to content

Commit

Permalink
Translate to a new universe and use a new render session (#783) (#784)
Browse files Browse the repository at this point in the history
* Translate to a new universe and use a new render session (#783)
* Fixing build issues on Linux, moving the multiple render sessions macro to hdarnold.h and running clang-format.

Co-authored-by: Julian Hodgson <julian.hodgson@autodesk.com>
Co-authored-by: Pal Mezei <pal.mezei@autodesk.com>
  • Loading branch information
3 people authored Jun 21, 2021
1 parent 2980c1d commit 9960333
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 6 deletions.
4 changes: 4 additions & 0 deletions render_delegate/hdarnold.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
/// Not blitting to a hardware buffer anymore, following the example of HdEmbree.
#define USD_DO_NOT_BLIT
#endif

#if AI_VERSION_NUMBER > 60201
#define AI_MULTIPLE_RENDER_SESSIONS
#endif
80 changes: 77 additions & 3 deletions render_delegate/render_delegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ HdArnoldRenderDelegate::HdArnoldRenderDelegate(HdArnoldRenderContext context) :
_nativeRprimParams.emplace(AiNodeEntryGetNameAtString(nodeEntry), std::move(paramList));
AiParamIteratorDestroy(paramIter);
}
AiRenderSetHintStr(str::render_context, _context == HdArnoldRenderContext::Hydra ? str::hydra : str::husk);
std::lock_guard<std::mutex> guard(_mutexResourceRegistry);
if (_counterResourceRegistry.fetch_add(1) == 0) {
_resourceRegistry.reset(new HdResourceRegistry());
Expand All @@ -388,13 +387,23 @@ HdArnoldRenderDelegate::HdArnoldRenderDelegate(HdArnoldRenderContext context) :
}
hdArnoldInstallNodes();

#ifdef AI_MULTIPLE_RENDER_SESSIONS
_universe = AiUniverse();
_render_session = AiRenderSession(_universe);
#else
_universe = nullptr;

#endif
_options = AiUniverseGetOptions(_universe);
for (const auto& o : _GetSupportedRenderSettings()) {
_SetRenderSetting(o.first, o.second.defaultValue);
}

#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSetHintStr(
_render_session, str::render_context, _context == HdArnoldRenderContext::Hydra ? str::hydra : str::husk);
#else
AiRenderSetHintStr(str::render_context, _context == HdArnoldRenderContext::Hydra ? str::hydra : str::husk);
#endif
_fallbackShader = AiNode(_universe, str::utility);
AiNodeSetStr(_fallbackShader, str::name, AtString(TfStringPrintf("fallbackShader_%p", _fallbackShader).c_str()));
AiNodeSetStr(_fallbackShader, str::shade_mode, str::ambocc);
Expand All @@ -411,14 +420,26 @@ HdArnoldRenderDelegate::HdArnoldRenderDelegate(HdArnoldRenderContext context) :
AiNodeSetStr(
_fallbackVolumeShader, str::name, AtString(TfStringPrintf("fallbackVolume_%p", _fallbackVolumeShader).c_str()));

#ifdef AI_MULTIPLE_RENDER_SESSIONS
_renderParam.reset(new HdArnoldRenderParam(this));
#else
_renderParam.reset(new HdArnoldRenderParam());
#endif

// We need access to both beauty and P at the same time.
if (_context == HdArnoldRenderContext::Husk) {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSetHintBool(_render_session, str::progressive, false);
#else
AiRenderSetHintBool(str::progressive, false);
#endif
AiNodeSetBool(_options, str::enable_progressive_render, false);
} else {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSetHintBool(_render_session, str::progressive_show_all_outputs, true);
#else
AiRenderSetHintBool(str::progressive_show_all_outputs, true);
#endif
}
}

Expand All @@ -429,6 +450,9 @@ HdArnoldRenderDelegate::~HdArnoldRenderDelegate()
_resourceRegistry.reset();
}
_renderParam->Interrupt();
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSessionDestroy(_render_session);
#endif
hdArnoldUninstallNodes();
AiUniverseDestroy(_universe);
AiEnd();
Expand Down Expand Up @@ -478,30 +502,52 @@ void HdArnoldRenderDelegate::_SetRenderSetting(const TfToken& _key, const VtValu
} else if (key == str::t_enable_progressive_render) {
if (_context != HdArnoldRenderContext::Husk) {
_CheckForBoolValue(value, [&](const bool b) {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSetHintBool(_render_session, str::progressive, b);
#else
AiRenderSetHintBool(str::progressive, b);
#endif
AiNodeSetBool(_options, str::enable_progressive_render, b);
});
}
} else if (key == str::t_progressive_min_AA_samples) {
if (_context != HdArnoldRenderContext::Husk) {
_CheckForIntValue(value, [&](const int i) { AiRenderSetHintInt(str::progressive_min_AA_samples, i); });
_CheckForIntValue(value, [&](const int i) {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSetHintInt(_render_session, str::progressive_min_AA_samples, i);
#else
AiRenderSetHintInt(str::progressive_min_AA_samples, i);
#endif
});
}
} else if (key == str::t_interactive_target_fps) {
if (_context != HdArnoldRenderContext::Husk) {
if (value.IsHolding<float>()) {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSetHintFlt(_render_session, str::interactive_target_fps, value.UncheckedGet<float>());
#else
AiRenderSetHintFlt(str::interactive_target_fps, value.UncheckedGet<float>());
#endif
}
}
} else if (key == str::t_interactive_target_fps_min) {
if (_context != HdArnoldRenderContext::Husk) {
if (value.IsHolding<float>()) {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSetHintFlt(_render_session, str::interactive_target_fps_min, value.UncheckedGet<float>());
#else
AiRenderSetHintFlt(str::interactive_target_fps_min, value.UncheckedGet<float>());
#endif
}
}
} else if (key == str::t_interactive_fps_min) {
if (_context != HdArnoldRenderContext::Husk) {
if (value.IsHolding<float>()) {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderSetHintFlt(_render_session, str::interactive_fps_min, value.UncheckedGet<float>());
#else
AiRenderSetHintFlt(str::interactive_fps_min, value.UncheckedGet<float>());
#endif
}
}
} else if (key == str::t_profile_file) {
Expand Down Expand Up @@ -630,27 +676,47 @@ VtValue HdArnoldRenderDelegate::GetRenderSetting(const TfToken& _key) const
return VtValue(AiNodeGetStr(_options, str::render_device) == str::GPU);
} else if (key == str::t_enable_progressive_render) {
bool v = true;
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderGetHintBool(_render_session, str::progressive, v);
#else
AiRenderGetHintBool(str::progressive, v);
#endif
return VtValue(v);
} else if (key == str::t_progressive_min_AA_samples) {
int v = -4;
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderGetHintInt(_render_session, str::progressive_min_AA_samples, v);
#else
AiRenderGetHintInt(str::progressive_min_AA_samples, v);
#endif
return VtValue(v);
} else if (key == str::t_log_verbosity) {
return VtValue(_GetLogVerbosityFromFlags(_verbosityLogFlags));
} else if (key == str::t_log_file) {
return VtValue(_logFile);
} else if (key == str::t_interactive_target_fps) {
float v = 1.0f;
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderGetHintFlt(_render_session, str::interactive_target_fps, v);
#else
AiRenderGetHintFlt(str::interactive_target_fps, v);
#endif
return VtValue(v);
} else if (key == str::t_interactive_target_fps_min) {
float v = 1.0f;
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderGetHintFlt(_render_session, str::interactive_target_fps_min, v);
#else
AiRenderGetHintFlt(str::interactive_target_fps_min, v);
#endif
return VtValue(v);
} else if (key == str::t_interactive_fps_min) {
float v = 1.0f;
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderGetHintFlt(_render_session, str::interactive_fps_min, v);
#else
AiRenderGetHintFlt(str::interactive_fps_min, v);
#endif
return VtValue(v);
} else if (key == str::t_profile_file) {
return VtValue(std::string(AiProfileGetFileName().c_str()));
Expand Down Expand Up @@ -686,7 +752,11 @@ VtDictionary HdArnoldRenderDelegate::GetRenderStats() const
VtDictionary stats;

float total_progress = 100.0f;
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderGetHintFlt(_render_session, str::total_progress, total_progress);
#else
AiRenderGetHintFlt(str::total_progress, total_progress);
#endif
stats[_tokens->percentDone] = total_progress;
return stats;
}
Expand Down Expand Up @@ -889,6 +959,10 @@ AtString HdArnoldRenderDelegate::GetLocalNodeName(const AtString& name) const

AtUniverse* HdArnoldRenderDelegate::GetUniverse() const { return _universe; }

#ifdef AI_MULTIPLE_RENDER_SESSIONS
AtRenderSession* HdArnoldRenderDelegate::GetRenderSession() const { return _render_session; }
#endif

AtNode* HdArnoldRenderDelegate::GetOptions() const { return _options; }

AtNode* HdArnoldRenderDelegate::GetFallbackShader() const { return _fallbackShader; }
Expand Down
14 changes: 12 additions & 2 deletions render_delegate/render_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ class HdArnoldRenderDelegate final : public HdRenderDelegate {
/// @return Pointer to the Arnold Universe used by the Render Delegate.
HDARNOLD_API
AtUniverse* GetUniverse() const;
#ifdef AI_MULTIPLE_RENDER_SESSIONS
/// Gets the active Arnold Render Session.
///
/// @return Pointer to the Render Session used by the Render Delegate.
HDARNOLD_API
AtRenderSession* GetRenderSession() const;
#endif
/// Gets the Arnold Options node.
///
/// @return Pointer to the Arnold Options Node.
Expand Down Expand Up @@ -452,8 +459,11 @@ class HdArnoldRenderDelegate final : public HdRenderDelegate {
/// This is shared with all the primitives, so they can control the flow of
/// rendering.
std::unique_ptr<HdArnoldRenderParam> _renderParam;
SdfPath _id; ///< Path of the Render Delegate.
AtUniverse* _universe; ///< Universe used by the Render Delegate.
SdfPath _id; ///< Path of the Render Delegate.
AtUniverse* _universe; ///< Universe used by the Render Delegate.
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AtRenderSession* _render_session; ///< Render session used by the Render Delegate.
#endif
AtNode* _options; ///< Pointer to the Arnold Options Node.
AtNode* _fallbackShader; ///< Pointer to the fallback Arnold Shader.
AtNode* _fallbackVolumeShader; ///< Pointer to the fallback Arnold Volume Shader.
Expand Down
37 changes: 37 additions & 0 deletions render_delegate/render_param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "render_param.h"
#include "render_delegate.h"

#include <ai.h>

PXR_NAMESPACE_OPEN_SCOPE

#ifdef AI_MULTIPLE_RENDER_SESSIONS
HdArnoldRenderParam::HdArnoldRenderParam(HdArnoldRenderDelegate* delegate) : _delegate(delegate)
#else
HdArnoldRenderParam::HdArnoldRenderParam()
#endif
{
_needsRestart.store(false, std::memory_order::memory_order_release);
_aborted.store(false, std::memory_order::memory_order_release);
Expand All @@ -45,15 +50,23 @@ HdArnoldRenderParam::Status HdArnoldRenderParam::Render()
return Status::Aborted;
}

#ifdef AI_MULTIPLE_RENDER_SESSIONS
const auto status = AiRenderGetStatus(_delegate->GetRenderSession());
#else
const auto status = AiRenderGetStatus();
#endif
if (status == AI_RENDER_STATUS_FINISHED) {
// If render restart is true, it means the Render Delegate received an update after rendering has finished
// and AiRenderInterrupt does not change the status anymore.
// For the atomic operations we are using a release-acquire model.
const auto needsRestart = _needsRestart.exchange(false, std::memory_order_acq_rel);
if (needsRestart) {
_paused.store(false, std::memory_order_release);
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderRestart(_delegate->GetRenderSession());
#else
AiRenderRestart();
#endif
return Status::Converging;
}
return Status::Converged;
Expand All @@ -64,9 +77,17 @@ HdArnoldRenderParam::Status HdArnoldRenderParam::Render()
const auto needsRestart = _needsRestart.exchange(false, std::memory_order_acq_rel);
if (needsRestart) {
_paused.store(false, std::memory_order_release);
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderRestart(_delegate->GetRenderSession());
#else
AiRenderRestart();
#endif
} else if (!_paused.load(std::memory_order_acquire)) {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderResume(_delegate->GetRenderSession());
#else
AiRenderResume();
#endif
}
return Status::Converging;
}
Expand All @@ -79,7 +100,11 @@ HdArnoldRenderParam::Status HdArnoldRenderParam::Render()
if (status == AI_RENDER_STATUS_FAILED) {
_aborted.store(true, std::memory_order_release);
_paused.store(false, std::memory_order_release);
#ifdef AI_MULTIPLE_RENDER_SESSIONS
const auto errorCode = AiRenderEnd(_delegate->GetRenderSession());
#else
const auto errorCode = AiRenderEnd();
#endif
if (errorCode == AI_ABORT) {
TF_WARN("[arnold-usd] Render was aborted.");
} else if (errorCode == AI_ERROR_NO_CAMERA) {
Expand All @@ -102,15 +127,27 @@ HdArnoldRenderParam::Status HdArnoldRenderParam::Render()
return Status::Aborted;
}
_paused.store(false, std::memory_order_release);
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderBegin(_delegate->GetRenderSession());
#else
AiRenderBegin();
#endif
return Status::Converging;
}

void HdArnoldRenderParam::Interrupt(bool needsRestart, bool clearStatus)
{
#ifdef AI_MULTIPLE_RENDER_SESSIONS
const auto status = AiRenderGetStatus(_delegate->GetRenderSession());
#else
const auto status = AiRenderGetStatus();
#endif
if (status != AI_RENDER_STATUS_NOT_STARTED) {
#ifdef AI_MULTIPLE_RENDER_SESSIONS
AiRenderInterrupt(_delegate->GetRenderSession(), AI_BLOCKING);
#else
AiRenderInterrupt(AI_BLOCKING);
#endif
}
if (needsRestart) {
_needsRestart.store(true, std::memory_order_release);
Expand Down
13 changes: 12 additions & 1 deletion render_delegate/render_param.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@

#include <pxr/imaging/hd/renderDelegate.h>

#include "hdarnold.h"

#include <atomic>

PXR_NAMESPACE_OPEN_SCOPE

class HdArnoldRenderDelegate;

/// Utility class to control the flow of rendering.
class HdArnoldRenderParam final : public HdRenderParam {
public:
Expand All @@ -51,8 +55,11 @@ class HdArnoldRenderParam final : public HdRenderParam {
};

/// Constructor for HdArnoldRenderParam.
#ifdef AI_MULTIPLE_RENDER_SESSIONS
HdArnoldRenderParam(HdArnoldRenderDelegate* delegate);
#else
HdArnoldRenderParam();

#endif
/// Destructor for HdArnoldRenderParam.
~HdArnoldRenderParam() override = default;

Expand Down Expand Up @@ -84,6 +91,10 @@ class HdArnoldRenderParam final : public HdRenderParam {
void Restart();

private:
#ifdef AI_MULTIPLE_RENDER_SESSIONS
/// The render delegate
const HdArnoldRenderDelegate* _delegate;
#endif
/// Indicate if render needs restarting, in case interrupt is called after rendering has finished.
std::atomic<bool> _needsRestart;
/// Indicate if rendering has been aborted at one point or another.
Expand Down

0 comments on commit 9960333

Please sign in to comment.