-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathrenderDelegate.cpp
More file actions
485 lines (408 loc) · 15.3 KB
/
renderDelegate.cpp
File metadata and controls
485 lines (408 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/************************************************************************
Copyright 2020 Advanced Micro Devices, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************/
#include "renderDelegate.h"
#include "aovDescriptor.h"
#include "pxr/imaging/rprUsd/materialRegistry.h"
#include "pxr/imaging/hd/extComputation.h"
#include "pxr/imaging/hgi/tokens.h"
#include "pxr/base/tf/diagnosticMgr.h"
#include "pxr/base/tf/getenv.h"
#include "pxr/base/arch/env.h"
#include "camera.h"
#include "config.h"
#include "renderPass.h"
#include "renderParam.h"
#include "mesh.h"
#include "instancer.h"
#include "domeLight.h"
#include "distantLight.h"
#include "light.h"
#include "material.h"
#include "renderBuffer.h"
#include "basisCurves.h"
#include "points.h"
#ifdef USE_VOLUME
#include "volume.h"
#include "field.h"
#endif
#include <ctime>
#include <iomanip>
#include <sstream>
#include <cstdio>
#ifdef BUILD_AS_HOUDINI_PLUGIN
#include "resourceWatcher.h"
#endif
PXR_NAMESPACE_OPEN_SCOPE
static HdRprApi* g_rprApi = nullptr;
class HdRprDiagnosticMgrDelegate : public TfDiagnosticMgr::Delegate {
public:
explicit HdRprDiagnosticMgrDelegate(std::string const& logFile) : m_outputFile(nullptr) {
if (logFile == "stderr") {
m_output = stderr;
} else if (logFile == "stdout") {
m_output = stdout;
} else {
m_outputFile = fopen(logFile.c_str(), "a+");
if (!m_outputFile) {
TF_RUNTIME_ERROR("Failed to open error output file: \"%s\". Defaults to stderr\n", logFile.c_str());
m_output = stderr;
} else {
m_output = m_outputFile;
}
}
}
~HdRprDiagnosticMgrDelegate() override {
if (m_outputFile) {
fclose(m_outputFile);
}
}
void IssueError(TfError const &err) override {
IssueDiagnosticBase(err);
};
void IssueFatalError(TfCallContext const &context, std::string const &msg) override {
std::string message = TfStringPrintf(
"[FATAL ERROR] %s -- in %s at line %zu of %s",
msg.c_str(),
context.GetFunction(),
context.GetLine(),
context.GetFile());
IssueMessage(msg);
};
void IssueStatus(TfStatus const &status) override {
IssueDiagnosticBase(status);
};
void IssueWarning(TfWarning const &warning) override {
IssueDiagnosticBase(warning);
};
private:
void IssueDiagnosticBase(TfDiagnosticBase const& d) {
std::string msg = TfStringPrintf(
"%s -- %s in %s at line %zu of %s",
d.GetCommentary().c_str(),
TfDiagnosticMgr::GetCodeName(d.GetDiagnosticCode()).c_str(),
d.GetContext().GetFunction(),
d.GetContext().GetLine(),
d.GetContext().GetFile());
IssueMessage(msg);
}
void IssueMessage(std::string const& message) {
std::time_t t = std::time(nullptr);
std::stringstream ss;
ss << "[" << std::put_time(std::gmtime(&t), "%T%z %F") << "] " << message;
auto str = ss.str();
fprintf(m_output, "%s\n", str.c_str());
}
private:
FILE* m_output;
FILE* m_outputFile;
};
TF_DEFINE_PRIVATE_TOKENS(_tokens,
(openvdbAsset) \
(percentDone) \
(RPR) \
(mtlx)
);
const TfTokenVector HdRprDelegate::SUPPORTED_RPRIM_TYPES = {
HdPrimTypeTokens->mesh,
HdPrimTypeTokens->basisCurves,
HdPrimTypeTokens->points,
#ifdef USE_VOLUME
HdPrimTypeTokens->volume,
#endif
};
const TfTokenVector HdRprDelegate::SUPPORTED_SPRIM_TYPES = {
HdPrimTypeTokens->camera,
HdPrimTypeTokens->material,
HdPrimTypeTokens->rectLight,
HdPrimTypeTokens->sphereLight,
HdPrimTypeTokens->cylinderLight,
HdPrimTypeTokens->domeLight,
HdPrimTypeTokens->diskLight,
HdPrimTypeTokens->distantLight,
HdPrimTypeTokens->extComputation
};
const TfTokenVector HdRprDelegate::SUPPORTED_BPRIM_TYPES = {
#ifdef USE_VOLUME
_tokens->openvdbAsset,
#endif
HdPrimTypeTokens->renderBuffer
};
HdRprDelegate* HdRprDelegate::m_lastCreatedInstance = nullptr;
HdRprDelegate::HdRprDelegate(HdRenderSettingsMap const& renderSettings) {
for (auto& entry : renderSettings) {
SetRenderSetting(entry.first, entry.second);
}
m_rprApi.reset(new HdRprApi(this));
g_rprApi = m_rprApi.get();
m_renderParam.reset(new HdRprRenderParam(m_rprApi.get(), &m_renderThread));
m_settingDescriptors = HdRprConfig::GetRenderSettingDescriptors();
_PopulateDefaultSettings(m_settingDescriptors);
m_renderThread.SetRenderCallback([this]() {
m_rprApi->Render(&m_renderThread);
});
m_renderThread.SetStopCallback([this]() {
m_rprApi->AbortRender();
});
m_renderThread.StartThread();
auto errorOutputFile = TfGetenv("HD_RPR_ERROR_OUTPUT_FILE");
if (!errorOutputFile.empty()) {
m_diagnosticMgrDelegate = DiagnostMgrDelegatePtr(
new HdRprDiagnosticMgrDelegate(errorOutputFile),
[](HdRprDiagnosticMgrDelegate* delegate) {
TfDiagnosticMgr::GetInstance().RemoveDelegate(delegate);
delete delegate;
});
TfDiagnosticMgr::GetInstance().AddDelegate(m_diagnosticMgrDelegate.get());
}
m_lastCreatedInstance = this;
#ifdef BUILD_AS_HOUDINI_PLUGIN
InitWatcher();
#endif
}
HdRprDelegate::~HdRprDelegate() {
#ifdef BUILD_AS_HOUDINI_PLUGIN
NotifyRenderFinished();
#endif
g_rprApi = nullptr;
m_lastCreatedInstance = nullptr;
}
HdRenderParam* HdRprDelegate::GetRenderParam() const {
return m_renderParam.get();
}
void HdRprDelegate::CommitResources(HdChangeTracker* tracker) {
// CommitResources() is called after prim sync has finished, but before any
// tasks (such as draw tasks) have run.
m_rprApi->CommitResources();
}
TfTokenVector HdRprDelegate::GetMaterialRenderContexts() const {
return {RprUsdMaterialRegistry::GetInstance().GetMaterialNetworkSelector(), _tokens->mtlx};
}
TfTokenVector const& HdRprDelegate::GetSupportedRprimTypes() const {
return SUPPORTED_RPRIM_TYPES;
}
TfTokenVector const& HdRprDelegate::GetSupportedSprimTypes() const {
return SUPPORTED_SPRIM_TYPES;
}
TfTokenVector const& HdRprDelegate::GetSupportedBprimTypes() const {
return SUPPORTED_BPRIM_TYPES;
}
HdResourceRegistrySharedPtr HdRprDelegate::GetResourceRegistry() const {
return HdResourceRegistrySharedPtr(new HdResourceRegistry());
}
HdRenderPassSharedPtr HdRprDelegate::CreateRenderPass(HdRenderIndex* index,
HdRprimCollection const& collection) {
return HdRenderPassSharedPtr(new HdRprRenderPass(index, collection, m_renderParam.get()));
}
HdInstancer* HdRprDelegate::CreateInstancer(HdSceneDelegate* delegate,
SdfPath const& id
HDRPR_INSTANCER_ID_ARG_DECL) {
return new HdRprInstancer(delegate, id HDRPR_INSTANCER_ID_ARG);
}
void HdRprDelegate::DestroyInstancer(HdInstancer* instancer) {
delete instancer;
}
HdRprim* HdRprDelegate::CreateRprim(TfToken const& typeId,
SdfPath const& rprimId
HDRPR_INSTANCER_ID_ARG_DECL) {
if (typeId == HdPrimTypeTokens->mesh) {
return new HdRprMesh(rprimId HDRPR_INSTANCER_ID_ARG);
} else if (typeId == HdPrimTypeTokens->basisCurves) {
return new HdRprBasisCurves(rprimId HDRPR_INSTANCER_ID_ARG);
} else if (typeId == HdPrimTypeTokens->points) {
return new HdRprPoints(rprimId HDRPR_INSTANCER_ID_ARG);
}
#ifdef USE_VOLUME
else if (typeId == HdPrimTypeTokens->volume) {
return new HdRprVolume(rprimId);
}
#endif
TF_CODING_ERROR("Unknown Rprim Type %s", typeId.GetText());
return nullptr;
}
void HdRprDelegate::DestroyRprim(HdRprim* rPrim) {
delete rPrim;
}
HdSprim* HdRprDelegate::CreateSprim(TfToken const& typeId,
SdfPath const& sprimId) {
if (typeId == HdPrimTypeTokens->camera) {
return new HdRprCamera(sprimId);
} else if (typeId == HdPrimTypeTokens->domeLight) {
return new HdRprDomeLight(sprimId);
} else if (typeId == HdPrimTypeTokens->distantLight) {
return new HdRprDistantLight(sprimId);
} else if (typeId == HdPrimTypeTokens->rectLight ||
typeId == HdPrimTypeTokens->sphereLight ||
typeId == HdPrimTypeTokens->cylinderLight ||
typeId == HdPrimTypeTokens->diskLight) {
return new HdRprLight(sprimId, typeId);
} else if (typeId == HdPrimTypeTokens->material) {
return new HdRprMaterial(sprimId);
} else if (typeId == HdPrimTypeTokens->extComputation) {
return new HdExtComputation(sprimId);
}
TF_CODING_ERROR("Unknown Sprim Type %s", typeId.GetText());
return nullptr;
}
HdSprim* HdRprDelegate::CreateFallbackSprim(TfToken const& typeId) {
// For fallback sprims, create objects with an empty scene path.
// They'll use default values and won't be updated by a scene delegate.
if (typeId == HdPrimTypeTokens->camera) {
return new HdRprCamera(SdfPath::EmptyPath());
} else if (typeId == HdPrimTypeTokens->domeLight) {
return new HdRprDomeLight(SdfPath::EmptyPath());
} else if (typeId == HdPrimTypeTokens->rectLight ||
typeId == HdPrimTypeTokens->sphereLight ||
typeId == HdPrimTypeTokens->cylinderLight ||
typeId == HdPrimTypeTokens->diskLight) {
return new HdRprLight(SdfPath::EmptyPath(), typeId);
} else if (typeId == HdPrimTypeTokens->distantLight) {
return new HdRprDistantLight(SdfPath::EmptyPath());
} else if (typeId == HdPrimTypeTokens->material) {
return new HdRprMaterial(SdfPath::EmptyPath());
} else if (typeId == HdPrimTypeTokens->extComputation) {
return new HdExtComputation(SdfPath::EmptyPath());
}
TF_CODING_ERROR("Unknown Sprim Type %s", typeId.GetText());
return nullptr;
}
void HdRprDelegate::DestroySprim(HdSprim* sPrim) {
delete sPrim;
}
HdBprim* HdRprDelegate::CreateBprim(TfToken const& typeId,
SdfPath const& bprimId) {
if (typeId == HdPrimTypeTokens->renderBuffer) {
return new HdRprRenderBuffer(bprimId, m_rprApi.get());
}
#ifdef USE_VOLUME
else if (typeId == _tokens->openvdbAsset) {
return new HdRprField(bprimId);
}
#endif
TF_CODING_ERROR("Unknown Bprim Type %s", typeId.GetText());
return nullptr;
}
HdBprim* HdRprDelegate::CreateFallbackBprim(TfToken const& typeId) {
return nullptr;
}
void HdRprDelegate::DestroyBprim(HdBprim* bPrim) {
delete bPrim;
}
HdAovDescriptor HdRprDelegate::GetDefaultAovDescriptor(TfToken const& name) const {
auto& rprAovDesc = HdRprAovRegistry::GetInstance().GetAovDesc(name);
HdAovDescriptor hdAovDesc;
hdAovDesc.format = rprAovDesc.format;
hdAovDesc.multiSampled = rprAovDesc.multiSampled;
hdAovDesc.clearValue = VtValue(rprAovDesc.clearValue);
return hdAovDesc;
}
HdRenderSettingDescriptorList HdRprDelegate::GetRenderSettingDescriptors() const {
return m_settingDescriptors;
}
std::string join(const std::vector<std::string>& vec, const std::string& delim)
{
std::stringstream res;
copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(res, delim.c_str()));
return res.str();
}
VtDictionary HdRprDelegate::GetRenderStats() const {
auto rprStats = m_rprApi->GetRenderStats();
VtDictionary stats;
stats[_tokens->percentDone.GetString()] = rprStats.percentDone;
stats["averageRenderTimePerSample"] = rprStats.averageRenderTimePerSample;
stats["averageResolveTimePerSample"] = rprStats.averageResolveTimePerSample;
stats["gpuUsedNames"] = join(m_rprApi->GetGpuUsedNames(), ", ");
stats["threadCountUsed"] = m_rprApi->GetCpuThreadCountUsed();
stats["firstIterationRenderTime"] = m_rprApi->GetFirstIterationRenerTime();
stats["totalRenderTime"] = rprStats.totalRenderTime;
stats["frameRenderTotalTime"] = rprStats.frameRenderTotalTime;
stats["frameResolveTotalTime"] = rprStats.frameResolveTotalTime;
stats["cacheCreationTime"] = rprStats.cacheCreationTime;
stats["syncTime"] = rprStats.syncTime;
return stats;
}
bool HdRprDelegate::IsPauseSupported() const {
return true;
}
bool HdRprDelegate::Pause() {
#ifdef BUILD_AS_HOUDINI_PLUGIN
NotifyRenderFinished();
#endif
m_renderThread.PauseRender();
return true;
}
bool HdRprDelegate::Resume() {
#ifdef BUILD_AS_HOUDINI_PLUGIN
NotifyRenderStarted();
#endif
m_renderThread.ResumeRender();
return true;
}
#if PXR_VERSION >= 2005
bool HdRprDelegate::IsStopSupported() const {
return true;
}
#if PXR_VERSION >= 2203
bool HdRprDelegate::Stop(bool blocking) {
#else
bool HdRprDelegate::Stop() {
#endif
m_renderThread.StopRender();
return true;
}
bool HdRprDelegate::Restart() {
m_renderParam->RestartRender();
m_renderThread.StartRender();
return true;
}
void HdRprDelegate::SetDrivers(HdDriverVector const& drivers) {
for (HdDriver* hdDriver : drivers) {
#ifdef HDRPR_ENABLE_VULKAN_INTEROP_SUPPORT
if (hdDriver->name == HgiTokens->Vulkan && hdDriver->driver.IsHolding<VtDictionary>()) {
VtDictionary dictionary = hdDriver->driver.UncheckedGet<VtDictionary>();
// Interop info is used to create context
static VkInteropInfo::VkInstance vkInstance;
static VkInteropInfo vkInteropInfo;
vkInteropInfo.instance_count = 1;
vkInteropInfo.main_instance_index = 0;
vkInteropInfo.frames_in_flight = dictionary["interop_info_frames_in_flight"].Get<unsigned int>();
vkInteropInfo.framebuffers_release_semaphores = dictionary["interop_info_semaphore_array"].Get<void*>();
vkInteropInfo.instances = &vkInstance;
vkInteropInfo.instances->physical_device = dictionary["interop_info_physicalDevice"].Get<void*>();
vkInteropInfo.instances->device = dictionary["interop_info_device"].Get<void*>();
m_rprApi->SetInteropInfo(&vkInteropInfo);
break;
}
#endif // HDRPR_ENABLE_VULKAN_INTEROP_SUPPORT
}
}
#endif // PXR_VERSION >= 2005
PXR_NAMESPACE_CLOSE_SCOPE
void HdRprSetRenderQuality(const char* quality) {
// Set environment variable that would outlive render delegate dll, because config is destroyed on plugin reload
// HDRPR_RENDER_QUALITY_OVERRIDE env setting wasn't used because it seems not possible to declare it in .h file
PXR_INTERNAL_NS::ArchSetEnv("HDRPR_USDVIEW_RENDER_QUALITY", quality, true);
}
char* HdRprGetRenderQuality() {
if (!PXR_INTERNAL_NS::g_rprApi) {
return nullptr;
}
auto currentRenderQuality = PXR_INTERNAL_NS::g_rprApi->GetCurrentRenderQuality().GetText();
auto len = std::strlen(currentRenderQuality);
auto copy = (char*)malloc(len + 1);
copy[len] = '\0';
std::strncpy(copy, currentRenderQuality, len);
return copy;
}
void HdRprFree(void* ptr) {
free(ptr);
}