Loading...
Searching...
No Matches
renderer.h
1//
2// Copyright 2018 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
8#define PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/imaging/plugin/hdEmbree/context.h"
13#include "pxr/imaging/plugin/hdEmbree/light.h"
14
15#include "pxr/imaging/hd/aov.h"
16#include "pxr/imaging/hd/renderThread.h"
17
19#include "pxr/base/gf/rect2i.h"
20
21#include <embree4/rtcore.h>
22#include <embree4/rtcore_device.h>
23#include <embree4/rtcore_ray.h>
24
25#include <random>
26#include <atomic>
27#include <map>
28#include <mutex>
29
30PXR_NAMESPACE_OPEN_SCOPE
31
32enum HdEmbree_RayMask: uint32_t {
33 None = 0,
34
35 Camera = 1 << 0,
36 Shadow = 1 << 1,
37
38 All = UINT_MAX,
39};
40
53{
54public:
55 using WriteMutex = std::mutex;
56 using ScopedLock = std::scoped_lock<WriteMutex>;
57
60
63
66 void SetScene(RTCScene scene);
67
70 void SetDataWindow(const GfRect2i &dataWindow);
71
75 void SetCamera(const GfMatrix4d& viewMatrix, const GfMatrix4d& projMatrix);
76
79 void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings);
80
82 void AddLight(SdfPath const& lightPath, HdEmbree_Light* light);
83
85 void RemoveLight(SdfPath const& lightPath, HdEmbree_Light* light);
86
89 HdRenderPassAovBindingVector const& GetAovBindings() const {
90 return _aovBindings;
91 }
92
96 void SetSamplesToConvergence(int samplesToConvergence);
97
101 void SetAmbientOcclusionSamples(int ambientOcclusionSamples);
102
106 void SetEnableSceneColors(bool enableSceneColors);
107
112 void SetRandomNumberSeed(int randomNumberSeed);
113
116 void SetEnableLighting(bool enableLighting);
117
123 void Render(HdRenderThread *renderThread);
124
126 void Clear();
127
130
133
134private:
135 // Perform validation and setup immediately before starting a render
136 void _PreRenderSetup();
137
138 // Validate the internal consistency of aov bindings provided to
139 // SetAovBindings. If the aov bindings are invalid, this will issue
140 // appropriate warnings. If the function returns false, Render() will fail
141 // early.
142 //
143 // This function thunks itself using _aovBindingsNeedValidation and
144 // _aovBindingsValid.
145 // \return True if the aov bindings are valid for rendering.
146 bool _ValidateAovBindings();
147
148 // Return the clear color to use for the given VtValue.
149 static GfVec4f _GetClearColor(VtValue const& clearValue);
150
151 // Render square tiles of pixels. This function is one unit of threadpool
152 // work. For each tile, iterate over pixels in the tile, generating camera
153 // rays, and following them/calculating color with _TraceRay. This function
154 // renders all tiles between tileStart and tileEnd.
155 void _RenderTiles(HdRenderThread *renderThread, int sampleNum,
156 size_t tileStart, size_t tileEnd);
157
158 // Cast a ray into the scene and if it hits an object, write to the bound
159 // aov buffers.
160 void _TraceRay(unsigned int x, unsigned int y,
161 GfVec3f const& origin, GfVec3f const& dir,
162 std::default_random_engine &random);
163
164 // Compute the color at the given ray hit.
165 GfVec4f _ComputeColor(RTCRayHit const& rayHit,
166 std::default_random_engine &random,
167 GfVec4f const& clearColor);
168 // Compute the depth at the given ray hit.
169 bool _ComputeDepth(RTCRayHit const& rayHit, float *depth, bool clip);
170 // Compute the given ID at the given ray hit.
171 bool _ComputeId(RTCRayHit const& rayHit, TfToken const& idType, int32_t *id);
172 // Compute the normal at the given ray hit.
173 bool _ComputeNormal(RTCRayHit const& rayHit, GfVec3f *normal, bool eye);
174 // Compute a primvar at the given ray hit.
175 bool _ComputePrimvar(RTCRayHit const& rayHit, TfToken const& primvar,
176 GfVec3f *value);
177
178 // Compute the ambient occlusion term at a given point by firing rays
179 // from "position" in the hemisphere centered on "normal"; the occlusion
180 // factor is the fraction of those rays that are visible.
181 //
182 // Modulating surface color by occlusionFactor is similar to taking
183 // the light contribution of an infinitely far, pure white dome light.
184 float _ComputeAmbientOcclusion(GfVec3f const& position,
185 GfVec3f const& normal,
186 std::default_random_engine &random);
187
190 GfVec3f _ComputeLighting(
191 GfVec3f const& position,
192 GfVec3f const& normal,
193 std::default_random_engine &random,
194 HdEmbreePrototypeContext const* prototypeContext) const;
195
196 // Return the visibility from `position` along `direction`
197 float _Visibility(GfVec3f const& position,
198 GfVec3f const& direction,
199 float offset = 1.0e-3f) const;
200
201 // Should the ray continue based on the possibly intersected prim's visibility settings?
202 bool _RayShouldContinue(RTCRayHit const& rayHit) const;
203
204 // The bound aovs for this renderer.
205 HdRenderPassAovBindingVector _aovBindings;
206 // Parsed AOV name tokens.
207 HdParsedAovTokenVector _aovNames;
208
209 // Do the aov bindings need to be re-validated?
210 bool _aovBindingsNeedValidation;
211 // Are the aov bindings valid?
212 bool _aovBindingsValid;
213
214 // Data window - as in CameraUtilFraming.
215 GfRect2i _dataWindow;
216
217 // The width of the render buffers.
218 unsigned int _width;
219 // The height of the render buffers.
220 unsigned int _height;
221
222 // View matrix: world space to camera space.
223 GfMatrix4d _viewMatrix;
224 // Projection matrix: camera space to NDC space.
225 GfMatrix4d _projMatrix;
226 // The inverse view matrix: camera space to world space.
227 GfMatrix4d _inverseViewMatrix;
228 // The inverse projection matrix: NDC space to camera space.
229 GfMatrix4d _inverseProjMatrix;
230
231 // Our handle to the embree scene.
232 RTCScene _scene;
233
234 // How many samples should we render to convergence?
235 int _samplesToConvergence;
236 // How many samples should we use for ambient occlusion?
237 int _ambientOcclusionSamples;
238 // Should we enable scene colors?
239 bool _enableSceneColors;
240 // If other than -1, use this to seed the random number generator with.
241 int _randomNumberSeed;
242 // Should we enable direct lighting from the scene?
243 bool _enableLighting;
244
245 // How many samples have been completed.
246 std::atomic<int> _completedSamples;
247
248 // Lights
249 mutable WriteMutex _lightsWriteMutex; // protects the 2 below
250 std::map<SdfPath, HdEmbree_Light*> _lightMap;
251};
252
253PXR_NAMESPACE_CLOSE_SCOPE
254
255#endif // PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:71
A 2D rectangle with integer coordinates.
Definition: rect2i.h:43
Basic type for a vector of 3 float components.
Definition: vec3f.h:46
Basic type for a vector of 4 float components.
Definition: vec4f.h:46
HdEmbreeRenderer implements a renderer on top of Embree's raycasting abilities.
Definition: renderer.h:53
void SetSamplesToConvergence(int samplesToConvergence)
Set how many samples to render before considering an image converged.
void SetRandomNumberSeed(int randomNumberSeed)
Sets a number to seed the random number generator with.
void RemoveLight(SdfPath const &lightPath, HdEmbree_Light *light)
Remove a light.
void SetAmbientOcclusionSamples(int ambientOcclusionSamples)
Set how many samples to use for ambient occlusion.
void SetEnableSceneColors(bool enableSceneColors)
Sets whether to use scene colors while rendering.
void Render(HdRenderThread *renderThread)
Rendering entrypoint: add one sample per pixel to the whole sample buffer, and then loop until the im...
void SetEnableLighting(bool enableLighting)
Sets whether to enable direct lighting (disables ambient occlusion).
void AddLight(SdfPath const &lightPath, HdEmbree_Light *light)
Add a light.
HdEmbreeRenderer()
Renderer constructor.
~HdEmbreeRenderer()
Renderer destructor.
void MarkAovBuffersUnconverged()
Mark the aov buffers as unconverged.
void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings)
Set the aov bindings to use for rendering.
int GetCompletedSamples() const
Get the number of samples completed so far.
void SetScene(RTCScene scene)
Set the embree scene that this renderer should raycast into.
void SetCamera(const GfMatrix4d &viewMatrix, const GfMatrix4d &projMatrix)
Set the camera to use for rendering.
void Clear()
Clear the bound aov buffers (typically before rendering).
void SetDataWindow(const GfRect2i &dataWindow)
Set the data window to fill (same meaning as in CameraUtilFraming with coordinate system also being y...
HdRenderPassAovBindingVector const & GetAovBindings() const
Get the aov bindings being used for rendering.
Definition: renderer.h:89
HdRenderThread is a utility that specific render delegates can choose to use depending on their needs...
Definition: renderThread.h:129
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
A small bit of state attached to each bit of prototype geometry in embree, for the benefit of HdEmbre...
Definition: context.h:29