Blender  V3.3
render_pass.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/render_pass.h"
6 #include "hydra/camera.h"
7 #include "hydra/output_driver.h"
8 #include "hydra/render_buffer.h"
10 #include "hydra/session.h"
11 
12 #ifdef WITH_HYDRA_DISPLAY_DRIVER
13 # include "hydra/display_driver.h"
14 #endif
15 
16 #include "scene/camera.h"
17 #include "scene/integrator.h"
18 #include "scene/scene.h"
19 
20 #include "session/session.h"
21 
22 #include <pxr/imaging/hd/renderPassState.h>
23 
25 
27  HdRprimCollection const &collection,
28  HdCyclesSession *renderParam)
29  : HdRenderPass(index, collection), _renderParam(renderParam)
30 {
31  Session *const session = _renderParam->session;
32  // Reset cancel state so session thread can continue rendering
33  session->progress.reset();
34 
35  session->set_output_driver(make_unique<HdCyclesOutputDriver>(renderParam));
36 
37  const auto renderDelegate = static_cast<const HdCyclesDelegate *>(
38  GetRenderIndex()->GetRenderDelegate());
39  if (renderDelegate->IsDisplaySupported()) {
40 #ifdef WITH_HYDRA_DISPLAY_DRIVER
41  session->set_display_driver(
42  make_unique<HdCyclesDisplayDriver>(renderParam, renderDelegate->GetHgi()));
43 #endif
44  }
45 }
46 
48 {
49  Session *const session = _renderParam->session;
50  session->cancel(true);
51 }
52 
54 {
55  for (const HdRenderPassAovBinding &aovBinding : _renderParam->GetAovBindings()) {
56  if (aovBinding.renderBuffer && !aovBinding.renderBuffer->IsConverged()) {
57  return false;
58  }
59  }
60 
61  return true;
62 }
63 
64 void HdCyclesRenderPass::ResetConverged()
65 {
66  for (const HdRenderPassAovBinding &aovBinding : _renderParam->GetAovBindings()) {
67  if (const auto renderBuffer = static_cast<HdCyclesRenderBuffer *>(aovBinding.renderBuffer)) {
68  renderBuffer->SetConverged(false);
69  }
70  }
71 }
72 
73 void HdCyclesRenderPass::_Execute(const HdRenderPassStateSharedPtr &renderPassState,
74  const TfTokenVector &renderTags)
75 {
76  Scene *const scene = _renderParam->session->scene;
77  Session *const session = _renderParam->session;
78 
79  if (session->progress.get_cancel()) {
80  return; // Something went wrong and cannot continue without recreating the session
81  }
82 
83  if (scene->mutex.try_lock()) {
84  const auto renderDelegate = static_cast<HdCyclesDelegate *>(
85  GetRenderIndex()->GetRenderDelegate());
86 
87  const unsigned int settingsVersion = renderDelegate->GetRenderSettingsVersion();
88 
89  // Update requested AOV bindings
90  const HdRenderPassAovBindingVector &aovBindings = renderPassState->GetAovBindings();
91  if (_renderParam->GetAovBindings() != aovBindings ||
92  // Need to resync passes when denoising is enabled or disabled to update the pass mode
93  (settingsVersion != _lastSettingsVersion &&
94  scene->integrator->use_denoise_is_modified())) {
95  _renderParam->SyncAovBindings(aovBindings);
96 
97  if (renderDelegate->IsDisplaySupported()) {
98  // Update display pass to the first requested color AOV
99  HdRenderPassAovBinding displayAovBinding = !aovBindings.empty() ? aovBindings.front() :
100  HdRenderPassAovBinding();
101  if (displayAovBinding.aovName == HdAovTokens->color && displayAovBinding.renderBuffer) {
102  _renderParam->SetDisplayAovBinding(displayAovBinding);
103  }
104  else {
105  _renderParam->SetDisplayAovBinding(HdRenderPassAovBinding());
106  }
107  }
108  }
109 
110  // Update camera dimensions to the viewport size
111 #if PXR_VERSION >= 2102
112  CameraUtilFraming framing = renderPassState->GetFraming();
113  if (!framing.IsValid()) {
114  const GfVec4f vp = renderPassState->GetViewport();
115  framing = CameraUtilFraming(GfRect2i(GfVec2i(0), int(vp[2]), int(vp[3])));
116  }
117 
118  scene->camera->set_full_width(framing.dataWindow.GetWidth());
119  scene->camera->set_full_height(framing.dataWindow.GetHeight());
120 #else
121  const GfVec4f vp = renderPassState->GetViewport();
122  scene->camera->set_full_width(int(vp[2]));
123  scene->camera->set_full_height(int(vp[3]));
124 #endif
125 
126  if (const auto camera = static_cast<const HdCyclesCamera *>(renderPassState->GetCamera())) {
127  camera->ApplyCameraSettings(_renderParam, scene->camera);
128  }
129  else {
131  renderPassState->GetWorldToViewMatrix(),
132  renderPassState->GetProjectionMatrix(),
133  renderPassState->GetClipPlanes(),
134  scene->camera);
135  }
136 
137  // Reset session if the session, scene, camera or AOV bindings changed
138  if (scene->need_reset() || settingsVersion != _lastSettingsVersion) {
139  _lastSettingsVersion = settingsVersion;
140 
141  // Reset convergence state of all render buffers
142  ResetConverged();
143 
144  BufferParams buffer_params;
145 #if PXR_VERSION >= 2102
146  buffer_params.full_x = static_cast<int>(framing.displayWindow.GetMin()[0]);
147  buffer_params.full_y = static_cast<int>(framing.displayWindow.GetMin()[1]);
148  buffer_params.full_width = static_cast<int>(framing.displayWindow.GetSize()[0]);
149  buffer_params.full_height = static_cast<int>(framing.displayWindow.GetSize()[1]);
150 
151  buffer_params.window_x = framing.dataWindow.GetMinX() - buffer_params.full_x;
152  buffer_params.window_y = framing.dataWindow.GetMinY() - buffer_params.full_y;
153  buffer_params.window_width = framing.dataWindow.GetWidth();
154  buffer_params.window_height = framing.dataWindow.GetHeight();
155 
156  buffer_params.width = buffer_params.window_width;
157  buffer_params.height = buffer_params.window_height;
158 #else
159  buffer_params.width = static_cast<int>(vp[2]);
160  buffer_params.height = static_cast<int>(vp[3]);
161  buffer_params.full_width = buffer_params.width;
162  buffer_params.full_height = buffer_params.height;
163  buffer_params.window_width = buffer_params.width;
164  buffer_params.window_height = buffer_params.height;
165 #endif
166 
167  session->reset(session->params, buffer_params);
168  }
169 
170  scene->mutex.unlock();
171 
172  // Start Cycles render thread if not already running
173  session->start();
174  }
175 
176  session->draw();
177 }
178 
179 void HdCyclesRenderPass::_MarkCollectionDirty()
180 {
181 }
182 
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between camera
int full_x
Definition: buffers.h:84
int full_width
Definition: buffers.h:86
int height
Definition: buffers.h:72
int window_y
Definition: buffers.h:79
int full_height
Definition: buffers.h:87
int window_height
Definition: buffers.h:81
int window_width
Definition: buffers.h:80
NODE_DECLARE int width
Definition: buffers.h:71
int window_x
Definition: buffers.h:78
int full_y
Definition: buffers.h:85
void ApplyCameraSettings(PXR_NS::HdRenderParam *renderParam, CCL_NS::Camera *targetCamera) const
~HdCyclesRenderPass() override
Definition: render_pass.cpp:47
HdCyclesRenderPass(PXR_NS::HdRenderIndex *index, const PXR_NS::HdRprimCollection &collection, HdCyclesSession *renderParam)
Definition: render_pass.cpp:26
bool IsConverged() const override
Definition: render_pass.cpp:53
const PXR_NS::HdRenderPassAovBindingVector & GetAovBindings() const
Definition: hydra/session.h:52
CCL_NS::Session * session
Definition: hydra/session.h:61
void SetDisplayAovBinding(const PXR_NS::HdRenderPassAovBinding &aovBinding)
Definition: hydra/session.h:47
void SyncAovBindings(const PXR_NS::HdRenderPassAovBindingVector &aovBindings)
bool get_cancel() const
Definition: progress.h:90
void reset()
Definition: progress.h:62
void set_display_driver(unique_ptr< DisplayDriver > driver)
void cancel(bool quick=false)
void start()
Progress progress
SessionParams params
void reset(const SessionParams &session_params, const BufferParams &buffer_params)
void set_output_driver(unique_ptr< OutputDriver > driver)
Scene scene
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
Definition: hydra/config.h:17
Integrator * integrator
Definition: scene.h:210
thread_mutex mutex
Definition: scene.h:246
struct Object * camera
bool need_reset()
Definition: scene.cpp:443