Blender  V3.3
hydra/session.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2022 NVIDIA Corporation
3  * Copyright 2022 Blender Foundation */
4 
5 #include "hydra/session.h"
6 #include "scene/shader.h"
7 // Have to include shader.h before background.h so that 'set_shader' uses the correct 'set'
8 // overload taking a 'Node *', rather than the one taking a 'bool'
9 #include "scene/background.h"
10 #include "scene/light.h"
11 #include "scene/shader_graph.h"
12 #include "scene/shader_nodes.h"
13 #include "session/session.h"
14 
16 
17 namespace {
18 
19 const std::unordered_map<TfToken, PassType, TfToken::HashFunctor> kAovToPass = {
20  {HdAovTokens->color, PASS_COMBINED},
21  {HdAovTokens->depth, PASS_DEPTH},
22  {HdAovTokens->normal, PASS_NORMAL},
23  {HdAovTokens->primId, PASS_OBJECT_ID},
24  {HdAovTokens->instanceId, PASS_AOV_VALUE},
25 };
26 
27 } // namespace
28 
29 SceneLock::SceneLock(const HdRenderParam *renderParam)
30  : scene(static_cast<const HdCyclesSession *>(renderParam)->session->scene),
31  sceneLock(scene->mutex)
32 {
33 }
34 
36 {
37 }
38 
39 HdCyclesSession::HdCyclesSession(Session *session_, const bool keep_nodes)
40  : session(session_), keep_nodes(true), _ownCyclesSession(false)
41 {
42 }
43 
45  : session(new Session(params, SceneParams())), keep_nodes(false), _ownCyclesSession(true)
46 {
47  Scene *const scene = session->scene;
48 
49  // Create background with ambient light
50  {
51  ShaderGraph *graph = new ShaderGraph();
52 
53  BackgroundNode *bgNode = graph->create_node<BackgroundNode>();
54  bgNode->set_color(one_float3());
55  graph->add(bgNode);
56 
57  graph->connect(bgNode->output("Background"), graph->output()->input("Surface"));
58 
59  scene->default_background->set_graph(graph);
60  scene->default_background->tag_update(scene);
61  }
62 
63  // Wire up object color in default surface material
64  {
65  ShaderGraph *graph = new ShaderGraph();
66 
67  ObjectInfoNode *objectNode = graph->create_node<ObjectInfoNode>();
68  graph->add(objectNode);
69 
70  DiffuseBsdfNode *diffuseNode = graph->create_node<DiffuseBsdfNode>();
71  graph->add(diffuseNode);
72 
73  graph->connect(objectNode->output("Color"), diffuseNode->input("Color"));
74  graph->connect(diffuseNode->output("BSDF"), graph->output()->input("Surface"));
75 
76 #if 1
77  // Create the instanceId AOV output
78  const ustring instanceId(HdAovTokens->instanceId.GetString());
79 
80  OutputAOVNode *aovNode = graph->create_node<OutputAOVNode>();
81  aovNode->set_name(instanceId);
82  graph->add(aovNode);
83 
84  AttributeNode *instanceIdNode = graph->create_node<AttributeNode>();
85  instanceIdNode->set_attribute(instanceId);
86  graph->add(instanceIdNode);
87 
88  graph->connect(instanceIdNode->output("Fac"), aovNode->input("Value"));
89 #endif
90 
91  scene->default_surface->set_graph(graph);
92  scene->default_surface->tag_update(scene);
93  }
94 }
95 
97 {
98  if (_ownCyclesSession) {
99  delete session;
100  }
101 }
102 
104 {
105  Scene *const scene = session->scene;
106 
107  // Update background depending on presence of a background light
108  if (scene->light_manager->need_update()) {
109  Light *background_light = nullptr;
110  for (Light *light : scene->lights) {
111  if (light->get_light_type() == LIGHT_BACKGROUND) {
112  background_light = light;
113  break;
114  }
115  }
116 
117  if (!background_light) {
118  scene->background->set_shader(scene->default_background);
119  scene->background->set_transparent(true);
120  }
121  else {
122  scene->background->set_shader(background_light->get_shader());
123  scene->background->set_transparent(false);
124  }
125 
127  }
128 }
129 
130 void HdCyclesSession::SyncAovBindings(const HdRenderPassAovBindingVector &aovBindings)
131 {
132  Scene *const scene = session->scene;
133 
134  // Delete all existing passes
135  scene->delete_nodes(set<Pass *>(scene->passes.begin(), scene->passes.end()));
136 
137  // Update passes with requested AOV bindings
138  _aovBindings = aovBindings;
139  for (const HdRenderPassAovBinding &aovBinding : aovBindings) {
140  const auto cyclesAov = kAovToPass.find(aovBinding.aovName);
141  if (cyclesAov == kAovToPass.end()) {
142  // TODO: Use PASS_AOV_COLOR and PASS_AOV_VALUE for these?
143  TF_WARN("Unknown pass %s", aovBinding.aovName.GetText());
144  continue;
145  }
146 
147  const PassType type = cyclesAov->second;
148  const PassMode mode = PassMode::DENOISED;
149 
150  Pass *pass = scene->create_node<Pass>();
151  pass->set_type(type);
152  pass->set_mode(mode);
153  pass->set_name(ustring(aovBinding.aovName.GetString()));
154  }
155 }
156 
157 void HdCyclesSession::RemoveAovBinding(HdRenderBuffer *renderBuffer)
158 {
159  for (HdRenderPassAovBinding &aovBinding : _aovBindings) {
160  if (renderBuffer == aovBinding.renderBuffer) {
161  aovBinding.renderBuffer = nullptr;
162  break;
163  }
164  }
165 
166  if (renderBuffer == _displayAovBinding.renderBuffer) {
167  _displayAovBinding.renderBuffer = nullptr;
168  }
169 }
170 
KDTree *BLI_kdtree_nd_() new(unsigned int maxsize)
Definition: kdtree_impl.h:85
ThreadMutex mutex
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
void tag_update(Scene *scene)
HdCyclesSession(CCL_NS::Session *session_, const bool keep_nodes)
~HdCyclesSession() override
CCL_NS::Session * session
Definition: hydra/session.h:61
void RemoveAovBinding(PXR_NS::HdRenderBuffer *renderBuffer)
void SyncAovBindings(const PXR_NS::HdRenderPassAovBindingVector &aovBindings)
bool need_update() const
Definition: pass.h:48
ShaderInput * input(const char *name)
ShaderOutput * output(const char *name)
Depsgraph * graph
Scene scene
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
Definition: hydra/config.h:17
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
PassType
Definition: kernel/types.h:334
@ PASS_DEPTH
Definition: kernel/types.h:358
@ PASS_COMBINED
Definition: kernel/types.h:338
@ PASS_OBJECT_ID
Definition: kernel/types.h:363
@ PASS_NORMAL
Definition: kernel/types.h:360
@ PASS_AOV_VALUE
Definition: kernel/types.h:369
@ LIGHT_BACKGROUND
Definition: kernel/types.h:458
ccl_device_inline float3 one_float3()
Definition: math_float3.h:89
const std::unordered_map< TfToken, PassType, TfToken::HashFunctor > kAovToPass
PassMode
Definition: pass.h:19
SceneLock(const PXR_NS::HdRenderParam *renderParam)
CCL_NS::Scene * scene
Definition: hydra/session.h:18
T * create_node(Args &&...args)
Definition: scene.h:284
vector< Pass * > passes
Definition: scene.h:218
vector< Light * > lights
Definition: scene.h:216
Shader * default_background
Definition: scene.h:235
Background * background
Definition: scene.h:209
void delete_nodes(const set< T * > &nodes)
Definition: scene.h:315
LightManager * light_manager
Definition: scene.h:223