All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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/hd/renderThread.h"
13#include "pxr/imaging/hd/renderPassState.h"
14
16#include "pxr/base/gf/rect2i.h"
17
18#include <embree3/rtcore.h>
19#include <embree3/rtcore_ray.h>
20
21#include <random>
22#include <atomic>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
38{
39public:
42
45
48 void SetScene(RTCScene scene);
49
52 void SetDataWindow(const GfRect2i &dataWindow);
53
57 void SetCamera(const GfMatrix4d& viewMatrix, const GfMatrix4d& projMatrix);
58
61 void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings);
62
65 HdRenderPassAovBindingVector const& GetAovBindings() const {
66 return _aovBindings;
67 }
68
72 void SetSamplesToConvergence(int samplesToConvergence);
73
77 void SetAmbientOcclusionSamples(int ambientOcclusionSamples);
78
82 void SetEnableSceneColors(bool enableSceneColors);
83
88 void SetRandomNumberSeed(int randomNumberSeed);
89
95 void Render(HdRenderThread *renderThread);
96
98 void Clear();
99
102
105
106private:
107 // Validate the internal consistency of aov bindings provided to
108 // SetAovBindings. If the aov bindings are invalid, this will issue
109 // appropriate warnings. If the function returns false, Render() will fail
110 // early.
111 //
112 // This function thunks itself using _aovBindingsNeedValidation and
113 // _aovBindingsValid.
114 // \return True if the aov bindings are valid for rendering.
115 bool _ValidateAovBindings();
116
117 // Return the clear color to use for the given VtValue.
118 static GfVec4f _GetClearColor(VtValue const& clearValue);
119
120 // Render square tiles of pixels. This function is one unit of threadpool
121 // work. For each tile, iterate over pixels in the tile, generating camera
122 // rays, and following them/calculating color with _TraceRay. This function
123 // renders all tiles between tileStart and tileEnd.
124 void _RenderTiles(HdRenderThread *renderThread, int sampleNum,
125 size_t tileStart, size_t tileEnd);
126
127 // Cast a ray into the scene and if it hits an object, write to the bound
128 // aov buffers.
129 void _TraceRay(unsigned int x, unsigned int y,
130 GfVec3f const& origin, GfVec3f const& dir,
131 std::default_random_engine &random);
132
133 // Compute the color at the given ray hit.
134 GfVec4f _ComputeColor(RTCRayHit const& rayHit,
135 std::default_random_engine &random,
136 GfVec4f const& clearColor);
137 // Compute the depth at the given ray hit.
138 bool _ComputeDepth(RTCRayHit const& rayHit, float *depth, bool clip);
139 // Compute the given ID at the given ray hit.
140 bool _ComputeId(RTCRayHit const& rayHit, TfToken const& idType, int32_t *id);
141 // Compute the normal at the given ray hit.
142 bool _ComputeNormal(RTCRayHit const& rayHit, GfVec3f *normal, bool eye);
143 // Compute a primvar at the given ray hit.
144 bool _ComputePrimvar(RTCRayHit const& rayHit, TfToken const& primvar,
145 GfVec3f *value);
146
147 // Compute the ambient occlusion term at a given point by firing rays
148 // from "position" in the hemisphere centered on "normal"; the occlusion
149 // factor is the fraction of those rays that are visible.
150 //
151 // Modulating surface color by occlusionFactor is similar to taking
152 // the light contribution of an infinitely far, pure white dome light.
153 float _ComputeAmbientOcclusion(GfVec3f const& position,
154 GfVec3f const& normal,
155 std::default_random_engine &random);
156
157 // The bound aovs for this renderer.
158 HdRenderPassAovBindingVector _aovBindings;
159 // Parsed AOV name tokens.
160 HdParsedAovTokenVector _aovNames;
161
162 // Do the aov bindings need to be re-validated?
163 bool _aovBindingsNeedValidation;
164 // Are the aov bindings valid?
165 bool _aovBindingsValid;
166
167 // Data window - as in CameraUtilFraming.
168 GfRect2i _dataWindow;
169
170 // The width of the render buffers.
171 unsigned int _width;
172 // The height of the render buffers.
173 unsigned int _height;
174
175 // View matrix: world space to camera space.
176 GfMatrix4d _viewMatrix;
177 // Projection matrix: camera space to NDC space.
178 GfMatrix4d _projMatrix;
179 // The inverse view matrix: camera space to world space.
180 GfMatrix4d _inverseViewMatrix;
181 // The inverse projection matrix: NDC space to camera space.
182 GfMatrix4d _inverseProjMatrix;
183
184 // Our handle to the embree scene.
185 RTCScene _scene;
186
187 // How many samples should we render to convergence?
188 int _samplesToConvergence;
189 // How many samples should we use for ambient occlusion?
190 int _ambientOcclusionSamples;
191 // Should we enable scene colors?
192 bool _enableSceneColors;
193 // If other than -1, use this to seed the random number generator with.
194 int _randomNumberSeed;
195
196 // How many samples have been completed.
197 std::atomic<int> _completedSamples;
198};
199
200PXR_NAMESPACE_CLOSE_SCOPE
201
202#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:38
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 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...
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:65
HdRenderThread is a utility that specific render delegates can choose to use depending on their needs...
Definition: renderThread.h:129
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:147