Blender V5.3
render_delegate.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
7#include "hydra/camera.h"
8#include "hydra/curves.h"
9#include "hydra/field.h"
10#include "hydra/instancer.h"
11#include "hydra/light.h"
12#include "hydra/material.h"
13#include "hydra/mesh.h"
14#include "hydra/node_util.h"
15#include "hydra/pointcloud.h"
16#include "hydra/render_buffer.h"
17#include "hydra/render_pass.h"
18#include "hydra/session.h"
19#include "hydra/volume.h"
20#include "scene/integrator.h"
21#include "scene/scene.h"
22#include "session/session.h"
23
24#include <pxr/base/tf/getenv.h>
25#include <pxr/imaging/hgi/tokens.h>
26
28
30
31// clang-format off
33 (cycles)
34 (openvdbAsset)
35);
36// clang-format on
37
38namespace {
39
40const TfTokenVector kSupportedRPrimTypes = {
41 HdPrimTypeTokens->basisCurves,
42 HdPrimTypeTokens->mesh,
43 HdPrimTypeTokens->points,
44#ifdef WITH_OPENVDB
45 HdPrimTypeTokens->volume,
46#endif
47};
48
49const TfTokenVector kSupportedSPrimTypes = {
50 HdPrimTypeTokens->camera,
51 HdPrimTypeTokens->material,
52 HdPrimTypeTokens->diskLight,
53 HdPrimTypeTokens->distantLight,
54 HdPrimTypeTokens->domeLight,
55 HdPrimTypeTokens->rectLight,
56 HdPrimTypeTokens->sphereLight,
57};
58
59const TfTokenVector kSupportedBPrimTypes = {
60 HdPrimTypeTokens->renderBuffer,
61#ifdef WITH_OPENVDB
62 _tokens->openvdbAsset,
63#endif
64};
65
66SessionParams GetSessionParams(const HdRenderSettingsMap &settings)
67{
68 SessionParams params;
69 params.threads = 0;
70 params.background = false;
71 params.use_resolution_divider = false;
72
73 HdRenderSettingsMap::const_iterator it;
74
75 // Pull all setting that contribute to device creation first
76 it = settings.find(HdCyclesRenderSettingsTokens->threads);
77 if (it != settings.end()) {
78 params.threads = VtValue::Cast<int>(it->second).GetWithDefault(params.threads);
79 }
80
81 // Get the Cycles device from settings or environment, falling back to CPU
82 std::string deviceType = Device::string_from_type(DEVICE_CPU);
83 it = settings.find(HdCyclesRenderSettingsTokens->device);
84 if (it != settings.end()) {
85 deviceType = VtValue::Cast<std::string>(it->second).GetWithDefault(deviceType);
86 }
87 else {
88 const std::string deviceTypeEnv = TfGetenv("CYCLES_DEVICE");
89 if (!deviceTypeEnv.empty()) {
90 deviceType = deviceTypeEnv;
91 }
92 }
93
94 // Move to all uppercase for Device::type_from_string
95 std::transform(deviceType.begin(), deviceType.end(), deviceType.begin(), ::toupper);
96
97 vector<DeviceInfo> devices;
98 for (const DeviceInfo &info :
100 {
101 if (info.meets_driver_requirement) {
102 devices.push_back(info);
103 }
104 }
105 if (devices.empty()) {
107 if (!devices.empty()) {
108 params.device = devices.front();
109 }
110 }
111 else {
112 params.device = Device::get_multi_device(devices, params.threads, params.background);
113 }
114
115 /* Set same device for denoising for now. */
116 params.denoise_device = params.device;
117
118 return params;
119}
120
121} // namespace
122
123HdCyclesDelegate::HdCyclesDelegate(const HdRenderSettingsMap &settingsMap,
124 Session *session_,
125 const bool keep_nodes)
126 : HdRenderDelegate()
127{
128 _renderParam = session_ ? std::make_unique<HdCyclesSession>(session_, keep_nodes) :
129 std::make_unique<HdCyclesSession>(GetSessionParams(settingsMap));
130
131 for (const auto &setting : settingsMap) {
132 // Skip over the settings known to be used for initialization only
133 if (setting.first == HdCyclesRenderSettingsTokens->device ||
134 setting.first == HdCyclesRenderSettingsTokens->threads)
135 {
136 continue;
137 }
138
139 SetRenderSetting(setting.first, setting.second);
140 }
141}
142
144
145void HdCyclesDelegate::SetDrivers(const HdDriverVector &drivers)
146{
147 for (HdDriver *hdDriver : drivers) {
148 if (hdDriver->name == HgiTokens->renderDriver && hdDriver->driver.IsHolding<Hgi *>()) {
149 _hgi = hdDriver->driver.UncheckedGet<Hgi *>();
150 break;
151 }
152 }
153}
154
156{
157#if defined(_WIN32) && defined(WITH_HYDRA_DISPLAY_DRIVER)
158 return _hgi && _hgi->GetAPIName() == HgiTokens->OpenGL;
159#else
160 return false;
161#endif
162}
163
165{
166 return kSupportedRPrimTypes;
167}
168
170{
171 return kSupportedSPrimTypes;
172}
173
175{
176 return kSupportedBPrimTypes;
177}
178
180{
181 return _renderParam.get();
182}
183
184HdResourceRegistrySharedPtr HdCyclesDelegate::GetResourceRegistry() const
185{
186 return HdResourceRegistrySharedPtr();
187}
188
190{
191 return true;
192}
193
195{
196 _renderParam->session->set_pause(true);
197 return true;
198}
199
201{
202 _renderParam->session->set_pause(false);
203 return true;
204}
205
206HdRenderPassSharedPtr HdCyclesDelegate::CreateRenderPass(HdRenderIndex *index,
207 const HdRprimCollection &collection)
208{
209 return HdRenderPassSharedPtr(new HdCyclesRenderPass(index, collection, _renderParam.get()));
210}
211
212HdInstancer *HdCyclesDelegate::CreateInstancer(HdSceneDelegate *delegate,
213 const SdfPath &instancerId)
214{
215 return new HdCyclesInstancer(delegate, instancerId);
216}
217
218void HdCyclesDelegate::DestroyInstancer(HdInstancer *instancer)
219{
220 delete instancer;
221}
222
223HdRprim *HdCyclesDelegate::CreateRprim(const TfToken &typeId, const SdfPath &rprimId)
224{
225 if (typeId == HdPrimTypeTokens->mesh) {
226 return new HdCyclesMesh(rprimId);
227 }
228 if (typeId == HdPrimTypeTokens->basisCurves) {
229 return new HdCyclesCurves(rprimId);
230 }
231 if (typeId == HdPrimTypeTokens->points) {
232 return new HdCyclesPoints(rprimId);
233 }
234#ifdef WITH_OPENVDB
235 if (typeId == HdPrimTypeTokens->volume) {
236 return new HdCyclesVolume(rprimId);
237 }
238#endif
239
240 TF_CODING_ERROR("Unknown Rprim type %s", typeId.GetText());
241 return nullptr;
242}
243
245{
246 delete rPrim;
247}
248
249HdSprim *HdCyclesDelegate::CreateSprim(const TfToken &typeId, const SdfPath &sprimId)
250{
251 if (typeId == HdPrimTypeTokens->camera) {
252 return new HdCyclesCamera(sprimId);
253 }
254 if (typeId == HdPrimTypeTokens->material) {
255 return new HdCyclesMaterial(sprimId);
256 }
257 if (typeId == HdPrimTypeTokens->diskLight || typeId == HdPrimTypeTokens->distantLight ||
258 typeId == HdPrimTypeTokens->domeLight || typeId == HdPrimTypeTokens->rectLight ||
259 typeId == HdPrimTypeTokens->sphereLight)
260 {
261 return new HdCyclesLight(sprimId, typeId);
262 }
263
264 TF_CODING_ERROR("Unknown Sprim type %s", typeId.GetText());
265 return nullptr;
266}
267
268HdSprim *HdCyclesDelegate::CreateFallbackSprim(const TfToken &typeId)
269{
270 return CreateSprim(typeId, SdfPath::EmptyPath());
271}
272
274{
275 delete sPrim;
276}
277
278HdBprim *HdCyclesDelegate::CreateBprim(const TfToken &typeId, const SdfPath &bprimId)
279{
280 if (typeId == HdPrimTypeTokens->renderBuffer) {
281 return new HdCyclesRenderBuffer(bprimId);
282 }
283#ifdef WITH_OPENVDB
284 if (typeId == _tokens->openvdbAsset) {
285 return new HdCyclesField(bprimId, typeId);
286 }
287#endif
288
289 TF_CODING_ERROR("Unknown Bprim type %s", typeId.GetText());
290 return nullptr;
291}
292
293HdBprim *HdCyclesDelegate::CreateFallbackBprim(const TfToken &typeId)
294{
295 return CreateBprim(typeId, SdfPath::EmptyPath());
296}
297
299{
300 delete bPrim;
301}
302
303void HdCyclesDelegate::CommitResources(HdChangeTracker *tracker)
304{
305 TF_UNUSED(tracker);
306
307 const SceneLock lock(_renderParam.get());
308
309 _renderParam->UpdateScene();
310}
311
313{
314 return HdTokens->full;
315}
316
318{
319 return {_tokens->cycles};
320}
321
323{
324 const Stats &stats = _renderParam->session->stats;
325 const Progress &progress = _renderParam->session->progress;
326
327 double totalTime;
328 double renderTime;
329 progress.get_time(totalTime, renderTime);
330 const double fractionDone = progress.get_progress();
331
332 std::string status;
333 std::string substatus;
334 progress.get_status(status, substatus);
335 if (!substatus.empty()) {
336 status += " | " + substatus;
337 }
338
339 return {{"rendererName", VtValue("Cycles")},
340 {"rendererVersion", VtValue(GfVec3i(0, 0, 0))},
341 {"percentDone", VtValue(floor_to_int(fractionDone * 100))},
342 {"fractionDone", VtValue(fractionDone)},
343 {"loadClockTime", VtValue(totalTime - renderTime)},
344 {"peakMemory", VtValue(stats.mem_peak)},
345 {"totalClockTime", VtValue(totalTime)},
346 {"totalMemory", VtValue(stats.mem_used)},
347 {"renderProgressAnnotation", VtValue(status)}};
348}
349
350HdAovDescriptor HdCyclesDelegate::GetDefaultAovDescriptor(const TfToken &name) const
351{
352 if (name == HdAovTokens->color) {
353 HdFormat colorFormat = HdFormatFloat32Vec4;
354 if (IsDisplaySupported()) {
355 // Can use Cycles 'DisplayDriver' in OpenGL, but it only supports 'half4' format
356 colorFormat = HdFormatFloat16Vec4;
357 }
358
359 return HdAovDescriptor(colorFormat, false, VtValue(GfVec4f(0.0f)));
360 }
361 if (name == HdAovTokens->depth) {
362 return HdAovDescriptor(HdFormatFloat32, false, VtValue(1.0f));
363 }
364 if (name == HdAovTokens->normal) {
365 return HdAovDescriptor(HdFormatFloat32Vec3, false, VtValue(GfVec3f(0.0f)));
366 }
367 if (name == HdAovTokens->primId || name == HdAovTokens->instanceId ||
368 name == HdAovTokens->elementId)
369 {
370 return HdAovDescriptor(HdFormatInt32, false, VtValue(-1));
371 }
372
373 return HdAovDescriptor();
374}
375
376HdRenderSettingDescriptorList HdCyclesDelegate::GetRenderSettingDescriptors() const
377{
378 Scene *const scene = _renderParam->session->scene.get();
379
380 HdRenderSettingDescriptorList descriptors;
381
382 descriptors.push_back({
383 "Time Limit",
384 HdCyclesRenderSettingsTokens->timeLimit,
385 VtValue(0.0),
386 });
387 descriptors.push_back({
388 "Sample Count",
389 HdCyclesRenderSettingsTokens->samples,
390 VtValue(1024),
391 });
392 descriptors.push_back({
393 "Sample Offset",
394 HdCyclesRenderSettingsTokens->sampleOffset,
395 VtValue(0),
396 });
397
398 for (const SocketType &socket : scene->integrator->type->inputs) {
399 descriptors.push_back({socket.ui_name.string(),
400 TfToken("cycles:integrator:" + socket.name.string()),
401 GetNodeValue(scene->integrator, socket)});
402 }
403
404 return descriptors;
405}
406
407void HdCyclesDelegate::SetRenderSetting(const PXR_NS::TfToken &key, const PXR_NS::VtValue &value)
408{
409 Scene *const scene = _renderParam->session->scene.get();
410 Session *const session = _renderParam->session;
411
412 if (key == HdCyclesRenderSettingsTokens->stageMetersPerUnit) {
413 _renderParam->SetStageMetersPerUnit(
414 VtValue::Cast<double>(value).GetWithDefault(_renderParam->GetStageMetersPerUnit()));
415 }
416 else if (key == HdCyclesRenderSettingsTokens->timeLimit) {
417 session->set_time_limit(
418 VtValue::Cast<double>(value).GetWithDefault(session->params.time_limit));
419 }
420 else if (key == HdCyclesRenderSettingsTokens->samples) {
421 static const int max_samples = Integrator::MAX_SAMPLES;
422 int samples = VtValue::Cast<int>(value).GetWithDefault(session->params.samples);
423 samples = std::min(std::max(1, samples), max_samples);
424 session->set_samples(samples);
425 }
426 else if (key == HdCyclesRenderSettingsTokens->sampleOffset) {
427 session->params.sample_subset_offset = VtValue::Cast<int>(value).GetWithDefault(
430 session->params.use_sample_subset = session->params.sample_subset_offset > 0;
431 ++_settingsVersion;
432 }
433 else {
434 const std::string &keyString = key.GetString();
435 if (keyString.rfind("cycles:integrator:", 0) == 0) {
436 const ustring socketName(keyString, sizeof("cycles:integrator:") - 1);
437 if (const SocketType *socket = scene->integrator->type->find_input(socketName)) {
438 SetNodeValue(scene->integrator, *socket, value);
439 ++_settingsVersion;
440 }
441 }
442 }
443}
444
445VtValue HdCyclesDelegate::GetRenderSetting(const TfToken &key) const
446{
447 Scene *const scene = _renderParam->session->scene.get();
448 Session *const session = _renderParam->session;
449
450 if (key == HdCyclesRenderSettingsTokens->stageMetersPerUnit) {
451 return VtValue(_renderParam->GetStageMetersPerUnit());
452 }
453 if (key == HdCyclesRenderSettingsTokens->device) {
454 return VtValue(TfToken(Device::string_from_type(session->params.device.type)));
455 }
456 if (key == HdCyclesRenderSettingsTokens->threads) {
457 return VtValue(session->params.threads);
458 }
459 if (key == HdCyclesRenderSettingsTokens->timeLimit) {
460 return VtValue(session->params.time_limit);
461 }
462 if (key == HdCyclesRenderSettingsTokens->samples) {
463 return VtValue(session->params.samples);
464 }
465 if (key == HdCyclesRenderSettingsTokens->sampleOffset) {
466 return VtValue((session->params.use_sample_subset) ? session->params.sample_subset_offset : 0);
467 }
468
469 const std::string &keyString = key.GetString();
470 if (keyString.rfind("cycles:integrator:", 0) == 0) {
471 const ustring socketName(keyString, sizeof("cycles:integrator:") - 1);
472 if (const SocketType *socket = scene->integrator->type->find_input(socketName)) {
473 return GetNodeValue(scene->integrator, *socket);
474 }
475 }
476
477 return VtValue();
478}
479
volatile int lock
DeviceType type
static DeviceInfo get_multi_device(const vector< DeviceInfo > &subdevices, const int threads, bool background)
static DeviceType type_from_string(const char *name)
static string string_from_type(DeviceType type)
static vector< DeviceInfo > available_devices(const uint device_type_mask=DEVICE_MASK_ALL)
HdCyclesCamera(const PXR_NS::SdfPath &sprimId)
HdCyclesCurves(const PXR_NS::SdfPath &rprimId)
bool IsPauseSupported() const override
PXR_NS::HdBprim * CreateFallbackBprim(const PXR_NS::TfToken &typeId) override
bool Resume() override
~HdCyclesDelegate() override
void SetRenderSetting(const PXR_NS::TfToken &key, const PXR_NS::VtValue &value) override
PXR_NS::HdInstancer * CreateInstancer(PXR_NS::HdSceneDelegate *delegate, const PXR_NS::SdfPath &id) override
void DestroyRprim(PXR_NS::HdRprim *rPrim) override
const PXR_NS::TfTokenVector & GetSupportedSprimTypes() const override
PXR_NS::HdRenderParam * GetRenderParam() const override
PXR_NS::HdRenderPassSharedPtr CreateRenderPass(PXR_NS::HdRenderIndex *index, const PXR_NS::HdRprimCollection &collection) override
PXR_NS::HdSprim * CreateFallbackSprim(const PXR_NS::TfToken &typeId) override
PXR_NS::HdResourceRegistrySharedPtr GetResourceRegistry() const override
void DestroySprim(PXR_NS::HdSprim *sPrim) override
bool Pause() override
void CommitResources(PXR_NS::HdChangeTracker *tracker) override
const PXR_NS::TfTokenVector & GetSupportedBprimTypes() const override
const PXR_NS::TfTokenVector & GetSupportedRprimTypes() const override
PXR_NS::HdRprim * CreateRprim(const PXR_NS::TfToken &typeId, const PXR_NS::SdfPath &rprimId) override
PXR_NS::TfTokenVector GetMaterialRenderContexts() const override
HdCyclesDelegate(const PXR_NS::HdRenderSettingsMap &settingsMap, CCL_NS::Session *session_=nullptr, const bool keep_nodes=false)
PXR_NS::HdAovDescriptor GetDefaultAovDescriptor(const PXR_NS::TfToken &name) const override
PXR_NS::HdSprim * CreateSprim(const PXR_NS::TfToken &typeId, const PXR_NS::SdfPath &sprimId) override
PXR_NS::VtValue GetRenderSetting(const PXR_NS::TfToken &key) const override
void SetDrivers(const PXR_NS::HdDriverVector &drivers) override
bool IsDisplaySupported() const
PXR_NS::HdRenderSettingDescriptorList GetRenderSettingDescriptors() const override
PXR_NS::TfToken GetMaterialBindingPurpose() const override
void DestroyBprim(PXR_NS::HdBprim *bPrim) override
PXR_NS::VtDictionary GetRenderStats() const override
void DestroyInstancer(PXR_NS::HdInstancer *instancer) override
PXR_NS::HdBprim * CreateBprim(const PXR_NS::TfToken &typeId, const PXR_NS::SdfPath &bprimId) override
HdCyclesField(const PXR_NS::SdfPath &bprimId, const PXR_NS::TfToken &typeId)
Definition field.cpp:50
HdCyclesInstancer(PXR_NS::HdSceneDelegate *delegate, const PXR_NS::SdfPath &instancerId)
Definition instancer.cpp:21
HdCyclesLight(const PXR_NS::SdfPath &sprimId, const PXR_NS::TfToken &lightType)
HdCyclesMaterial(const PXR_NS::SdfPath &sprimId)
Definition material.cpp:230
HdCyclesMesh(const PXR_NS::SdfPath &rprimId)
HdCyclesPoints(const PXR_NS::SdfPath &rprimId)
HdCyclesRenderBuffer(const PXR_NS::SdfPath &bprimId)
HdCyclesRenderPass(PXR_NS::HdRenderIndex *index, const PXR_NS::HdRprimCollection &collection, HdCyclesSession *renderParam)
HdCyclesVolume(const PXR_NS::SdfPath &rprimId)
static const int MAX_SAMPLES
Definition integrator.h:99
void get_status(string &status_, string &substatus_) const
Definition progress.h:290
void get_time(double &total_time_, double &render_time_) const
Definition progress.h:153
double get_progress() const
Definition progress.h:185
Definition scene.h:133
Integrator * integrator
Definition scene.h:149
bool use_resolution_divider
DeviceInfo denoise_device
DeviceInfo device
SessionParams params
void set_time_limit(const double time_limit)
void set_samples(const int samples)
size_t mem_used
Definition util/stats.h:30
size_t mem_peak
Definition util/stats.h:31
#define DEVICE_MASK(type)
@ DEVICE_MASK_CPU
@ DEVICE_CPU
#define index(i)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
TF_DEFINE_PRIVATE_TOKENS(_tokens,(visibleInPrimaryRay)(treatAsPoint)(falloff))
ccl_device_inline int floor_to_int(const float f)
Definition math_base.h:459
void SetNodeValue(Node *node, const SocketType &socket, const VtValue &value)
VtValue GetNodeValue(const Node *node, const SocketType &socket)
HDCYCLES_NAMESPACE_OPEN_SCOPE TF_DEFINE_PUBLIC_TOKENS(HdCyclesRenderSettingsTokens, HD_CYCLES_RENDER_SETTINGS_TOKENS)
#define HD_CYCLES_RENDER_SETTINGS_TOKENS
vector< SocketType, std::allocator< SocketType > > inputs
Definition node_type.h:131
const SocketType * find_input(ustring name) const
const NodeType * type
Definition graph/node.h:179
ustring name
Definition node_type.h:84
ustring ui_name
Definition node_type.h:91