renderer.h
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
25 #define PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/imaging/hd/renderThread.h"
30 #include "pxr/imaging/hd/renderPassState.h"
31 
32 #include "pxr/base/gf/matrix4d.h"
33 #include "pxr/base/gf/rect2i.h"
34 
35 #include <embree3/rtcore.h>
36 #include <embree3/rtcore_ray.h>
37 
38 #include <random>
39 #include <atomic>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
54 class HdEmbreeRenderer final
55 {
56 public:
59 
62 
65  void SetScene(RTCScene scene);
66 
69  void SetDataWindow(const GfRect2i &dataWindow);
70 
74  void SetCamera(const GfMatrix4d& viewMatrix, const GfMatrix4d& projMatrix);
75 
78  void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings);
79 
82  HdRenderPassAovBindingVector const& GetAovBindings() const {
83  return _aovBindings;
84  }
85 
89  void SetSamplesToConvergence(int samplesToConvergence);
90 
94  void SetAmbientOcclusionSamples(int ambientOcclusionSamples);
95 
99  void SetEnableSceneColors(bool enableSceneColors);
100 
106  void Render(HdRenderThread *renderThread);
107 
109  void Clear();
110 
113 
115  int GetCompletedSamples() const;
116 
117 private:
118  // Validate the internal consistency of aov bindings provided to
119  // SetAovBindings. If the aov bindings are invalid, this will issue
120  // appropriate warnings. If the function returns false, Render() will fail
121  // early.
122  //
123  // This function thunks itself using _aovBindingsNeedValidation and
124  // _aovBindingsValid.
125  // \return True if the aov bindings are valid for rendering.
126  bool _ValidateAovBindings();
127 
128  // Return the clear color to use for the given VtValue.
129  static GfVec4f _GetClearColor(VtValue const& clearValue);
130 
131  // Render square tiles of pixels. This function is one unit of threadpool
132  // work. For each tile, iterate over pixels in the tile, generating camera
133  // rays, and following them/calculating color with _TraceRay. This function
134  // renders all tiles between tileStart and tileEnd.
135  void _RenderTiles(HdRenderThread *renderThread,
136  size_t tileStart, size_t tileEnd);
137 
138  // Cast a ray into the scene and if it hits an object, write to the bound
139  // aov buffers.
140  void _TraceRay(unsigned int x, unsigned int y,
141  GfVec3f const& origin, GfVec3f const& dir,
142  std::default_random_engine &random);
143 
144  // Compute the color at the given ray hit.
145  GfVec4f _ComputeColor(RTCRayHit const& rayHit,
146  std::default_random_engine &random,
147  GfVec4f const& clearColor);
148  // Compute the depth at the given ray hit.
149  bool _ComputeDepth(RTCRayHit const& rayHit, float *depth, bool clip);
150  // Compute the given ID at the given ray hit.
151  bool _ComputeId(RTCRayHit const& rayHit, TfToken const& idType, int32_t *id);
152  // Compute the normal at the given ray hit.
153  bool _ComputeNormal(RTCRayHit const& rayHit, GfVec3f *normal, bool eye);
154  // Compute a primvar at the given ray hit.
155  bool _ComputePrimvar(RTCRayHit const& rayHit, TfToken const& primvar,
156  GfVec3f *value);
157 
158  // Compute the ambient occlusion term at a given point by firing rays
159  // from "position" in the hemisphere centered on "normal"; the occlusion
160  // factor is the fraction of those rays that are visible.
161  //
162  // Modulating surface color by occlusionFactor is similar to taking
163  // the light contribution of an infinitely far, pure white dome light.
164  float _ComputeAmbientOcclusion(GfVec3f const& position,
165  GfVec3f const& normal,
166  std::default_random_engine &random);
167 
168  // The bound aovs for this renderer.
169  HdRenderPassAovBindingVector _aovBindings;
170  // Parsed AOV name tokens.
171  HdParsedAovTokenVector _aovNames;
172 
173  // Do the aov bindings need to be re-validated?
174  bool _aovBindingsNeedValidation;
175  // Are the aov bindings valid?
176  bool _aovBindingsValid;
177 
178  // Data window - as in CameraUtilFraming.
179  GfRect2i _dataWindow;
180 
181  // The width of the render buffers.
182  unsigned int _width;
183  // The height of the render buffers.
184  unsigned int _height;
185 
186  // View matrix: world space to camera space.
187  GfMatrix4d _viewMatrix;
188  // Projection matrix: camera space to NDC space.
189  GfMatrix4d _projMatrix;
190  // The inverse view matrix: camera space to world space.
191  GfMatrix4d _inverseViewMatrix;
192  // The inverse projection matrix: NDC space to camera space.
193  GfMatrix4d _inverseProjMatrix;
194 
195  // Our handle to the embree scene.
196  RTCScene _scene;
197 
198  // How many samples should we render to convergence?
199  int _samplesToConvergence;
200  // How many samples should we use for ambient occlusion?
201  int _ambientOcclusionSamples;
202  // Should we enable scene colors?
203  bool _enableSceneColors;
204 
205  // How many samples have been completed.
206  std::atomic<int> _completedSamples;
207 };
208 
209 PXR_NAMESPACE_CLOSE_SCOPE
210 
211 #endif // PXR_IMAGING_PLUGIN_HD_EMBREE_RENDERER_H
void MarkAovBuffersUnconverged()
Mark the aov buffers as unconverged.
A 2D rectangle with integer coordinates.
Definition: rect2i.h:60
Basic type for a vector of 3 float components.
Definition: vec3f.h:62
~HdEmbreeRenderer()
Renderer destructor.
void SetEnableSceneColors(bool enableSceneColors)
Sets whether to use scene colors while rendering.
HdEmbreeRenderer()
Renderer constructor.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
void SetCamera(const GfMatrix4d &viewMatrix, const GfMatrix4d &projMatrix)
Set the camera to use for rendering.
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:87
void SetSamplesToConvergence(int samplesToConvergence)
Set how many samples to render before considering an image converged.
void SetScene(RTCScene scene)
Set the embree scene that this renderer should raycast into.
void SetAmbientOcclusionSamples(int ambientOcclusionSamples)
Set how many samples to use for ambient occlusion.
Basic type for a vector of 4 float components.
Definition: vec4f.h:62
int GetCompletedSamples() const
Get the number of samples completed so far.
void Clear()
Clear the bound aov buffers (typically before rendering).
HdEmbreeRenderer implements a renderer on top of Embree's raycasting abilities.
Definition: renderer.h:54
void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings)
Set the aov bindings to use for rendering.
HdRenderPassAovBindingVector const & GetAovBindings() const
Get the aov bindings being used for rendering.
Definition: renderer.h:82
void SetDataWindow(const GfRect2i &dataWindow)
Set the data window to fill (same meaning as in CameraUtilFraming with coordinate system also being y...
HdRenderThread is a utility that specific render delegates can choose to use depending on their needs...
Definition: renderThread.h:146
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
void Render(HdRenderThread *renderThread)
Rendering entrypoint: add one sample per pixel to the whole sample buffer, and then loop until the im...